<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="140px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="所在目录" prop="directory">
                            <el-input v-model="formData.directory" clearable placeholder="请输入所在目录" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单位工程名称" prop="unit_project_name">
                            <el-input v-model="formData.unit_project_name" clearable placeholder="请输入单位工程名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="时间" prop="time">
                            <el-date-picker class="flex-1 !flex" v-model="formData.time" clearable type="date"
                                value-format="YYYY-MM-DD" placeholder="请选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="形象进度" prop="image_progress">
                            <el-input v-model="formData.image_progress" clearable placeholder="请输入形象进度" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="申报造价(土建)" prop="declared_cost_tj">
                            <el-input v-model="formData.declared_cost_tj" clearable placeholder="请输入申报造价(土建)"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="合同付款比例（%）(%)" prop="dcntract_payment_rate">
                            <el-input v-model="formData.dcntract_payment_rate" clearable placeholder="请输入合同付款比例（%）(%)"
                                type="number" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="申报造价(安装)" prop="declared_cost_az">
                            <el-input v-model="formData.declared_cost_az" clearable placeholder="请输入申报造价(安装)"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="申报造价(其他)" prop="declared_cost_other">
                            <el-input v-model="formData.declared_cost_other" clearable placeholder="请输入申报造价(其他)"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="进度申报造价小计" prop="jdsbzj_xiaoji">
                            <el-input v-model="formData.jdsbzj_xiaoji" clearable placeholder="系统自动计算" disabled
                                type="number" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="审核造价(土建)" prop="audit_cost_tj">
                            <el-input v-model="formData.audit_cost_tj" clearable placeholder="请输入审核造价(土建)" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核造价(安装)" prop="audit_cost_az">
                            <el-input v-model="formData.audit_cost_az" clearable placeholder="请输入审核造价(安装)" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审核造价(其他)" prop="audit_cost_other">
                            <el-input v-model="formData.audit_cost_other" clearable placeholder="请输入审核造价(其他)"
                                type="number" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="进度审核造价小计" prop="jdshzj_xiaoji">
                            <el-input v-model="formData.jdshzj_xiaoji" clearable placeholder="请输入进度审核造价小计" type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="审减额(元)" prop="deduction_amount">
                            <el-input v-model="formData.deduction_amount" clearable placeholder="系统自动计算" disabled
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="完成产值比例（%）(%)" prop="completed_rate">
                            <el-input v-model="formData.completed_rate" clearable placeholder="请输入完成产值比例（%）(%)"
                                type="number" />
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="合同工程款总额" prop="contract_total_amount">
                            <el-input v-model="formData.contract_total_amount" clearable placeholder="请输入合同工程款总额"
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="累计完成工程款" prop="ljwc_funds">
                            <el-input v-model="formData.ljwc_funds" clearable placeholder="系统自动计算" disabled type="number" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="付款比例（%）(%)" prop="payment_rate">
                            <el-input v-model="formData.payment_rate" clearable placeholder="系统自动计算" disabled
                                type="number" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="当期应付工程款(元)" prop="current_payable_funds">
                            <el-input v-model="formData.current_payable_funds" clearable placeholder="系统自动计算" disabled
                                type="number" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="projectProgressPaymentDetailEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProjectProgressPaymentDetailAdd, apiProjectProgressPaymentDetailEdit, apiProjectProgressPaymentDetailDetail } from '@/api/project_progress_payment_detail'
import { timeFormat } from '@/utils/util'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目进度款明细' : '新增项目进度款明细'
})

// 表单数据
const formData = reactive({
    id: '',
    project_progress_payment_id: '',
    directory: '',
    unit_project_name: '',
    time: '',
    image_progress: '',
    declared_cost_tj: '',
    declared_cost_az: '',
    declared_cost_other: '',
    jdsbzj_xiaoji: '',
    audit_cost_tj: '',
    audit_cost_az: '',
    audit_cost_other: '',
    jdshzj_xiaoji: '',
    deduction_amount: '',
    completed_rate: '',
    contract_total_amount: '',
    ljwc_funds: '',
    current_payable_funds: '',
    payment_rate: '',
    dcntract_payment_rate: '',
    remark: '',

})

// 监听计算
watch(
    () => formData,
    (value) => {
        formData.jdsbzj_xiaoji = Number(formData.declared_cost_tj) + Number(formData.declared_cost_az) + Number(formData.declared_cost_other)
        formData.jdshzj_xiaoji = Number(formData.audit_cost_tj) + Number(formData.audit_cost_az) + Number(formData.audit_cost_other)
        formData.payment_rate = formData.dcntract_payment_rate
        formData.deduction_amount = formData.jdsbzj_xiaoji - formData.jdshzj_xiaoji
        formData.ljwc_funds = formData.jdshzj_xiaoji
        formData.current_payable_funds = (Number(formData.ljwc_funds) * Number(formData.payment_rate)) || 0
    },
    {
        deep: true
    }
)



// 表单验证
const formRules = reactive<any>({

})


// 获取详情
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 apiProjectProgressPaymentDetailDetail({
        id: row.id
    })
    setFormData(data)
}


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