<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="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="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="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="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="图例说明">
                            <!-- <material-picker v-model="formData.annex" :limit="100" /> -->
                            <annexUpload :annex="formData.annex" @handleAvatarSuccess="handleAvatarSuccess_four"
                                @delFile="delFileFn" />
                        </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 { timeFormat } from '@/utils/util'
import { isEmail, isIdCard, isPhone } from '@/utils/validate'
import type { PropType } from 'vue'
// 上传文件
import annexUpload from "@/components/annexUpload/index.vue"
const handleAvatarSuccess_four = (response: any) => {
    // @ts-ignore
    response.code != 0 ? formData.annex.push({ uri: response.data.uri, name: response.data.name }) : ElMessage.error(response.msg);
};

// 删除上传的文件
const delFileFn = (index: number) => { formData.annex.splice(index, 1) }
const showDialog = ref(false)
const subentry_engineering = ref('')
const subentry_engineering_code = ref('')
const sub_division_engineering = ref('')
const division_engineering = ref('')
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: '',
    process_step_no: '',
    process_step: '',
    quality_control_points: '',
    division_id: '',
    annex: []
})

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