<template>
  <div class="content-main-manage">
    <div class="content-main">
     <div class="content-body-wrap">
       <div class="content-body">
          <div class="search-list">
            <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="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>
       </div>      
     </div>
    </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="140">
        <FormItem label="能耗类型名称：" prop="name">
          <Input v-model.trim="formOption.name" maxlength="10" show-word-limit placeholder="请输入能耗名称"/>
        </FormItem>
        <FormItem label="能耗单位：" prop="unit">
          <Input v-model="formOption.unit" maxlength="10" show-word-limit placeholder="请输入能耗单位"/>
        </FormItem>
        <FormItem prop="coalCoefficient" class="else-formitem">
          <div slot="label">折标准煤系数<br>(kgce/unit)：</div>
          <Input v-model="formOption.coalCoefficient" maxlength="10" show-word-limit placeholder="请输入折算系数"/>
        </FormItem>
          <FormItem prop="co2Coefficient" class="else-formitem">
          <div slot="label">CO2当量排放因子<br>(kgCO2e/unit)：</div>
          <Input v-model="formOption.co2Coefficient" maxlength="10" show-word-limit placeholder="请输入折算系数" />
        </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">
        <i :class="'iconfont '+ activeClass" :style="{ color: activeColor}" class="modal-icon" v-show="commonTitle!='确认删除'"></i>
      <Icon type="ios-information-circle" :style="{ color: activeColor}" class="modal-icon" v-show="commonTitle=='确认删除'"></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 = {name: '',unit: '',coalCoefficient: '',co2Coefficient: ''}
export default {
  name: "EnergyTypeManage",
  components:{
  },
  data() {
    return {
      loading: true,
      tableParams: {
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      tableTotal: 0,
      tablePage: 0,
      columns: [
          {
            title: '序号',
            type: 'index',
            align: 'center',
          },
          {
            title: '能耗类型名称',
            key: 'name',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '能耗单位',
            key: 'unit',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '折标准煤系数\n(kgce/unit)',
            key: 'coalCoefficient',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: 'CO2当量排放因子\n(kgCO2e/unit)',
            key: 'co2Coefficient',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '创建时间',
            key: 'createTime',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '修改日期',
            key: 'updateTime',
            align: 'center',
            ellipsis: true,
            tooltip: true
          },
          {
            title: '操作',
            align: 'center',
            slot: 'action',
            width: 120,
          }
      ],   
      showModal: false,
      permissStatus: false, 
      modalStatus: false,
      title: '新增能耗类型',
      commonTitle: "确认删除",
      activeColor: '#E92E2E',
      activeClass: '',
      modalTitle: '',
      formOption: {name: '',unit: '',coalCoefficient: '',co2Coefficient: ''},
      ruleValidate: {
        name: [{
          required: true,
          message: '请输入能耗类型名称',
          trigger: 'blur'
        }],
      },
    };
  },
  mounted() {
    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 = JSON.parse(JSON.stringify(this.tableParams))
      this.loading = true
      this.$get('metroapi/energyConsumption/queryEnergyConsumptionPage', 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.$refs[name].resetFields()
     this.tableParams.pageNum = 1
     this.getTableData()
    },   
    addClick () {
      this.formOption = JSON.parse(JSON.stringify(defaultFrom))
      this.title = '新增能耗类型'
      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) {
      let url = ''
      if (this.title == '新增能耗类型') {
        url = 'metroapi/energyConsumption/addEnergyConsumption'
      } else {
        url = 'metroapi/energyConsumption/editEnergyConsumption'
      }
      let params = JSON.parse(JSON.stringify(this.formOption)) //把对象中的数据给了某个变量，改变一个对象的值，另一个对象也变化的解决方法
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$post(url, 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 () {
      let params = {
        energyConsumptionId: this.rowObj.id
      }
      this.delData(params)
    },
    commonCancel () {
      this.modalStatus = false
    },
    // 删除 关闭/接口
    delData (params) {
      this.$get('metroapi/energyConsumption/delEnergyConsumption',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 {
  overflow: hidden;
  height: calc(100% - 50px);
  width: 100%;
}
.content-main {
  width: 100%;
  height: 100%;
}
.content-body-wrap {
  width: 100%;
  height: 100%;
  background: #06214d;
  padding: 10px;
}
.content-body {
  height: 100%;
  position: relative;
}
.search-list {
  display: flex;
  justify-content: flex-end;
  padding: 10px 0;
}
.manage-main-center {
  width: 100%;
  height: calc(100% - 150px);
}
.common-table {
  max-height: 100%;
}
>>> .common-table .ivu-table .ivu-table-header .ivu-table-cell {
 white-space: pre;
}
>>> .common-table .ivu-table th {
  height: 54px;
 }
 >>> .common-table .ivu-table td {
  height: 55px;
 }
  >>> .common-table .ivu-spin-fix {
   top: 54px;
 }
  >>> .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;
  }
}
>>> .else-formitem .ivu-form-item-label{
line-height: 16px;
}
</style>