<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="name" v-if="mode == 'add'">
                    <el-select clearable v-model="org_id1" placeholder="请选择部门" @change="area_change">
                        <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="dept_id">
                    <el-select clearable v-model="formData.dept_id" placeholder="请选择部门" @change="area_change1">
                        <el-option v-for="(item, index) in jobs" :key="index" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="岗位名称" prop="name">

                    <el-input class="w-[280px]" v-model="formData.name" clearable @keyup.enter="getLists" />
                </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="备注" prop="remark">
                    <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" maxlength="200" show-word-limit />
                </el-form-item> -->
                <el-form-item label="岗位状态" required prop="status">
                    <el-switch v-model="formData.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { jobsEdit, jobsAdd, jobsDetail, jobsAll } from '@/api/org/post'
import Popup from '@/components/popup/index.vue'
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const orglist = reactive([])
const org_id1 = ref('')
const org_id2 = ref('')
const jobs = reactive([])
const postions = reactive([])
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑岗位' : '新增岗位'
})
const formData = reactive({
    id: '',
    name: '',
    dept_id: '',
    sort: 0,
    status: 1
})

const formRules = {
    dept_id: [
        {
            required: true,
            message: '请选择部门',
            trigger: ['change']
        }
    ],
    status: [
        {
            required: true,
            message: '请选择状态',
            trigger: ['change']
        }
    ],
    name: [
        {
            required: true,
            message: '请输入岗位名称',
            trigger: ['blur']
        }
    ]
}
const getlista = () => {
    getAll().then((res) => {
        // orglist = res.data

        Object.assign(orglist, res)

    })
}
const area_change = (e: any) => {


    let data = { 'org_id': e }
    // console.log(data)
    deptAll(data).then((res) => {
        // orglist = res.data
        console.log(res, '2222')
        Object.assign(jobs, res)

    })
}
const area_change1 = (e: any) => {

    jobsAll({ dept_id: e }).then((res) => {

        Object.assign(postions, res)

    })


}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await jobsEdit(formData) : await jobsAdd(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 jobsDetail({
        id: row.id
    })

    if (mode.value != 'add') {
        deptAll({ 'org_id': data.org_id }).then((res) => {
            // orglist = res.data
            console.log(res, '2222')
            Object.assign(jobs, res)

            setFormData(data)
        })
    } else {
        setFormData(data)
    }

}

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

})
</script>
