<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="转出类型">
                    <el-radio-group v-model="formData.one_type">
                        <el-radio label="1">门店</el-radio>
                        <el-radio label="2">仓库</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="转出门店" prop="one_id" v-if="formData.one_type == 1">
                    <el-input
                        v-model="formData.one_name"
                        clearable
                        placeholder="请选择转出方"
                        :readonly="false"
                        @click="showStore = true"
                    />
                </el-form-item>
                <el-form-item label="转出仓库" prop="one_id" v-if="formData.one_type == 2">
                    <el-input
                        v-model="formData.one_name"
                        clearable
                        placeholder="请选择转出方"
                        :readonly="false"
                        @click="showWarehouse = true"
                    />
                </el-form-item>
                <el-form-item label="转入类型">
                    <el-radio-group v-model="formData.two_type">
                        <el-radio label="1">门店</el-radio>
                        <el-radio label="2">仓库</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="转入门店" prop="two_id" v-if="formData.two_type == 1">
                    <el-input
                        v-model="formData.two_name"
                        clearable
                        placeholder="请选择转入方"
                        :readonly="false"
                        @click="showStoreTwo = true"
                    />
                </el-form-item>
                <el-form-item label="转入仓库" prop="two_id" v-if="formData.two_type == 2">
                    <el-input
                        v-model="formData.two_name"
                        clearable
                        placeholder="请选择转入方"
                        :readonly="false"
                        @click="showWarehouseTwo = true"
                    />
                </el-form-item>
                <el-form-item label="商品" prop="product_id" v-if="formData.one_id">
                    <el-input
                        v-model="formData.product_name"
                        clearable
                        placeholder="请选择商品"
                        :readonly="false"
                        @click="showProduct = true"
                    />
                </el-form-item>
                <el-form-item label="数量" prop="nums" v-if="formData.one_id">
                    <el-input
                        v-model="formData.nums"
                        clearable
                        placeholder="请输入数量"
                        :readonly="false"
                    />
                </el-form-item>
            </el-form>
        </popup>
        <el-dialog v-model="showStore" title="选择门店" width="70%">
            <store-pop @onBindStore="onBindStore"></store-pop>
        </el-dialog>
        <el-dialog v-model="showStoreTwo" title="选择门店" width="70%">
            <store-pop @onBindStore="onBindStoreTwo"></store-pop>
        </el-dialog>
        <el-dialog v-model="showProduct" title="选择商品" width="70%">
            <product-pop
                @onBindStore="onBindProduct"
                :is_branch="1"
                :store_id="store_id"
                :warehouse_id="warehouse_id"
                :is_warehouse="is_warehouse"
            ></product-pop>
        </el-dialog>
        <el-dialog v-model="showWarehouse" title="选择仓库1" width="70%">
            <warehouse-pop @onBindWarehouse="onBindWarehouse"></warehouse-pop>
        </el-dialog>
        <el-dialog v-model="showWarehouseTwo" title="选择仓库2" width="70%">
            <warehouse-pop @onBindWarehouse="onBindWarehouseTwo"></warehouse-pop>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="inventoryTransferEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {
    apiInventoryTransferAdd,
    apiInventoryTransferEdit,
    apiInventoryTransferDetail
} from '@/api/inventory_transfer'
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 showProduct = ref(false) // 选择商品列表是否显示
const showStore = ref(false) // 选择门店列表是否显示
const showStoreTwo = ref(false) // 选择门店列表是否显示
const showWarehouse = ref(false) // 选择仓库列表是否显示
const showWarehouseTwo = ref(false) // 选择仓库列表是否显示
const store_id = ref(0) // 门店id
const warehouse_id = ref(0) //仓库id
const is_warehouse = ref(0) //是否仓库

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑商品调拨' : '新增商品调拨'
})

// 表单数据
const formData = reactive({
    id: '',
    product_id: '',
    product_name: '',
    nums: '',
    before_nums: '',
    after_nums: '',
    one_type: '1',
    two_type: '1',
    one_id: '',
    one_name: '',
    two_id: '',
    two_name: '',
    warehouse_name: '',
    warehouse_name_two: ''
})

// 表单验证
const formRules = reactive<any>({
    product_id: [
        {
            required: true,
            message: '请输入商品',
            trigger: ['blur']
        }
    ],
    nums: [
        {
            required: true,
            message: '请输入数量',
            trigger: ['blur']
        }
    ],
    type: [
        {
            required: true,
            message: '请输入1商户2仓库',
            trigger: ['blur']
        }
    ],
    one_id: [
        {
            required: true,
            message: '请输入转出id',
            trigger: ['blur']
        }
    ],
    two_id: [
        {
            required: true,
            message: '请输入转入id',
            trigger: ['blur']
        }
    ]
})
const onBindProduct = (e: any[]) => {
    formData.product_name = e[0].store_name
    formData.product_id = e[0].product_id
    showProduct.value = false
}

// 选择门店
const onBindStore = (e: any[]) => {
    formData.one_id = e[0].id
    formData.one_name = e[0].name
    showStore.value = false
    store_id.value = e[0].id
}
const onBindStoreTwo = (e: any[]) => {
    formData.two_id = e[0].id
    formData.two_name = e[0].name
    showStoreTwo.value = false
}
//选择仓库
const onBindWarehouse = (e: any[]) => {
    formData.one_name = e.name
    formData.one_id = e.id
    warehouse_id.value = e.id
    is_warehouse.value = 1
    showWarehouse.value = false
}
//选择仓库
const onBindWarehouseTwo = (e: any[]) => {
    formData.two_name = e.name
    formData.two_id = e.id
    showWarehouseTwo.value = false
}
// 获取详情
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 handleType = (e) => {
    formData.one_id = 0
    formData.two_id = 0
    formData.one_name = ''
    formData.two_name = ''
    store_id.value = 0
    is_warehouse.value = 0
    warehouse_id.value = 0
}
const getDetail = async (row: Record<string, any>) => {
    const data = await apiInventoryTransferDetail({
        id: row.id
    })
    setFormData(data)
}

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

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

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

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