<!--车辆滞磅时间管理-->
<template>
  <div class="systemBase">
    <div>
      <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 :show-overflow-tooltip="true" sortable min-width="120px" prop="baseSpotNo" label="计量点编号" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="130px" prop="baseSpotName" label="计量点名称" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="100px" prop="retentionTime" label="停留时间（s）" align="right"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="200px" prop="memo" label="备注"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="createManName" label="创建人" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="160px" prop="createTime" label="创建时间" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="80px" prop="updateManName" label="修改人" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable 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"  :inline="true" :model="baseData">
              <el-form-item prop="baseSpotNo" label="计量点">
                 <el-select v-model="baseData.baseSpotNo" clearable filterable collapse-tags size="small" style="width:150px;">
                  <el-option v-for="(item,index) in meteringPoint" :key="index" :label="item.baseSpotName"
                    :value="item.baseSpotNo">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="retentionTime" label="停留时间(S)">
                <el-input v-model="baseData.retentionTime" 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>
  </div>
</template>

<script>
   import {
    meterbasespotinfos,
    meterbasecarholdtimes
  } from '../../../utils/api.js';
   import {
    add,
    modify,
    Delete
  } from '../../../utils/httpRequest.js';
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        meteringPoint: [],
        icoreFilterFlag: true,
        baseData: {
          retentionTime: '',
          memo: '',
          baseSpotNo: ''
        },
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        multipleSelection: [],
        tableHeight: 0,
        ruleValidate: {
          baseSpotNo: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          retentionTime: [{
            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.tableHeight = this.getTableHeight(this.$refs['dataTable'].$el) - 80;
      this.getDataList();
      this.getDataList2();
    },
    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.retentionTime = _this.baseData.retentionTime
            baseDataAdd.memo = _this.baseData.memo
            baseDataAdd.baseSpotNo = _this.baseData.baseSpotNo
            baseDataAdd.baseSpotName =_this.getCode (this.baseData.baseSpotNo, this.meteringPoint, 'baseSpotNo', 'baseSpotName')
            _this.operateLoading = true;
            add(meterbasecarholdtimes, 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.baseSpotName = _this.getCode (this.baseData.baseSpotNo, this.meteringPoint, 'baseSpotNo', 'baseSpotName')
            modify(meterbasecarholdtimes + _this.baseData.holdTimeNo + '/', _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(meterbasecarholdtimes, this.multipleSelection, (e) => {
              this.operateLoading = false;
              if (!e) {
                this.getDataList();
              }
            })
          })
          .catch(action => {
            this.$message({
              type: 'info',
              message: action === 'cancel' ? '放弃保存并离开页面' : '停留在当前页面'
            })
          });
      },
      handleSelectionChange (val) {
        this.multipleSelection = val;
      },
      getTableHeight (dom) {
        return (window.innerHeight - dom.offsetTop);
      },
      pageSizeChange (size) {
        this.page.pageSize = size;
        this.getDataList();
      },

      pageCurrentChange (index) {
        this.page.pageNum = index;
        this.getDataList();
      },
      getDataList () {
        this.dataLoading = true;
        this.axios.get(meterbasecarholdtimes + '?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(meterbasespotinfos + 'noPage' + '?validFlag=1')
          .then(res => {
            this.meteringPoint = res.data;
            this.baseData.baseSpotNo = res.data[0].baseSpotNo ? res.data[0].baseSpotNo : ''
          });
      }
    }
  }
</script>

<style scoped>
  .el-select {
    width: 100%;
  }
</style>
