<template>
  <div class="content-main-manage">
    <div class="search-list">
      <div class="search-left">
        <Form class="common-form common-form-list" ref="tableParams" :model="tableParams" inline>
          <FormItem label="">
            <DatePicker type="datetimerange" :value="dateRange" format="yyyy-MM-dd HH:mm:ss" placeholder="更新时间范围" class="common-date-picker date-picker-main" :clearable="false" :editable="false" @on-change="changePicker"></DatePicker>
          </FormItem>
          <FormItem label="" prop="keywords">
            <Input placeholder="输入关键字查询" clearable search v-model="tableParams.keywords" />
          </FormItem>
          <FormItem label="" >
            <Button type="primary" class="common-btn-search" @click="searchClick">
              <Icon type="ios-search" style="margin-right:4px;font-size: 16px"/> 筛选
            </Button> 
            <Button type="primary" class="common-btn-search" style="margin-left:10px" @click="resetClick('tableParams')">
              <Icon type="ios-search" style="margin-right:4px;font-size: 16px"/> 重置
            </Button>   
          </FormItem>
        </Form>
    </div>
      <div class="btn-container">
        <Button icon="ios-add" class="common-add-btn" @click="addClick">新增等级</Button>
      </div>
    </div>
    <div class="manage-main-center">
      <Table :columns="columns" :data="tableData" class="common-table app-table" no-data-text="" :row-class-name="rowClassName" :loading="loading">
        <template slot="loading">
            <Loading-animation></Loading-animation>
        </template>
        <template slot-scope="{ row }" slot="colorValue">
          <i class="iconfont icon-gaojing1" style="vertical-align: middle" :style="'color:' + row.colorValue"></i>
        </template>
         <template slot-scope="{ row }" slot="action">
           <Tooltip content="详情" placement="top">
              <i class="iconfont icon-xiangqingmingxi" style="cursor:pointer;color:#B8B532;fontSize:20px;marginRight:6px" @click="detailClick(row)"></i>
           </Tooltip>
           <Tooltip content="编辑" placement="top">
              <i class="iconfont icon-bianji" style="cursor:pointer;color:#64ACFE;fontSize:20px;marginRight:6px" @click="editClick(row)"></i>
           </Tooltip>
           <Tooltip content="删除" placement="top">
              <i class="iconfont icon-shanchu" style="cursor:pointer;color:#E92E2E;fontSize:20px;marginRight:6px" @click="delClick(row)"></i>
           </Tooltip>
        </template>
      </Table>
    </div>
    <div class="common-page">
      <div class="common-page-total">
        共<span>{{tablePage}}</span>页 / <span>{{tableTotal}}</span>条数据
      </div>
      <Page :total="tableTotal" :current="tableParams.pageNum" :page-size="tableParams.pageSize" @on-change="changePage" @on-page-size-change="sizeChange" show-elevator />
    </div>
    <Modal
      v-model="showModal"
      :title="title"
      width="34"
      ref="modalGrag"
      draggable
      reset-drag-position
      @on-visible-change="modalChange"
      :mask-closable="false"
      class-name="common-modal">
      <Form class="common-form" ref="formOption" :model="formOption" :rules="ruleValidate" :label-width="130">
          <div class="common-form-item-tips" v-if="title == '编辑告警等级'">
            <i class="iconfont icon-tishi" style="vertical-align: middle;color:#FFB31A;margin-right:12px"></i>
            编辑提交后，系统所有历史警示信息将同时变动，可能造成统计数据混乱，请谨慎操作。
          </div> 
         <FormItem label="告警等级名称：" prop="name">
          <Input v-model.trim="formOption.name" maxlength="20" show-word-limit /> 
          <div class="common-form-item-text">告警等级名称不可重复，范围包含现有等级名称、关闭状态等级名称</div> 
        </FormItem>
        <FormItem label="警示颜色：" prop="color">
         <RadioGroup v-model="formOption.color">
            <Radio :label="item.id"  v-for="(item,i) in alarmColor" :key="i">
               <i class="iconfont icon-gaojing1" style="margin-right:12px" :style="'color:' + item.value"></i>
            </Radio>
        </RadioGroup>
        </FormItem>
         <FormItem label="说明：" prop="remark">
          <Input v-model="formOption.remark" type="textarea" :rows="6" maxlength="200" show-word-limit />
        </FormItem>
         <FormItem label="启用状态：" prop="enabled" v-if="title == '新增告警等级'">
          <i-switch true-color="#57C44F" v-model="formOption.enabled" disabled></i-switch>
          <span style="color: #718EBD;margin-left:6px">关</span>
          <span style="color: #718EBD;margin-left:12px">（所有新增等级均为关闭，需在列表打开）</span>
        </FormItem>
			</Form>
      <div slot="footer">
          <Button @click="modalCancel">取消</Button>
          <Button type="primary" @click="modalOk('formOption')">确定</Button>
        </div>
    </Modal>
    <Modal
      v-model="modalStatus"
      :title="commonTitle"
      width="30"
      ref="modalGrag"
      draggable
      reset-drag-position
      :mask-closable="false"
      class-name="common-modal">
      <Icon type="ios-information-circle" :style="{ color: activeColor}" class="modal-icon"></Icon>
      <span class="modal-text">{{modalTitle}}</span>
      <div slot="footer">
          <Button @click="commonCancel">取消</Button>
          <Button type="primary" @click="commonOk">确定</Button>
        </div>
    </Modal>
    <Modal
      v-model="detailStatus"
      title="告警等级详情"
      width="45"
      ref="modalGrag"
      draggable
      reset-drag-position
      footer-hide
      :mask-closable="false"
      class-name="common-modal common-modal-detail">
      <div v-for="(item,index) in detailLabel" :key="index">
        <div class="common-modal-top">
        <span class="common-modal-top-text">{{item.name}}</span>
        </div>
        <div class="common-modal-content">
          <div class="modal-content-item" v-for="(val,i) in item.arr" :key="i" :class="{'item-especial-bot':(i==item.arr.length-1 || i==item.arr.length-2) && item.arr.length%2==0}">
            <span class="modal-content-item-name">
            {{val.name}}
            </span>
            <!-- <div class="modal-content-item-value"> 
              <Tooltip placement="top-start" transfer max-width="350" v-if="val.value || val.value===0">
                <span v-if="val.name == '启用状态'">{{val.value==1 ? '启用' : '关闭'}}</span>
                <span v-else>{{val.value}}</span>
                <div slot="content">
                  <span v-if="val.name == '启用状态'">{{val.value==1 ? '启用' : '关闭'}}</span>
                  <span v-else>{{val.value}}</span>
                </div>
              </Tooltip>
              <span v-else>{{val.value}}</span>
            </div> -->
            <span class="modal-content-item-value"  v-if="val.name == '警示颜色'">
              <i class="iconfont icon-gaojing1" style="vertical-align: middle" :style="'color:' + val.value"></i>
            </span>
            <span class="modal-content-item-value" v-else :class="{'item-especial-value': val.name == '已标注告警'}" @click="go(val)">
              <Tooltip placement="top-start" transfer max-width="350" v-if="val.value || val.value===0">
                {{val.name == '已标注告警' ?  val.value+'次' : val.value}}
                <div slot="content">
                 {{val.name == '已标注告警' ?  val.value+'次' : val.value}}
                </div>
              </Tooltip>
              <span v-else>{{val.value}}</span>
              <!-- {{val.name == '已标注告警' ?  val.value+'次' : val.value}} -->
            </span>
          </div>
        </div>
      </div>
    </Modal>
  </div>
