<!--计量时间间隔配置-->
<template>
  <div class="systemBase">
    <div class="common-title-div">计量时间间隔配置
       <div class="common-operation-button-div">
          <el-button icon="el-icon-circle-plus-outline" type="primary" size="mini" v-loading.fullscreen.lock="operateLoading"
          @click="addData" v-privilege="activeMenu + 'ADD'">新增</el-button>
          <el-button icon="el-icon-edit" type="primary" size="mini" v-loading.fullscreen.lock="operateLoading"
          @click="modifyData"  v-privilege="activeMenu + 'UPDATE'">修改</el-button>
          <el-button size="mini" type="danger" plain icon="el-icon-delete" v-loading.fullscreen.lock="operateLoading"
          @click="deleteData" v-privilege="activeMenu + 'DELETE'">删除</el-button>
        </div>
    </div>
    <div class="common-table-div">
      <el-table v-loading="dataLoading" @row-click="rowChange" ref="dataTable" highlight-current-row
          border :height="tableHeight" @selection-change="handleSelectionChange" :data="table.list"
          :icore-filter-flag="icoreFilterFlag">
        <el-table-column type="index" label="序号" fixed="left" align="center"></el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="meterTypeNo" label="计量类型编号" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="meterTypeName" label="计量类型名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="spotTypeName" label="计量点类型" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="spaceTime" label="时间间隔（分钟）" align="right"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="200px" prop="memo" label="备注" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="createManName" label="创建人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="updateManName" label="修改人" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="160px" prop="updateTime" label="修改时间" align="center"></el-table-column>
      </el-table>
    </div>
    <div class="common-foot-style">
      <el-pagination v-loading="dataLoading" @size-change="pageSizeChange" @current-change="pageCurrentChange"
          :current-page="page.pageNum" :page-sizes="[100, 500, 1000]" :page-size="page.pageSize" layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalPage">
      </el-pagination>
    </div>
    <!-- <div ref="editTitle" class="common-title-div" style="border-top: 1px solid #d5d5d5;">编辑区
    </div> -->
    <div ref="editDiv" class="commom-edit-div" style="border-top: 1px solid #d5d5d5; margin-top: 10px">
      <el-form ref="baseData" :rules="ruleValidate" :model="baseData" :inline="true">
            <el-form-item prop="meterTypeNo" label="计量类型">
              <el-select v-model="baseData.meterTypeNo" clearable filterable collapse-tags size="small" style="width:150px">
                <el-option v-for="(item,index) in type" :key="index" :label="item.baseName" :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="spotTypeNo" label="计量点类型">
              <el-select v-model="baseData.spotTypeNo" clearable filterable collapse-tags placeholder="请选择计量点类型" size="small" style="width:150px">
                <el-option v-for="(item,index) in spotType" :key="index" :label="item.baseName" :value="item.baseCode">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="spaceTime" label="时间间隔(分钟)">
              <el-input v-model="baseData.spaceTime" clearable size="small" placeholder="请输入" style="width:150px"></el-input>
            </el-form-item>
            <el-form-item prop="memo" label="备注">
              <el-input v-model="baseData.memo" clearable size="small" placeholder="请输入" style="width:150px"></el-input>
            </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
    combaseinfos,
    meterbasetimespaces
  } from '../../../utils/api.js';
   import {
    add,
    modify,
    Delete
  } from '../../../utils/httpRequest.js';
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        icoreFilterFlag: true,
        spotType: [],
        baseData: {
          spaceTime: '',
          memo: '',
          meterTypeNo: '',
          spotTypeNo: ''
        },
        type: [],
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        multipleSelection: [],
        tableHeight: 0,
        ruleValidate: {
          spaceTime: [{
            required: true,
            message: '时间间隔不能为空',
            trigger: 'change'
          }],
          meterTypeNo: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          spotTypeNo: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
        },
        activeMenu: ''
      }
    },
    created(){
      this.tableHeight = (window.innerHeight - 220) > 400 ? window.innerHeight - 220 : 400;
      this.activeMenu = window.top.localStorage.getItem('activeMenu')
    },
    mounted () {
      this.getDataList();
      this.getDataList2();
      this.getMeterType()
    },
    methods: {
      getCode (code, arr, codeKey, nameKey) {
       if (!code) {
         return '';
       }
       let targetRow = arr.find(item => {
         return item[codeKey] === code;
       });
       return targetRow ? targetRow[nameKey] : '';
      },
      rowChange (row, column, event) {
        this.baseData = Object.assign ({}, row);
      },
      addData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            let baseDataAdd = {};
             baseDataAdd.spaceTime = _this.baseData.spaceTime
             baseDataAdd.memo = _this.baseData.memo
             baseDataAdd.meterTypeNo = _this.baseData.meterTypeNo;
             baseDataAdd.spotTypeNo = _this.baseData.spotTypeNo;
             baseDataAdd.meterTypeName = _this.getCode (this.baseData.meterTypeNo, this.type, 'baseCode', 'baseName');
             baseDataAdd.spotTypeName = _this.getCode (this.baseData.spotTypeNo, this.spotType, 'baseCode', 'baseName');
            _this.operateLoading = true;
            add(meterbasetimespaces, baseDataAdd, (e) => {
              _this.operateLoading = false;
              _this.getDataList();
              if (!e) {
                _this.$refs['baseData'].resetFields && _this.$refs['baseData'].resetFields();
              }
            })
          }
        });
      },
      modifyData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            _this.operateLoading = true;
            _this.baseData.meterTypeName = _this.getCode (this.baseData.meterTypeNo, this.type, 'baseCode', 'baseName');
            _this.baseData.spotTypeName = _this.getCode (this.baseData.spotTypeNo, this.spotType, 'baseCode', 'baseName');
            modify(meterbasetimespaces + _this.baseData.timeSpaceNo + '/', _this.baseData, (e) => {
              _this.operateLoading = false;
              _this.getDataList();
              if (!e) {
                _this.$refs['baseData'].resetFields && _this.$refs['baseData'].resetFields();
              }
            })
          }
        });
      },
      deleteData () {
        this.$confirm('确定删除选中的配置吗？', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          })
          .then(() => {
            this.operateLoading = true;
            Delete(meterbasetimespaces, this.multipleSelection, (e) => {
              this.operateLoading = false;
              if (!e) {
                this.getDataList();
              }
            })
          })
          .catch(action => {
            this.$message({
              type: 'info',
              message: action === 'cancel' ? '放弃保存并离开页面' : '停留在当前页面'
            })
          });
      },
      handleSelectionChange (val) {
        this.multipleSelection = val;
      },
      pageSizeChange (size) {
        this.page.pageSize = size;
        this.getDataList();
      },
      pageCurrentChange (index) {
        this.page.pageNum = index;
        this.getDataList();
      },
      getDataList () {
        this.dataLoading = true;
        this.axios.get(meterbasetimespaces + '?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize)
          .then(res => {
            this.dataLoading = false;
            this.table.list = res.data.list;
            this.page.totalPage = res.data.total;
          }).catch(e => {
            this.dataLoading = false;
          });
      },
      getDataList2 () {
        this.axios.get(combaseinfos + 'noPage' + '?pBaseCode=001002' + '&validFlag=1')
          .then(res => {
            this.spotType = res.data;
            this.baseData.spotTypeNo = res.data.length > 0 ? res.data[0].baseCode : ''
          })
      },
      getMeterType () {
         this.axios.get(combaseinfos + 'noPage' + '?pBaseCode=001006' + '&validFlag=1')
          .then(res => {
            this.type = res.data;
            this.baseData.meterTypeNo = res.data.length > 0 ? res.data[0].baseCode : ''
          })
      }
    }
  }
</script>

<style>
</style>
