<!--火车车厢固重配置-->
<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="primary" icon="el-icon-check" v-loading.fullscreen.lock="operateLoading"
           @click="recoveryData" v-privilege="activeMenu + 'RECOVERY'">启用</el-button>
          <el-button size="mini" type="danger" plain icon="el-icon-remove-outline" v-loading.fullscreen.lock="operateLoading"
           @click="discardData"  v-privilege="activeMenu + 'ABOLISH'">废除</el-button>
        </div>
      </div>
      <div class="common-table-div">
        <el-table ref="dataTable"  
          @row-click="rowChange" 
          v-loading="dataLoading"
          border 
          :height="tableHeight" 
          highlight-current-row
          @selection-change="handleSelectionChange"
          :row-class-name="tableRowClassName" 
          :data="table.list" 
          :exchangeFilterMap="{
          'railwayType':{'0':'内部车辆','1':'外部车辆'},
          'valueFlag':{'0':'有效','1':'无效'}}"
          :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="railwayNo" label="火车车厢号" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="railwayWeight" label="重量" align="right"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="railwayType" label="火车类型" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.railwayType == 0">内部车辆</span>
              <span v-else-if="scope.row.railwayType == 1">外部车辆</span>
            </template>
          </el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="railwayTypeName" label="轨道类型名称" align="center"></el-table-column>
           <el-table-column sortable :show-overflow-tooltip="true" prop="valueFlag" min-width="100px" label="状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.valueFlag == 0">有效</span>
              <span v-else-if="scope.row.valueFlag == 1">无效</span>
            </template>
          </el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="startTime1" label="有效开始时间" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="endTime1" label="有效结束时间" align="center"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="termValidity" label="有效期(天)" align="right"></el-table-column>
          <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" 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="100px" 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="editDiv" class="commom-edit-div" style="border-top: 1px solid #d5d5d5; margin-top: 10px">
        <el-form ref="baseData" :inline="true" :rules="ruleValidateTable" :model="baseData">
           <el-form-item prop="railwayNo" label="火车车厢号">
            <el-input v-model="baseData.railwayNo" size="small" clearable style="width:140px" placeholder="请输入"></el-input>
           </el-form-item>
            <el-form-item prop="railwayWeight" label="重量">
            <el-input v-model="baseData.railwayWeight" size="small" clearable style="width:140px" placeholder="请输入"></el-input>
           </el-form-item>
           <el-form-item prop="railwayType" label="火车类型">
               <el-select v-model="baseData.railwayType"  filterable size="small" style="width:140px">
                   <el-option v-for="(item,index) in railwayTypeItem" :key="index" :label="item.railwayType"
                    :value="item.railwayType">
                   </el-option>
               </el-select>
            </el-form-item>
           <el-form-item prop="railwayTypeName" label="轨道衡类型">
               <el-select v-model="baseData.railwayTypeName"  filterable size="small" style="width:140px">
                   <el-option v-for="(item,index) in railwayTypeNameItem" :key="index" :label="item.baseName"
                    :value="item.baseName">
                   </el-option>
               </el-select>
            </el-form-item>
            <el-form-item prop="termValidity" label="有效期(天)">
                <el-input v-model="baseData.termValidity" size="small" clearable style="width:140px" placeholder="请输入天数" @change="onChangeInput(baseData.termValidity)"></el-input>
            </el-form-item>
             <el-form-item prop="startTime" label="有效开始时间">
                <el-date-picker type="date" size="small" v-model="baseData.startTime" clearable @change="onChangeStartTime" style="width:140px">
                </el-date-picker>
                <!-- <el-date-picker type="datetime" v-model="baseData.startTime" size="small" @change="onChangeStartTime">
                </el-date-picker> -->
              </el-form-item>
              <el-form-item prop="endTime" label="有效结束时间">
                <el-date-picker type="date" size="small" v-model="baseData.endTime" clearable style="width:140px" readonly>
                </el-date-picker>
                <!-- <el-date-picker type="datetime" v-model="baseData.endTime" readonly size="small">
                </el-date-picker> -->
              </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import {formatDate} from '@/utils/util.js';
