<!--异常过磅统计-->
<template>
  <div class="systemBase">
    <div>
      <div style="margin-top: 10px; border-bottom: 1px solid #d5d5d5">
        <el-form
          ref="searchData1"
          :inline="true"
          :model="search"
          :rules="ruleValidate"
        >
          <el-form-item label-width="80px">
            <div style="font-size: 16px; padding: 0px 20px">异常过磅管理</div>
          </el-form-item>
          <el-form-item prop="baseSpotNo" label="计量点">
            <el-select
              v-model="search.baseSpotNo"
              clearable
              filterable
              collapse-tags
              size="small"
              style="width: 120px"
            >
              <el-option
                v-for="(item, index) in baseSpotNameItem"
                :key="index"
                :label="item.baseSpotName"
                :value="item.baseSpotNo"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="strDate" label="开始时间">
            <el-date-picker
              v-model="search.strDate"
              clearable
              type="datetime"
              style="width: 180px"
              placeholder="选择日期"
              size="small"
            ></el-date-picker>
          </el-form-item>
          <el-form-item prop="envDate" label="结束时间">
            <el-date-picker
              v-model="search.envDate"
              clearable
              type="datetime"
              style="width: 180px"
              placeholder="选择日期"
              size="small"
            ></el-date-picker>
          </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>
      </div>
      <el-row>
        <el-col :span="7">
          <div style="border-right: 1px solid #d5d5d5">
            <div class="common-title-div">异常过磅统计</div>
            <div class="common-table-div">
              <el-table
                v-loading="dataLoading"
                @row-click="getDataList2"
                ref="dataTable"
                highlight-current-row
                border
                :height="tableHeight"
                :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
                  sortable
                  :show-overflow-tooltip="true"
                  prop="meterReasonName"
                  label="异常类型"
                  min-width="120"
                  align="center"
                ></el-table-column>
                <el-table-column
                  sortable
                  :show-overflow-tooltip="true"
                  prop="abnormalNum"
                  label="异常次数"
                  min-width="120"
                  align="right"
                ></el-table-column>
                <el-table-column
                  sortable
                  :show-overflow-tooltip="true"
                  prop="abnormalWeight"
                  label="异常重量（T）"
                  min-width="120"
                  align="right"
                >
                  <template slot-scope="scope">
                    {{ Math.floor((scope.row.abnormalWeight - 0) / 10) / 100 }}
                  </template>
                </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>
        </el-col>
        <el-col :span="17">
          <div class="common-title-div">异常明细</div>
          <div class="common-table-div">
            <el-table
              v-loading="dataLoading"
              @row-click="getImageList"
              :row-class-name="tableRowClassName"
              ref="dataTable2"
              highlight-current-row
              border
              :height="tableHeight2"
              :data="table.list2"
              :icore-filter-flag="icoreFilterFlag"
              :exchangeFilterMap="{
                valueFlag: { '0': '作废', '1': '有效', '2': '洁净' },
              }"
            >
              <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"
                prop="actualFirstNo"
                label="作业编号"
                min-width="110"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="predictionNo"
                label="预报编号"
                min-width="80"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="mnemonicCode"
                label="车号"
                min-width="110"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="meterReasonNo "
                label="异常类型编号"
                min-width="120"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="meterReasonName"
                label="异常类型名称"
                min-width="120"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="carNo"
                label="挂号"
                min-width="80"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="matterName"
                label="物资名称"
                min-width="80"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="meterWeight"
                label="重量"
                align="right"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="meterNum"
                label="数量"
                align="right"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="addWeight"
                label="附加重量"
                align="right"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="baseSpotName"
                label="计量点"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="160"
                prop="createTime"
                label="计量时间"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="createManName"
                label="计量员"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="meterClass"
                label="班次"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="meterGroup"
                label="班组"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="contractNo"
                label="合同号"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="batchNo"
                label="批次号"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="200"
                prop="customerSupplierName"
                label="供应商"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="200"
                prop="forwardingUnitName"
                label="发货单位"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="200"
                prop="receivingUintName"
                label="收货单位"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="materialName"
                label="材质"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="specName"
                label="规格"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="bindCardNo"
                label="绑定卡号"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="loadPointName"
                label="卸货地点"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="sampleNo"
                label="取样编号"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="sampleVoucher"
                label="取样凭证"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="200"
                prop="carrierUnitName"
                label="承运单位"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                min-width="80"
                prop="meterTypeName"
                label="业务类型"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="valueFlag"
                label="状态"
                min-width="60"
                align="center"
              >
                <template slot-scope="scope">
                  <span v-if="scope.row.valueFlag == 0">作废</span>
                  <span v-else-if="scope.row.valueFlag == 1">有效</span>
                  <span v-else-if="scope.row.valueFlag == 2">结净</span>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="common-foot-style">
            <el-pagination
              v-loading="dataLoading2"
              @size-change="pageSizeChange2"
              @current-change="pageCurrentChange2"
              :current-page="page2.pageNum"
              :page-sizes="[100, 500, 1000]"
              :page-size="page2.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page2.totalPage"
            >
            </el-pagination>
          </div>
          <div id="getBigImage">
            <ul>
              <li v-for="(item, index) in urlImage" :key="index">
                <img :src="item.image" width="97%" @click="imageChange" />
              </li>
            </ul>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import { formatDate } from '@/utils/util.js';
