<template>
  <div class="content-main common-scroll">
    <div class="search-list">
      <div class="search-left">
        <Form class="common-form common-form-list" ref="tableParams" :model="tableParams" inline>
          <FormItem label="" prop="energyConsumptionId">
            <Select v-model="tableParams.energyConsumptionId" placeholder="能耗类型">
              <Option v-for="item in energyTypeData" :value="item.id" :key="item.id">{{ item.name }}</Option>
            </Select>
          </FormItem>
          <FormItem label="" prop="instrumentCount">
            <Select v-model="tableParams.instrumentCount" placeholder="接入仪表数量">
                <Option v-for="item in associatedEquipData" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          </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>
     <div class="btn-container">
      <Button icon="ios-add" class="common-add-btn" @click="addClick">新增仪表类型</Button>
    </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="action">
           <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="36"
      ref="modalGrag"
      draggable
      reset-drag-position
      :mask-closable="false"
      @on-visible-change="modalChange"
      class-name="common-modal">
      <Form class="common-form" ref="formOption" :model="formOption" :rules="ruleValidate" :label-width="130">
        <FormItem label="能耗类型名称：" prop="energyConsumptionId">
            <Select v-model="formOption.energyConsumptionId" placeholder="能耗类型">
              <Option v-for="item in energyTypeModalData" :value="item.id" :key="item.id">{{ item.name }}</Option>
            </Select>
          </FormItem>
				<FormItem label="仪表类型名称：" prop="name">
          <Input v-model.trim="formOption.name" maxlength="20" show-word-limit />  
				</FormItem>
				<FormItem label="备注：" prop="remark">
           <Input v-model="formOption.remark" type="textarea" :rows="6" maxlength="200" show-word-limit /> 
        </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>
</div>
</template>
<script>
const defaultFrom = {energyConsumptionId:'',name: '',remark: ''}
import _ from 'lodash'
export default {
  name: "InstruTypeManage",
  components:{
  },
  data() {
    return {
      tableTotal: 0,
      tablePage: 0,
      tableParams: {
        applicationId: '',
        instrumentType: '',
        energyConsumptionId: '',
        instrumentCount: '',
        keywords: '',
        pageNum: 1,
        pageSize: 10
      },
      loading: true,
      tableData: [],
      columns: [
          {
            title: '仪表类型名称（唯一）',
            key: 'name',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '能耗类型',
            key: 'energyConsumptionName',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '关联应用数量',
            key: 'appRelationCount',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '接入仪表数量',
            key: 'instrumentCount',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '操作',
            align: 'center',
            slot: 'action',
            width: 120
          }
      ],
      rowObj: {},
      formOption: {energyConsumptionId:'',name: '',remark: ''},
      ruleValidate: {
        energyConsumptionId: [{
          required: true,
          type: 'number',
          message: '请选择能耗类型名称',
          trigger: 'change'
        }],
        name: [{
          required: true,
          message: '请输入仪表类型名称',
          trigger: 'blur'
        }],
      }, 
      energyTypeData: [],
      energyTypeModalData: [],
      associatedEquipData:[
        {
          value: '-1',
          label: '接入仪表数量'
        },
        {
          label: '0',
          value: '0'
        },
        {
          label: '其他',
          value: '1'
        }
      ],
    showModal: false,
    modalStatus: false,
    title: '新增仪表类型',
    commonTitle: "确认删除",
    activeColor: '#E92E2E',
    activeClass: '',
    modalTitle: '',
    };
  },
  created() {
    this.getType()
    this.getTableData()
  },
  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()
    },
    //跳转
    sizeChange (val) {
      this.tableParams.pageSize = val
      this.getTableData()
    },
    getTableData () {
      let { ...params } = this.tableParams
      params.energyConsumptionId = params.energyConsumptionId == '-1' ? '':params.energyConsumptionId
      params.instrumentCount = params.instrumentCount == '-1' ? '':params.instrumentCount
      this.loading = true
      this.$get('metroapi/instrumentType/queryInstrumentTypePage', 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()
      // this.$router.push({ path: '/MainPage/ApplicationManage',  query: {deptName: "2"}})
    },
    resetClick (name) {
     this.$refs[name].resetFields()
     this.tableParams.pageNum = 1
     this.getTableData()
    }, 
    getType  () {
      // 获取能耗类型
      this.$get('metroapi/energyConsumption/queryEnergyConsumption').then(res=>{
        if (res.httpCode == 1 ){
          this.energyTypeModalData = JSON.parse(JSON.stringify(res.data))
          this.energyTypeData = res.data
          this.energyTypeData.unshift({ id: '-1', name: '能耗类型' })
        } else {
          this.energyTypeData = []
          this.energyTypeModalData = []
        }
      })
    },
    addClick () {
      this.title = '新增仪表类型'
      this.formOption = JSON.parse(JSON.stringify(defaultFrom))
      this.showModal = true
    },
    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) {
       if (this.title == '新增仪表类型') {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$post('metroapi/instrumentType/addInstrumentType', this.formOption).then(res=>{
              if (res.httpCode == 1 ){
                this.showModal = false
                this.$emit('changeTree')
                this.$Message.info(res.msg)
                this.getTableData()
              } else {
                this.$Message.info(res.msg)
              }
            })
          } else {
            this.showModal = true
          }
       })
      } else {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$post('metroapi/instrumentType/editInstrumentType', this.formOption).then(res=>{
              if (res.httpCode == 1 ){
                this.showModal = false
                this.$emit('changeTree')
                this.$Message.info(res.msg)
                this.getTableData()
              } else {
                this.$Message.info(res.msg)
              }
            })
          } else {
            this.showModal = true
          }
       })
      }
    },
    modalCancel () {
      this.showModal = false
    },
    commonOk () {
      let params = {
        instrumentTypeId: this.rowObj.id
      }
      this.delData(params)
    },
    commonCancel () {
      this.modalStatus = false
    },
    // 删除接口
    delData (params) {
      this.$get('metroapi/instrumentType/delInstrumentType',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;
  height: calc(100% - 50px);
}
.search-list {
  display: flex;
  padding: 10px 0 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: 165px;
     height: 32px;
   }
   >>> .ivu-date-picker-editor .ivu-input {
     width: 210px;
   }
   >>> .ivu-input-prefix, >>> .ivu-input-suffix {
     height: 32px;
     line-height: 32px;
   }
   >>> .ivu-input-icon {
     height: 32px;
     line-height: 32px;
   }
   >>> .date-picker-main .ivu-date-picker-header {
    border-bottom: 0.0052083333rem solid #204384;
 }
}
.btn-container {
  display: flex;
  justify-content: flex-end;
  padding-bottom: 10px;
}
>>> .common-form-list .ivu-form-item {
  margin-bottom: 10px;
}
.manage-main-center {
  width: 100%;
  height: calc(100% - 150px);
}
.common-table {
  max-height: 100%;
}
>>> .common-table .ivu-table th {
  height: 51px;
 }
 >>> .common-table .ivu-table td {
  height: 51px;
 }
  >>> .common-table .ivu-spin-fix {
   top: 51px;
 }
  >>> .app-table .ivu-spin-fix {
   height: calc(100vh - 375px);
 }
 >>> .ivu-table-tip {
  background: url('../../../assets/images/noData.png') no-repeat center;
}
>>> .app-table .ivu-table-tip {
  height: calc(100vh - 375px);
 }
>>> .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;
  }
}
</style>