<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="84px" :rules="formRules">
                <el-form-item label="组织" prop="org_id">
                    <el-select clearable v-model="formData.org_id" placeholder="请选择组织" @change="area_change"
                        class="flex-1">
                        <el-option v-for="(item, index) in orglist" :key="index" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入部门名称" />
                </el-form-item>
                <el-form-item label="负责人" prop="leader">
                    <el-input v-model="formData.leader_name" readonly placeholder="请输入负责人姓名" :maxlength="30"
                        @click="openDialog" />
                </el-form-item>
                <el-form-item label="联系电话" prop="mobile">
                    <el-input v-model="formData.mobile" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <div>
                        <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                        <div class="form-tips">默认为0， 数值越大越排前</div>
                    </div>
                </el-form-item>
                <el-form-item label="部门状态">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
        <personnelSelector v-if="showDiolg" ref="personnel" type="1" @confirm="submituser" />

    </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { deptEdit, deptAdd, deptDetail, deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
import personnelSelector from "@/components/personnelselector/index.vue"
import { nextTick } from 'vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const showDiolg = ref(false)
const personnel = ref(null)

const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑部门' : '新增部门'
})
const formData = reactive({
    id: '',
    org_id: '' as string | number,
    name: '',
    leader: '',
    leader_name: '',
    mobile: '',
    status: 1,
    sort: ''
})

const submituser = (e) => {
    formData.leader = e.id
    formData.leader_name = e.name
    showDiolg.value = false
}

const openDialog = async () => {
    showDiolg.value = true
    await nextTick()
    personnel.value?.open()
}


const orglist = reactive([])

const jobs = reactive([])
const checkMobile = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback()
    } else {
        const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
        if (reg.test(value)) {
            callback()
        } else {
            return callback(new Error('请输入正确的手机号'))
        }
    }
}
const formRules = {
    name: [
        {
            required: true,
            message: '请输入部门名称',
            trigger: ['blur']
        }
    ],

    mobile: [
        {
            validator: checkMobile,
            trigger: ['blur']
        }
    ]
}


const getlista = () => {
    getAll().then((res) => {
        Object.assign(orglist, res)

    })
}
const area_change = (e: any) => {
    formData.org_id = e
    let data = { 'org_id': e }
    deptAll(data).then((res) => {
        Object.assign(jobs, res)

    })
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await deptEdit(formData) : await deptAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = (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 deptDetail({
        id: row.id
    })

    setFormData(data)
}

const handleClose = () => {
    emit('close')
}
getlista()
defineExpose({
    open,
    setFormData,
    getDetail,

})
</script>
