<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="org_id" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select v-model="formData.org_id" clearable placeholder="请选择组织" @change="deptrmt">
                                <el-option v-for="(item, index) in list1" :key="index" :label="item.name" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="部门名称" prop="dept_id" :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                            <el-select v-model="formData.dept_id" clearable placeholder="请选择部门">
                                <el-option v-for="( item, index ) in  list2 " :key="index" :label="item.name" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col>



                    <el-col :span="24">
                        <el-form-item label="检查性质名称" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="请输入检查性质名称" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="24">
                        <el-form-item label="创建人" prop="user">
                            <el-input v-model="formData.user" clearable placeholder="请输入创建人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="说明" prop="illustrate">
                            <el-input v-model="formData.illustrate" type="textarea" clearable placeholder="请输入说明" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="创建日期" prop="date">
                            <el-date-picker v-model="formData.date" clearable type="date" value-format="YYYY-MM-DD" placeholder="选择创建日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>


                </el-row>





            </el-form>
        </popup>

    </div>
</template>

<script lang="ts" setup name="projectEdit">

import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { qualitychecknatureAdd, qualitychecknatureEdit, qualitychecknatureDetail } from '@/api/quality_check_nature'
import { toChinesNum } from "@/utils/util";
import { getAllProjectTypes } from '@/api/projecttype'
import { timeFormat } from '@/utils/util'
import { isEmail, isIdCard, isPhone } from '@/utils/validate'
import type { PropType } from 'vue'
import configs from "@/config"
import useUserStore from "@/stores/modules/user";
const protype = reactive([])
const base_url = configs.baseUrl + configs.urlPrefix
const userStore = useUserStore();
const active = ref(0)
const formDataannex = reactive([])
const list1 = reactive([])
const list2 = reactive([])
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
const userInfo = userStore.userInfo

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 getlist = () => {
    getAll().then((res) => {
        Object.assign(list1, res)

    })
}
//获取部门
const deptrmt = (e) => {
    getlist1(e)
}
//获取所有部门
const getlist1 = (id: any) => {
    deptAll({ 'org_id': id }).then((res) => {
        Object.assign(list2, res)
    })
}

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑检查性质表' : '新增检查性质表'
})

// 表单数据
const formData = reactive({
    id: '',
    user: "",
    illustrate: "",
    date: '',
    name: "",

    dept_id: '',
    org_id: "",

})


// 表单验证
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]
        }
    }
    if (data.dept_id) {
        getlist1(data.dept_id
        )
    }



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


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


    await formRef.value?.validate()


    const data = { ...formData }
    mode.value == 'edit'
        ? await qualitychecknatureEdit(data)
        : await qualitychecknatureAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
    getlist()

}

// 关闭回调
const handleClose = () => {
    emit('close')
}



defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
