<!--火车车厢固重配置-->
<template>
  <div class="systemBase">
    <div>
      <div class="common-title-div">
        <el-form
          :inline="true"
          :model="search"
          ref="searchForm"
          @submit.native.prevent
          style="padding-left: 15px; text-align: end;"
        >
          <!-- <el-form-item prop="matterName" label="物料名称">
            <el-input
              v-model="search.matterName"
              clearable
              size="small"
              style="width: 150px"
              placeholder="请输入"
              @keyup.enter.native="searchDatas"
            ></el-input>
          </el-form-item> -->
          <el-form-item>
            <!-- <el-button
              icon="el-icon-search"
              type="primary"
              size="mini"
              v-loading.fullscreen.lock="operateLoading"
              @click="getDataList"
              >查询</el-button
            > -->
            <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 + 'PUT'"
              >修改</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="common-table-div">
        <el-table
          ref="dataTable"
          @row-click="rowChange"
          v-loading="dataLoading"
          border
          :height="tableHeight"
          highlight-current-row
          :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
            sortable
            :show-overflow-tooltip="true"
            min-width="210px"
            prop="tareTypeName"
            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"
            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="tareTypeNo" label="动轨期限皮重类型">
            <el-select
              v-model="baseData.tareTypeNo"
              filterable
              size="small"
              style="width: 140px"
              @change="typeChange"
            >
              <el-option
                v-for="item in tareTypeList"
                :key="item.baseCode"
                :label="item.baseName"
                :value="item.baseCode"
              >
              </el-option>
            </el-select>
          </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="tarePlace" label="磅点">
            <el-select
              v-model="baseData.tarePlace"
              filterable
              size="small"
              style="width: 140px"
            >
              <el-option
                v-for="item in tarePlaceList"
                :key="item.baseSpotNo"
                :label="item.baseSpotName"
                :value="item.baseSpotNo"
              >
              </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: []
      },
      search: {
        matterName: ''
      },
      baseData: {
        railwayNo: '',
        railwayWeight: '',
        railwayType: '内部车辆',
        railwayTypeName: '动态轨道衡',
        termValidity: '',
        startTime: '',
        endTime: '',
        tareTypeNo: '',
        tareTypeName: '',
        tarePlace: ''
      },
      multipleSelection: [],
      tableHeight: 0,
      ruleValidateTable: {
        tareTypeNo: [{
          required: true,
          message: '该项不能为空',
          trigger: 'change'
        }],
        tarePlace: [{
          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: '',
      tareTypeList: [],
      tarePlaceList: []
    }
  },
  created () {
    this.tableHeight = (window.innerHeight - 220) > 400 ? window.innerHeight - 220 : 400;
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
  },
  mounted () {
    this.getDataList();
    this.getRailwayTypeNameItem();
    // 获取基础配置表中动轨期限皮类型
    this.getDynamicTareType()
    // 获取磅点数据
    this.getTarePlace()
  },
  methods: {
    searchDatas () {

    },
    async getDynamicTareType () {
      const { data: res } = await this.axios.get('pass/systemBase/v1/combaseinfos/?pageNum=1&pageSize=500&pBaseCode=001032')
      console.log(res, '期限皮重类型数据')
      this.tareTypeList = [...res.list]
    },
    async getTarePlace () {
      const { data: res } = await this.axios.get('pass/systemBase/v1/meterbasespotinfos/?validFlag=1&spotTypeNo=001002002')
      console.log(res, '磅点list')
      this.tarePlaceList = [...res.list.filter(item => item.baseSpotName.indexOf('动态') !== -1)]
    },
    getCode (code, arr, codeKey, nameKey) {
      if (!code) {
        return '';
      }
      let targetRow = arr.find(item => {
        return item[codeKey] === code;
      });
      return targetRow ? targetRow[nameKey] : '';
    },
    typeChange (val) {
      if (val) {
        let item = this.tareTypeList.find(item => item.baseCode === val)
        this.baseData.tareTypeName = item.baseName
      }
    },
    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: '内部车辆',
        termValidity: '',
        startTime: '',
        endTime: ''
      },
      // this.baseData.railwayTypeName = this.railwayTypeNameItem[0].baseName
      this.$refs['baseData'].resetFields();
      this.baseData.railwayTypeName = '动态轨道衡'
    },
    addData () {
      this.$refs['baseData'].validate((valid) => {
        if (valid) {
          let baseDataAdd = {}
          baseDataAdd.valueFlag = '1'
          baseDataAdd.tareType = 'CS'
          baseDataAdd.railwayNo = this.baseData.tareTypeNo.slice(6, 10)
          baseDataAdd.memo = this.baseData.tarePlace.indexOf('砂石') !== -1 ? '00026' : '00025'
          baseDataAdd.railwayWeight = this.baseData.railwayWeight
          baseDataAdd.railwayTypeName = this.baseData.railwayTypeName
          baseDataAdd.termValidity = this.baseData.termValidity
          baseDataAdd.startTime = formatDate(this.baseData.startTime).slice(0, 10)
          baseDataAdd.endTime = formatDate(this.baseData.endTime).slice(0, 10)
          baseDataAdd.tareTypeNo = this.baseData.tareTypeNo
          baseDataAdd.tareTypeName = this.baseData.tareTypeName
          baseDataAdd.railwayType = this.getCode(this.baseData.railwayType, this.railwayTypeItem, 'railwayType', 'id')
          console.log(this.baseData.railwayTypeName)
          console.log(this.getCode(this.baseData.railwayTypeName, this.railwayTypeNameItem, 'baseName', 'baseCode'))
          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)
          })
        }
      })
    },
    rowChange (row, column, event) {
      this.baseData = Object.assign({}, row);
      this.baseData.railwayType = this.getCode(this.baseData.railwayType, this.railwayTypeItem, 'id', 'railwayType')
      this.baseData.tarePlace = row.tareTypeName.indexOf('砂石') !== -1 ? '砂石计检站动态衡' : '选矿计检站动态衡'
    },
    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}&railwayTypeName=动态轨道衡`)
        .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;
        })
    },
    tableRowClassName ({
      row,
      rowIndex
    }) {
      if (row.valueFlag == '1') {
        return 'error-row2';
      }
      return '';
    },
  },
}
</script>
<style scoped>
</style>