import { momentImmutableMethods } from 'moment-immutable-methods'
import moment from 'moment'
momentImmutableMethods(moment)
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        icoreFilterFlag: true,
        railwayTypeNameItem: [],//轨道衡类型
        railwayTypeItem: [
          {id:'0', railwayType:'内部车辆'},
          {id:'1', railwayType:'外部车辆'},
        ],//火车类型
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        baseData: {
            railwayNo: '',
            railwayWeight: '',
            railwayType: '内部车辆',
            railwayTypeName: '',
            termValidity: '',
            startTime: '',
            endTime: ''
        },
        multipleSelection: [],
        tableHeight: 0,
        ruleValidateTable: {
          railwayNo: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          railwayWeight: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          railwayType: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          railwayTypeName: {
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          },
          termValidity: {
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          },
          startTime: {
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          },
          endTime: {
            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.getRailwayTypeNameItem();
    },
    methods: {
     getCode (code, arr, codeKey, nameKey) {
      if (!code) {
        return '';
      }
      let targetRow = arr.find(item => {
        return item[codeKey] === code;
      });
      return targetRow ? targetRow[nameKey] : '';
    },
    onChangeInput(e){
      e = Number(e)
      var startTime = this.baseData.startTime;
      this.baseData.endTime = moment(this.baseData.startTime).add('days',e);
     },
     onChangeStartTime() {
      var e = Number(this.baseData.termValidity)
       var startTime = this.baseData.startTime;
       if (e!=='' || e!== null) {
          this.baseData.endTime = moment(this.baseData.startTime).add('days',e);
       } else {
         return 
       }
     },
    refresh () {
      this.baseData = {
            railwayNo: '',
            railwayWeight: '',
            railwayType: '内部车辆',
            railwayTypeName: '',
            termValidity: '',
            startTime: '',
            endTime: ''
        },
        this.baseData.railwayTypeName = this.railwayTypeNameItem[0].baseName
        this.$refs['baseData'].resetFields();
    },
    addData () {
       this.$refs['baseData'].validate((valid) => {
          if (valid) {
             let baseDataAdd = {}
             baseDataAdd.railwayNo = this.baseData.railwayNo
             baseDataAdd.railwayWeight = this.baseData.railwayWeight
             baseDataAdd.railwayTypeName = this.baseData.railwayTypeName
             baseDataAdd.termValidity = this.baseData.termValidity
             baseDataAdd.startTime = this.baseData.startTime
             baseDataAdd.endTime = this.baseData.endTime
             baseDataAdd.railwayType = this.getCode (this.baseData.railwayType, this.railwayTypeItem, 'railwayType', 'id')
             baseDataAdd.railwayTypeNo = this.getCode (this.baseData.railwayTypeName, this.railwayTypeNameItem, 'baseName', 'baseCode')
             this.axios.post('pass/systemBase/v1/meterbaserailwayweights/add', baseDataAdd, {individualType: 'json'}).then(res=>{
               if (res.succeed) {
                  this.$message.success('新增成功');
                  this.getDataList ();
                  this.refresh();
               }  else {
                this.$message.error(res.message)
               }
               }).catch(e => {
                this.$message.error(e.message)
              })
          }
       })
    },
    modifyData () {
      this.$refs['baseData'].validate((valid) => {
          if (valid) {
              this.baseData.railwayType = this.getCode (this.baseData.railwayType, this.railwayTypeItem, 'railwayType', 'id')
              this.baseData.railwayTypeNo = this.getCode (this.baseData.railwayTypeName, this.railwayTypeNameItem, 'baseName', 'baseCode')
              this.axios.post('pass/systemBase/v1/meterbaserailwayweights/update', this.baseData, {individualType: 'json'}).then(res=>{
                if (res.succeed) {
                   this.$message.success('修改成功');
                   this.getDataList ();
                   this.refresh();
                }  else {
                 this.$message.error(res.message)
                }
                }).catch(e => {
                 this.$message.error(e.message)
               })
          }
      })
    },
    discardData () {
    if (this.multipleSelection.length < 1) {
        this.$message.warning('请选择您要废除的数据！');
        return;
      }
      this.axios.post('pass/systemBase/v1/meterbaserailwayweights/abolish' ,this.multipleSelection, {individualType: 'json'}).then(res => {
         if (res.succeed) {
           this.$message.success('废除成功');
           this.getDataList ();
        }  else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
    },
    recoveryData () {
    if (this.multipleSelection.length < 1) {
        this.$message.warning('请选择您要启用的数据！');
        return;
      }
      this.axios.post('pass/systemBase/v1/meterbaserailwayweights/recover',this.multipleSelection, {individualType: 'json'}).then(res => {
         if (res.succeed) {
           this.$message.success('启用成功');
           this.getDataList ();
        }  else {
         this.$message.error(res.message)
        }
        }).catch(e => {
         this.$message.error(e.message)
       })
    },
    rowChange (row, column, event) {
       this.baseData = Object.assign ({}, row);
       this.baseData.railwayType = this.getCode (this.baseData.railwayType, this.railwayTypeItem, 'id', 'railwayType')
    },
      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('pass/systemBase/v1/meterbaserailwayweights/' + '?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize)
          .then(res => {
            this.dataLoading = false;
            this.table.list = res.data.list;
            this.table.list.forEach(item => {
              item.startTime1 = formatDate (item.startTime, 'yyyy-MM-dd')
              item.endTime1 = formatDate (item.endTime, 'yyyy-MM-dd')
            });
            this.page.totalPage = res.data.total;
          }).catch(e => {
            this.dataLoading = false;
          });
      },
     getRailwayTypeNameItem () {
          this.axios.get('pass/systemBase/v1/combaseinfos/noPage' + '?pBaseCode=001019' + '&validFlag=1')
          .then(res => {
            this.railwayTypeNameItem = res.data;
            this.baseData.railwayTypeName = res.data.length > 0 ? res.data[0].baseName : '';
          })
      },
     tableRowClassName ({
        row,
        rowIndex
      }) {
        if (row.valueFlag == '1') {
          return 'error-row2';
        }
        return '';
      },
    },
  }
</script>
<style scoped>
</style>