<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="商品名称" prop="store_name">
                    <el-input v-model="formData.store_name" disabled />
                </el-form-item>
                <el-form-item label="供应商" v-if="formData.financial_pm == 1">
                    <el-select
                        v-model="formData.supplier_id"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="输入供应商名称搜索"
                        remote-show-suffix
                        :remote-method="remoteMethodSupplier"
                        :loading="supplierLoading"
                        style="width: 240px"
                    >
                        <el-option
                            v-for="item in supplierList"
                            :key="item.id"
                            :label="item.mer_name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="数量" prop="nums">
                    <el-input
                        v-model="formData.nums"
                        clearable
                        placeholder="请输入数量"
                        :readonly="false"
                        @change="handleChange()"
                    />
                </el-form-item>
                <el-form-item label="采购价" prop="purchase" v-if="formData.financial_pm == 1">
                    <el-input
                        v-model="formData.purchase"
                        clearable
                        placeholder="请输入采购价"
                        :readonly="false"
                        @change="handleChange()"
                    />
                </el-form-item>
                <el-form-item label="零售价" prop="price" v-else>
                    <el-input
                        v-model="formData.price"
                        clearable
                        placeholder="请输入零售价"
                        :readonly="false"
                        @change="handleChange()"
                    />
                </el-form-item>
                <el-form-item label="总价格" prop="total_price">
                    <el-input
                        v-model="formData.total_price"
                        clearable
                        placeholder="请输入总价格"
                        :readonly="false"
                    />
                </el-form-item>
				<el-form-item label="生产日期" prop="manufacture">
				    <el-date-picker v-model="formData.manufacture" type="date" clearable value-format="YYYY-MM-DD" placeholder="请输入生产日期"/>
				</el-form-item>
				<el-form-item label="保质期" prop="expiration_date">
				    <el-date-picker v-model="formData.expiration_date" type="date" clearable value-format="YYYY-MM-DD" placeholder="请输入保质期"/>
				</el-form-item>
                <el-form-item label="付款方式" prop="pay_type" v-if="formData.financial_pm == 1">
                    <el-radio-group v-model="formData.pay_type">
                        <el-radio :label="1" :value="1">赊账</el-radio>
                        <el-radio :label="2" :value="2">现金</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="warehouseOrderEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiWarehouseOrderDetail } from '@/api/warehouse_order'
import { apiWarehouseProductEdit } from '@/api/warehouse_product'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { apiSupplierLists } from '@/api/supplier'

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: '',
    oid: '',
    supplier_id: '',
    store_name: '',
    total_price: '',
    nums: '',
    pay_type: '',
    purchase: '',
    financial_pm: '',
    price: '',
	manufacture: '',
	expiration_date: ''
})

// 表单验证
const formRules = reactive<any>({
    warehouse_id: [
        {
            required: true,
            message: '请输入仓库',
            trigger: ['blur']
        }
    ],
    total_price: [
        {
            required: true,
            message: '请输入总价格',
            trigger: ['blur']
        }
    ]
})
function handleChange() {
    if (formData.nums > 0 && formData.purchase > 0 && formData.financial_pm == 1) {
        formData.total_price = (parseFloat(formData.purchase) * parseFloat(formData.nums)).toFixed(
            2
        )
    } else if (formData.nums > 0 && formData.price > 0 && formData.financial_pm == 0) {
        formData.total_price = (parseFloat(formData.price) * parseFloat(formData.nums)).toFixed(2)
    }
}

// 获取详情
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]
        }
    }
    remoteMethodSupplier()
}
const supplierLoading = ref(false)
const supplierList = ref([])
const remoteMethodSupplier = (e = '') => {
    supplierLoading.value = true
    apiSupplierLists({ mer_name: e })
        .then((res) => {
            supplierList.value = res.lists
            setTimeout(() => {
                supplierLoading.value = false
            }, 300)
        })
        .catch((err) => {
            setTimeout(() => {
                supplierLoading.value = false
            }, 300)
        })
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    await apiWarehouseProductEdit(data)
    popupRef.value?.close()
    emit('success')
}

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

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

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