<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="project_name">
                            <el-input v-model="formData.project_name" clearable placeholder="点击选择项目" readonly
                                @click="showDialog = true" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="节点名称" prop="node_name">
                            <el-input v-model="formData.node_name" clearable placeholder="请输入节点名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="节点类型" prop="node_type">
                            <el-select v-model="formData.node_type" clearable placeholder="请选择节点类型" class="flex-1">
                                <el-option v-for="(item, index) in dictData.check_item_node_type" :key="index"
                                    :label="item.name" :value="parseInt(item.value)" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="节点编号" prop="node_code">
                            <el-input v-model="formData.node_code" clearable placeholder="请输入节点编号" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="父节点">
                            <el-input v-model="formData.p_node_name" clearable placeholder="如果创建的节点为初始节点，可不选择" readonly
                                @click="showDialog1 = true" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>

                    <el-col :span="24">
                        <el-form-item label="检查依据">
                            <editor v-model="formData.inspection_basis" :height="200" width="80vw" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="参考法规">
                            <editor v-model="formData.reference_law" :height="200" width="80vw" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <formTable :formData="formData.check_item_detail" :config="tableConfig"></formTable>
                <el-dialog v-model="showDialog" title="选择项目" width="70%">
                    <dialogTable @customEvent="customEvent" :config="supervision_project" />
                </el-dialog>
                <el-dialog v-model="showDialog1" title="选择父节点" width="70%">
                    <dialogTable :config="supervision_check_item" @customEvent="customEvent1"></dialogTable>
                </el-dialog>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="supervisionCheckItemEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiSupervisionCheckItemAdd, apiSupervisionCheckItemEdit, apiSupervisionCheckItemDetail, apisupervision_check_item_detailLists, apisupervision_check_item_detail_delete } from '@/api/supervision_check_item'
import { supervision_project } from "@/components/dialogTable/dialogTableConfig"
import type { PropType } from 'vue'
import { supervision_check_item } from "@/components/dialogTable/dialogTableConfig"


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 showDialog1 = ref(false)


// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑工程监理--新增检查项' : '新增工程监理--新增检查项'
})

// 表单数据
const formData = reactive({
    id: '',
    project_id: '',
    project_name: "",
    node_name: '',
    node_type: '',
    node_code: '',
    pid: '',
    p_node_name: "",
    inspection_basis: '',
    reference_law: '',
    check_item_detail: []
})
const customEvent = (e) => {
    formData.project_id = e.id
    formData.project_name = e.project_name
    showDialog.value = false
}

const customEvent1 = (e: any) => {
    formData.pid = e.id
    formData.p_node_name = e.node_name
    showDialog1.value = false
}


const tableConfig = reactive(
    {
        title: "检查项",
        tableConfig: [
            {
                label: "检查类别",
                value: 'check_type'

            },
            {
                label: "检查类容",
                value: 'check_content'

            },
            {
                label: "是否必检",
                value: 'must_check',
                select: [
                    {
                        name: '否',
                        value: 0
                    },
                    {
                        name: '是',
                        value: 1
                    }
                ]
            },
        ],
        deleteApi: apisupervision_check_item_detail_delete,
    }
)


// 表单验证
const formRules = reactive<any>({
    project_id: [{
        required: true,
        message: '请选择项目',
        trigger: ['blur']
    }],
    node_name: [{
        required: true,
        message: '请输入节点名称',
        trigger: ['blur']
    }],
    node_type: [{
        required: true,
        message: '请输入节点类型',
        trigger: ['blur']
    }],
    node_code: [{
        required: true,
        message: '请输入节点编号',
        trigger: ['blur']
    }],
    inspection_basis: [{
        required: true,
        message: '请输入检查依据',
        trigger: ['blur']
    }],
    reference_law: [{
        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]
        }
    }
    apisupervision_check_item_detailLists({ node_id: formData.id }).then(res => {
        formData.check_item_detail = res.lists
    })


}

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


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