</template>
<script>
const defaultFrom = {name: '',color:null,remark: '',enabled: 0}
export default {
  name: "AlarmLevelConfig",
  components:{
  },
  data() {
    return {
      loading: true,
      modalLoading: true,
      tableParams: {
        keywords: '',
        enabled: '',
        beginDate: '',
        endDate: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      tableTotal: 0,
      tablePage: 0,
      dateRange: [],
      columns: [
          {
            title: '告警等级名称',
            key: 'name',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '警示颜色',
            align: 'center',
            slot: 'colorValue',
            width: 120,
          },
          {
            title: '已配置策略',
            key: 'confAlertRankNum',
            align: 'center'
          },
          {
            title: '已标注告警',
            key: 'markedAlarmNum',
            align: 'center'
          },
          {
            title: '更新时间',
            key: 'updateTime',
            align: 'center'
          },
          {
          title: '启用状态',
          key: 'enabled',
          align: 'center',
          width: 120,
          render: (h, params) => {
            return h('div', [                             
              h('i-switch', {
                props: { value: params.row.enabled,'true-color': '#57C44F', trueValue:1,falseValue: 0
                },
                nativeOn:{
                    "mousedown":(event)=>{ // 监听组件原生事件mousedown,此事件在click之前触发
                      this.rowObj =  params.row
                      if (params.row.enabled == 1) {
                        this.commonTitle = '确认关闭'
                        this.activeColor = '#E92E2E'
                        this.activeClass = 'icon-guanbi'
                        this.modalTitle = '关闭后将无法新增类型对应策略，该等级将从系统筛选选择项中消失。'
                        this.modalStatus = true
                      } else {
                         this.commonTitle = '确认启用'
                        this.activeColor = '#57C44F'
                        this.activeClass = 'icon-qiyong'
                        this.modalTitle = '启用后，请手动开启等级对应策略。'
                        this.modalStatus = true
                        
                      }
                    },
                  }
                }),
              ])
            }
          },
          {
            title: '操作',
            align: 'center',
            slot: 'action',
            width: 120,
          }
      ],    
      showModal: false,
      detailStatus: false, 
      modalStatus: false,
      title: '新增告警等级',
      commonTitle: "确认删除",
      activeColor: '#E92E2E',
      activeClass: '',
      modalTitle: '',
      formOption: {name: '',color:null,remark: '',enabled: 0},
      alarmColor: [],
      ruleValidate: {
        name: [{
          required: true,
          message: '请输入告警等级名称',
          trigger: 'blur'
        }],
        color: [{
          required: true,
          type: 'number',
          message: '请选择警示颜色',
          trigger: 'change'
        }],
      }, 
      detailLabel: [],
    };
  },
  mounted() {
    this.getType()
    this.getTableData()
  },
  methods: {
    // 跳转到综合查询页面
    go (val) {
      if (val.name == '已标注告警') {
        let menuNameArr = []
        menuNameArr = ['设备分析','告警分析','综合查询']
        sessionStorage.setItem('routname', '/MainPage/ComprehensiveQuery')
        sessionStorage.setItem('menuNameArr',JSON.stringify(menuNameArr))
        this.$router.push({ name: 'ComprehensiveQuery', params: { alertRankId:this.rowObj.id }})
      }
    },
    changePicker (date) {
      this.tableParams.beginDate = date[0]
      this.tableParams.endDate = date[1]
    },
    rowClassName(row, index) {
      if (index % 2 == 0) {
        return "ivu-table-stripe-even";
      } else {
        return "ivu-table-stripe-odd";
      }
    },
    // 分页
    changePage (val) {
      this.tableParams.pageNum = val
      this.getTableData()
    },
    //跳转
    sizeChange (val) {
      this.tableParams.pageSize = val
      this.getTableData()
    },
    getType  () {
      //告警等级警示颜色
      this.$get('metroapi/dic/queryDictionary', {name:'告警等级警示颜色'}).then(res=>{
        if (res.httpCode == 1 ){
          this.alarmColor = res.data
        } else {
          this.alarmColor = []
        }
      })
    },
    // 获取表格数据
    getTableData () {
      let params = JSON.parse(JSON.stringify(this.tableParams))
      this.loading = true
      this.$get('metroapi/alert/rank/list', params).then(res=>{
        this.loading = false
          if (res.httpCode == 1 ){
            this.tableData = res.data.data
            this.tableTotal = res.data.count
            if (res.data.data.length==0) {
              this.tablePage = 0
            } else {
              this.tablePage = res.data.count<= 10 ? 1: Math.ceil(res.data.count/this.tableParams.pageSize)
            }
            if (res.data.data.length==0 && this.tableParams.pageNum>1) {
              this.tableParams.pageNum--
              return this.getTableData()
            }
          } else {
            this.tableData = []
            this.tableTotal = 0
          }
        })
    },
    searchClick () {
      this.tableParams.pageNum = 1
      this.getTableData()
    },
    resetClick (name) {
     this.dateRange = []
     this.tableParams.beginDate = ''
     this.tableParams.endDate = ''
     this.$refs[name].resetFields()
     this.tableParams.pageNum = 1
     this.getTableData()
    },   
    addClick () {
      this.formOption = JSON.parse(JSON.stringify(defaultFrom))
      this.title = '新增告警等级'
      this.showModal = true
    },
    detailClick (row) {
      this.rowObj = row
      this.currentModalTabs = '基础信息'   
      this.detailStatus = true
      this.$get('metroapi/alert/rank/info', {id:row.id}).then(res=>{
        if (res.httpCode == 1 ){
          this.detailLabel = res.data
        } else {
          this.$Message.info(res.msg)
        }
      }) 
    },
    editClick (row) {
      this.rowObj = row
      this.formOption = JSON.parse(JSON.stringify(row));
      this.title = '编辑告警等级'
      this.showModal = true
    },
    delClick (row) {
      this.rowObj = row
      this.commonTitle = '确认删除'
      this.activeColor = '#E92E2E'
      this.activeClass = ''
      this.modalTitle = '删除的同时将解绑所有运营数据，且数据不可恢复，请慎重操作。'
      this.modalStatus = true
    },
    modalChange (modalStatus) {
     if (!modalStatus) {
      this.$refs.formOption.resetFields();
     }
    },
    modalOk (formName) {
      let params = JSON.parse(JSON.stringify(this.formOption)) //把对象中的数据给了某个变量，改变一个对象的值，另一个对象也变化的解决方法
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$post('metroapi/alert/rank/save', params).then(res=>{
            if (res.httpCode == 1 ){
              this.showModal = false
              this.$Message.info(res.msg)
              this.getTableData()
            } else {
              this.$Message.info(res.msg)
            }
          })
        } else {
          this.showModal = true
        }
      })
    },
    modalCancel () {
      this.showModal = false
    },
    commonOk () {
      if (this.commonTitle == '确认启用') {
        let params = JSON.parse(JSON.stringify(this.rowObj))
        params.enabled = 1
        this.getSwitchStatus(params)
      } else if (this.commonTitle == '确认关闭') {
        let params = JSON.parse(JSON.stringify(this.rowObj))
        params.enabled = 0
        this.getSwitchStatus(params)
      } else {
        let params = {
          id: this.rowObj.id
        }
        this.delData(params)
      }
    },
    commonCancel () {
      this.modalStatus = false
    },
    // 启用 关闭/接口
    getSwitchStatus (params) {
      this.$post('metroapi/alert/rank/save',params).then(res=>{
        if (res.httpCode == 1 ){
          if (params.enabled == 0) {
            this.rowObj.enabled = 0
          } else {
            this.rowObj.enabled = 1
          }
          this.modalStatus = false
        } else {
          this.rowObj.enabled = 0
          this.$Message.info(res.msg)
        }
      })
    },
    // 删除 关闭/接口
    delData (params) {
      this.$post('metroapi/alert/rank/delete',params).then(res=>{
        if (res.httpCode == 1 ){
          this.modalStatus = false
          this.$Message.info(res.msg)
          this.getTableData()
        } else {
          this.$Message.info(res.msg)
        }
      })
    },
  }
};
</script>
<style scoped lang="stylus">
.content-main-manage {
  position: relative;
  overflow: hidden;
  height: calc(100% - 50px);
}
.search-list {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
}
.search-left {
  display: flex;
  align-items: center;
   .ivu-select {
     width: 150px;
     height: 32px;
   }
   .ivu-select-single .ivu-select-selection {
     height: 100%;
   }
    >>> .ivu-input {
     width: 160px;
     height: 32px;
   }
   >>> .common-date-picker .ivu-input {
    width: 332px;
   }
    >>> .ivu-input-prefix, >>> .ivu-input-suffix {
     height: 32px;
     line-height: 32px;
   }
}
.manage-main-center {
  width: 100%;
  height: calc(100% - 150px);
}
.common-table {
  max-height: 100%;
}
>>> .common-table .ivu-table th {
  height: 53px;
 }
 >>> .common-table .ivu-table td {
  height: 53px;
 }
  >>> .common-table .ivu-spin-fix {
   top: 53px;
 }
  >>> .app-table .ivu-spin-fix {
   height: calc(100vh - 355px);
 }
 >>> .common-table .ivu-table-tip {
  background: url('../../../assets/images/noData.png') no-repeat center;
}
>>> .common-table .ivu-table-tip table {
 display: none;
}
>>> .app-table .ivu-table-tip {
   height: calc(100vh - 355px);
 }
 >>> .modal-table {
   overflow: auto !important;
 }
 >>> .modal-table .ivu-spin-fix {
   height: 200px;
 }
  >>> .modal-table .ivu-table-tip {
   height: 200px;
 }
