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

                <el-form-item label="部门名称" prop="department_ids"
                    :rules="[{ required: true, message: '不可为空', trigger: 'blur' }]">
                    <el-select v-model="formData.department_ids" multiple placeholder="请选择部门" class="flex-1">
                        <el-option v-for="item in list2" :key="item.value" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="所属分类" prop="type">
                    <el-select class="flex-1" v-model="formData.type" clearable placeholder="请选择所属分类">
                        <el-option v-for="(item, index) in dictData.flow_type" :key="index" :label="item.name"
                            :value="parseInt(item.value)" />
                    </el-select>
                </el-form-item>
                <el-form-item label="审批名称" prop="title">
                    {{ dictData.show_status }}
                    <el-input v-model="formData.title" clearable placeholder="请输入审批名称" />
                </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="status" v-if="mode == 'edit'">
                    <el-radio-group v-model="formData.status" placeholder="请选择状态">
                        <el-radio v-for="(item, index) in dictData.flow_type_status" :key="index" :label="(item.value)">
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="flowTypeEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiFlowTypeAdd, apiFlowTypeEdit, apiFlowTypeDetail } from '@/api/flow_type'
import type { PropType } from 'vue'
import { deptAll } from '@/api/org/department'
import { getAll } from '@/api/org/organization'

let props = 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 list1 = reactive([])
const list2 = reactive([])

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑审批类型' : '新增审批类型'
})

// 表单数据
const formData = reactive({
    id: '',
    type: '',
    title: '',
    name: '',
    department_ids: [],
    status: '',
    org_id: "",
})


// 表单验证
const formRules = reactive<any>({
    type: [{
        required: true,
        message: '请选择所属分类',
        trigger: ['blur']
    }],
    title: [{
        required: true,
        message: '请输入审批名称',
        trigger: ['blur']
    }],
    name: [{
        required: true,
        message: '请输入审批标识',
        trigger: ['blur']
    }],
    department_ids: [{
        required: true,
        message: '请输入应用部门',
        trigger: ['blur']
    }],
    status: [{
        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]
        }
    }
    formData.status = String(formData.status)
    formData.department_ids = data.department_ids.map(str => parseInt(str))
    deptrmt(formData.org_id)

}

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


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData, }
    mode.value == 'edit'
        ? await apiFlowTypeEdit(data)
        : await apiFlowTypeAdd(data)
    popupRef.value?.close()
    emit('success')
}

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

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


//获取所有组织
const getlist = () => {
    getAll().then((res) => {
        Object.assign(list1, res)

    })
}
//获取部门
const deptrmt = (e: any) => {
    getlist1(e)

}
//获取所有部门
const getlist1 = (id: any) => {
    deptAll({ 'org_id': id }).then((res) => {
        list2.splice(0, list2.length, ...res)
    })
}

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