<template>
    <div class="layout-padding">
        <div class="layout-padding-auto layout-padding-view">
            <el-row v-show="showSearch">
                <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
                    <el-form-item :label="t('flowrule.type')" prop="type">
                        <el-select v-model="state.queryForm.type" :placeholder="t('flowrule.inputTypeTip')" clearable filterable>
                            <el-option v-for="(item, index) in DIC_PROP.PARAM_RULE_TYPE" :key="index" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="t('flowrule.valType')" prop="type">
                        <el-select v-model="state.queryForm.valType" :placeholder="t('flowrule.inputValTypeTip')" clearable filterable>
                            <el-option v-for="(item, index) in DIC_PROP.VAL_TYPE" :key="index" :label="item.label" :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item :label="$t('flowrule.defFlowId')" prop="defFlowId">
                        <el-select v-model="state.queryForm.defFlowId" :placeholder="t('flowrule.inputDefFlowIdTip')"
                                   clearable filterable @change="cascadeChange('defFlowId', ['flowNodeId', 'flowNodeRelId'])">
                            <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-form-item :label="t('flowrule.flowNodeId')" prop="flowNodeId">
                        <el-select v-model="state.queryForm.flowNodeId" :placeholder="t('flowrule.inputFlowNodeIdTip')" clearable filterable>
                            <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-form-item :label="t('flowrule.flowNodeRelId')" prop="flowNodeRelId">
                        <el-select v-model="state.queryForm.flowNodeRelId" :placeholder="t('flowrule.inputFlowNodeRelIdTip')" clearable filterable>
                            <el-option v-for="(item, index) in cascadeDic.flowNodeRelId" :key="index" :label="item.label" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item class="ml2">
                        <el-button icon="search" type="primary" @click="getDataList">
                            {{ $t('common.queryBtn') }}
                        </el-button>
                        <el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
                    </el-form-item>
                </el-form>
            </el-row>
            <el-row>
                <div class="mb8" style="width: 100%">
                    <el-tooltip placement="top">
                        <template #content>
                            {{ $t('common.addBtn') }}
                        </template>
                        <el-button icon="Plus" type="primary" class="ml10" @click="formDialogRef.openDialog('add')"
                                   v-auth="'jsonflow_flowrule_add'">
                        </el-button>
                    </el-tooltip>

                    <el-tooltip placement="top">
                        <template #content>
                            {{ $t('common.delBtn') }}
                        </template>
                        <el-button plain :disabled="multiple" icon="Delete" type="primary" class="ml10"
                                   v-auth="'jsonflow_flowrule_del'" @click="handleDelete(selectObjs)">
                        </el-button>
                    </el-tooltip>

                    <right-toolbar v-model:showSearch="showSearch" class="ml10" style="float: right;margin-right: 20px"
                                   :export="'jsonflow_flowrule_export'" @exportExcel="exportExcel"
                                   @queryTable="getDataList"></right-toolbar>
                </div>
            </el-row>
            <el-table :data="state.dataList" v-loading="state.loading" style="width: 100%"
                      @selection-change="handleSelectionChange" @sort-change="sortChangeHandle">
                <el-table-column type="selection" width="40" align="center" />
                <el-table-column fixed="left" type="index" :label="t('flowrule.index')" width="40" />
                <el-table-column prop="type" :label="t('flowrule.type')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.PARAM_RULE_TYPE" :value="scope.row.type"></dict-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="valType" :label="t('flowrule.valType')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.VAL_TYPE" :value="scope.row.valType"></dict-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="defFlowId" :label="t('flowrule.defFlowId')"  show-overflow-tooltip>
                    <template #default="scope">
                        <convert-name :options="dicData.defFlowId" :value="scope.row.defFlowId"
                                      :valueKey="'id'" :showKey="'flowName'"></convert-name>
                    </template>
                </el-table-column>
                <el-table-column prop="flowNodeId" :label="t('flowrule.flowNodeId')"  show-overflow-tooltip>
                    <template #default="scope">
                        <convert-name :options="dicData.flowNodeId" :value="scope.row.flowNodeId"
                                      :valueKey="'id'" :showKey="'nodeName'"></convert-name>
                    </template>
                </el-table-column>
                <el-table-column prop="groupId" :label="t('flowrule.groupId')" show-overflow-tooltip/>
                <el-table-column prop="groupsType" :label="t('flowrule.groupsType')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.OR_OR_AND" :value="scope.row.groupsType"></dict-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="groupType" :label="t('flowrule.groupType')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.OR_OR_AND" :value="scope.row.groupType"></dict-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="varKeyVal" :label="t('flowrule.varKeyVal')" show-overflow-tooltip/>
                <el-table-column prop="operator" :label="t('flowrule.operator')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.OPERATOR" :value="scope.row.operator"></dict-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="varVal" :label="t('flowrule.varVal')" show-overflow-tooltip/>

                <el-table-column prop="jobType" :label="t('flowrule.jobType')" show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.JOB_USER_TYPE" :value="scope.row.jobType"></dict-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="roleId" :label="t('flowrule.roleId')">
                    <template #default="scope">
                        <convert-role-name :options="{users: dicData.users, roles: dicData.roles, posts: dicData.posts, depts: dicData.depts }"
                                           :value="scope.row"></convert-role-name>
                    </template>
                </el-table-column>

                <el-table-column prop="paramFrom" :label="t('flowrule.paramFrom')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.PARAM_FROM" :value="scope.row.paramFrom"></dict-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="paramValType" :label="t('flowrule.paramValType')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.PARAM_VAL_TYPE" :value="scope.row.paramValType"></dict-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="targetProp" :label="t('flowrule.targetProp')" show-overflow-tooltip/>
                <el-table-column prop="paramType" :label="t('flowrule.paramType')"  show-overflow-tooltip>
                    <template #default="scope">
                        <dict-tag :options="DIC_PROP.PARAM_TYPES" :value="scope.row.paramType"></dict-tag>
                    </template>
                </el-table-column>

                <el-table-column :label="$t('common.action')" fixed="right" width="100">
                    <template #default="scope">
                        <el-tooltip placement="top">
                            <template #content>
                                {{ $t('common.viewBtn') }}
                            </template>
                            <el-button text type="primary" icon="view" @click="formDialogRef.openDialog('view', scope.row.id)">
                            </el-button>
                        </el-tooltip>
                        <el-tooltip placement="top">
                            <template #content>
                                {{ $t('common.editBtn') }}
                            </template>
                            <el-button icon="edit-pen" text type="primary" @click="formDialogRef.openDialog('edit', scope.row.id)">
                            </el-button>
                        </el-tooltip>
                        <el-tooltip placement="top">
                            <template #content>
                                {{ $t('common.delBtn') }}
                            </template>
                            <el-button icon="delete" text type="primary" @click="handleDelete([scope.row.id])">
                            </el-button>
                        </el-tooltip>

                    </template>
                </el-table-column>
            </el-table>
            <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
        </div>

        <!-- 编辑、新增  -->
        <form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
    </div>
