<!--计量票据打印-->
<template>
  <div class="systemBase">
    <div>
      <div class="common-title-div">计量票据打印管理
        <div class="common-operation-button-div">
          <el-form :inline="true" :model="search"  @submit.native.prevent>
           <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 label-width="20px">
            <el-button icon="el-icon-search" type="primary" size="mini" @click="searchDatas" v-loading.fullscreen.lock="operateLoading"  v-privilege="activeMenu + 'SEARCH'">查询</el-button>
          </el-form-item>
          <el-form-item label-width="20px">
          <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-form-item>
           <el-form-item label-width="20px">
          <el-button icon="el-icon-edit" type="primary" size="mini" v-loading.fullscreen.lock="operateLoading"
          @click="modifyData" v-privilege="activeMenu + 'UPDATE'">修改</el-button>
           </el-form-item>
           <el-form-item>
            <el-button size="mini" type="danger" plain icon="el-icon-delete" v-loading.fullscreen.lock="operateLoading"
             @click="deleteData" v-privilege="activeMenu + 'DELETE'">删除</el-button>
           </el-form-item>
          </el-form>
        </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="100px" prop="baseSpotName" label="计量点" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="120px" prop="matterName" label="物料名称" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="120px" prop="loadPointName" label="卸货地点" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="120px" prop="meterTypeName" label="计量类型" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="100px" prop="weightTypeName" label="重量类型" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="60px" prop="printNum" label="张数" align="right"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="200px" prop="memo" label="备注" align="center"></el-table-column>
          <el-table-column :show-overflow-tooltip="true" sortable min-width="100px" 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="100px" 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" label-width="70px" :inline="true" :rules="ruleValidate" :model="baseData">
                  <el-form-item prop="matterNo" label="物料名称">
                    <el-select v-model="baseData.matterNo" clearable filterable collapse-tags size="small" style="width:150px" :filter-method="getMatterName">
                      <el-option v-for="item in metterType" :key="item.matterNo" :label="item.matterName" :value="item.matterNo">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <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="weightTypeNo" label="重量类型">
                    <el-select v-model="baseData.weightTypeNo" clearable filterable collapse-tags size="small" style="width:150px">
                      <el-option v-for="(item,index) in weightType" :key="index" :label="item.baseName" :value="item.baseCode">
                      </el-option>
                    </el-select>
                  </el-form-item>
                  <el-form-item prop="meterTypeNo" label="计量类型">
                    <el-select v-model="baseData.meterTypeNo" clearable filterable collapse-tags size="small" style="width:150px">
                     <el-option v-for="(item,index) in type" :key="index" :label="item.baseName" :value="item.baseCode">
                     </el-option>
                   </el-select>
                  </el-form-item>
                  <el-form-item prop="loadPointNo" label="卸货点">
                   <el-select v-model="baseData.loadPointNo" clearable filterable collapse-tags size="small" style="width:150px">
                     <el-option v-for="(item,index) in  balancePoint" :key="index" :label="item.loadPointName"
                     :value="item.loadPointNo">
                     </el-option>
                   </el-select>
                  </el-form-item>
                  <el-form-item prop="printNum" label="张数">
                    <el-input v-model="baseData.printNum" clearable size="small" style="width:150px" placeholder="请输入"></el-input>
                  </el-form-item>
              <el-form-item prop="memo" label="备注">
                <el-input v-model="baseData.memo" clearable size="small" style="width:150px" placeholder="请输入"></el-input>
              </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {
    meterbasematterinfos,
    meterbasespotinfos,
    meterbaseloadpoints,
    meterbasebillprints,
    combaseinfos
  } from '../../../utils/api.js';
   import {
    add,
    modify,
    Delete
  } from '../../../utils/httpRequest.js';
  export default {
    data () {
      return {
        dataLoading: false,
        operateLoading: false,
        icoreFilterFlag: true,
        typeIndex: 0,
        weightTypeIndex: 0,
        metterTypeIndex: 0,
        meteringPointIndex: 0,
        balancePointIndex: 0,
        meteringPoint: [],
        balancePoint: [],
        baseData: {
          printNum: '',
          memo: '',
          matterNo: '',
          baseSpotNo: '',
          weightTypeNo: '',
          meterTypeNo: '',
          loadPointNo: ''
        },
        type: [],
        weightType: [],
        metterType: [],
        page: {
          pageSize: 500,
          pageNum: 1,
          totalPage: 0
        },
        table: {
          list: []
        },
        multipleSelection: [],
        search: {
           matterName: '',
        },
        tableHeight: 0,
        ruleValidate: {
          printNum: [{
            required: true,
            message: '张数不能为空',
            trigger: 'change'
          },
          { required: true, validator: this.printNumIsNum, trigger: 'blur'},
          ],
          matterNo: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          // baseSpotNo: [{
          //   required: true,
          //   message: '该项不能为空',
          //   trigger: 'change'
          // }],
          weightTypeNo: [{
            required: true,
            message: '该项不能为空',
            trigger: 'change'
          }],
          // meterTypeNo: [{
          //   required: true,
          //   message: '该项不能为空',
          //   trigger: 'change'
          // }],
          // loadPointNo: [{
          //   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();
      this.getDataList3();
      this.getDataList4();
      this.getType();
      this.getWeightType()
    },
    methods: {
      rowChange (row, column, event) {
         this.baseData = Object.assign ({}, row);
         this.axios.get('pass/systemBase/v1/meterbasematterinfos/noPage'+ '?validFlag=1' + '&matterNo='+ this.baseData.matterNo)
         .then(res => {
           this.metterType = res.data;
         })
         console.log(this.baseData.billPrintNo)
      },
      getCode (code, arr, codeKey, nameKey) {
       if (!code) {
         return '';
       }
       let targetRow = arr.find(item => {
         return item[codeKey] === code;
       });
       return targetRow ? targetRow[nameKey] : '';
     },
      printNumIsNum (rule, value, callback) {
        const patrn =  /^[0-9]*$/
        if (!patrn.test(this.baseData.printNum)) {
          callback(new Error('请输入正整数！'))
        } else{
          callback()
       }
      },
      getMatterName (val) {
       this.axios.get('pass/systemBase/v1/meterbasematterinfos/noPage'+ '?validFlag=1' + '&matterName='+ val)
         .then(res => {
           this.metterType = res.data;
         })
      },
      searchDatas () {
        this.page.pageNum = 1
        this.searchData ()
      },
      searchData () {
         let obj = {
          pageNum: this.page.pageNum,
          pageSize: this.page.pageSize,
          matterName: this.search.matterName,
        }
         this.axios.post('pass/systemBase/v1/meterbasebillprints/listLike',obj,{individualType: 'json'})
         .then(res => {
           this.table.list = res.data.list;
           this.page.totalPage = res.data.total
         })
      },
      addData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            let baseDataAdd = {};
            baseDataAdd.printNum = _this.baseData.printNum
            baseDataAdd.memo = _this.baseData.memo
            baseDataAdd.meterTypeNo = _this.baseData.meterTypeNo;
            baseDataAdd.weightTypeNo = _this.baseData.weightTypeNo;
            baseDataAdd.matterNo = _this.baseData.matterNo;
            baseDataAdd.baseSpotNo = _this.baseData.baseSpotNo;
            baseDataAdd.loadPointNo = _this.baseData.loadPointNo;
            baseDataAdd.meterTypeName = _this.getCode (this.baseData.meterTypeNo, this.type, 'baseCode', 'baseName')
            baseDataAdd.weightTypeName = _this.getCode (this.baseData.weightTypeNo, this.weightType, 'baseCode', 'baseName') 
            baseDataAdd.matterName = _this.getCode (this.baseData.matterNo, this.metterType, 'matterNo', 'matterName')
            baseDataAdd.baseSpotName = _this.getCode (this.baseData.baseSpotNo, this.meteringPoint, 'baseSpotNo', 'baseSpotName')
            baseDataAdd.loadPointName = _this.getCode (this.baseData.loadPointNo, this.balancePoint, 'loadPointNo', 'loadPointName')
            _this.operateLoading = true;
            add(meterbasebillprints, baseDataAdd, (e) => {
              _this.operateLoading = false;
              // _this.getDataList();
              _this.searchDatas();
              if (!e) {
                _this.$refs['baseData'].resetFields && _this.$refs['baseData'].resetFields();
              }
            })
          }
        });
      },
      modifyData () {
        let _this = this;
        _this.$refs['baseData'].validate((valid) => {
          if (valid) {
            _this.baseData.meterTypeName = _this.getCode (this.baseData.meterTypeNo, this.type, 'baseCode', 'baseName')
            _this.baseData.weightTypeName = _this.getCode (this.baseData.weightTypeNo, this.weightType, 'baseCode', 'baseName') 
            _this.baseData.matterName = _this.getCode (this.baseData.matterNo, this.metterType, 'matterNo', 'matterName')
            _this.baseData.baseSpotName = _this.getCode (this.baseData.baseSpotNo, this.meteringPoint, 'baseSpotNo', 'baseSpotName')
            _this.baseData.loadPointName = _this.getCode (this.baseData.loadPointNo, this.balancePoint, 'loadPointNo', 'loadPointName')
            _this.operateLoading = true;
            modify(meterbasebillprints + _this.baseData.billPrintNo + '/', _this.baseData, (e) => {
              _this.operateLoading = false;
              // _this.getDataList();
              _this.searchDatas();
              if (!e) {
                _this.$refs['baseData'].resetFields && _this.$refs['baseData'].resetFields();
              }
            })
          }
        });
      },
      deleteData () {
        this.$confirm('确定删除选中的配置吗？', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          })
          .then(() => {
            this.operateLoading = true;
            Delete(meterbasebillprints, this.multipleSelection, (e) => {
              this.operateLoading = false;
              if (!e) {
                // this.getDataList();
                this.searchDatas();
              }
            })
          })
          .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();
        this.searchData()
      },

      pageCurrentChange (index) {
        this.page.pageNum = index;
        // this.getDataList();
        this.searchData()
      },
      getDataList () {
        this.dataLoading = true;
        this.axios.get(meterbasebillprints + '?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(meterbasematterinfos + 'noPage' + '?validFlag=1')
      //     .then(res => {
      //       this.metterType = res.data;
      //       this.baseData.matterNo = res.data.length > 0 ? res.data[0].matterNo : ''
      //     })
      // },
      getDataList3 () {
        this.axios.get(meterbasespotinfos + 'noPage' + '?validFlag=1')
          .then(res => {
            this.meteringPoint = res.data;
            // this.baseData.baseSpotNo = res.data.length > 0 ? res.data[0].baseSpotNo : ''
          });
      },
      getDataList4 () {
        this.axios.get(meterbaseloadpoints + 'noPage' + '?validFlag=1')
          .then(res => {
            this.balancePoint = res.data;
            // this.baseData.loadPointNo = res.data.length > 0 ? res.data[0].loadPointNo : ''
          });
      },
      getType () {
        this.axios.get(combaseinfos + 'noPage' + '?pBaseCode=001006' + '&validFlag=1')
          .then(res => {
            this.type = res.data;
            // this.baseData.meterTypeNo = res.data.length > 0 ? res.data[0].baseCode : ''
          })
      },
      getWeightType () {
        this.axios.get(combaseinfos + 'noPage' + '?pBaseCode=001008' + '&validFlag=1')
          .then(res => {
            this.weightType = res.data;
            // this.baseData.weightTypeNo = res.data.length > 0 ? res.data[0].baseCode : ''
          })
      }
    }
  }
</script>

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