<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="80%" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="160px" :rules="formRules">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="项目" prop="project_id" @click="showDialog = true" :rules="[{ required: true, message: '不可为空', trigger: 'change' }]">
                            <el-input v-model="project_name" readonly clearable placeholder="请选择项目" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="项目编码" prop="project_id">
                            <el-input v-model="project_code" readonly clearable placeholder="请选择项目" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="材料预算花费比例" prop="material_budget_expenditure_ratio">
                            <el-input v-model="formData.material_budget_expenditure_ratio" clearable placeholder="请输入材料预算花费比例" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="人工预算花费比例" prop="labour_budget_expenditure_ratio">
                            <el-input v-model="formData.labour_budget_expenditure_ratio" clearable placeholder="请输入人工预算花费比例" />
                        </el-form-item> </el-col>
                    <el-col :span="8">
                        <el-form-item label="费用预算花费比例" prop="cost_budget_expenditure_ratio">
                            <el-input v-model="formData.cost_budget_expenditure_ratio" clearable placeholder="请输入费用预算花费比例" />
                        </el-form-item> </el-col>


                    <el-col :span="8">
                        <el-form-item label="分包预算花费比例" prop="subcontract_budget_expenditure_ratio">
                            <el-input v-model="formData.subcontract_budget_expenditure_ratio" clearable placeholder="请输入分包预算花费比例" />
                        </el-form-item> </el-col>

                </el-row>

            </el-form>
        </popup>

        <el-dialog v-model="showDialog" title="选择项目" width="70%">
            <projectDialog @customEvent="customEvent"></projectDialog>
        </el-dialog>
    </div>
</template>

<script lang="ts" setup name="procurementContractEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import projectDialog from '@/components/project/index.vue'
import { apiProjectalarmAdd, apiProjectalarmEdit, apiProjectalarmDetail } from '@/api/project_alarm_set'

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 showDialog = ref(false)
const project_name = ref('')
const project_code = ref('')

import feedback from '@/utils/feedback'
import configs from "@/config"

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑项目预警设置' : '新增项目预警设置'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    material_budget_expenditure_ratio: '',
    labour_budget_expenditure_ratio: '',
    cost_budget_expenditure_ratio: '',
    subcontract_budget_expenditure_ratio: '',

})

//获取值
const customEvent = (e) => {
    formData.project_id = e.id
    project_name.value = e.name
    project_code.value = e.project_code
    showDialog.value = false

}
// 表单验证
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]
        }
    }
    project_name.value = data.project_name
    project_code.value = data.project_code

}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiProjectalarmDetail({
        id: row.id
    })
    setFormData(data)
}


// 提交按钮
const handleSubmit = async () => {

    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiProjectalarmEdit(data)
        : await apiProjectalarmAdd(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>