.common-page {
  margin-top: 15px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  position: absolute;
  bottom: 0;
  right: 0;
}
.common-page-total {
  color: #fff;
  padding-right: 10px;
  font-size: 14px;
  span {
    color #409EFF;
  }
}
>>> .modal-tabs-item {
  margin-top: 10px;
}
// >>> .common-modal .ivu-modal-body {
//   min-height: 243px;
// }
>>> .common-modal-top-text {
  font-size: 14px;
  font-weight: bold;
  color: #29A0FF;
  line-height: 42px;
  border-left: 3px solid #1590F1;
  padding-left: 12px;
}
>>> .common-modal-content {
  display: flex;
  width: 100%;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-bottom: 15px;
  border-right: 1px solid #21437B;
}
>>> .modal-content-item {
  width: 50%;
  display: flex;
}
>>> .item-especial {
  width: 100%;
}
>>> .modal-content-item-name {
  width: 50%;
  height: 45px;
  line-height: 45px;
  background: #13305F;
  border-top: 1px solid #21437B;
  border-left: 1px solid #21437B;
  font-size: 14px;
  color: #F5F5F5;
  padding-left: 20px;
}
>>> .modal-content-item-value {
  width: 50%;
  height: 45px;
  line-height: 45px;
  background: #06214D;
  border-top: 1px solid #21437B;
  border-left: 1px solid #21437B;
  font-size: 14px;
  color: #F5F5F5;
  padding-left: 20px;
}
>>> .item-especial-bot .modal-content-item-name {
  border-bottom: 1px solid #21437B;
}
>>> .item-especial-bot .modal-content-item-value {
  border-bottom: 1px solid #21437B;
}
>>> .item-especial-value .ivu-tooltip-rel{
  cursor: pointer;
  text-decoration: underline;
}
.common-form-item-tips {
  height: 37px;
  background: #2D2515;
  border: 1px solid #FFB31A;
  border-radius: 4px;
  color: #BD9952;
  font-size: 14px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.common-form-item-text {
  font-size: 11px;
  color: #85A3D4;
}
>>> .modal-content-item-value .ivu-tooltip {
  width: 100%;
}
>>> .modal-content-item-value .ivu-tooltip-rel {
  overflow: hidden;
  text-overflow:ellipsis; 
  white-space: nowrap;
}
</style>