<template>
  <div class="content-main common-scroll">
    <div class="content-body">
      <Row :gutter="8" style="width:100%;height:100%">
        <i-col span="24" style="height:100%">
          <div class="right-main">
            <div class="manage-main-center">
              <Table :columns="columns" :data="tableData" class="common-table" no-data-text="" :row-class-name="rowClassName" :loading="loading">
                <template slot="loading">
                    <Loading-animation></Loading-animation>
                </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>
                </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" show-elevator />
            </div>
          </div>
        </i-col>
      </Row>
    </div>
     <Modal
      v-model="showModal"
      :title="title"
      width="45"
      ref="modalGrag"
      draggable
      reset-drag-position
      :mask-closable="false"
      class-name="common-modal">
      <div class="common-form-item-tips">
        <i class="iconfont icon-tishi" style="vertical-align: middle;color:#FFB31A;margin-right:12px"></i>
        编辑提交后，系统所有历史警示信息将同时变动，可能造成统计数据混乱，请谨慎操作。
      </div> 
      <div class="common-modal-loading" v-if="editLoading">
        <loading-animation></loading-animation>
      </div>
      <Table :columns="editColumns" :data="editTableData" class="common-table edit-table" :row-class-name="rowClassName" :loading="loading" v-if="!editLoading">
        <template slot="targetName" slot-scope="{ row,index }">
          <Input v-model.trim="row.targetName" style="width: 100%; padding: 6px" maxlength="20" show-word-limit @on-change="inputChange(row,index)"/>
        </template>
      </Table>
      <div slot="footer">
          <Button @click="modalCancel">取消</Button>
          <Button type="primary" @click="modalOk('formOption')">确定</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">
       <Tabs :value="currentModalTabs" @on-click="tabsClick" class="common-tabs">
        <TabPane v-for="tab in tabsModalData" :key="tab.label" :label="tab.label" :name="tab.label"></TabPane>
      </Tabs>
      <rolling-detail-table :columns1="columnsModal" :apiUrl="apiUrl" ref="detailTable" class="rolling-detail-table"></rolling-detail-table>
    </Modal>
