<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-input v-model="project_code" clearable placeholder="请输入项目编码" @click="showDialog = true"
                                :disabled="project" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id">
                            <el-input v-model="project_name" clearable placeholder="请输入项目名称" disabled />
                        </el-form-item> </el-col>

                    <el-col :span="8">
                        <el-form-item label="姓名" @click="showDialog1 = true">
                            <el-input v-model="person_name" clearable placeholder="请选择项目人员" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="身份证号" prop="project_person_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="idcard" clearable placeholder="系统自动填写" disabled />
                        </el-form-item> </el-col>

                    <el-col :span="8">
                        <el-form-item label="投保日期" prop="insurance_date">
                            <el-date-picker class="flex-1 !flex" v-model="formData.insurance_date" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择投保日期">
                            </el-date-picker>
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="到期日期" prop="due_date" :rules="{
                            validator: chekcDate,
                            trigger: ['blur']
                        }">
                            <el-date-picker class="flex-1 !flex" v-model="formData.due_date" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="选择到期日期">
                            </el-date-picker>
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="保险种类" prop="type">
                            <el-input v-model="formData.type" clearable placeholder="请输入保险种类" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="保单号" prop="insurance_no">
                            <el-input v-model="formData.insurance_no" clearable placeholder="请输入保单号" v-type="'code'" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="保费（元）" prop="insurance">
                            <el-input v-model="formData.insurance" clearable type="number" placeholder="请输入保费（元）" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="投保金额（元）" prop="insured_amount">
                            <el-input v-model="formData.insured_amount" clearable type="number" placeholder="请输入投保金额（元）" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="保险公司" prop="insurance_company">
                            <el-input v-model="formData.insurance_company" clearable placeholder="请输入保险公司" />
                        </el-form-item> </el-col>
                    <el-col :span="8">

                        <el-form-item label="保单明细" prop="insurance_detail">
                            <el-input v-model="formData.insurance_detail" clearable
                                :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]" placeholder="请输入保单明细" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="发票编号" prop="invoice_no">
                            <el-input v-model="formData.invoice_no" clearable placeholder="请输入发票编号" type="number" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="经办人" prop="handler">
                            <el-input v-model="formData.handler" clearable placeholder="请输入经办人" />
                        </el-form-item> </el-col>



                    <el-col :span="24">
                        <el-form-item label="合同附件">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <projectDialog @customEvent="customEvent"></projectDialog>
            </el-dialog>
            <div v-if="showDialog1">
                <el-dialog v-model="showDialog1" title="选择项目人员" width="70%">
                    <customDialog @customEvent="customEvent1" :project_id="formData.project_id"></customDialog>
                </el-dialog>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { insurancemanagementAdd, insurancemanagementEdit, insurancemanagementDetail } from '@/api/project_insurance_management'
import customDialog from '@/components/project_personnel/index.vue'
import { useDictData } from '@/hooks/useDictOptions'
import type { PropType } from 'vue'
let props = defineProps({
    project: Object
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const { dictData } = useDictData('labor_contract_status,labor_contract_type,labor_contract_name')
const showDialog = ref(false)
const showDialog1 = ref(false)
const project_name = ref('')
const project_code = ref('')
const person_name = ref('')
const idcard = ref('')

import configs from "@/config"
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();
const userInfo = userStore.userInfo
// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_person_id: '',
    insurance_date: '',
    due_date: '',
    type: '',
    insurance_no: '',
    insurance: '',
    insured_amount: '',
    insurance_company: '',
    insurance_detail: '',
    invoice_no: '',
    handler: '',
    annex: [],
})

const chekcDate = (rule: any, value: any, callback: any) => {
    if (new Date(formData.due_date) < new Date(formData.insurance_date)) {
        callback(new Error('到期日期不能早于投保日期'))
    } else {
        callback()
    }
}

//获取值
const customEvent = (e) => {
    formData.project_id = e.id
    project_name.value = e.name
    project_code.value = e.project_code
    person_name.value = ''
    showDialog.value = false

}

if (props.project) customEvent(props.project);

const customEvent1 = (e) => {
    formData.project_person_id = e.id
    idcard.value = e.idcard
    person_name.value = e.name
    showDialog1.value = false

}
// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑保险管理' : '新增保险管理'
})




// 表单验证
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]
        }
    }
    project_name.value = data.project_name
    project_code.value = data.project_code
    idcard.value = data.person_idcard
    person_name.value = data.person_name

}

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


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

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

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

const checkCode = (e) => {
    console.log(e)
    e = 12412121
}

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