<!--
 * @Author: micky 1254597151@qq.com
 * @Date: 2023-09-10 16:40:31
 * @LastEditors: micky 1254597151@qq.com
 * @LastEditTime: 2023-12-25 15:11:21
 * @FilePath: \housekeeping-admin\src\views\decorate\component\widgets\category\add-category.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <div>
            <del-wrap class="max-w-[400px]" v-for="(item, index) in modelValue" :key="index" @close="handleDelete(index)">
                <div class="bg-fill-light flex items-center w-full p-4 mb-4">
                    <material-picker v-model="item.image" :excludeDomain="true" upload-class="bg-body" size="60px">
                        <template #upload>
                            <div class="upload-btn w-[60px] h-[60px]">
                                <icon name="el-icon-Plus" :size="20" />
                            </div>
                        </template>
                    </material-picker>
                    <div class="ml-3 flex-1">
                        <div class="flex">
                            <span class="text-tx-regular flex-none mr-3">名称</span>
                            <el-input v-model="item.name" placeholder="请输入名称" maxlength="5" />
                        </div>
                    </div>
                </div>
            </del-wrap>
        </div>
        <div>
            <el-button type="primary" @click="handleAdd" v-show="modelValue.length < 1">添加</el-button>
        </div>
    </div>
</template>
<script lang="ts" setup>
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'

const props = defineProps({
    modelValue: {
        type: Array as PropType<any[]>,
        default: () => []
    },
    max: {
        type: Number,
        default: 1
    },
    min: {
        type: Number,
        default: 1
    }
})

const handleAdd = () => {
    if (props.modelValue?.length < props.max) {
        props.modelValue.push({
            image: '',
            name: '',
            link: {},
            showImage: true,
            showList: true
        })
    } else {
        feedback.msgError(`最多添加${props.max}个`)
    }
}
const handleDelete = (index: number) => {
    // if (props.modelValue?.length <= props.min) {
    //     return feedback.msgError(`最少保留${props.min}个`)
    // }
    props.modelValue.splice(index, 1)
}
</script>

<style lang="scss" scoped>
:deep(.file-item) {
    width: 80px !important;
}
</style>
