<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="110px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目名称" prop="project_id">
                            <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" disabled />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="合同名称">
                            <el-input v-model="formData.contract_name" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同类型">
                            <el-input v-model="formData.contract_type" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方签约单位">
                            <el-input v-model="formData.part_a" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="乙方签约单位">
                            <el-input v-model="formData.part_b" disabled />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="签约金额">
                            <el-input v-model="formData.sign_money" disabled />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="签订时间">
                            <el-input v-model="formData.sign_time" disabled />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="开票申请金额">
                            <el-input v-model="formData.apply_amount" disabled />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="累计已开票">
                            <el-input v-model="formData.total_invoice_amount" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="累计已到账">
                            <el-input v-model="formData.total_refund_amount" disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="开票编号" prop="invoice_id">
                            <el-input v-model="formData.invoice_code" clearable placeholder="点击选择开票台账"
                                @click="showDialog1 = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="本次到账金额" prop="amount">
                            <el-input v-model="formData.amount" clearable placeholder="请输入本次到账金额" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <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-col>
                    <el-col :span="8">
                        <el-form-item label="说明" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入说明" type="textarea" />
                        </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>
            <div v-if="showDialog1">
                <el-dialog v-model="showDialog1" title="选择开票编号" width="70%">
                    <dialogTable :config="zjzx_invoice" :query="{ project_id: formData.project_id }"
                        @customEvent="customEvent1">
                    </dialogTable>
                </el-dialog>
            </div>
        </popup>
    </div>
</template>

<script lang="ts" setup name="zjzxRefundEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiZjzxRefundAdd, apiZjzxRefundEdit, apiZjzxRefundDetail } from '@/api/zjzx_refund'
import type { PropType } from 'vue'
import { cost_project, zjzx_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: '',
    project_id: '',
    project_name: "",
    invoice_id: '',
    invoice_code: '',
    amount: '',
    date: '',
    remark: '',
    create_user: '',
    create_time: '',
    "project_num": "",
    "contract_name": "",
    "contract_type": "",
    "part_a": "",
    "part_b": "",
    "sign_money": "",
    "sign_time": "",
    "apply_amount": "",
    "total_invoice_amount": 0,
    "total_refund_amount": 0
})

const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
    formData.invoice_id = ''
    formData.invoice_code = ''
    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 customEvent1 = (e) => {
    formData.invoice_id = e.id
    formData.invoice_code = e.invoice_code
    formData.apply_amount = e.apply_amount
    showDialog1.value = false

}

// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        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']
    }],
    create_time: [{
        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 apiZjzxRefundDetail({
        id: row.id
    })
    setFormData(data)
}


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