</div>
</template>
<script>
import _ from 'lodash'
export default {
  name: "DeviceModelManage",
  components:{
  },
  data() {
    return {
      tableTotal: 0,
      tablePage: 0,
      tableParams: {
        equipmentTypeId: '',
        applicationId: '',
        keywords: '',
        pageNum: 1,
        pageSize: 10
      },
      editLoading: true,
      loading: true,
      tableData: [],
      columns: [
          {
            title: '设备类型名称',
            key: 'equipmentTypeName',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '模型监测指标',
            key: 'targetCount',
            align: 'center'
          },
          {
            title: '等级判定策略覆盖指标数量',
            key: 'rankCount',
            align: 'center'
          },
          {
            title: '连续监测策略覆盖指标数量',
            key: 'durativeCount',
            align: 'center'
          },
          {
            title: '操作',
            align: 'center',
            slot: 'action',
            width: 80
          }
      ],
      rowObj: {},
      formOption: {equipmentTypeId:null},
      ruleValidate: {
        name: [{
          required: true,
          message: '请输入应用名称',
          trigger: 'change'
        }],
      }, 
    equipTypeData: [],
    showModal: false,
    detailStatus: false, 
    modalStatus: false,
    title: '编辑监测模型',
    editTableData: [],
    editColumns: [
       {
          title: '序号',
          type: 'index',
          align: 'center',
          width: 80
        },
        {
          title: '设备指标',
          slot: 'targetName',
          align: 'center',
        },
        {
          title: '对应数据库字段',
          key: 'targetCode',
          align: 'center',
          ellipsis: true,
          tooltip: true
        },
    ],
    detailStatus: false,
    currentModalTabs:'监测指标',
    tabsModalData: [{label:'监测指标'},{label:'关联设备'}],
    columnsModal: [],
    columnsModalType: [
      {
        title: '序号',
        type: 'index',
        align: 'center',
        width: 80
      },
      {
        title: '设备指标',
        key: 'targetName',
        align: 'center',
        ellipsis: true,
        tooltip: true
      },
      {
        title: '对应数据库字段',
        key: 'targetCode',
        align: 'center',
        ellipsis: true,
        tooltip: true
      },
      {
        title: '等级判定策略',
        key: 'rankCount',
        align: 'center'
      },
      {
        title: '连续监测策略',
        key: 'durativeCount',
        align: 'center'
      },
    ],
    columnsRelatedEquip: [
        {
          title: '序号',
          type: 'index',
          align: 'center',
          width: 80
        },
        {
          title: '设备ID',
          key: 'equipmentId',
          align: 'center',
          ellipsis: true,
          tooltip: true
        },
        {
          title: '设备名称',
          key: 'equipmentName',
          align: 'center',
          ellipsis: true,
          tooltip: true
        },
        {
          title: '归属线路',
          key: 'lineName',
          align: 'center',
          ellipsis: true,
          tooltip: true
        },
        {
          title: '归属站点',
          key: 'stationName',
          align: 'center',
          ellipsis: true,
          tooltip: true
        },
        {
          title: '状态',
          key: 'equipmentStatusValue',
          align: 'center',
          ellipsis: true,
          tooltip: true
        },
    ],
    detailParams: {
        equipmentTypeId: null,
        pageNum: 1,
        pageSize: 9999
    },
    apiUrl: '',
    appNameObj: {
      level: '',
      lineId: '',
      stationId: '',
      equipmentCheck: '',
      instrumentCheck: '',
      pageNum: 1,
      pageSize: 9999
    },
    appNameList: []
    };
  },
  mounted() {
  },
  methods: {
    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()
    },
    getTableData (keywords,arr,level) {
      if (level) {
        this.tableParams.keywords = ''
        if (level == 2) {
          this.tableParams.applicationId = arr[arr.length-1]
          this.tableParams.equipmentTypeId = ''
        } else if (level == 3) {
          this.tableParams.applicationId = arr[arr.length-2]
          this.tableParams.equipmentTypeId = arr[arr.length-1]
        } else {
          this.tableParams.applicationId = ''
          this.tableParams.equipmentTypeId = ''
        }
      } else {
        this.tableParams.applicationId = ''
        this.tableParams.equipmentTypeId = ''
        if (keywords == '合肥轨道交通' || keywords == '' || keywords == undefined) {
          this.tableParams.keywords = ''
        } else {
          this.tableParams.keywords = keywords
        }
      }
      this.loading = true
      this.$get('metroapi/target/queryTargetPage', this.tableParams).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
          }
        })
    },
    detailClick (row) {
      this.detailParams.equipmentTypeId = Number(row.equipmentTypeId)
      this.detailParams.pageSize = 999
      this.rowObj = row
      this.detailStatus = true
      this.currentModalTabs = '监测指标'
      this.$nextTick(() => {
        this.apiUrl = 'metroapi/target/queryTargetCountByEquTypeId'
        this.columnsModal = this.columnsModalType
        document.querySelector(".rolling-detail-table .ivu-table-body").scrollTo(0, 0) 
        this.$refs.detailTable.getChartsDetail(this.detailParams,this.apiUrl)
      });
    },
    editClick (row) {
      this.getstrategyData(row.equipmentTypeId)
      this.editLoading = true
      this.rowObj = row
      this.formOption = JSON.parse(JSON.stringify(row))
      this.title = '编辑监测模型'
      this.$nextTick(()=> {
        this.showModal = true
      })
    },
    getstrategyData (equipmentTypeId) {
      this.$get('metroapi/target/queryTargetByEquipmentType', {equipmentTypeId:equipmentTypeId}).then(res=>{
        this.editLoading = false
        if (res.httpCode == 1 ){
          this.editTableData = res.data
          this.formOption.targetInfoList = res.data
        } else {
          this.$Message.info(res.msg)
        }
      })
    },
    inputChange (row,index) {
      this.formOption.targetInfoList[index].targetName = row.targetName
    },
    modalOk (formName) {  
      let canSave = true;
      let arr = this.formOption.targetInfoList
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].targetName == '') {
           this.$Message['error']({
              background: true,
              content: '指标名称不可为空'
            });
            return
        }
          for (var j = i + 1; j < arr.length; j++) {
              if (arr[i].targetName == arr[j].targetName) 
              { 
                canSave = false; 
                break;
              }
          }
      }
      if(canSave) {
        this.$post('metroapi/target/editTarget', this.formOption).then(res=>{
          if (res.httpCode == 1 ){
            this.showModal = false
            this.$Message.info(res.msg)
            this.getTableData()
          } else {
            this.$Message.info(res.msg)
          }
        })
      } else {
        this.$Message['error']({
          background: true,
          content: '模型内各监测指标名称不可重复'
        });
      }
    },
    modalCancel () {
      this.showModal = false
    },
    tabsClick (name) {
      this.currentModalTabs = name
      if (this.currentModalTabs == '监测指标') {
        this.detailStatus
        this.apiUrl = 'metroapi/target/queryTargetCountByEquTypeId'
        this.columnsModal = this.columnsModalType
        this.detailParams.pageSize = 999
      } else if (this.currentModalTabs == '关联设备') {
        this.apiUrl = 'metroapi/equipment/queryEquipmentByTypePage'
        this.columnsModal = this.columnsRelatedEquip
        this.detailParams.pageSize = 10
      }
      this.$nextTick(() => {    
        document.querySelector(".rolling-detail-table .ivu-table-body").scrollTo(0, 0) 
        this.$refs.detailTable.getChartsDetail(this.detailParams,this.apiUrl)
      });
    }
  }
};
</script>
<style scoped lang="stylus">
.content-main {
  width: 100%;
  height: calc(100% - 50px);
  overflow: hidden;
}
.common-search {
  width: 100%;
}
>>> .common-search .ivu-input {
  background-color: #06214D;
  border: 1px solid #2355A6;
  border-radius: 15px;
  color: #fff;
  height: 32px;
}
>>> .common-search .ivu-input:focus {
    border-color: #0185ea;
}
.content-body {
  width: 100%;
  height: 100%;
  background: #06214D;
}
.station-tree {
  height: 100%;
  padding: 10px;
}
.station-tree-body {
  border: 1px solid #204384;
  height: 100%;
  padding: 10px;
}
.station-tree-center {
  height: calc(100% - 32px);
  padding-top: 10px;
  // display: flex;
}
.station-tree-left {
  height: 100%;
  overflow: auto;
}
.station-tree-left-notree {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  fon-size: 14px;
}
.right-main {
  height: 100%;
  padding: 10px 0;
  position: relative;
  // padding: 16px;
}
.manage-main-top {
  padding: 12px 0;
  display: flex;
  justify-content: flex-end;
}
.manage-main-center {
  width: 100%;
  height: calc(100% - 100px);
}
.manage-main-center .common-table {
  max-height: 100%;
}
>>> .common-table .ivu-table th {
  height: 59px;
 }
 >>> .common-table .ivu-table td {
  height: 59px;
 }
