<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公式名称" prop="formulaName">
        <el-input
          v-model="queryParams.formulaName"
          placeholder="请输入公式名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属指标" prop="formulaIndicator">
        <el-select v-model="queryParams.formulaIndicator" placeholder="请选择所属指标">
          <el-option
            v-for="item in formulaIndicator"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['eval:formulas:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate(null)"
          v-hasPermi="['eval:formulas:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['eval:formulas:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['eval:formulas:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formulasList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公式名称" align="center" prop="formulaName" />
      <el-table-column label="公式简称" align="center" prop="formulaShortName" />
      <el-table-column label="所属指标" align="center" prop="formulaIndicator" >
        <template slot-scope="scope">
          {{scope.row.formulaIndicator==='0'? '一级指标':scope.row.formulaIndicator==='1'?'二级指标':'三级指标'}}
        </template>
      </el-table-column>
      <el-table-column label="原始公式" align="center" prop="originalFormula" />
      <el-table-column label="说明" align="center" prop="remark" >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.remark" placement="top-start">
            <span>{{scope.row.remark}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-link
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eval:formulas:edit']"
          >修改</el-link>
          |
          <el-link
            size="mini"
            type="warning"
            icon="el-icon-view"
            @click="infoDataShow(scope.row)"
          >详情</el-link>
          |
          <el-link
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eval:formulas:remove']"
          >删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <FormDialog
      v-model="open"
      :data-info="currentRow"
      :availableParams="availableParams"
      :options="options"
      @input="getList"
    />
  </div>
</template>

<script>
import { listFormulas, getFormulas, delFormulas, addFormulas, updateFormulas } from "@/api/eval/formulas";
import FormDialog from './form.vue';

export default {
  name: "Formulas",
  components: {
    FormDialog
  },
  data() {
    return {
      formulaIndicator:[
        {'label': '一级指标','value' : '0'},
        {'label': '二级指标','value' : '1'},
        {'label': '三级指标','value' : '2'},
      ],
      currentRow: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      options: 'add',
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公式管理管理表格数据
      formulasList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        formulaIndicator:'',
        pageNum: 1,
        pageSize: 10,
        formulaName: null,
        formulas: null,
      },
      // 表单参数
      formData: {
        formulaId: '',
        formulaName: '',
        originalFormula: '',
        formulaShortName: '',
        formulaStoredProcedure: '',
        remark: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: '公式名称不能为空', trigger: 'blur' }
        ],
        formulas: [
          { required: true, message: '公式内容不能为空', trigger: 'blur' }
        ]
      },
      // 新增：存储过程代码
      procedureCode: "",
      // 新增：可用参数列表
      availableParams: ['α', 'Precision', 'Recall', 'TP', 'TN', 'FP', 'FN']
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公式管理管理列表 */
    getList() {
      this.loading = true;
      listFormulas(this.queryParams).then(response => {
        this.formulasList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.formData = {
        formulaId: '',
        formulaName: '',
        originalFormula: '',
        formulaShortName: '',
        formulaStoredProcedure: '',
        remark: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: ''
      };
      this.resetForm("formData");
      this.procedureCode = "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.formulaId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.options = "add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.options = "edit";
      if (row) {
        this.currentRow = Object.assign({}, row);
      } else if (this.ids.length === 1) {
        const selectedRow = this.formulasList.find(item => item.formulaId === this.ids[0]);
        if (selectedRow) {
          this.currentRow = Object.assign({}, selectedRow);
        }
      }
    },
    /**
     * 查看详情
     */
    infoDataShow(row) {
      this.options = 'info';
      this.currentRow = Object.assign({}, row);
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.formulaId || this.ids;
      this.$confirm('是否确认删除公式管理管理编号为"' + ids + '"的数据项？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return delFormulas(ids);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 假设 download 方法已在全局或当前组件中定义
      this.download('eval/formulas/export', {
        ...this.queryParams
      }, `formulas_${new Date().getTime()}.xlsx`);
    },
  }
};
</script>
