<template>
  <div class="app-container">
    <div class="Pd20">
      <span style="font-size: 20px">全部算法</span>
      <el-table
        :data="tableList"
        style="width: 100%; margin-top: 20px"
        empty-text
      >
        <el-table-column
          prop="algorithmId"
          align="center"
          label="算法ID"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="algorithmVersion"
          align="center"
          label="算法版本"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="algorithmName"
          label="算法名称"
          align="center"
        ></el-table-column>
        <el-table-column prop="algorithmLevel" align="center" label="算法等级">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.algorithm_level"
              :value="Number(scope.row.algorithmLevel)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="tableEdit(scope.row)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    // 修改弹窗部分的模板
    <el-dialog
      title="修改等级"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
      width="30%"
      center
    >
      <el-form :model="form" :rules="rules" ref="form" inline>
        <el-form-item label="算法等级" prop="algorithmLevel">
          <el-select
            v-model="form.algorithmLevel"
            placeholder="请选择等级"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.algorithm_level"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAlgorithmList,
  addAlgorithm,
  updateAlgorithm,
  delAlgorithm,
} from "@/api/business/algorithmconfig.js";
export default {
  dicts: ["algorithm_level"],
  name: "AlgorithmConfig",
  props: {},
  components: {},
  data() {
    return {
      tableList: [], //表格数据
      total: 0, //总条数
      queryParams: {
        //查询参数
        pageNum: 1,
        pageSize: 10,
      },
      editDialogVisible: false,
      form: {
        algorithmId: null,
        algorithmLevel: null,
      },
      rules: {
        algorithmLevel: [
          { required: true, message: "请选择算法等级", trigger: "change" },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    //获取列表
    getList() {
      getAlgorithmList(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableList = res.rows;
          this.total = res.total;
        } else {
          this.$modal.msgError(res.msg);
        }
      });
    },
    // 编辑按钮点击事件
    tableEdit(row) {
      this.form = {
        algorithmId: row.algorithmId,
        algorithmLevel: Number(row.algorithmLevel),
      };
      this.editDialogVisible = true;
    },

    // 确认修改方法
    handleEditConfirm() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        updateAlgorithm(this.form).then((res) => {
          if (res.code === 200) {
            this.$modal.msgSuccess("修改成功");
            this.getList();
            this.editDialogVisible = false;
          }
        });
      });
    },
  },
};
</script>

<style scoped lang="scss">
.Pd20 {
  padding: 20px;
  background: #fff;
}
</style>
