<template>
  <section class="app-container">
    <el-row>
      <el-col :span="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
          <el-form-item label="信号编码">
            <el-input v-model="queryParams.signalCode" placeholder="请输入信号编码" clearable size="small" style="width: 200px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="信号名称">
            <el-input v-model="queryParams.signalName" placeholder="请输入信号名称" clearable size="small" style="width: 200px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="信号量">
            <el-input v-model="queryParams.signalValue" placeholder="请输入信号量" clearable size="small" style="width: 200px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="信号量范围">
            <el-input v-model="queryParams.signalValueRange" placeholder="信号量范围" clearable size="small" style="width: 200px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="类型">
            <el-input v-model="queryParams.signalType" placeholder="类型" clearable size="small" style="width: 200px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" 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 size="mini" type="primary" icon="el-icon-plus" @click="handleAdd">增加</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete">批量删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column prop="signalCode" align="center" label="信号编码">
          </el-table-column>
          <el-table-column prop="signalName" align="center" label="信号名称">
          </el-table-column>
          <el-table-column align="center" label="信号量">
            <template slot-scope="scope">{{ scope.row.status == 1 ? scope.row.signalValue : '无' }}</template>
          </el-table-column>
          <el-table-column align="center" label="是否告警">
            <template slot-scope="scope">{{ warningIsList.find(v => v.value == scope.row.warningIs).label }}</template>
          </el-table-column>
          <el-table-column prop="signalValueRange" align="center" label="信号量范围">
          </el-table-column>
          <el-table-column prop="description" align="center" label="描述">
          </el-table-column>
          <el-table-column prop="signalType" align="center" label="类型">
          </el-table-column>
          <el-table-column prop="remark" align="center" label="备注">
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="300px">
            <template slot-scope="scope">
              <el-button v-if="scope.row.status == 2" size="mini" type="text" icon="el-icon-edit" @click="handleOptionUpdate(scope.row)">
                信号量选项</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button class="text-danger" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(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" />
      </el-col>
    </el-row>
    <!-- 添加更新配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="信号编码" prop="signalCode">
          <el-input v-model="form.signalCode" :disabled="!!form.id" placeholder="请输入信号编码" />
        </el-form-item>
        <el-form-item label="信号名称" prop="signalName">
          <el-input v-model="form.signalName" placeholder="请输入信号名称" />
        </el-form-item>
        <el-form-item label="信号量选项" prop="status">
          <el-select :disabled="!!form.id" v-model="form.status" clearable placeholder="请选择">
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否告警" prop="warningIs">
          <el-select v-model="form.warningIs" placeholder="请选择">
            <el-option v-for="item in warningIsList" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.status == 1" label="信号量" prop="signalValue">
          <el-input v-model="form.signalValue" placeholder="请输入信号量" />
        </el-form-item>
        <el-form-item label="信号量范围" prop="signalValueRange">
          <el-input v-model="form.signalValueRange" placeholder="请输入信号量范围" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="类型">
          <el-input v-model="form.signalType" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="optionForm.signalName + ' 信号量选项编辑'" :visible.sync="optionOpen">
      <el-form ref="optionForm" :model="optionForm" label-width="100px" style="max-height: 500px;overflow: auto;">
        <section v-for="(item, index) in optionForm.optionList" :key="index">
          <el-row>
            <el-col :span="7">
              <el-form-item label="选项名称" :prop="'optionList.' + index + '.optionName'"
                :rules="{ required: true, message: '选项不能为空', trigger: 'blur' }">
                <el-input v-model="item.optionName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="选项值" :prop="'optionList.' + index + '.optionVal'"
                :rules="{ required: true, message: '选项值不能为空', trigger: 'blur' }">
                <el-input v-model="item.optionVal"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="选项值范围">
                <el-input v-model="item.signalValueRange"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="text-center" :span="2">
              <el-button type="text" @click.prevent="removeOption(item)">移除</el-button>
            </el-col>
          </el-row>
        </section>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" @click="addOption">新 增</el-button>
        <el-button type="primary" @click="submitOption">确 定</el-button>
        <el-button @click="optionOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import { list, info, optionInfo, save, optionSave, del, optionDel, select } from "@/api/signal/signal";
export default {
  name: "signalList",
  data () {
    return {
      // 选中数组
      ids: [],
      // 表格数据
      tableData: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      statusList: [
        { 'label': '无选项', 'value': 1 },
        { 'label': '有选项', 'value': 2 }
      ],
      warningIsList: [
        { 'label': '告警', 'value': 1 },
        { 'label': '不告警', 'value': 2 }
      ],
      rules: {
        signalCode: [
          { required: true, message: "信号编码不能为空", trigger: "blur" },
        ],
        signalName: [
          { required: true, message: "信号名称不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "信号选项类型不能为空", trigger: "blur" },
        ],
        signalValue: [
          { required: true, message: "信号量不能为空", trigger: "blur" },
        ],
        signalValueRange: [
          { required: true, message: "信号量范围不能为空", trigger: "blur" },
        ],
        warningIs: [
          { required: true, message: "是否告警不能为空", trigger: "blur" },
        ],
      },
      optionOpen: false,
      optionForm: {
        signalName: '',
        signalCode: '',
        optionList: [],
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 获取信号量列表
    getList () {
      list(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParams = {};
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加信号";
    },
    handleOptionUpdate (row) {
      this.optionOpen = true;
      this.resetForm("optionForm");
      this.optionForm.optionList = [{ 'optionName': '', 'optionVal': '', 'signalValueRange': '' }];
      this.optionForm.signalCode = row.signalCode;
      this.optionForm.signalName = row.signalName;
      select({ signalCode: row.signalCode }).then(res => {
        if (200 == res.code) {
          this.optionForm.optionList.unshift(...res.select);
        } else {
          this.msgError(res.msg);
        }
      })
    },
    removeOption (option) {
      let index = this.optionForm.optionList.indexOf(option);
      if (index !== -1) {
        this.optionForm.optionList.splice(index, 1);
      }
    },
    addOption () {
      this.optionForm.optionList.push({ 'optionName': '', 'optionVal': '', 'signalValueRange': '' });
    },
    submitOption () {
      this.$refs.optionForm.validate((valid) => {
        if (valid) {
          if (this.optionForm.optionList.length == 0) return;
          let snap = new Set(this.optionForm.optionList.map(v => v.optionVal));
          if ( this.optionForm.optionList.length != snap.size) {
            this.msgError('选项值不能重复');
            return;
          }
          optionSave(this.optionForm).then(({ code, msg }) => {
            if (200 == code) {
              this.optionOpen = false;
              this.msgSuccess(msg);
            } else {
              this.msgError(msg)
            }
          })
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = Object.assign({}, row);
      this.open = true;
      this.title = "修改信号";
    },
    /** 提交按钮 */
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          save(this.form).then(res => {
            this.open = false;
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return del(ids);
        })
        .then((res) => {
          if (200 == res.code) {
            this.getList();
            this.msgSuccess("删除成功");
          } else {
            this.msgError(res.msg);
          }
        })
        .catch((res) => { });
    },
    // 表单重置
    reset () {
      this.form = {
        warningIs: 2,
        signalCode: "",
        signalName: "",
        signalValue: "",
        signalValueRange: "",
      };
      this.resetForm("form");
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
  },
}
</script>

<style>
</style>