<template>
  <div class="app-container">
    <el-table border height="760" v-loading="loading" :data="safeLevelList" @selection-change="handleSelectionChange">

      <el-table-column label="安全等级" align="center" prop="levelName"/>
      <el-table-column label="风险等级" align="center" prop="riskLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.risk_level" :value="scope.row.riskLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="得分区间:"  align="center" prop="description"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['core.safetyIndex:safeLevel:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['core.safetyIndex:safeLevel:edit']"
          >查看
          </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"
    />

    <!-- 添加或修改安全等级配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="等级名称" prop="levelName">
          <el-input  v-model="form.levelName" placeholder="请输入安全等级名称" disabled/>
        </el-form-item>
        <el-form-item label="等级风险" prop="riskLevel">
          <el-select disabled style="width: 100%" v-model="form.riskLevel" placeholder="请选择等级风险">
            <el-option v-for="dict in dict.type.risk_level" :key="dict.value" :label="dict.label"
                       :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="最小值" prop="minValue">
          <el-input-number style="width: 100%" :max="100" :min="0"  :disabled="this.title=='查看安全等级配置'" v-model="form.minValue" placeholder="请输入最小值"/>
        </el-form-item>
        <el-form-item label="最大值" prop="maxValue">
          <el-input-number style="width: 100%"  :max="100" :min="0" :disabled="this.title=='查看安全等级配置'" v-model="form.maxValue" placeholder="请输入最大值"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="this.title!='查看安全等级配置'" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSafeLevel, getSafeLevel, delSafeLevel, addSafeLevel, updateSafeLevel} from "@/api/core/safeLevel/safeLevel";

export default {
  name: "SafeLevel",
  dicts: ["risk_level"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个停用
      single: true,
      // 非多个停用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全等级配置表格数据
      safeLevelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        levelName: null,
        minValue: null,
        maxValue: null,
        riskLevel: null,
        description: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询安全等级配置列表 */
    getList() {
      this.loading = true;
      listSafeLevel(this.queryParams).then(response => {
        this.safeLevelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        levelName: null,
        minValue: null,
        maxValue: null,
        riskLevel: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安全等级配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSafeLevel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改安全等级配置";
      });
    },
    /** 查看按钮操作 */
    handleDetail(row){
      this.reset();
      const id = row.id || this.ids
      getSafeLevel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看安全等级配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.log("this.form.minValue",this.form.minValue)
          console.log("this.form.maxValue",this.form.maxValue)
          if (this.form.minValue > this.form.maxValue) {
            this.$modal.msgError("最小值不能大于最大值");
            return;
          }
          if (this.form.minValue < 0 || this.form.maxValue < 0) {
            this.$modal.msgError("最小值或最大值不能小于0")
            return;
          }
          if (this.form.maxValue > 100) {
            this.$modal.msgError("最大值不能大于100");
            return;
          }

          if (this.form.id != null) {
            updateSafeLevel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSafeLevel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除安全等级配置编号为"' + ids + '"的数据项？').then(function () {
        return delSafeLevel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('core.safetyindex/safeLevel/export', {
        ...this.queryParams
      }, `safeLevel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
