<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            title="复制商品到门店"
            :async="true"
            width="1200px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <div>
                <el-form ref="formRef" :model="formData" label-width="90px">
                    <el-form-item label="商品名称">
                        <el-input v-model="formData.store_name" type="input" rows="4" disabled />
                    </el-form-item>
                    <el-form-item label="门店" prop="store_id">
                        <div class="flex-1 w-full">
                            <div class="mb-2">
                                <el-button type="primary" @click="showStore = true"
                                    >添加门店</el-button
                                >
                            </div>
                            <el-table :data="storeList">
                                <el-table-column label="ID" prop="id" show-overflow-tooltip />
                                <el-table-column
                                    label="门店名称"
                                    prop="name"
                                    show-overflow-tooltip
                                />
                                <el-table-column
                                    label="手机号码"
                                    prop="phone"
                                    show-overflow-tooltip
                                />
                                <el-table-column
                                    label="详细地址"
                                    prop="detailed_address"
                                    show-overflow-tooltip
                                />
                                <el-table-column
                                    label="门店logo"
                                    prop="image"
                                    show-overflow-tooltip
                                >
                                    <template #default="{ row }">
                                        <el-image
                                            :src="row.image"
                                            class="w-[40px] h-[40px]"
                                            :preview-teleported="true"
                                        />
                                    </template>
                                </el-table-column>
                                <el-table-column
                                    label="营业状态"
                                    prop="is_show"
                                    show-overflow-tooltip
                                >
                                    <template #default="{ row }">
                                        {{ row.is_show ? '营业' : '打烊' }}
                                    </template>
                                </el-table-column>
                                <el-table-column label="操作" width="120" fixed="right">
                                    <template #default="{ row }">
                                        <el-button
                                            v-perms="['system_store.system_store/delete']"
                                            type="danger"
                                            link
                                            @click="handleDelete(row.id)"
                                        >
                                            删除
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </div>
                    </el-form-item>
                </el-form>
            </div>
            <el-dialog v-model="showStore" title="选择门店" width="70%">
                <store-pop @onBindStore="onBindStore"></store-pop>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="storeProductEdit">
import { ElMessage, type FormInstance } from 'element-plus'
import { apiStoreProductCopy } from '@/api/store_product'
import type { PropType } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import Popup from '@/components/popup/index.vue'

const popupRef = shallowRef<InstanceType<typeof Popup>>()

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()

// 表单数据
const formData = reactive({
    id: '',
    store_name: ''
})

const showStore = ref(false) // 选择门店列表是否显示
// 门店列表
const storeList = ref([])
// 删除已有门店
const handleDelete = (id: number) => {
    storeList.value = storeList.value.filter((item: any) => item.id !== id)
}
// 选择门店
const onBindStore = (e: any[]) => {
    e.forEach((item: any) => {
        if (!storeList.value.find((t: any) => t.id == item.id)) {
            storeList.value.push(item)
        }
    })
    showStore.value = false
}
//打开弹窗
const open = (type = 'add') => {
    popupRef.value?.open()
}

const router = useRouter()
// 提交按钮
const handleSubmit = async () => {
    const store_arr = storeList.value.map((item: any) => item.id)
    apiStoreProductCopy({
        store_arr,
        product_id: formData.id
    }).then((res) => {
        // removeTab()
        // if (res.code == 1) {
        // ElMessage.success(res.msg)
        setTimeout(() => {
            router.push({
                path: '/store/store_branch_product'
            })
        }, 2000)
        // } else {
        //     ElMessage.error(res.msg)
        // }
    })
}
// 获取详情
const setFormData = async (data: Record<any, any>) => {
    formData.id = data.id
    formData.store_name = data.store_name
}

// 关闭回调
const handleClose = () => {
    emit('close')
}
defineExpose({
    open,
    setFormData
})
</script>
