<template>
    <view class="mt-[30rpx]">
        <view class="text-base">二维码风格</view>
        <view class="style-container overflow-hidden">
            <view
                v-for="(item, index) in template"
                :key="index"
                class="float-left style-item"
                :class="{
                    'style-active': item.id == modelValue
                }"
                @click="emit('update:modelValue', item.id)"
            >
                <image
                    class="w-[200rpx] h-[200rpx]"
                    :src="item.preview_img"
                    mode="aspectFill"
                    alt="风格模版"
                />
                <view class="text-center py-[6rpx]">
                    {{ item.name }}
                </view>
            </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<{
        template?: any
        modelValue: any
    }>(),
    {
        template: [],
        modelValue: ''
    }
)

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

<style scoped>
.style-container .style-item {
    margin-top: 20rpx;
    margin-right: 20rpx;
    border-radius: 8rpx;
    width: 216rpx;
    padding: 4rpx;
    border: 4rpx solid #e5e5e5;
}
.style-container .style-item:nth-child(3n) {
    margin-right: 0px;
}
.style-container .style-active {
    border: 4rpx solid;
    @apply border-primary;
}
</style>
