<template>
    <view class="text-base font-bold mt-[30rpx]">
        <text>模型选择</text>
    </view>

    <!--    模型    -->
    <view class="mt-[10rpx]">
        <view
            v-for="item in modelList"
            :key="item.name"
            class="model-item"
            :class="{
                'model-item_active': modelValue == item.id
            }"
            @click="emit('update:modelValue', item.id)"
        >
            <view
                class="model-item-bg"
                :style="{
                    'background-image': `url(${item.preview_img})`
                }"
            >
            </view>
            <view class="model-item-text">{{ item.name }}</view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { watchEffect } from 'vue'

const emit = defineEmits<{
    (event: 'update:modelValue', value: string): void
}>()
const props = withDefaults(
    defineProps<{
        modelValue?: any
        modelList?: any
    }>(),
    {
        modelValue: '',
        modelList: []
    }
)

watchEffect(() => {
    const modelList = props.modelList
    if (modelList.length !== 0) {
        emit('update:modelValue', modelList[0].id)
    }
})
</script>

<style scoped>
.model-item {
    display: inline-block;
    position: relative;
    width: 216rpx;
    height: 146rpx;
    padding: 8rpx;
    margin-right: 20rpx;
    border: 4rpx solid #e5e5e5;
    color: #ffffff;
    border-radius: 8rpx;
}
.model-item:nth-child(3n) {
    margin-right: 0px;
}
.model-item_active {
    border: 4rpx solid var(--color-primary);
}
.model-item-bg {
    background-size: cover;
    width: 100%;
    height: 100%;
}
.model-item-text {
    position: absolute;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    margin: 8rpx;
    width: 190rpx;
    height: 48rpx;
    border-radius: 8rpx;
    background-color: rgba(0, 0, 0, 0.5);
}
</style>
