<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="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入审批流名称" />
                </el-form-item>
                <el-form-item label="流程类型" prop="check_type">
                    <el-input v-model="formData.check_type" clearable placeholder="请输入流程类型" />
                </el-form-item>
                <el-form-item label="应用模块" prop="type">
                    <el-input v-model="formData.type" clearable placeholder="请输入应用模块" />
                </el-form-item>
                <el-form-item label="应用审批类型id" prop="flow_cate">
                    <el-input v-model="formData.flow_cate" clearable placeholder="请输入应用审批类型id" />
                </el-form-item>
                <el-form-item label="应用部门ID（0为全部）1,2,3" prop="department_ids">
                    <el-input v-model="formData.department_ids" clearable placeholder="请输入应用部门ID（0为全部）1,2,3" />
                </el-form-item>
                <el-form-item label="抄送人ID" prop="copy_uids">
                    <el-input v-model="formData.copy_uids" clearable placeholder="请输入抄送人ID" />
                </el-form-item>
                <el-form-item label="流程说明" prop="remark">
                    <el-input v-model="formData.remark" clearable placeholder="请输入流程说明" />
                </el-form-item>
                <el-form-item label="流程数据序列化" prop="flow_list">
                    <el-input v-model="formData.flow_list" clearable placeholder="请输入流程数据序列化" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="oaFlowEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiOaFlowAdd, apiOaFlowEdit, apiOaFlowDetail } from '@/api/oa_flow'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
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 popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑审批流程表' : '新增审批流程表'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    check_type: '',
    type: '',
    flow_cate: '',
    department_ids: '',
    copy_uids: '',
    remark: '',
    flow_list: '',
})


// 表单验证
const formRules = reactive<any>({
    name: [{
        required: true,
        message: '请输入审批流名称',
        trigger: ['blur']
    }],
    check_type: [{
        required: true,
        message: '请输入流程类型',
        trigger: ['blur']
    }],
    type: [{
        required: true,
        message: '请输入应用模块',
        trigger: ['blur']
    }],
    flow_cate: [{
        required: true,
        message: '请输入应用审批类型id',
        trigger: ['blur']
    }],
    department_ids: [{
        required: true,
        message: '请输入应用部门ID（0为全部）1,2,3',
        trigger: ['blur']
    }],
    copy_uids: [{
        required: true,
        message: '请输入抄送人ID',
        trigger: ['blur']
    }],
    remark: [{
        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]
        }
    }


}

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


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