<template>
  <div class="app-container">
    <el-card>
      <el-col :span="15" style="width: 1000px">
        <span class="show-text">说明:</span>
        <el-input v-model="AlarmName" clearable placeholder="请输入报警说明" style="width: 230px;margin-right: 26px"/>
        <span class="show-text">类型:</span>
        <el-select
          id="selectPersonalRoleValue"
          v-model="AlarmTypeValue"
          :disabled="!AlarmSetting"
          class="personal-role-value"
          collapse-tags
          multiple
          placeholder="请选择报警类型"
          style="width:230px;margin-right: 14px">
          <el-option
            v-for="item in AlarmType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-checkbox v-model="AlarmSetting" style="font-size: 16px;margin-right: 26px">已设定</el-checkbox>
        <el-button :disabled="loading" icon="el-icon-search" plain type="primary" @click="searchTable()">查询
        </el-button>
        <el-button icon="el-icon-download" style="margin-left: 14px" type="info" @click="searchTableALL()">导出
        </el-button>
        <!--      <el-button style="margin-left: 14px" type="success" icon="el-icon-circle-plus-outline" @click="handleAdd('form')">新增</el-button>-->
        <div style="margin-top: 26px">
          <el-table
            v-loading="loading"
            :data="tableData"
            :header-row-style="{fontSize: '18px',fontFamily:'MiSans Demibold',height: '64px',backgroundColor:'#fff'}"
            :row-style="{fontSize: '16px',fontFamily:'MiSans Regular',height: '64px'}"
            border
            element-loading-background="rgba(0, 0, 0, 0.2)"
            element-loading-spinner="el-icon-loading"
            element-loading-text="数据加载中"
            height="694"
            size="mini"
            stripe
            style="width: 992px"
            tooltip-effect="dark"
            @selection-change="handleSelectionChange">
            <el-table-column align="center" type="selection" width="50"/>
            <el-table-column align="center" label="线体类型" width="141">
              <template slot-scope="scope">
                {{ scope.row.线体类型 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="变量地址" width="200">
              <template slot-scope="scope">
                {{ scope.row.变量地址 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="变量说明" show-overflow-tooltip width="400">
              <template slot-scope="scope">
                {{ scope.row.变量说明 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="报警类型" width="200">
              <template slot-scope="scope">
                {{ scope.row.报警类型 }}
              </template>
            </el-table-column>
          </el-table>
          <div class="block" style="margin-top: 10px;">
            <el-pagination
              v-if="!loading"
              :current-page="pageList"
              :page-size="pageSize"
              :page-sizes="[50, 100, 200, 300]"
              :total="total"
              background
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"/>
          </div>
        </div>
      </el-col>
      <el-col :span="8" style="width: 535px;margin-left: 16px">
        <span class="show-text">名称:</span>
        <el-input v-model="AlarmTypeName" clearable placeholder="报警类型名称" style="width: 210px;margin-right: 26px"/>
        <el-button :disabled="loading1" icon="el-icon-search" type="primary" @click="searchAlarmTypeTable()">查询
        </el-button>
        <el-button
          icon="el-icon-circle-plus-outline"
          style="margin-left: 14px"
          type="success"
          @click="handleAdd('form')">新增
        </el-button>
        <div style="margin-top: 26px;margin-bottom: 26px">
          <el-table
            v-loading="loading1"
            :data="tableData1"
            :header-row-style="{fontSize: '18px',fontFamily:'MiSans Demibold',height: '64px',backgroundColor:'#fff'}"
            :row-style="{fontSize: '16px',fontFamily:'MiSans Regular',height: '64px'}"
            border
            element-loading-background="rgba(0, 0, 0, 0.2)"
            element-loading-spinner="el-icon-loading"
            element-loading-text="数据加载中"
            height="300"
            size="mini"
            stripe
            style="width: 992px"
            tooltip-effect="dark"
            @selection-change="handleSelectionChange">
            <el-table-column align="center" label="类型名称">
              <template slot-scope="scope">
                {{ scope.row.AlarmTypeName }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="编辑" min-width="150">
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-edit"
                  style="font-size: 16px;font-family: MiSans Regular,serif;color: #4d77d6"
                  type="text"
                  @click="handleEdit(scope.row)">编辑
                </el-button>
                <el-button
                  icon="el-icon-delete"
                  style="font-size: 16px;font-family: MiSans Regular,serif;color: #c61a1a"
                  type="text"
                  @click="handleDelete(scope.row)">删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-divider style="margin: 0"/>
        <el-select
          id="selectPersonalRoleValue"
          v-model="AlarmTypeValue1"
          :disabled="!alarmSelectCount"
          class="personal-role-value"
          placeholder="请选择报警类型"
          style="width:230px;margin-right: 14px">
          <el-option
            v-for="item in AlarmType"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <el-button
          :disabled="!alarmSelectCount"
          icon="el-icon-check"
          style="margin-left: 14px;width: 180px"
          type="success"
          @click="alarmTypeSettingSelect()">设定报警类型
        </el-button>
      </el-col>
    </el-card>

    <el-dialog :visible.sync="dialogFormVisible1" center title="新增报警类型" width="440px">
      <el-divider style="margin-top: 40px"/>
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="100px">
        <el-form-item label="类型名称" prop="类型名称">
          <el-input
            v-model="form.AlarmTypeName"
            clearable
            placeholder="类型名称"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd('form')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff('form')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" center title="编辑报警类型" width="440px">
      <el-divider style="margin: 0"/>
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="100px">
        <el-form-item label="类型名称" prop="类型名称" style="display: inline-block">
          <el-input
            v-model="form1.AlarmTypeName"
            clearable
            placeholder="类型名称"
            style="width: 240px;border-radius: 4px"/>
        </el-form-item>
        <el-row>
          <el-col :span="14"/>
          <el-form-item>
            <el-button style="width: 80px;height: 40px" type="primary" @click="submitAdd1('form1')">确 定</el-button>
            <el-button style="width: 80px;height: 40px" @click="callOff1('form1')">取消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      AlarmName: '',
      AlarmTypeName: '',
      AlarmSetting: false,
      loading: false,
      loading1: false,
      tableData: [],
      tableData1: [],
      alarmSelect: [],
      alarmSelectCount: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      AlarmTypeValue: [],
      AlarmTypeValue1: '',
      AlarmType: [],
      form: {
        AlarmTypeName: ''
      },
      form1: {
        AlarmTypeName: '',
        AlarmTypeCode: ''
      },
      rules: {
        AlarmTypeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }]
      },
      rules1: {
        AlarmTypeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }]
      },
      total: 0, // 总条数
      pageSize: 50, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.searchTable()
    this.searchAlarmTypeTable()
  },
  methods: {
    // 查询
    searchTable() {
      let type = '('
      if (this.AlarmSetting) {
        if (this.AlarmTypeValue.length === 0) {
          this.$message.warning('请选择报警类型')
          return
        }
        for (const info of this.AlarmTypeValue) {
          type += info + ','
        }
        type = type.replace(/,$/gi, '')
        type += ')'
      }
      this.loading = true
      this.tableData = []
      var param = []
      param[0] = ['变量类型名称', this.AlarmName]
      param[1] = ['PageCurrent', this.pageList]
      param[2] = ['PageSize', this.pageSize]
      param[3] = ['PageCount', '1111', 'int', '1']
      param[4] = ['ItemCount', '1111', 'int', '1']
      param[5] = ['type', type]
      param[6] = ['isSetting', this.AlarmSetting ? 1 : 0]
      var Data = this.CreateData('11', '基础数据_报警设置_查询报警信息', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData = response.data.result
        }
        this.total = parseInt(response.data.output[0].ItemCount)
        this.loading = false
      })
    },
    // 导出数据
    searchTableALL() {
      var param = []
      if (this.tableData.length === 0) {
        this.$message.warning('请查询要导出的数据')
        return
      }
      let type = '('
      if (this.AlarmSetting) {
        if (this.AlarmTypeValue.length === 0) {
          this.$message.warning('请选择报警类型')
          return
        }
        for (const info of this.AlarmTypeValue) {
          type += info + ','
        }
        type = type.replace(/,$/gi, '')
        type += ')'
      }
      param[0] = ['变量类型名称', this.AlarmName]
      param[1] = ['type', type]
      param[2] = ['isSetting', this.AlarmSetting ? 1 : 0]
      var Data = this.CreateData('2003', '基础数据_报警设置_查询报警信息_导出', param)
      const blob = 'data:image/gif;base64,R0lGODlhCQACAIAAAMzMzP///yH5BAEAAAEALAAAAAAJAAIAAAIEjI9pUAA7'
      this.exportExcel_NPOI(Data, blob)
    },
    // 查询
    searchAlarmTypeTable() {
      this.loading1 = true
      this.tableData1 = []
      this.AlarmType = []
      var param = []
      param[0] = ['AlarmTypeName', this.AlarmTypeName]
      var Data = this.CreateData('11', '基础数据_报警类型设定_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableData1 = response.data
        }
        for (const info of this.tableData1) {
          this.AlarmType.push({
            label: info.AlarmTypeName,
            value: info.AlarmTypeCode
          })
        }
        this.AlarmTypeValue = [this.tableData1[0].AlarmTypeCode]
        this.AlarmTypeValue1 = this.tableData1[0].AlarmTypeCode
        this.loading1 = false
      })
    },
    // 编辑
    handleEdit(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.form1.AlarmTypeName = row.AlarmTypeName
      this.form1.AlarmTypeCode = row.AlarmTypeCode
      this.dialogFormVisible2 = true
    },
    // 确认编辑焦点
    submitAdd1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.tableData1.filter(res => res.AlarmTypeName === this.form1.AlarmTypeName).length > 0) {
            this.$message.warning('存在重复类型名称！')
            return
          }
          var param = []
          param[0] = ['AlarmTypeName', this.form1.AlarmTypeName]
          param[1] = ['AlarmTypeCode', this.form1.AlarmTypeCode]
          var Data = this.CreateData('12', '基础数据_报警类型设定_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('编辑成功')
              this.form1.AlarmTypeName = ''
              this.form1.AlarmTypeCode = ''
              this.searchAlarmTypeTable()
              this.dialogFormVisible2 = false
            } else {
              this.$message.error('编辑失败')
            }
          })
        }
      })
    },
    // 增加焦点
    handleAdd() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.dialogFormVisible1 = true
    },
    // 确认增加焦点
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.tableData = []
          var param = []
          param[0] = ['AlarmTypeName', this.form.AlarmTypeName]
          var Data = this.CreateData('12', '基础数据_报警类型设定_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('增加成功')
              this.form.AlarmTypeName = ''
              this.dialogFormVisible1 = false
            } else {
              this.$message.error('增加失败')
            }
            this.searchAlarmTypeTable()
          })
        }
      })
    },
    // 重置表单
    callOff1(formName) {
      this.dialogFormVisible2 = false
      this.$refs[formName].resetFields()
    },
    // 重置表单
    callOff(formName) {
      this.dialogFormVisible1 = false
      this.$refs[formName].resetFields()
    },
    // 删除
    handleDelete(row) {
      this.$confirm('当前报警类型将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirm-confirm-button',
        cancelButtonClass: 'confirm-cancel-button',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['AlarmTypeCode', row.AlarmTypeCode]
        var Data = this.CreateData('12', '基础数据_报警类型设定_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable()
            this.searchAlarmTypeTable()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 行选择
    handleSelectionChange(val) {
      this.alarmSelect = []
      for (const info of val) {
        this.alarmSelect.push(info.ID)
      }
      this.alarmSelect.length === 0 ? this.alarmSelectCount = false : this.alarmSelectCount = true
    },
    // 设定报警类型
    alarmTypeSettingSelect() {
      var param = []

      let IDGroup = ''
      for (const info of this.alarmSelect) {
        IDGroup += info + ','
      }
      IDGroup = IDGroup.replace(/,$/gi, '')
      param[0] = ['IDGroup', IDGroup]
      param[1] = ['AlarmTypeCode', this.AlarmTypeValue1]
      var Data = this.CreateData('12', '基础数据_报警设置_设定报警类型循环', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result === '1') {
          this.$message.success('设定成功')
          this.searchTable()
        } else {
          this.$message.error('设定失败')
        }
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
<style scoped>
.el-card {
  margin: 0px;
}

::v-deep .el-checkbox__label {
  font-size: 20px;
}

::v-deep .el-checkbox__inner {
  width: 20px;
  height: 20px;
}

::v-deep .el-checkbox__inner::after {
  top: 4px;
  left: 7px;
}
</style>
<style>
.el-pagination.is-background .el-pager li:not(.disabled) {
  background-color: #f5f5f5;
  color: #4d77d6;
}

.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #4d77d6;
  color: #f5f5f5;
}

.el-pagination.is-background .el-pager li:not(.disabled):hover {
  background-color: #7794d5;
  color: #f5f5f5;
}
</style>
