<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="130px" :rules="formRules">
                <el-form-item label="合同名称" prop="contract_name">
                    <el-input v-model="formData.contract_name" clearable placeholder="点击选择合同" @click="showDialog = true"
                        readonly />
                </el-form-item>
                <el-form-item label="合同类型">
                    <el-input v-model="formData.contract_type" clearable placeholder="系统自动获取" disabled />
                </el-form-item>

                <el-form-item label="甲方签约单位">
                    <el-input v-model="formData.part_a" clearable placeholder="系统自动获取" disabled />
                </el-form-item>
                <el-form-item label="乙方签约单位">
                    <el-input v-model="formData.part_b" clearable placeholder="系统自动获取" disabled />
                </el-form-item>
                <el-form-item label="签订金额(元）">
                    <el-input v-model="formData.sign_money" clearable placeholder="系统自动获取" disabled />
                </el-form-item>
                <el-form-item label="合同签订日期">
                    <el-input v-model="formData.sign_time" clearable placeholder="系统自动获取" disabled />
                </el-form-item>
                <el-form-item label="累计已开票（元）">
                    <el-input v-model="formData.total_invoice_amount" clearable placeholder="系统自动获取" disabled />
                </el-form-item>
                <el-form-item label="累计已到账（元）">
                    <el-input v-model="formData.total_refund_amount" clearable placeholder="系统自动获取" disabled />
                </el-form-item>
                <el-form-item label="开票台账编号" prop="invoice_code">
                    <el-input v-model="formData.invoice_code" clearable placeholder="点击选择开票台账" readonly
                        @click="showDialog1 = true" />
                </el-form-item>
                <el-form-item label="本次到账金额" prop="amount">
                    <el-input v-model="formData.amount" clearable placeholder="请输入本次到账金额" type="number" />
                </el-form-item>
                <el-form-item label="到账日期" prop="date">
                    <el-date-picker class="flex-1 !flex" v-model="formData.date" clearable value-format="YYYY-MM-DD"
                        placeholder="选择到账日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="说明" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入说明" type="textarea" />
                </el-form-item>
                <createUserLable :formData="formData" />
            </el-form>
            <el-dialog v-model="showDialog" title="选择合同" width="70%">
                <dialogTable :config="cost_approved_projects" @customEvent="customEvent" :query="{ status: 2 }">
                </dialogTable>
            </el-dialog>
            <div v-if="showDialog1">
                <el-dialog v-model="showDialog1" title="选择开票台账" width="70%">
                    <dialogTable :config="financial_invoice" @customEvent="customEvent1"
                        :query="{ contract_id: formData.contract_id }">
                    </dialogTable>
                </el-dialog>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="financialRefundEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiFinancialRefundAdd, apiFinancialRefundEdit, apiFinancialRefundDetail } from '@/api/financial_refund'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
import { cost_approved_projects, financial_invoice } 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 showDialog1 = ref(false)



// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑财务管理--到账台账' : '新增财务管理--到账台账'
})

// 表单数据
const formData = reactive({
    id: '',
    contract_id: '',
    contract_name: '',
    invoice_id: '',
    invoice_code: '',
    amount: '',
    date: '',
    remark: '',
    create_user: '',
    create_time: '',
    "contract_type": "",
    "part_a": "",
    "part_b": "",
    "sign_money": "",
    "sign_time": "",
    "invoice_code": "",
    "apply_amount": "",
    "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 customEvent1 = (e) => {
    formData.invoice_id = e.id
    formData.invoice_code = e.invoice_code
    showDialog1.value = false
}


// 表单验证
const formRules = reactive<any>({
    contract_id: [{
        required: true,
        message: '请输入合同id',
        trigger: ['blur']
    }],
    invoice_id: [{
        required: true,
        message: '请输入开票id',
        trigger: ['blur']
    }],
    amount: [{
        required: true,
        message: '请输入本次到账金额',
        trigger: ['blur']
    }],
    date: [{
        required: true,
        message: '请输入到账日期',
        trigger: ['blur']
    }],
    remark: [{
        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 apiFinancialRefundDetail({
        id: row.id
    })
    setFormData(data)
}


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