<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="auto" :rules="formRules">
                <el-row :gutter="10">
                    <el-col :span="24">
                        <el-form-item label="分部工程" prop="division_id" @click="showDialog = true"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.division_engineering" readonly clearable placeholder="点击选择分部分项工程" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="子分部工程" prop="division_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.sub_division_engineering" clearable disabled placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="分项工程" prop="division_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.subentry_engineering" clearable disabled placeholder="系统自动填写" />

                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="分项工程编码" prop="division_id"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.subentry_engineering_code" clearable disabled
                                placeholder="系统自动填写" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="工序步骤号" prop="process_step_no"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.process_step_no" clearable placeholder="请输入工序步骤号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="工序步骤" prop="process_step"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.process_step" clearable placeholder="请输入工序步骤" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="图例说明">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="质量控制点" prop="quality_control_points"
                            :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-input v-model="formData.quality_control_points" clearable placeholder="请输入质量控制点" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog" title="选择客户" width="70%">
                    <customDialog @customEvent="customEvent"></customDialog>
                </el-dialog>



            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">
import customDialog from '@/components/build_division/index.vue'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { buildprocesssettingsAdd, buildprocesssettingsEdit, buildprocesssettingsDetail } from '@/api/build/build_process_settings'
import type { PropType } from 'vue'

const showDialog = ref(false)

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: '',
    "division_engineering": "",
    "sub_division_engineering": "",
    "subentry_engineering": "",
    "subentry_engineering_code": "",
    process_step_no: '',
    process_step: '',
    quality_control_points: '',
    division_id: '',
    annex: []
})

const customEvent = (e) => {
    formData.division_id = e.id;
    formData.subentry_engineering = e.subentry_engineering
    formData.subentry_engineering_code = e.subentry_engineering_code
    formData.sub_division_engineering = e.sub_division_engineering
    formData.division_engineering = e.division_engineering
    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]
        }
    }


}

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


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



    await formRef.value?.validate()

    const data = { ...formData }
    mode.value == 'edit'
        ? await buildprocesssettingsEdit(data)
        : await buildprocesssettingsAdd(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>
