<!--互检差值配置-->
<template>
  <div class="systemBase common-container-deriction">
    <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-remove-outline" v-loading.fullscreen.lock="operateLoading"
        @click="abolishData" v-privilege="activeMenu + 'ABOLISH'">废除</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>
      </div>
    </div>
    <div class="common-table-div">
       <el-table v-loading="dataLoading" @row-click="rowChange" ref="dataTable" :row-class-name="tableRowClassName"  highlight-current-row
          border :height="tableHeight" @current-change="handleCurrentChange" :data="table.list"
          :exchangeFilterMap="{'valueFlag':{'1':'无效','0':'有效'}}"
          :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="120px" prop="baseSpotOneName" label="计量点1号名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="120px" prop="baseSpotSecondName" label="计量点2号名称" align="center"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="100px" prop="diffWeight" label="差值（kg）" align="right"></el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="80px" prop="valueFlag" label="状态" align="center">
          <template slot-scope="scope">
              <span v-if="scope.row.valueFlag == 1">无效</span>
              <span v-else-if="scope.row.valueFlag == 0">有效</span>
          </template>
        </el-table-column>
        <el-table-column sortable :show-overflow-tooltip="true" min-width="200px" prop="memo" 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" label-width="60px" :rules="ruleValidate" :inline="true" :model="baseData">
          <el-form-item prop="baseSpotOneNo" label="计量点1">
            <el-select v-model="baseData.baseSpotOneNo" clearable filterable collapse-tags size="small" style="width:160px">
            <el-option v-for="(item,index) in meteringPoint1" :key="index" :label="item.baseSpotName"
              :value="item.baseSpotNo">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item prop="diffWeight" label="差 值kg">
            <el-input v-model="baseData.diffWeight" clearable size="small" placeholder="请输入" style="width:160px"></el-input>
          </el-form-item>
          <el-form-item prop="baseSpotSecondNo" label="计量点2">
            <el-select v-model="baseData.baseSpotSecondNo" clearable filterable collapse-tags size="small" style="width:160px">
            <el-option v-for="(item,index) in meteringPoint2" :key="index" :label="item.baseSpotName"
              :value="item.baseSpotNo">
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item prop="memo" label="备注">
            <el-input v-model="baseData.memo" clearable size="small" placeholder="请输入" style="width:160px"></el-input>
          </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {
    meterbasespotinfos,
    // meterbasescalepoints,
    meterbasemutualdiffs
  } from '../../../utils/api.js';
   import {
    add,
    modify,
    DeleteOne,
    RecoveryOne,
    discardOne
  } from '../../../utils/httpRequest.js';
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        icoreFilterFlag: true,
        meteringPoint2: [],
        meteringPoint1: [],
        baseData: {
          diffWeight: '',
          memo: '',
          baseSpotOneNo:'',
          baseSpotSecondNo: ''
        },
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        tableHeight: 0,
        ruleValidate: {
          diffWeight: [{
            required: true,
            message: '差值不能为空',
            trigger: 'change'
          }],
          baseSpotSecondNo: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          baseSpotOneNo: [{
            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();
    },
    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.diffWeight = _this.baseData.diffWeight
            baseDataAdd.memo = _this.baseData.memo
            baseDataAdd.baseSpotOneNo = _this.baseData.baseSpotOneNo;
            baseDataAdd.baseSpotSecondNo = _this.baseData.baseSpotSecondNo;
            baseDataAdd.baseSpotOneName = _this.getCode (this.baseData.baseSpotOneNo, this.meteringPoint1, 'baseSpotNo', 'baseSpotName')
            baseDataAdd.baseSpotSecondName = _this.getCode (this.baseData.baseSpotSecondNo, this.meteringPoint2, 'baseSpotNo', 'baseSpotName')
            _this.operateLoading = true;
            add(meterbasemutualdiffs, 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.baseSpotOneName = _this.getCode (this.baseData.baseSpotOneNo, this.meteringPoint1, 'baseSpotNo', 'baseSpotName')
            _this.baseData.baseSpotSecondName = _this.getCode (this.baseData.baseSpotSecondNo, this.meteringPoint2, 'baseSpotNo', 'baseSpotName')
            modify(meterbasemutualdiffs + _this.baseData.mutualDiffNo + '/', _this.baseData, (e) => {
              _this.operateLoading = false;
              _this.getDataList();
              if (!e) {
                _this.$refs['baseData'].resetFields && _this.$refs['baseData'].resetFields();
              }
            })
          }
        });
      },
      recoveryData () {
        let _this = this;
        _this.operateLoading = true;
        RecoveryOne(meterbasemutualdiffs + 'Recovery', _this.multipleSelection, (e) => {
          _this.operateLoading = false;
          if (!e) {
            _this.getDataList();
          }
        });
      },
       abolishData () {
        let _this = this;
        _this.operateLoading = true;
        discardOne(meterbasemutualdiffs + 'abolish', _this.multipleSelection, (e) => {
          _this.operateLoading = false;
          if (!e) {
            _this.getDataList();
          }
        });
      },
      handleCurrentChange (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(meterbasemutualdiffs + '?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;
          });
      },
      tableRowClassName ({
        row,
        rowIndex
      }) {
        if (row.valueFlag != 0) {
          return 'error-row2';
        }
        return '';
      },
      getDataList2 () {
        this.axios.get(meterbasespotinfos + 'noPage' + '?validFlag=1')
          .then(res => {
            this.meteringPoint1 = res.data;
            this.meteringPoint2 = res.data;
            this.baseData.baseSpotOneNo = res.data.length > 0 ? res.data[0].baseSpotNo : ''
            this.baseData.baseSpotSecondNo = res.data.length > 0 ? res.data[0].baseSpotNo : ''
          });
      }
    }
  }
</script>

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