<template>

  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-righ"
            size="mini"
            @click="goWarn"
            v-hasPermi="['warning:rule:add']"
          >返回配置页面</el-button>
        </el-col>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['warning:rule:add']"
        >新增</el-button>
      </el-col>

    </el-row>

    <el-table :data="ruleList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="规则名称" align="center" prop="ruleName" />
      <el-table-column label="策略名称" align="center" prop="strategyName" />
      <el-table-column label="报文数据类型" align="center" prop="messageField" />
      <el-table-column label="滑窗时间" align="center" prop="slideTime" />
      <el-table-column label="滑窗频率" align="center" prop="slideFrequency">
        <template slot-scope="scope">
          <span v-if="scope.row.slideFrequency===1">1分之1</span>
          <span v-if="scope.row.slideFrequency===2">2分之1</span>
          <span v-if="scope.row.slideFrequency===3">3分之1</span>
          <span v-if="scope.row.slideFrequency===4">4分之1</span>
          <span v-if="scope.row.slideFrequency===5">5分之1</span>
        </template>
      </el-table-column>
      <el-table-column label="增长率" align="center" prop="growthRate" />
      <el-table-column label="波动率" align="center" prop="volatilityRate" />
      <el-table-column label="下降率" align="center" prop="decreaseRate" />
      <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="['warning:rule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warning:rule:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改预警规则对话框 -->
    <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="ruleName">
          <el-input v-model="form.ruleName" placeholder="请输入规则名称" />
        </el-form-item>
        <el-form-item label="报文数值">
           <el-select v-model="form.msgTypeId" placeholder="请选择报文数值">
             <el-option
               v-for="item in templateTypeList"
               :key="item.messageTemplateTypeId"
               :label="item.messageField"
               :value="item.messageTemplateTypeId">
             </el-option>
            </el-select>
       </el-form-item>
        <el-form-item label="滑窗时间" prop="slideTime">
          <el-input v-model="form.slideTime" placeholder="请输入滑窗时间" />
        </el-form-item>
        <el-form-item label="滑窗频率" prop="slideFrequency">
          <el-select v-model="form.slideFrequency" placeholder="请选择滑窗频率">
            <el-option label="1分之1" value="1"></el-option>
            <el-option label="2分之1" value="2"></el-option>
            <el-option label="3分之1" value="3"></el-option>
            <el-option label="4分之1" value="4"></el-option>
            <el-option label="5分之1" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="增长率" prop="growthRate">
          <el-input v-model="form.growthRate" placeholder="请输入增长率" />
        </el-form-item>
        <el-form-item label="波动率" prop="volatilityRate">
          <el-input v-model="form.volatilityRate" placeholder="请输入波动率" />
        </el-form-item>
        <el-form-item label="下降率" prop="decreaseRate">
          <el-input v-model="form.decreaseRate" placeholder="请输入下降率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {
  addWarnRule,
  selectByTemplateId,
  selectListByStrategyId, selectRuleById, selectWarnRuleRespList,
  updWarnRule
} from '@/api/saas/saas'

export default {
  name: "Rule",
  data() {
    return {
      strategyName:null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警规则表格数据
      ruleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      strategyId:null,
      templateTypeList:[]
    };
  },
  created() {
    this.receiveStrategyId()
    this.selectTemplateByStrategyId()
  },
  methods: {

    /**返回配置*/
    goWarn(){
      this.$router.push({
        path: 'warn'
      })
    },
    /**根据报文ID查报文字段*/
    selectTemplateByStrategyId(){
      this.templateId = this.$route.query.templateId
      selectByTemplateId(this.templateId).then(
        res=>{
          this.templateTypeList=res.data
        }
      )
    },



    /** 接收预警id */
    receiveStrategyId(){
       this.strategyId = this.$route.query.id
       selectListByStrategyId(this.strategyId).then((res)=>{
        this.ruleList=res.data
      })
    },



    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ruleName: null,
        strategyId: null,
        msgTypeId: null,
        slideTime: null,
        slideFrequency: null,
        growthRate: null,
        volatilityRate: null,
        decreaseRate:null
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预警规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      selectRuleById(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预警规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.strategyId=this.strategyId
          if (this.form.id != null) {
            updWarnRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarnRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      location.reload()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预警规则编号为"' + ids + '"的数据项？').then(function() {
        return delRule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
