<!--双秤差值配置-->
<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-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 :show-overflow-tooltip="true" sortable min-width="200px" prop="baseSpotName" label="计量点名称" align="center" ></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="200px" prop="scalePointOneName" label="秤点1号" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="200px" prop="scalePointSecondName" label="秤点2号" align="center"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="90px"  prop="diffWeight" label="差值（kg）" align="right"></el-table-column>
        <el-table-column :show-overflow-tooltip="true" sortable min-width="70px"  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 :show-overflow-tooltip="true" sortable 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" size="mini" background>
      </el-pagination>
    </div>
    <div ref="editDiv" class="commom-edit-div" style="border-top: 1px solid #d5d5d5; margin-top: 10px">
      <el-form ref="baseData" label-width="60px" :inline="true" :rules="ruleValidate" :model="baseData">
           <el-form-item prop="baseSpotNo" label="计量点">
             <!-- <el-tooltip :content="baseData.baseSpotNo" placement="bottom" effect="light"> -->
               <el-select v-model="baseData.baseSpotNo" clearable filterable collapse-tags size="small" style="width:160px" @change="getDataList3(baseData.baseSpotNo)">
                <el-option v-for="(item,index) in meteringPoint" :key="index" :label="item.baseSpotName"
                  :value="item.baseSpotNo">
                </el-option>
              </el-select>
             <!-- </el-tooltip> -->
           </el-form-item>
           <el-form-item prop="scalePointOneNo" label="秤点1号">
             <el-select v-model="baseData.scalePointOneNo" clearable filterable collapse-tags size="small" style="width:160px">
             <el-option v-for="(item,index) in  balancePoint1" :key="index" :label="item.scalePointName"
               :value="item.scalePointNo">
             </el-option>
           </el-select>
           </el-form-item>
           <el-form-item prop="scalePointSecondNo" label="秤点2号">
             <el-select v-model="baseData.scalePointSecondNo" clearable filterable collapse-tags size="small" style="width:160px">
             <el-option v-for="(item,index) in  balancePoint2" :key="index" :label="item.scalePointName"
               :value="item.scalePointNo">
             </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="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,
    meterbasedoublediffs
  } from '../../../utils/api.js';
   import {
    add,
    modify,
    DeleteOne,
    RecoveryOne,
    discardOne
  } from '../../../utils/httpRequest.js';
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        // meteringPointIndex: 0,
        // balancePointIndex1: 0,
        // balancePointIndex2: 0,
        meteringPoint: [],
        balancePoint1: [],
        balancePoint2: [],
        baseData: {
          diffWeight: '',
          memo: '',
          scalePointOneNo: '',
          scalePointSecondNo: '',
          baseSpotNo: ''
        },
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        // multipleSelection: {},
        tableHeight: 0,
        icoreFilterFlag: true,
        ruleValidate: {
          diffWeight: [{
            required: true,
            message: '差值不能为空',
            trigger: 'change'
          }],
          baseSpotNo: [
            {
            required: true,
            message: '该项不能为空',
            trigger: 'change'
           }
          ],
          scalePointOneNo: [
            {
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }
          ],
          scalePointSecondNo: [
            {
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          },
          {
            required: false, 
            validator: this.scalePointSecondNoIsLike,
            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] : '';
     },
     scalePointSecondNoIsLike (rule, value, callback) {
       if (this.baseData.scalePointSecondNo) {
        if (this.baseData.scalePointSecondNo == this.baseData.scalePointOneNo) {
          return callback(new Error('秤点2号不能与秤点1号相同'))
        } else {
          return callback()
        }
       }
      }, 
      // scalePointOneNoIsLike (rule, value, callback) {
      //  if (this.baseData.scalePointOneNo) {
      //   if (this.baseData.scalePointSecondNo == this.baseData.scalePointOneNo) {
      //     return callback(new Error('秤点1号不能与秤点2号相同'))
      //   } else {
      //     return callback()
      //   }
      //  } else {
      //     return callback()
      //   }
      // },
      rowChange (row, column, event) {
        this.baseData = Object.assign ({}, row)
        this.getDataList4(this.baseData.baseSpotNo)

      },
      addData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            let baseDataAdd = {}; 
            baseDataAdd.diffWeight = _this.baseData.diffWeight;
            baseDataAdd.memo = _this.baseData.memo;
            baseDataAdd.baseSpotNo = _this.baseData.baseSpotNo;
            baseDataAdd.scalePointOneNo = _this.baseData.scalePointOneNo
            baseDataAdd.scalePointSecondNo =  _this.baseData.scalePointSecondNo
            baseDataAdd.baseSpotName = _this.getCode (this.baseData.baseSpotNo, this.meteringPoint, 'baseSpotNo', 'baseSpotName')
            baseDataAdd.scalePointOneName = _this.getCode (this.baseData.scalePointOneNo, this.balancePoint1, 'scalePointNo', 'scalePointName')
            baseDataAdd.scalePointSecondName = _this.getCode (this.baseData.scalePointSecondNo, this.balancePoint2, 'scalePointNo', 'scalePointName')
            _this.operateLoading = true;
            add(meterbasedoublediffs, 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')
            _this.baseData.scalePointOneName = _this.getCode (this.baseData.scalePointOneNo, this.balancePoint1, 'scalePointNo', 'scalePointName')
            _this.baseData.scalePointSecondName = _this.getCode (this.baseData.scalePointSecondNo, this.balancePoint2, 'scalePointNo', 'scalePointName')
            modify(meterbasedoublediffs + _this.baseData.doubleDiffNo + '/', _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(meterbasedoublediffs + 'Recovery', _this.multipleSelection, (e) => {
          _this.operateLoading = false;
          if (!e) {
            _this.getDataList();
          }
        });
      },
       abolishData () {
        let _this = this;
        _this.operateLoading = true;
        discardOne(meterbasedoublediffs + 'abolish', _this.multipleSelection, (e) => {
          _this.operateLoading = false;
          if (!e) {
            _this.getDataList();
          }
        });
      },
      //  deleteData () {
      //   this.$confirm('确定删除选中的配置吗？', {
      //       confirmButtonText: '确定',
      //       cancelButtonText: '取消'
      //     })
      //     .then(() => {
      //       this.operateLoading = true;
      //       DeleteOne(meterbasedoublediffs + this.multipleSelection.doubleDiffNo, (e) => {
      //         this.operateLoading = false;
      //         if (!e) {
      //           this.getDataList();
      //         }
      //       })
      //     })
      //     .catch(action => {
      //       this.$message({
      //         type: 'info',
      //         message: action === 'cancel' ? '放弃保存并离开页面' : '停留在当前页面'
      //       })
      //     });
      // },
      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(meterbasedoublediffs + '?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.meteringPoint = res.data;
            this.baseData.baseSpotNo = res.data[0].baseSpotNo
            this.getDataList3(this.baseData.baseSpotNo)
          });
      },
      getDataList3 (baseSpotNo) {
        // const baseSpotNo = this.meteringPoint[index].baseSpotNo
        this.axios.get(meterbasescalepoints + 'noPage' + '?validFlag=1' + '&baseSpotNo=' + baseSpotNo)
          .then(res => {
            this.balancePoint1 = res.data;
            this.balancePoint2 = res.data;
            if (res.data.length > 0) {
              this.baseData.scalePointOneNo = res.data[0].scalePointNo
              this.baseData.scalePointSecondNo = res.data[0].scalePointNo
            } else {
              this.baseData.scalePointOneNo = ''
              this.baseData.scalePointSecondNo = ''
            }
          });
      },
      getDataList4 (baseSpotNo) {
        // const baseSpotNo = this.meteringPoint[index].baseSpotNo
        this.axios.get(meterbasescalepoints + 'noPage' + '?validFlag=1' + '&baseSpotNo=' + baseSpotNo)
          .then(res => {
            this.balancePoint1 = res.data;
            this.balancePoint2 = res.data;
          });
      }
    }
  }
</script>
<style scoped>
  .el-select {
    width: 100%;
  }
</style>
