<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80vw" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="125px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称">
                            <el-input v-model="formData.project_name" readonly @click="showDialog = true" clearable
                                placeholder="点击选择项目" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="项目编号">
                            <el-input v-model="formData.project_num" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同名称">
                            <el-input v-model="formData.contract_name" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同类型">
                            <el-input v-model="formData.contract_type" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方签约单位">
                            <el-input v-model="formData.part_a" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="乙方签约单位">
                            <el-input v-model="formData.part_b" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订金额">
                            <el-input v-model="formData.sign_money" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同签订日期">
                            <el-input v-model="formData.sign_time" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="累计已开票">
                            <el-input v-model="formData.total_invoice_amount" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="累计已到账">
                            <el-input v-model="formData.total_refund_amount" readonly clearable disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票编号">
                            <el-input v-model="formData.invoice_code" clearable placeholder="系统自动生成" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="发票类型" prop="invoice_type">
                            <el-select class="flex-1" v-model="formData.invoice_type" clearable placeholder="请选择发票类型">
                                <el-option v-for="(item, index) in dictData.zjzx_invoice_type" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票申请金额" prop="apply_amount">
                            <el-input v-model="formData.apply_amount" clearable placeholder="请输入开票申请金额" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票单位名称" prop="apply_company">
                            <el-input v-model="formData.apply_company" clearable placeholder="请输入开票单位名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="纳税人识别号">
                            <el-input v-model="formData.apply_company_number" clearable placeholder="请输入纳税人识别号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票单位注册地址">
                            <el-input v-model="formData.apply_company_address" clearable placeholder="请输入开票单位注册地址" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票单位电话">
                            <el-input v-model="formData.apply_company_telephone" clearable placeholder="请输入开票单位电话" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户银行">
                            <el-input v-model="formData.apply_company_bank" clearable placeholder="请输入开户银行" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开户账号">
                            <el-input v-model="formData.apply_company_account" clearable placeholder="请输入开户账号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票联系人">
                            <el-input v-model="formData.apply_contact" clearable placeholder="请输入开票联系人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="对应付款方式">
                            <el-input v-model="formData.pay_type" clearable placeholder="请输入对应付款方式" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票内容">
                            <el-input v-model="formData.invoice_content" clearable placeholder="请输入开票内容" />
                        </el-form-item>
                    </el-col>
                    <createUserLable :formData="formData" flag></createUserLable>
                </el-row>
            </el-form>
            <el-dialog v-model="showDialog" title="选择项目" width="70%">
                <dialogTable :config="cost_project" @customEvent="customEvent">
                </dialogTable>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="zjzxInvoiceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiZjzxInvoiceAdd, apiZjzxInvoiceEdit } from '@/api/zjzx_invoice'
import { cost_project } from "@/components/dialogTable/dialogTableConfig"
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 showDialog = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑造价咨询--开票台账' : '新增造价咨询--开票台账'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    invoice_code: '',
    invoice_type: '',
    apply_amount: '',
    apply_company: '',
    apply_company_number: '',
    apply_company_address: '',
    apply_company_telephone: '',
    apply_company_bank: '',
    apply_company_account: '',
    apply_contact: '',
    pay_type: '',
    invoice_content: '',
    create_user: '',
    create_time: '',
    contract_type: "",
    part_a: "",
    part_b: "",
    sign_money: "",
    sign_time: "",
    total_invoice_amount: "",
    total_refund_amount: "",
    "project_num": "",
    "contract_name": "",

})

const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
    let list = ['project_num', 'contract_name', 'part_a', 'part_b', 'total_invoice_amount', 'total_refund_amount']
    list.forEach(item => {
        formData[item] = e[item] ?? (e.contract)[item]
    })
    formData.contract_type = e.contract.contract_type_text
    formData.sign_money = e.contract.money
    formData.sign_time = e.contract.create_time

}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    invoice_code: [{
        required: true,
        message: '请输入开票编号',
        trigger: ['blur']
    }],
    invoice_type: [{
        required: true,
        message: '请选择发票类型',
        trigger: ['blur']
    }],
    apply_amount: [{
        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 handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiZjzxInvoiceEdit(data)
        : await apiZjzxInvoiceAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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



defineExpose({
    open,
    setFormData,

})
</script>