import Viewer from 'viewerjs'
import 'viewerjs/dist/viewer.css'
export default {
  data () {
    return {
      dataLoading: false,
      dataLoading2: false,
      operateLoading: false,
      icoreFilterFlag: true,
      baseSpotNameItem: [],
      page: {
        pageSize: 500,
        pageNum: 1,
        totalPage: 0
      },
      page2: {
        pageSize: 500,
        pageNum: 1,
        totalPage: 0
      },
      table: {
        list: [],
        list2: []
      },
      tableHeight: 0,
      tableHeight2: 0,
      actualFirst: {},
      firstRow: {},
      ruleValidate: {
        envDate: [{
          required: true,
          validator: this.endTimeVerify,
          trigger: 'blur'
        }],
        strDate: [{
          required: true,
          validator: this.strTimeVerify,
          trigger: 'blur'
        }]
      },
      search: {
        baseSpotNo: '',
        strDate: formatDate(new Date(), 'yyyy-MM-dd') + ' 00:00:00',
        envDate: formatDate(new Date(), 'yyyy-MM-dd') + ' 23:59:59'
      },
      urlImage: [],
      activeMenu: ''
    }
  },
  components: {
    Viewer
  },
  created () {
    this.tableHeight = (window.innerHeight - 220) > 400 ? window.innerHeight - 220 : 400
    this.tableHeight2 = (window.innerHeight - 320) > 300 ? window.innerHeight - 320 : 300
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
  },
  mounted () {
    this.getBaseSpotNameItem()
  },
  methods: {
    endTimeVerify (rule, value, callback) {
      if (this.search.strDate && !this.search.envDate) {
        callback(new Error('结束时间不能为空！'));
      } else if (this.search.envDate) {
        if (Date.parse(this.search.envDate) > Date.parse(this.search.strDate)) {
          callback();
        } else {
          callback(new Error('结束时间不得早于开始时间！'));
        }
      } else {
        callback();
      }
    },
    strTimeVerify (rule, value, callback) {
      if (this.search.envDate && !this.search.strDate) {
        callback(new Error('开始时间不能为空！'));
      } else {
        callback()
      }
    },
    imageChange (e) {
      if (e.target.nodeName == 'IMG') {
        this.initImageTools();
      }
    },
    initImageTools () {
      const ViewerDom = document.getElementById('getBigImage');
      const viewer = new Viewer(ViewerDom, {
        url: 'src',  //设置大图片的 url
        show: function () {
          viewer.update();
        },
        hide: function () { //在图片消失的时候销毁viewer
          viewer.destroy()
        }
      });
    },
    getImageList (row) {
      this.firstRow = row
      if (row === undefined || row === null) {
        this.urlImage = []
        return
      }
      this.axios.get('pass/systemBase/v1/meterworkimages/getUrl' + '?actualFirstNo=C' + row.actualFirstNo)
        .then(res => {
          if (res.data !== null && res.data.length !== 0) {
            const imageList = res.data
            for (let i = 0; i < res.data.length; i++) {
              if (this.urlImage.length < res.data.length) {
                this.urlImage.push({ image: '' })
              }
              this.urlImage[i].image = '/weblib' + res.data[i]
            }
          }
        });
    },
    getDataList2 (row) {
      this.actualFirst = row
      if (row === undefined || row === null) {
        this.table.list2 = []
        return
      }
      this.dataLoading2 = true;
      this.axios.get('pass/systemBase/v1/meterworkcaractualfirsts/getByAbnormal' + '?pageNum=' + this.page2.pageNum
        + '&pageSize=' + this.page2.pageSize + '&meterReasonNo=' + row.meterReasonNo + '&meterReasonName=' + row.meterReasonName)
        .then(res => {
          this.dataLoading2 = false;
          this.table.list2 = res.data.list;
          this.page2.totalPage = res.data.total;
          if (this.table.list.length > 0) {
            this.$refs.dataTable2.setCurrentRow(this.table.list2[0])
            this.getImageList(this.table.list2[0])
          }
        }).catch(e => {
          this.dataLoading2 = false;
        });
    },
    searchDatas () {
      this.page.pageNum = 1;
      this.searchData()
    },
    searchData () {
      this.$refs['searchData1'].validate((valid) => {
        if (valid) {
          this.dataLoading = true;
          if (isNaN(this.search.strDate) && !isNaN(Date.parse(this.search.strDate))) {
          } else {
            this.search.strDate = formatDate(this.search.strDate, 'yyyy-MM-dd HH:mm:ss')
          }
          if (isNaN(this.search.envDate) && !isNaN(Date.parse(this.search.envDate))) {
          } else {
            this.search.envDate = formatDate(this.search.envDate, 'yyyy-MM-dd HH:mm:ss')
          }
          this.table.list2 = []
          this.axios.get('pass/systemBase/v1/meterworkcaractualfirsts/abnormal' + '?pageNum=' + this.page.pageNum + '&pageSize=' + this.page.pageSize + '&baseSpotNo=' + this.search.baseSpotNo
            + '&startTime=' + this.search.strDate + '&endTime=' + this.search.envDate)
            .then(res => {
              this.dataLoading = false;
              this.table.list = res.data.list;
              this.page.totalPage = res.data.total;
              if (this.table.list.length > 0) {
                this.$refs.dataTable.setCurrentRow(this.table.list[0])
                this.getDataList2(this.table.list[0])
              }
            }).catch(e => {
              this.dataLoading = false;
            });
        }
      })
    },
    getBaseSpotNameItem () {
      this.axios.get('pass/systemBase/v1/meterbasespotinfos/noPage' + '?validFlag=1')
        .then(res => {
          this.baseSpotNameItem = res.data;
        })
    },
    pageSizeChange (size) {
      this.page.pageSize = size;
      this.searchData()
    },
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.searchData()
    },
    pageSizeChange2 (size) {
      this.page2.pageSize = size;
      this.getDataList2(this.actualFirst);
    },
    pageCurrentChange2 (index) {
      this.page2.pageNum = index;
      this.getDataList2(this.actualFirst);
    },
    tableRowClassName ({
      row,
      rowIndex
    }) {
      if (row.valueFlag === '0') {
        return 'error-row2';
      }
      return '';
    },
  }
}
</script>

<style scoped>
</style>