<template>
    <el-dialog :title="title" v-model="visible" width="60%"
               :close-on-click-modal="false" draggable>
        <el-form ref="dataFormRef" :model="form" :rules="dataRules" label-width="90px" v-loading="loading" :disabled="operType === 'view'">
            <el-row :gutter="24">

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.defFlowId')" prop="defFlowId">
                        <el-select v-model="form.defFlowId" :placeholder="t('flowClazz.inputDefFlowIdTip')" clearable filterable
                                   @change="defFlowIdChange">
                            <el-option v-for="(item, index) in dicData.defFlowId" :key="index" :label="item.flowName" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.flowNodeId')" prop="flowNodeId">
                        <el-select v-model="form.flowNodeId" :placeholder="t('flowClazz.inputFlowNodeIdTip')" clearable filterable
                                   @change="cascadeChange('flowNodeId', ['nodeJobId'])">
                            <el-option v-for="(item, index) in cascadeDic.flowNodeId" :key="index" :label="item.nodeName" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.clazz')" prop="clazz">
                        <el-input v-model="form.clazz" :placeholder="t('flowClazz.inputClazzTip')"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.httpUrl')" prop="httpUrl">
                        <el-input v-model="form.httpUrl" :placeholder="t('flowClazz.inputHttpUrlTip')"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.httpMethod')" prop="httpMethod">
                        <el-select v-model="form.httpMethod" :placeholder="t('flowClazz.inputHttpMethodTip')">
                            <el-option v-for="(item, index) in DIC_PROP.HTTP_METHODS" :key="index" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.methods')" prop="methods">
                        <el-select v-model="form.methods" :placeholder="t('flowClazz.inputMethodsTip')" clearable filterable multiple>
                            <el-option v-for="(item, index) in DIC_PROP.FLOW_METHODS" :key="index" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.valType')" prop="valType">
                        <el-select v-model="form.valType" :placeholder="t('flowClazz.inputValTypeTip')" clearable filterable>
                            <el-option v-for="(item, index) in DIC_PROP.VAL_TYPE.slice(2, 5)" :key="index" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.varKeyVal')" prop="varKeyVal">
                        <el-input v-model="form.varKeyVal" :placeholder="t('flowClazz.inputVarKeyValTip')" clearable/>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.operator')" prop="operator">
                        <el-select v-model="form.operator" :placeholder="t('flowClazz.inputOperatorTip')" clearable filterable>
                            <el-option v-for="(item, index) in DIC_PROP.OPERATOR" :key="index" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.varVal')" prop="varVal">
                        <el-input v-model="form.varVal" :placeholder="t('flowClazz.inputVarValTip')"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.type')" prop="type">
                        <el-select v-model="form.type" :placeholder="t('flowClazz.inputTypeTip')" clearable filterable>
                            <el-option v-for="(item, index) in DIC_PROP.CLAZZ_TYPE" :key="index" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.sort')" prop="sort">
                        <el-input-number :min="1" :max="1000" v-model="form.sort" :placeholder="t('flowClazz.inputSortTip')"></el-input-number>
                    </el-form-item>
                </el-col>

                <el-col :span="12" class="mb20">
                    <el-form-item :label="t('flowClazz.remark')" prop="remark">
                        <el-input v-model="form.remark" type="textarea" :placeholder="t('flowClazz.inputRemarkTip')"/>
                    </el-form-item>
                </el-col>

            </el-row>
        </el-form>
        <template #footer v-if="operType !== 'view'">
        <span class="dialog-footer">
          <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
        </span>
        </template>
    </el-dialog>
</template>

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

    import { useMessage } from "/@/hooks/message";
    import { getObj, addObj, putObj } from '/@/api/jsonflow/flow-clazz'
    import { useI18n } from "vue-i18n"
    import { rule } from '/@/utils/validate';
    import {onCascadeChange, onLoadDicUrl} from "/@/flow/components/convert-name/convert";
    import {notifyLeft} from "/@/flow";
    const emit = defineEmits(['refresh']);

    const { t } = useI18n();

    // 定义变量内容
    const dataFormRef = ref();
    const visible = ref(false);
    const loading = ref(false);
    const operType = ref(false);
    const title = ref('');
    // 定义字典
    const dicData = reactive({});
    const cascadeDic = reactive({});
    const onLoad = onLoadDicUrl({key: "defFlowId"});
    const onCascade = onCascadeChange(cascadeDic, {key: "defFlowId", cascades: ["flowNodeId"]});
    onMounted(() => {
        onLoad(dicData);
    });
    function cascadeChange(key, cascades){
        onCascade(form, {key: key, cascades: cascades});
    }
    const defFlowIdChange = (newVal: any) => {
        onCascade(form, {key: 'defFlowId', cascades: ['flowNodeId']});
        if (newVal) {
            const defFlow = dicData.defFlowId.find(f => f.id === newVal)
            form.flowKey = defFlow.flowKey
        } else {
            form.flowKey = null
        }
    }

    // 提交表单数据
    const form = reactive({
        sort: 0,
        defFlowId: '',
        flowNodeId: '',
        clazz: '',
        httpUrl: '',
        httpMethod: 'GET',
        methods: '',
        valType: '',
        varKeyVal: '',
        operator: '',
        varVal: '',
        remark: '',
        type: '',
    });

    // 定义校验规则
    const dataRules = ref({
        defFlowId: [{required: true, message: '流程名称不能为空', trigger: 'blur'}],
        methods: [{required: true, message: '方法名称不能为空', trigger: 'blur'}],
        type: [{required: true, message: '事件类型不能为空', trigger: 'blur'}],
    })

    // 打开弹窗
    const openDialog = (type: string, id: string) => {
        visible.value = true
        operType.value = type;
        form.id = ''

        if (type === 'add') {
            title.value = t('common.addBtn');
        } else if (type === 'edit') {
            title.value = t('common.editBtn');
        } else if (type === 'view') {
            title.value = t('common.viewBtn');
        }

        // 重置表单数据
        nextTick(() => {
            dataFormRef.value?.resetFields();
        });

        // 获取FlowClazz信息
        if (id) {
            form.id = id
            getFlowClazzData(id)
        }
    };

    // 提交
    const onSubmit = async () => {
        const valid = await dataFormRef.value.validate().catch(() => {});
        if (!valid) return false;

        if (!form.clazz && !form.httpUrl) {
            notifyLeft('请填写 监听类 或者 Http请求地址', 'warning')
            return false
        }

        try {
            loading.value = true;
            form.id ? await putObj(form) : await addObj(form);
            useMessage().success(t(form.id ? 'common.editSuccessText' : 'common.addSuccessText'));
            visible.value = false;
            emit('refresh');
        } catch (err: any) {
            useMessage().error(err.msg);
        } finally {
            loading.value = false;
        }
    };

    // 初始化表单数据
    const getFlowClazzData = (id: string) => {
        // 获取数据
        loading.value = true
        getObj(id).then((res: any) => {
            Object.assign(form, res.data)
            onCascade(form);
        }).finally(() => {
            loading.value = false
        })
    };

    // 暴露变量
    defineExpose({
        openDialog
    });
</script>
