<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="135px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="合同名称" prop="contract_name">
                            <el-input v-model="formData.contract_name" clearable placeholder="点击选择合同"
                                @click="showDialog = true" readonly />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="合同类型">
                            <el-input v-model="formData.contract_type" clearable placeholder="系统自动获取" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方签约单位">
                            <el-input v-model="formData.part_a" clearable placeholder="系统自动获取" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="乙方签约单位">
                            <el-input v-model="formData.part_b" clearable placeholder="系统自动获取" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订金额(元）">
                            <el-input v-model="formData.sign_money" clearable placeholder="系统自动获取" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同签订日期">
                            <el-input v-model="formData.sign_time" clearable placeholder="系统自动获取" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="累计已开票（元）">
                            <el-input v-model="formData.total_invoice_amount" clearable placeholder="系统自动获取" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="累计已到账（元）">
                            <el-input v-model="formData.total_refund_amount" clearable placeholder="系统自动获取" 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="发票接收邮箱" prop="apply_email">
                            <el-input v-model="formData.apply_email" 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 flag :formData="formData" />
                </el-row>
            </el-form>
            <el-dialog v-model="showDialog" title="选择合同" width="70%">
                <dialogTable :config="cost_approved_projects" @customEvent="customEvent" :query="{ status: 1 }">
                </dialogTable>
            </el-dialog>
        </popup>
    </div>
</template>

<script lang="ts" setup name="financialInvoiceEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiFinancialInvoiceAdd, apiFinancialInvoiceEdit, apiFinancialInvoiceDetail } from '@/api/financial_invoice'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { cost_approved_projects } from "@/components/dialogTable/dialogTableConfig";



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: '',
    contract_id: '',
    contract_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: '',
    apply_email: '',
    pay_type: '',
    invoice_content: '',
    create_user: '',
    create_time: '',
    "contract_type": "",
    "part_a": "",
    "part_b": "",
    "sign_money": "",
    "sign_time": "",
    "total_invoice_amount": 0,
    "total_refund_amount": 0
})

const customEvent = (e) => {
    formData.contract_id = e.id
    formData.contract_name = e.contract_name
    formData.contract_type = e.contract_type_text
    formData.part_a = e.part_a
    formData.part_b = e.part_b
    formData.sign_money = e.money
    formData.sign_time = e.sign_date
    formData.total_invoice_amount = e.total_invoice_amount
    formData.total_refund_amount = e.total_refund_amount
    showDialog.value = false
}

// 表单验证
const formRules = reactive<any>({
    contract_name: [{
        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']
    }],
    apply_company: [{
        required: true,
        message: '请输入开票单位名称',
        trigger: ['blur']
    }],
    apply_company_number: [{
        required: true,
        message: '请输入纳税人识别号',
        trigger: ['blur']
    }],
    apply_company_address: [{
        required: true,
        message: '请输入开票单位注册地址',
        trigger: ['blur']
    }],
    apply_company_telephone: [{
        required: true,
        message: '请输入开票单位电话',
        trigger: ['blur']
    }],
    apply_company_bank: [{
        required: true,
        message: '请输入开户银行',
        trigger: ['blur']
    }],
    apply_company_account: [{
        required: true,
        message: '请输入开户账号',
        trigger: ['blur']
    }],
    apply_contact: [{
        required: true,
        message: '请输入开票联系人',
        trigger: ['blur']
    }],
    apply_email: [
        {
            required: true,
            message: '请输入邮箱',
            trigger: 'blur',
        },
        {
            type: 'email',
            message: '请输入正确的邮箱',
            trigger: ['blur', 'change'],
        },
    ],
    pay_type: [{
        required: true,
        message: '请输入对应付款方式',
        trigger: ['blur']
    }],
    invoice_content: [{
        required: true,
        message: '请输入开票内容',
        trigger: ['blur']
    }],
    create_user: [{
        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 getDetail = async (row: Record<string, any>) => {
    const data = await apiFinancialInvoiceDetail({
        id: row.id
    })
    setFormData(data)
}


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