<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="90px" :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 clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同名称">
                            <el-input v-model="formData.contract_name" disabled clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同类型">
                            <el-input v-model="formData.contract_type" disabled clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="甲方单位">
                            <el-input v-model="formData.part_a" disabled clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="乙方单位">
                            <el-input v-model="formData.part_b" disabled clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订金额">
                            <el-input v-model="formData.sign_money" disabled clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="签订时间">
                            <el-input v-model="formData.sign_time" disabled clearable />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="累计已开票">
                            <el-input v-model="formData.total_invoice_amount" disabled clearable />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="累计已到账">
                            <el-input v-model="formData.total_refund_amount" disabled clearable />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="结算金额">
                            <el-input v-model="formData.amount" clearable placeholder="请输入结算金额" type="number" />
                        </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="备注">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件">
                            <upload-annex :formData="formData"></upload-annex>
                        </el-form-item>
                    </el-col>

                </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="zjzxSettlementEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiZjzxSettlementAdd, apiZjzxSettlementEdit } from '@/api/zjzx_settlement'
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: "",
    amount: '',
    date: '',
    remark: '',
    annex: [],

    "project_num": "",
    "contract_name": "",
    "contract_type": "",
    "part_a": "",
    "part_b": "",
    "sign_money": "",
    "sign_time": "",
    "total_invoice_amount": 0,
    "total_refund_amount": 0




})

const customEvent = (e) => {
    let list = ['project_num', , 'project_name', '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.project_id = e.id
    formData.sign_money = e.contract.money
    formData.sign_time = e.contract.create_time
    showDialog.value = false


}



// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    amount: [{
        required: true,
        message: '请输入结算金额',
        trigger: ['blur']
    }],
    date: [{
        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 apiZjzxSettlementEdit(data)
        : await apiZjzxSettlementAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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



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