<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="120px" :rules="formRules">
                <el-form-item label="采购合同名称" prop="procurement_contract_id">
                    <el-input v-model="formData.procurement_contract_code" clearable readonly placeholder="点击选则采购合同"
                        @click="showDialog = true" />
                </el-form-item>
                <el-form-item label="材料采购申请" prop="material_procurement_application_id">
                    <el-input v-model="formData.material_purchase_request_code"
                        :disabled="!formData.procurement_contract_code" readonly placeholder="请输入材料采购申请"
                        @click="showDialog1 = true" />
                </el-form-item>
                <el-form-item label="数量" prop="num">
                    <el-input v-model="formData.num" clearable placeholder="请输入数量" type="number" />
                </el-form-item>
                <el-form-item label="含税单价" prop="unit_price_including_tax">
                    <el-input v-model="formData.unit_price_including_tax" clearable placeholder="请输入含税单价" type="number" />
                </el-form-item>
                <el-form-item label="税率" prop="tax_rate">
                    <el-select class="flex-1" v-model="formData.tax_rate" clearable placeholder="请选择税率">
                        <el-option v-for="(item, index) in dictData.tax_rate" :key="index" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="不含税金额" prop="amount_excluding_tax">
                    <el-input v-model="formData.amount_excluding_tax" clearable placeholder="请输入不含税金额" />
                </el-form-item>
                <el-form-item label="含税金额" prop="amount_including_tax">
                    <el-input v-model="formData.amount_including_tax" clearable placeholder="请输入含税金额" />
                </el-form-item>
            </el-form>

            <el-dialog v-model="showDialog" title="选择采购合同" width="70%">
                <procurementsubcontractingSelector @customEvent="customEvent"></procurementsubcontractingSelector>
            </el-dialog>
            <div v-if="showDialog1">
                <el-dialog v-model="showDialog1" title="选择采购合同" width="70%">
                    <materialPurchase @customEvent="customEvent1" :project_id="formData.project_id"></materialPurchase>
                </el-dialog>
            </div>


        </popup>
    </div>
</template>

<script lang="ts" setup name="procurementContractDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProcurementContractDetailAdd, apiProcurementContractDetailEdit, apiProcurementContractDetailDetail } from '@/api/procurement_contract_detail'
import type { PropType } from 'vue'

import procurementsubcontractingSelector from "@/components/procurementsubcontracting_selector/index.vue"
import materialPurchase from "@/components/materialPurchase/index.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 showDialog = ref(false)
const showDialog1 = ref(false)


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

// 表单数据
const formData = reactive({
    id: '',
    procurement_contract_id: '',
    project_id: "",
    procurement_contract_code: "",
    material_procurement_application_id: '',
    material_purchase_request_code: "",
    num: '',
    unit_price_including_tax: '',
    tax_rate: '',
    amount_excluding_tax: '',
    amount_including_tax: '',
})

const customEvent = (e: any) => {
    console.log(e)
    formData.procurement_contract_id = e.id
    formData.project_id = e.project_id
    formData.procurement_contract_code = e.contract_name
    formData.material_procurement_application_id = ''
    formData.material_purchase_request_code = ''
    showDialog.value = false

}
const customEvent1 = (e: any) => {
    formData.material_procurement_application_id = e.id
    formData.material_purchase_request_code = e.material_purchase_request_code
    showDialog1.value = false
}
// 表单验证
const formRules = reactive<any>({

})


// 获取详情
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 apiProcurementContractDetailDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProcurementContractDetailEdit(data)
        : await apiProcurementContractDetailAdd(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>