</template>

<script setup lang="ts" name="FlowRule">
    import { BasicTableProps, useTable } from "/@/hooks/table";
    import { fetchList, delObjs } from "/@/api/jsonflow/flow-rule";
    import { useMessage, useMessageBox } from "/@/hooks/message";
    import { useI18n } from "vue-i18n";
    import {onCascadeChange, onLoadDicUrl} from "/@/flow/components/convert-name/convert";

    // 引入组件
    const FormDialog = defineAsyncComponent(() => import('./form.vue'));
    const { t } = useI18n()

    // 定义查询字典
    const dicData = reactive({});
    const cascadeDic = reactive({});
    const onLoad = onLoadDicUrl({key: "defFlowId"}, {key: "flowNodeId"}, {key: "users"}, {key: "roles"}, {key: "posts"}, {key: "depts"});
    const onCascade = onCascadeChange(cascadeDic, {key: "defFlowId", cascades: ["flowNodeId", "flowNodeRelId"]});
    onMounted(() => {
        onLoad(dicData);
    });
    function cascadeChange(key, cascades){
        onCascade(state.queryForm, {key: key, cascades: cascades});
    }

    // 定义变量内容
    const formDialogRef = ref()
    // 搜索变量
    const queryRef = ref()
    const showSearch = ref(true)
    // 多选变量
    const selectObjs = ref([]) as any
    const multiple = ref(true)

    const state: BasicTableProps = reactive<BasicTableProps>({
        queryForm: {},
        pageList: fetchList,
        descs: ["create_time"]
    })

    //  table hook
    const {
        getDataList,
        currentChangeHandle,
        sizeChangeHandle,
        sortChangeHandle,
        downBlobFile
    } = useTable(state)

    // 清空搜索条件
    const resetQuery = () => {
        queryRef.value.resetFields()
        // 清空多选
        selectObjs.value = []
        getDataList()
    }

    // 多选事件
    const handleSelectionChange = (objs: any) => {
        selectObjs.value = objs.map(({ id }) => id);
        multiple.value = !objs.length
    }

    // 导出excel
    const exportExcel = () => {
        downBlobFile('/jsonflow/flow-rule/export', state.queryForm, 'flowrule.xlsx')
    }

    // 删除操作
    const handleDelete = async (ids: string[]) => {
        try {
            await useMessageBox().confirm(t('common.delConfirmText'));
        } catch {
            return;
        }

        try {
            await delObjs(ids);
            getDataList();
            useMessage().success(t('common.delSuccessText'));
        } catch (err: any) {
            useMessage().error(err);
        }
    };
</script>