>>> .common-table .ivu-spin-fix {
  top: 59px;
  height: calc(100vh - 280px);
}
>>> .common-table .ivu-table-tip {
  background: url('../../../assets/images/noData.png') no-repeat center;
  height: calc(100vh - 280px);
}
>>> .common-table .ivu-table-tip table {
 display: none;
}
.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;
  }
}

>>> .edit-table .ivu-table-body{
  max-height: 475px;
  overflow: hidden; 
  overflow-y: auto;
}
>>> .edit-table .ivu-table th {
  height: 48PX;
 }
 >>> .edit-table .ivu-table td {
  height: 48PX;
 }
>>> .edit-table .ivu-spin-fix {
  top: 0;
  height: auto;
}
>>> .edit-table .ivu-table-tip {
  background: none;
  height: auto;
}
>>> .edit-table .ivu-table-tip table {
 display: block;
}
>>> .edit-table .ivu-table-tip table td{
 border-bottom: none;
}
// >>> .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-top-especial {
//   margin-bottom: -10px;
// }
// >>> .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: 100%;
//   display: flex;
// }
// >>> .item-especial {
//   width: 100%;
// }
// >>> .modal-content-item-name {
//   width: 25%;
//   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: 75%;
//   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 .modal-content-item-name{
//   width: 25%;
//   height: auto;
//   min-height: 45px;
//   border-bottom: 1px solid #21437B;
//   display: flex;
//   align-items: center;
// }
// >>> .item-especial .modal-content-item-value{
//   width: 75%;
//   height: auto;
//   min-height: 45px;
//   border-bottom: 1px solid #21437B;
//   display: flex;
//   align-items: center;
//   line-height: 22px;
// }
// >>> .item-especial .modal-content-item-name {
//   border-bottom: 1px solid #21437B;
// }
// >>> .item-especial .modal-content-item-value {
//   border-bottom: 1px solid #21437B;
// }
// >>> .modal-content-item-value .ivu-tooltip {
//   width: 100%;
// }
// >>> .modal-content-item-value .ivu-tooltip-rel {
//   overflow: hidden;
//   text-overflow:ellipsis; 
//   white-space: nowrap;
// }
.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;
}
</style>