<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="60vw" @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="company_name">
                            <el-input v-model="formData.company_name" clearable placeholder="请输入所属单位"
                                @click="showDialog1 = true" readonly />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="设备名称" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入设备名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质名称1" prop="qualification_name_one">
                            <el-input v-model="formData.qualification_name_one" clearable placeholder="请输入资质名称1" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="认证机构1" prop="certification_body_one">
                            <el-input v-model="formData.certification_body_one" clearable placeholder="请输入认证机构1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质编号1" prop="qualification_number_one">
                            <el-input v-model="formData.qualification_number_one" clearable placeholder="请输入资质编号1" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="发证日期1" prop="get_date_one">
                            <el-date-picker class="flex-1 !flex" v-model="formData.get_date_one" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="有效日期1" prop="effective_date_one">
                            <el-date-picker class="flex-1 !flex" v-model="formData.effective_date_one" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质1状态 " prop="qualification_one_status">
                            <el-select class="flex-1" v-model="formData.qualification_one_status" clearable
                                placeholder="请选择">
                                <el-option label="有效" :value="0" />
                                <el-option label="无效" :value="1" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质名称2" prop="qualification_name_two">
                            <el-input v-model="formData.qualification_name_two" clearable placeholder="请输入资质名称2" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="认证机构2" prop="certification_body_two">
                            <el-input v-model="formData.certification_body_two" clearable placeholder="请输入认证机构2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质编号2" prop="qualification_number_two">
                            <el-input v-model="formData.qualification_number_two" clearable placeholder="请输入资质编号2" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="发证日期2" prop="get_date_two">
                            <el-date-picker class="flex-1 !flex" v-model="formData.get_date_two" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="8">
                        <el-form-item label="有效日期2" prop="effective_date_two">
                            <el-date-picker class="flex-1 !flex" v-model="formData.effective_date_two" clearable
                                value-format="YYYY-MM-DD " placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="资质2状态" prop="qualification_two_status">
                            <el-select class="flex-1" v-model="formData.qualification_two_status" clearable
                                placeholder="请选择">
                                <el-option label="有效" :value="0" />
                                <el-option label="无效" :value="1" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <createUserLable :formData="formData" flag></createUserLable>
                    <el-col :span="8">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="formData.remark" clearable placeholder="请输入备注" type="textarea" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="附件" prop="annex">
                            <uploadAnnex :formData="formData"></uploadAnnex>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-dialog v-model="showDialog1" title="选择单位" width="70%">
                    <dialogTable :config="supervision_participating_units_qualifications" @customEvent="customEvent1">
                    </dialogTable>
                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionLargeEquipmentEdit">
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import {
    apiSupervisionLargeEquipmentAdd,
    apiSupervisionLargeEquipmentEdit,
} from "@/api/supervision_large_equipment";
import { supervision_participating_units_qualifications } 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 showDialog1 = ref(false);

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == "edit"
        ? "编辑工程监理--大型设备及仪器具"
        : "新增工程监理--大型设备及仪器具";
});

// 表单数据
const formData = reactive({
    id: "",
    unit_qualification_id: "",
    company_name: "",
    name: "",
    qualification_name_one: "",
    certification_body_one: "",
    qualification_number_one: "",
    get_date_one: "",
    effective_date_one: "",
    qualification_one_status: "",
    qualification_name_two: "",
    certification_body_two: "",
    qualification_number_two: "",
    get_date_two: "",
    effective_date_two: "",
    qualification_two_status: "",
    remark: "",
    annex: [],
    create_user: "",
    create_time: "",
});
const customEvent1 = (e) => {
    formData.unit_qualification_id = e.id;
    formData.company_name = e.company_name;
    showDialog1.value = false;
};

// 表单验证
const formRules = reactive<any>({
    unit_qualification_id: [
        {
            required: true,
            message: "请选择项目",
            trigger: ["blur"],
        },
    ],
    name: [
        {
            required: true,
            message: "请输入设备名称",
            trigger: ["blur"],
        },
    ],
    qualification_name_one: [
        {
            required: true,
            message: "请输入资质名称1",
            trigger: ["blur"],
        },
    ],
    certification_body_one: [
        {
            required: true,
            message: "请输入认证机构1",
            trigger: ["blur"],
        },
    ],
    qualification_number_one: [
        {
            required: true,
            message: "请输入资质编号1",
            trigger: ["blur"],
        },
    ],
    get_date_one: [
        {
            required: true,
            message: "请输入发证日期1",
            trigger: ["blur"],
        },
    ],
    effective_date_one: [
        {
            required: true,
            message: "请输入有效日期1",
            trigger: ["blur"],
        },
    ],
    qualification_one_status: [
        {
            required: true,
            message: "请输入资质1状态 0-有效 1-无效",
            trigger: ["blur"],
        },
    ],
    qualification_name_two: [
        {
            required: true,
            message: "请输入资质名称2",
            trigger: ["blur"],
        },
    ],
    certification_body_two: [
        {
            required: true,
            message: "请输入认证机构2",
            trigger: ["blur"],
        },
    ],
    qualification_number_two: [
        {
            required: true,
            message: "请输入资质编号2",
            trigger: ["blur"],
        },
    ],
    get_date_two: [
        {
            required: true,
            message: "请输入发证日期2",
            trigger: ["blur"],
        },
    ],
    effective_date_two: [
        {
            required: true,
            message: "请输入有效日期2",
            trigger: ["blur"],
        },
    ],
    qualification_two_status: [
        {
            required: true,
            message: "请输入资质2状态 0-有效 1-无效",
            trigger: ["blur"],
        },
    ],
    remark: [
        {
            required: true,
            message: "请输入备注",
            trigger: ["blur"],
        },
    ],
    create_user: [
        {
            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 getDetail = async (row: Record<string, any>) => {
    const data = await apiSupervisionLargeEquipmentDetail({
        id: row.id,
    });
    setFormData(data);
};

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