<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="1200px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <div class="flex-1 w-full">
                    <div class="mb-2">
                        <el-button type="primary" @click="showOrder = true">添加订单</el-button>
                    </div>
                    <el-table :data="orderList">
                        <el-table-column label="订单ID" prop="id" show-overflow-tooltip />

                        <el-table-column
                            label="订单编号"
                            prop="order_id"
                            min-width="200"
                            show-overflow-tooltip
                        />
                        <el-table-column
                            label="门店"
                            prop="store_name"
                            min-width="80"
                            show-overflow-tooltip
                        />
                        <el-table-column
                            label="用户"
                            prop="nickname"
                            min-width="80"
                            show-overflow-tooltip
                        />
                        <el-table-column
                            label="支付金额"
                            prop="pay_price"
                            min-width="80"
                            show-overflow-tooltip
                        />
                        <el-table-column
                            label="创建时间"
                            prop="create_time"
                            min-width="80"
                            show-overflow-tooltip
                        />
                        <el-table-column label="操作" width="120" fixed="right">
                            <template #default="{ row }">
                                <el-button
                                    v-perms="['system_store.system_store/delete']"
                                    type="danger"
                                    link
                                    @click="handleDeleteProdut(row.id)"
                                >
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-form>
        </popup>
        <el-dialog v-model="showOrder" title="选择订单" width="70%">
            <order-pop @onBindOrder="onBindOrder"></order-pop>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="purchaseOrderEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {
    apiPurchaseOrderAdd,
    apiPurchaseOrderEdit,
    apiPurchaseOrderDetail
} from '@/api/purchase_order'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑采购订单' : '新增采购订单'
})

// 表单数据
const formData = reactive({
    id: '',
    store_id: '',
    order_arr: '',
    order_id: '',
    total: '',
    actual: '',
    money: '',
    paid: '',
    file: '',
    data: '',
    is_opurchase: '',
    is_mer: '',
    storage: ''
})
const showOrder = ref(false) // 选择商品列表是否显示
// 商品列表
const orderList = ref([])
// 删除已有商品
const handleDeleteProdut = (id: number) => {
    orderList.value = orderList.value.filter((item: any) => item.id !== id)
}
// 选择商品
const onBindOrder = (e: any[]) => {
    e.forEach((item: any) => {
        if (!orderList.value.find((t: any) => t.id == item.id)) {
            orderList.value.push(item)
        }
    })
    showOrder.value = false
}
// 表单验证
const formRules = reactive<any>({
    store_id: [
        {
            required: true,
            message: '请输入所属商户',
            trigger: ['blur']
        }
    ],
    order_id: [
        {
            required: true,
            message: '请输入单据编号',
            trigger: ['blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiPurchaseOrderDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    const order_arr = orderList.value.map((item: any) => {
        return {
            oid: item.id,
            order_id: item.order_id,
            store_id: item.store_id
        }
    })
    mode.value == (await apiPurchaseOrderAdd(order_arr))
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
