<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-row>
              <el-form-item label="规则名称" prop="rulesName">
                <el-input v-model="queryParams.rulesName" placeholder="请输入规则名称" clearable @keyup.enter="handleQuery" />
              </el-form-item>
              <el-form-item label="规则描述" prop="description">
                <el-input v-model="queryParams.description" placeholder="请输入规则描述" clearable @keyup.enter="handleQuery" />
              </el-form-item>
              <el-form-item label="规则状态" prop="state">
                <el-select v-model="queryParams.state" placeholder="请输入规则状态">
                  <el-option v-for="dict in rule_mian_state" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="决策类型" prop="decisionType">
                <el-select v-model="queryParams.decisionType" placeholder="请输入决策类型">
                  <el-option v-for="dict in rule_decision_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-row>
            <el-form-item style="margin-left: 20px">
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="mainList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="规则名称" align="center" prop="rulesName" />
        <el-table-column label="规则描述" align="center" prop="description" />
        <el-table-column label="决策类型" align="center" prop="decisionType">
          <template #default="scope">
            <dict-tag :options="rule_decision_type" :value="scope.row.decisionType" />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="rule_mian_state" :value="scope.row.state" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="参数查看" placement="top">
              <el-button
                v-hasPermi="['rule:parameter:addParameter']"
                link
                type="primary"
                icon="BottomRight"
                @click="handleOpenParms(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="决策审核" placement="top">
              <el-button
                v-hasPermi="['rule:parameter:toDecisionTree']"
                link
                type="primary"
                icon="Position"
                @click="toDecisionTree(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 补充入参对话框 -->
    <el-dialog v-model="parmsDialog.visible" :title="parmsDialog.title" width="900px" append-to-body>
      <el-card shadow="never">
        <el-table v-loading="parmsloading" :data="parmsList">
          <el-table-column label="参数" align="center" prop="fieldName">
            <template #default="scope">
              <el-input v-model="scope.row.fieldName" placeholder="请输入参数" />
            </template>
          </el-table-column>
          <el-table-column label="参数类型" align="center" prop="fieldType" >
            <template #default="scope">
              <el-select v-model="scope.row.fieldType" placeholder="请输入参数类型">
                <el-option v-for="dict in rule_parms_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="参数解释" align="center" prop="fieldDescription">
            <template #default="scope">
              <el-input v-model="scope.row.fieldDescription" placeholder="请输入参数解释" />
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelParms" type="primary">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="RuleAudit" lang="ts">
import { MainForm, MainQuery, MainVO } from '@/api/rule/definition/mian_types';
import { getParms, listMain } from '@/api/rule/definition';
import { InputParameterVO } from '@/api/rule/definition/parms_types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { rule_mian_state } = toRefs<any>(proxy?.useDict('rule_mian_state'));
const { rule_decision_type } = toRefs<any>(proxy?.useDict('rule_decision_type'));
const { rule_parms_type } = toRefs<any>(proxy?.useDict('rule_parms_type'));
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const mainList = ref<MainVO[]>([]);
const queryFormRef = ref<ElFormInstance>();
const parmsloading = ref(false);
const parmsList = ref<InputParameterVO[]>([]);

const initFormData: MainForm = {
  rulesId: undefined,
  rulesName: undefined,
  description: undefined,
  inputParameter: undefined,
  decisionType: undefined,
  decisionTree: undefined,
  rulesEntityId: undefined,
  state: undefined
};
const data = reactive<PageData<MainForm, MainQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    rulesName: undefined,
    description: undefined,
    inputParameter: undefined,
    decisionType: undefined,
    decisionTree: undefined,
    rulesEntityId: undefined,
    state: '1',
    params: {}
  },
  rules: {
    rulesName: [{ required: true, message: '规则名称不能为空', trigger: 'blur' }],
    description: [{ required: true, message: '规则描述不能为空', trigger: 'blur' }],
    decisionType: [{ required: true, message: '决策类型不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

const parmsDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 查询规则信息主体列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMain(queryParams.value);
  mainList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 参数查看 */
const handleOpenParms = async (row?: MainVO) => {
  const _rulesId = row?.rulesId;
  const res = await getParms(_rulesId);
  parmsloading.value = true;
  parmsDialog.visible = true;
  parmsDialog.title = '规则参数';
  parmsList.value = res.data;
  parmsloading.value = false;
};

/** 取消按钮 */
const cancelParms = () => {
  parmsDialog.visible = false;
};

const toDecisionTree = async (row?: MainVO) => {
  const _rulesId = row?.rulesId;
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/rule/definition/def`,
    query: {
      rulesId: _rulesId,
      showSaveFlag: 'false'
    }
  });
};

onMounted(() => {
  getList();
});
</script>


<style scoped lang="scss"></style>
