<!--抓斗秤维护页面-->
<template>
  <div class="systemBase product_wrap_xb">
    <div>
      <div style="margin-top: 10px; border-bottom: 1px solid #d5d5d5">
        <el-form ref="searchData" :inline="true" :model="searchForm">
          <el-form-item label-width="65px" prop="dataTime" label="查询日期">
            <el-date-picker
              v-model="pageForm.dataTime"
              clearable
              type="date"
              style="width: 180px"
              @change="timeChange(pageForm.dataTime, 'dataTime', 'search')"
              size="mini"
            ></el-date-picker>
          </el-form-item>
          <el-form-item prop="banci" label="班次">
            <el-select
              v-model="searchBanci"
              clearable
              size="mini"
              style="width: 120px"
            >
              <el-option
                v-for="item in classList"
                :key="item.id"
                :value="item.value"
                :label="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label-width="65px" label="仓库名称">
            <!-- <el-input
              type="text"
              size="mini"
              v-model="searchForm.storeName"
              clearable
              style="width: 120px"
            ></el-input> -->
            <el-select
              v-model="searchForm.storeName"
              clearable
              size="mini"
              style="width: 120px"
            >
              <el-option
                v-for="item in storeList"
                :key="item.id"
                :value="item.id"
                :label="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label-width="65px" label="行车号">
            <el-input
              type="text"
              size="mini"
              v-model="searchForm.dataHch"
              clearable
              style="width: 120px"
            ></el-input>
          </el-form-item>
          <el-form-item label-width="65px" label="数据类型">
            <el-select
              v-model="searchForm.dataType"
              clearable
              size="mini"
              style="width: 120px"
            >
              <el-option
                v-for="item in matTypeList"
                :key="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label-width="65px" label="数据状态">
            <el-select
              v-model="searchForm.validFlag"
              clearable
              size="mini"
              style="width: 120px"
            >
              <el-option
                v-for="item in flagList"
                :key="item.id"
                :value="item.id"
                :label="item.val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label-width="65px" label="异常类型">
            <el-select
              v-model="searchForm.exceptionTypeName"
              clearable
              size="mini"
              style="width: 120px"
            >
              <el-option
                v-for="item in exceptionList"
                :key="item.baseCode"
                :value="item.baseName"
                :label="item.baseName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label-width="20px">
            <el-button
              icon="el-icon-search"
              type="primary"
              size="mini"
              @click="queryData(1)"
              v-loading.fullscreen.lock="operateLoading"
              v-privilege="activeMenu + 'QUERY'"
              >查询</el-button
            >
          </el-form-item>
          <!-- <el-button
              icon="el-icon-circle-plus-outline"
              type="primary"
              size="mini"
              @click="addData"
              v-loading.fullscreen.lock="operateLoading"
              v-privilege="activeMenu + 'ADD'"
              >新增</el-button
            > -->
          <el-form-item label-width="20px">
            <el-button
              icon="el-icon-circle-plus-outline"
              type="primary"
              size="mini"
              @click="showBatchDialog"
              v-loading.fullscreen.lock="operateLoading"
              >批量新增</el-button
            >
          </el-form-item>
          <!-- <el-button
              icon="el-icon-edit"
              type="primary"
              size="mini"
              @click="modifyData"
              v-loading.fullscreen.lock="operateLoading"
              v-privilege="activeMenu + 'PUT'"
              >修改</el-button
            > -->
          <el-form-item label-width="20px">
            <el-button type="danger" size="mini" @click="showDropDialog"
              >作废</el-button
            >
          </el-form-item>
          <el-form-item label-width="20px">
            <el-button type="primary" size="mini" @click="enableData"
              >启用</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-row>
        <div style="border-right: 1px solid #d5d5d5">
          <div
            style="
              display: flex;
              padding-left: 10px;
              padding-top: 10px;
              margin-bottom: 10px;
              margin-right: 20px;
              border-bottom: 1px solid #d5d5d5;
              height: 45px;
            "
          >
            抓斗秤计量数据
            <div style="margin-left: 40px">
              <!-- <el-button
                size="mini"
                type="primary"
                icon="el-icon-check"
                @click="enableData"
                v-loading.fullscreen.lock="operateLoading"
                v-privilege="activeMenu + 'RECOVERY'"
                >启用</el-button
              >
              <el-button
                size="mini"
                type="danger"
                plain
                icon="el-icon-remove-outline"
                @click="discardData"
                v-loading.fullscreen.lock="operateLoading"
                v-privilege="activeMenu + 'ABOLISH'"
                >废除</el-button
              > -->
            </div>
          </div>
          <div class="common-table-div">
            <el-table
              v-loading="dataLoading"
              @row-click="rowChange"
              :row-class-name="tableRowClassName"
              ref="dataTable"
              highlight-current-row
              border
              @selection-change="selectChange"
              :height="tableHeight"
              :data="dataList"
              :exchangeFilterMap="{
                storeName: { '1': '顶吹炉', '2': '闪速炉' },
                weightType: {
                  '0': '毛重',
                  '1': '常规皮重',
                  '2': '期限皮重',
                  '3': '历史皮重',
                  '4': '标识皮重',
                  '5': '自重皮重',
                },
              }"
              :icore-filter-flag="icoreFilterFlag"
            >
              <el-table-column
                type="index"
                label="序号"
                fixed="left"
                align="center"
              ></el-table-column>
              <el-table-column
                type="selection"
                width="55"
                fixed="left"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="actualNo"
                label="作业编号"
                min-width="110"
                align="center"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="dataHch"
                label="行车号"
                min-width="80"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="dataType"
                label="数据类型"
                min-width="80"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="dataArea"
                label="计量区域"
                min-width="80"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="grossWeight"
                label="毛重"
                min-width="80"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="tareWeight"
                min-width="80"
                label="皮重"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="netWeight"
                min-width="80"
                label="净重"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="wetWeight"
                min-width="80"
                label="湿重"
                align="left"
              ></el-table-column>
              <!-- <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="water"
                min-width="80"
                label="水分"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="koZa"
                min-width="80"
                label="扣杂"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="dryWeight"
                min-width="80"
                label="干重"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="niWeight"
                min-width="80"
                label="镍量"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="coWeight"
                min-width="80"
                label="钴量"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="cuWeight"
                min-width="80"
                label="铜量"
                align="left"
              ></el-table-column> -->
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="storeName"
                min-width="80"
                label="仓库名称"
                align="left"
              >
                <template slot-scope="scope">
                  {{ scope.row.storeName - 0 === 1 ? "顶吹炉" : "闪速炉" }}
                </template>
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="dataTime"
                min-width="200"
                label="计量时间"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="meterClass"
                min-width="80"
                label="班次"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="meterGroup"
                min-width="80"
                label="班组"
                align="left"
              ></el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="meterOperator"
                label="计量员"
                min-width="100"
                align="right"
              >
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="validFlag"
                label="状态"
                min-width="100"
                align="center"
              >
                <template slot-scope="scope">
                  <span v-if="scope.row.validFlag == 0">作废</span>
                  <span v-else-if="scope.row.validFlag == 1">有效</span>
                </template>
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="exceptionTypeName"
                label="异常类型"
                min-width="100"
                align="right"
              >
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="memo"
                label="备注"
                min-width="100"
                align="right"
              >
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="createManName"
                min-width="110"
                label="创建人"
                align="right"
              >
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="createTime"
                min-width="110"
                label="创建时间"
                align="right"
              >
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="updateManName"
                min-width="110"
                label="修改人"
                align="right"
              >
              </el-table-column>
              <el-table-column
                sortable
                :show-overflow-tooltip="true"
                prop="updateTime"
                min-width="110"
                label="修改时间"
                align="center"
              >
              </el-table-column>
            </el-table>
          </div>
          <div class="common-foot-style">
            <div class="weight_wrap">
              <span>总车数：{{ wgtObj.sumCarNum }}车 </span>
              <span>总净重：{{ wgtObj.sumNetWgt }}吨 </span>
              <span>平均净重：{{ wgtObj.avgWgt }}吨 </span>
            </div>
            <el-pagination
              v-loading="dataLoading"
              @size-change="pageSizeChange"
              @current-change="pageCurrentChange"
              :current-page="pageForm.pageNum"
              :page-sizes="[50, 100, 200, 400]"
              :page-size="pageForm.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pageForm.totalPage"
              style="display: inline-block"
            >
            </el-pagination>
          </div>
          <!-- <div
            ref="editTitle"
            class="common-title-div"
            style="margin-top: 20px; border-top: 1px solid #d5d5d5"
          >
            抓斗秤数据补录
          </div>
          <div ref="editDiv" class="commom-edit-div">
            <el-form
              ref="baseData"
              label-width="80px"
              :inline="true"
              :rules="formRules"
              :model="baseData"
              class="grab_edit_wrap"
            >
              <el-form-item prop="dataHch" label="行车号">
                <el-input
                  v-model="baseData.dataHch"
                  clearable
                  size="small"
                  style="width: 120px"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item label="数据类型">
                <el-select
                  v-model="baseData.dataType"
                  clearable
                  size="mini"
                  style="width: 120px"
                >
                  <el-option
                    v-for="item in matTypeList"
                    :key="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="grossWeight" label="毛重">
                <el-input
                  v-model="baseData.grossWeight"
                  size="small"
                  clearable
                  type="number"
                  style="width: 120px"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item prop="tareWeight" label="皮重">
                <el-input
                  v-model="baseData.tareWeight"
                  size="small"
                  clearable
                  type="number"
                  style="width: 120px"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item prop="netWeight" label="净重">
                <el-input
                  v-model="baseData.netWeight"
                  size="small"
                  clearable
                  type="number"
                  style="width: 120px"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item prop="wetWeight" label="湿重">
                <el-input
                  v-model="baseData.wetWeight"
                  size="small"
                  clearable
                  type="number"
                  style="width: 120px"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item prop="storeName" label="仓库名称">
                <el-select
                  v-model="baseData.storeName"
                  clearable
                  size="small"
                  style="width: 120px"
                >
                  <el-option
                    v-for="item in storeList"
                    :key="item.id"
                    :value="item.id"
                    :label="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="dataTime" label="补录时间">
                <el-date-picker
                  type="datetime"
                  clearable
                  size="small"
                  style="width: 120px"
                  @change="dateChange(baseData.dataTime, 'dataTime', 'edit')"
                  v-model="baseData.dataTime"
                ></el-date-picker>
              </el-form-item>
              <el-form-item prop="meterOperator" label="计量员">
                <el-input
                  v-model="baseData.meterOperator"
                  size="small"
                  clearable
                  style="width: 120px"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
              <el-form-item prop="exceptionTypeName" label="异常类型">
                <el-select
                  v-model="baseData.exceptionTypeName"
                  clearable
                  filterable
                  size="mini"
                  style="width: 120px"
                  @change="(val) => exceptionChange(val, 'edit')"
                >
                  <el-option
                    v-for="item in exceptionList"
                    :key="item.baseCode"
                    :value="item.baseName"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="memo" label="备注">
                <el-input
                  v-model="baseData.memo"
                  size="small"
                  clearable
                  type="textarea"
                  style="width: 200px"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-form>
          </div> -->
        </div>
      </el-row>
      <el-dialog
        @close="closeDialog"
        :visible.sync="exceptionDialogFlag"
        customClass="customWidth_syst exception_wrap"
        title="异常信息录入"
        width="540px"
        :close-on-click-modal="false"
      >
        <el-form
          ref="exceptionObj"
          label-width="80px"
          :inline="true"
          :model="exceptionObj"
        >
          <el-form-item prop="exceptionTypeName" label="异常类型">
            <el-select
              v-model="exceptionObj.exceptionTypeName"
              clearable
              filterable
              size="mini"
              style="width: 120px"
              @change="(val) => exceptionChange(val, 'drop')"
            >
              <el-option
                v-for="item in exceptionList"
                :key="item.baseCode"
                :value="item.baseName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="memo" label="备注">
            <el-input
              v-model="exceptionObj.memo"
              size="mini"
              clearable
              type="textarea"
              style="width: 180px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" size="mini" @click="discardData"
            >确定</el-button
          >
          <el-button size="mini" type="default" @click="closeDialog"
            >取消</el-button
          >
        </span>
      </el-dialog>
      <el-dialog
        @close="closeBatDialog"
        :visible.sync="batchDialogFlag"
        customClass="customWidth_syst"
        title="抓斗秤实绩批量录入"
        :close-on-click-modal="false"
      >
        <el-form
          ref="batchData"
          label-width="80px"
          :inline="true"
          :rules="formRules"
          :model="batchObj"
          class="grab_edit_wrap"
        >
          <el-form-item prop="dataHch" label="行车号">
            <el-input
              v-model="batchObj.dataHch"
              @keyup.native.enter="getAvgGross"
              clearable
              size="small"
              style="width: 120px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="数据类型">
            <el-select
              v-model="batchObj.dataType"
              clearable
              size="mini"
              style="width: 120px"
            >
              <el-option
                v-for="item in matTypeList"
                :key="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="grossWeight" label="毛重">
            <el-input
              v-model="batchObj.grossWeight"
              size="small"
              clearable
              type="number"
              style="width: 120px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item prop="tareWeight" label="皮重">
            <el-input
              v-model="batchObj.tareWeight"
              size="small"
              clearable
              type="number"
              style="width: 120px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item prop="netWeight" label="净重">
            <el-input
              v-model="batchObj.netWeight"
              size="small"
              clearable
              type="number"
              style="width: 120px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item prop="wetWeight" label="湿重">
            <el-input
              v-model="batchObj.wetWeight"
              size="small"
              clearable
              type="number"
              style="width: 120px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item prop="storeName" label="仓库名称">
            <el-select
              v-model="batchObj.storeName"
              clearable
              size="small"
              style="width: 120px"
            >
              <el-option
                v-for="item in storeList"
                :key="item.id"
                :value="item.id"
                :label="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="meterOperator" label="计量员">
            <el-input
              v-model="batchObj.meterOperator"
              size="small"
              clearable
              style="width: 120px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item prop="exceptionTypeName" label="异常类型">
            <el-select
              v-model="batchObj.exceptionTypeName"
              clearable
              filterable
              size="mini"
              style="width: 120px"
              @change="(val) => exceptionChange(val, 'batch')"
            >
              <el-option
                v-for="item in exceptionList"
                :key="item.baseCode"
                :value="item.baseName"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="dataTime" label="补录时间">
            <el-date-picker
              type="date"
              clearable
              size="small"
              style="width: 120px"
              @change="dateChange(batchObj.dataTime, 'dataTime', 'batch')"
              v-model="batchObj.dataTime"
            ></el-date-picker>
          </el-form-item>
          <el-form-item prop="banci" label="班次">
            <el-select
              v-model="banci"
              clearable
              size="mini"
              style="width: 120px"
            >
              <el-option
                v-for="item in classList"
                :key="item.id"
                :value="item.id"
                :label="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="memo" label="备注">
            <el-input
              v-model="batchObj.memo"
              size="small"
              clearable
              type="textarea"
              style="width: 200px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="新增条数">
            <el-input
              v-model="insertNum"
              size="small"
              clearable
              style="width: 120px"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" size="mini" @click="batchInsertData"
            >确定</el-button
          >
          <el-button size="mini" type="default" @click="closeBatDialog"
            >取消</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { formatDate, getCookie } from '@/utils/util.js';
import { cookieUserId, cookieUserName } from '@/config/config.js';
import { recordChange } from '../../../../utils/utils'
import { MessageBox } from 'element-ui'

let userId = getCookie(cookieUserId)
let userName = getCookie(cookieUserName)
export default {
  data () {
    let numberCheck = (rule, val, callback) => {
      let reg = /^(\d+)(\.\d+)?$/
      if (!reg.test(val)) {
        callback(new Error('计量重量需要为数字'))
      } else {
        callback()
      }
    }
    return {
      pageForm: {
        dataTime: formatDate(new Date(), 'yyyy-MM-dd'),
        pageNum: 1,
        pageSize: 100,
        totalPage: 0
      },
      searchForm: {
        dataArea: '',
        dataHch: '',
        dataType: '',
        validFlag: '1',
        exceptionTypeName: '',
        storeName: ''
      },
      dataList: [],
      operateLoading: false,
      dataLoading: false,
      tableHeight: 350,
      icoreFilterFlag: true,
      baseData: {
        dataHch: '',
        dataType: '自产料',
        dataArea: '',
        grossWeight: 0,
        tareWeight: 0,
        netWeight: 0,
        wetWeight: 0,
        water: '',
        koZa: 0,
        dryWeight: '',
        niWeight: '',
        coWeight: '',
        cuWeight: '',
        dataTime: '',
        meterClass: '',
        meterGroup: '',
        exceptionTypeName: '',
        exceptionTypeNo: '',
        memo: '',
        meterOperator: '',
        storeName: ''
      },
      baseDataCache: {
        dataHch: '',
        dataType: '自产料',
        dataArea: '',
        grossWeight: 0,
        tareWeight: 0,
        netWeight: 0,
        wetWeight: 0,
        water: '',
        koZa: 0,
        dryWeight: '',
        niWeight: '',
        coWeight: '',
        cuWeight: '',
        dataTime: '',
        meterClass: '',
        meterGroup: '',
        exceptionTypeName: '',
        exceptionTypeNo: '',
        memo: '',
        meterOperator: '',
        storeName: ''
      },
      batchObj: {
        dataHch: '',
        dataType: '自产料',
        dataArea: '',
        grossWeight: 0,
        tareWeight: 0,
        netWeight: 0,
        wetWeight: 0,
        water: '',
        koZa: 0,
        dryWeight: '',
        niWeight: '',
        coWeight: '',
        cuWeight: '',
        dataTime: '',
        meterClass: '',
        meterGroup: '',
        exceptionTypeName: '',
        exceptionTypeNo: '',
        memo: '',
        meterOperator: '',
        storeName: ''
      },
      batchObjCache: {
        dataHch: '',
        dataType: '自产料',
        dataArea: '',
        grossWeight: 0,
        tareWeight: 0,
        netWeight: 0,
        wetWeight: 0,
        water: '',
        koZa: 0,
        dryWeight: '',
        niWeight: '',
        coWeight: '',
        cuWeight: '',
        dataTime: '',
        meterClass: '',
        meterGroup: '',
        exceptionTypeName: '',
        exceptionTypeNo: '',
        memo: '',
        meterOperator: '',
        storeName: ''
      },
      formRules: {
        dataHch: [{ required: true, message: '行车号不能为空', trigger: 'blur' }],
        dataType: [{ required: true, message: '数据类型不能为空', trigger: 'blur' }],
        meterOperator: [{ required: true, message: '计量员不能为空', trigger: 'blur' }],
        dataTime: [{ required: true, message: '计量时间不能为空', trigger: 'blur' }],
        exceptionTypeName: [{ required: true, message: '异常类型不能为空', trigger: 'blur' }],
        storeName: [{ required: true, message: '仓库名称不能为空', trigger: 'blur' }]
      },
      flagList: [
        {
          id: '0',
          val: '作废'
        },
        {
          id: '1',
          val: '有效'
        }
      ],
      storeList: [
        {
          id: '1',
          value: '顶吹炉'
        },
        {
          id: '2',
          value: '闪速炉'
        }
      ],
      exceptionList: [],
      exceptionDialogFlag: false,
      exceptionObj: {
        exceptionTypeNo: '',
        exceptionTypeName: '',
        memo: ''
      },
      exceptionObjCache: {
        exceptionTypeNo: '',
        exceptionTypeName: '',
        memo: ''
      },
      batchDialogFlag: false,
      insertNum: 0,
      wgtObj: {
        sumCarNum: 0,
        sumNetWgt: 0,
        avgWgt: 0
      },
      batchList: [],
      matTypeList: ['自产料', '外购料'],
      ctrlFlag: false,
      classList: [
        {
          id: ' 01:23:45',
          value: '20-8'
        },
        {
          id: ' 09:10:11',
          value: '8-20'
        }
      ],
      banci: '',
      searchBanci: '8-20'
    }
  },
  created () {
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
    this.userNo = getCookie(cookieUserId)
    this.userName = getCookie(cookieUserName)
    this.baseData.meterOperator = userName
    this.baseDataCache.meterOperator = userName
    this.batchObj.meterOperator = userName
    this.batchObjCache.meterOperator = userName
  },
  mounted () {
    let _this = this
    this.init()
    // // 监听页面的按键事件
    document.onkeydown = function (event) {
      // 判断 Ctrl
      if (event.ctrlKey) {
        if (!_this.ctrlFlag) {
          _this.ctrlFlag = true
          event.preventDefault();//取消默认行为
        }
      }
    }
    document.onkeyup = function (e) {
      if (e.ctrlKey) {
        _this.ctrlFlag = false;
        e.preventDefault();//取消默认行为
      }
    }
  },
  methods: {
    // 初始化
    init () {
      this.queryData();
      this.getExceptionList();
    },
    // 查询
    async queryData (flag = 1) {
      // 时间不能为空
      if (this.pageForm.dataTime === '') {
        return this.$message.warning('时间不能为空')
      }
      if (this.pageForm.dataTime instanceof Date) {
        this.pageForm.dataTime = formatDate(this.pageForm.dataTime, 'yyyy-MM-dd')
      }
      if (flag === 1) {
        this.pageForm.pageNum = 1
      }
      let startTime = '', endTime = ''
      if (this.searchBanci === '') return this.$message.warning('请选择要查询数据所在的班次')
      switch (this.searchBanci) {
        case '20-8':
          let earlyDate = formatDate(new Date(new Date(this.pageForm.dataTime.replace('-', '/')).getTime() - 24 * 3600 * 1000), 'yyyy-MM-dd')
          startTime = earlyDate + ' 19:20:00';
          endTime = this.pageForm.dataTime + ' 07:20:00'
          break;
        case '8-20':
          startTime = this.pageForm.dataTime + ' 07:20:00';
          endTime = this.pageForm.dataTime + ' 19:20:00'
          break;
      }
      this.groupQuery(startTime, endTime);
      let paramStr = `pageNum=${this.pageForm.pageNum}&pageSize=${this.pageForm.pageSize}&createTime1=${startTime}&createTime2=${endTime}&storeName=${this.searchForm.storeName}&dataHch=${this.searchForm.dataHch}&dataType=${this.searchForm.dataType}&validFlag=${this.searchForm.validFlag}&exceptionTypeName=${this.searchForm.exceptionTypeName}`
      const { data: res } = await this.axios.get(`pass/systemBase/v1/meterworkgrabactuals/like/?${paramStr}`)
      if (res && res.list) {
        this.dataList = [...res.list]
        this.pageForm.totalPage = res.total
      } else {
        this.dataList = []
        this.pageForm.totalPage = 0
      }
    },
    async getExceptionList () {
      const { data: res } = await this.axios.get('pass/systemBase/v1/combaseinfos/noPage' + '?pBaseCode=001042' + '&validFlag=1')
      if (res) {
        this.exceptionList = [...res]
      } else {
        this.exceptionList = []
      }
    },
    floatComputed1 (arg1, arg2, sign) {
      if (!arg1) {
        arg1 = 0;
      }
      if (!arg2) {
        arg2 = 0;
      }
      let val1 = arg1 + '';
      let val2 = arg2 + '';
      let rate = 1;
      let rate1 = 1;
      let rate2 = 1;
      if (val1.indexOf('.') < 0) {
        rate1 = 0;
      } else {
        rate1 = val1.length - val1.indexOf('.') - 1;
      }
      if (val2.indexOf('.') < 0) {
        rate2 = 0;
      } else {
        rate2 = val2.length - val2.indexOf('.') - 1;
      }
      rate = rate2;
      val2 = val2.replace('.', '');
      val1 = val1.replace('.', '');
      if (rate1 > rate2) {
        rate = rate1;
        for (let i = 0; i < rate1 - rate2; i++) {
          val2 = val2 + '0'
        }
      } else {
        for (let i = 0; i < rate2 - rate1; i++) {
          val1 = val1 + '0';
        }
      }
      let rateNum = Math.pow(10, rate);
      if (sign === '-') {
        return (Number(val1) - Number(val2)) / rateNum
      }
      if (sign === '+') {
        return (Number(val1) + Number(val2)) / rateNum
      }
      if (sign === '*') {
        return (Number(val1) * Number(val2)) / (rateNum * rateNum)
      }
      if (sign === '/') { // 默认保留4位
        let num = 0;
        if (Number(val1) - 0 !== 0 && Number(val2) - 0 !== 0) {
          num = (Number(val1) / Number(val2)) + '';
          if (num.split('.').length > 0) {
            if (num[1].length > 4) {
              num = num.toFixed(4);
            }
          }
        }
        return num;
      }
      return null;
    },
    // 获取重量汇总
    async groupQuery (start, end) {
      // meterworkgrabactuals/queryCount
      let paramStr = `pageNum=${this.pageForm.pageNum}&pageSize=${this.pageForm.pageSize}&createTime1=${start}&createTime2=${end}&storeName=${this.searchForm.storeName}&dataHch=${this.searchForm.dataHch}&dataType=${this.searchForm.dataType}&validFlag=${this.searchForm.validFlag}&exceptionTypeName=${this.searchForm.exceptionTypeName}`
      const { data: res } = await this.axios.get(`pass/systemBase/v1/meterworkgrabactuals/queryCount?${paramStr}`)
      if (res && res[0]) {
        this.wgtObj.sumCarNum = res[0].actualNo
        this.wgtObj.sumNetWgt = res[0].netWeight - 0 !== 0 ? (this.floatComputed1(res[0].netWeight - 0, 1000, '/') - 0).toFixed(2) : 0
        this.wgtObj.avgWgt = res[0].wetWeight - 0 !== 0 ? (this.floatComputed1(res[0].wetWeight - 0, 1000, '/') - 0).toFixed(2) : 0
      } else {
        this.wgtObj.sumCarNum = 0
        this.wgtObj.sumNetWgt = 0
        this.wgtObj.avgWgt = 0
      }
    },
    // 关闭批量新增弹窗
    closeBatDialog () {
      this.batchDialogFlag = false
      this.batchObj = { ...this.batchObjCache }
    },
    // 新增
    addData () {
      this.$refs.baseData.validate(async valid => {
        if (!valid) return this.$message.error('请规范填写数据')
        // 将重量转换为吨
        let grossWeight = this.baseData.grossWeight - 0 > 0 ? this.floatComputed1(this.baseData.grossWeight, 1000, '*') : 0
        let tareWeight = this.baseData.tareWeight - 0 > 0 ? this.floatComputed1(this.baseData.tareWeight, 1000, '*') : 0
        let netWeight = this.baseData.netWeight - 0 > 0 ? this.floatComputed1(this.baseData.netWeight, 1000, '*') : 0
        let wetWeight = this.baseData.wetWeight - 0 > 0 ? this.floatComputed1(this.baseData.wetWeight, 1000, '*') : 0
        const data = await this.axios.post('pass/systemBase/v1/meterworkgrabactuals/add', { ...this.baseData, grossWeight, tareWeight, netWeight, wetWeight }, { individualType: 'json' })
        if (data.status === 'succeed') {
          this.queryData()
          this.$message.success('新增成功')
          this.baseData = { ...this.baseDataCache }
        } else {
          this.$message.error(data.message)
        }
      })
    },
    // 修改
    modifyData () {
      this.$refs.baseData.validate(async valid => {
        if (!valid) return this.$message.error('请规范填写数据')
        // 将重量转换为吨
        let grossWeight = this.baseData.grossWeight - 0 > 0 ? this.floatComputed1(this.baseData.grossWeight, 1000, '*') : 0
        let tareWeight = this.baseData.tareWeight - 0 > 0 ? this.floatComputed1(this.baseData.tareWeight, 1000, '*') : 0
        let netWeight = this.baseData.netWeight - 0 > 0 ? this.floatComputed1(this.baseData.netWeight, 1000, '*') : 0
        let wetWeight = this.baseData.wetWeight - 0 > 0 ? this.floatComputed1(this.baseData.wetWeight, 1000, '*') : 0
        // this.baseData.dataTime = formatDate(new Date())
        const data = await this.axios.post('pass/systemBase/v1/meterworkgrabactuals/update', { ...this.baseData, grossWeight, tareWeight, netWeight, wetWeight }, { individualType: 'json' })
        if (data.status === 'succeed') {
          this.queryData()
          this.$message.success('修改成功')
          this.baseData = { ...this.baseDataCache }
        } else {
          this.$message.error(data.message)
        }
      })
    },
    // 获取平均毛重
    async getAvgGross () {
      if (!this.batchObj.dataHch || this.batchObj.dataHch == '') return this.$message.warning('请输入行车号')
      if (!this.batchObj.storeName || this.batchObj.storeName == '') return this.$message.warning('请选择仓库名称')
      const { data: res } = await this.axios.get(`pass/systemBase/v1/meterworkgrabactuals/queryAvgGross?storeName=${this.batchObj.storeName}&dataHch=${this.batchObj.dataHch}`)
      debugger;
      if (res && res.length > 0) {
        this.batchObj.grossWeight = (res[0].wetWeight && res[0].wetWeight - 0 > 0) ? this.floatComputed1(res[0].wetWeight - 0, 1000, '/') : 0
      } else {
        this.batchObj.grossWeight = 0
      }
    },
    // 显示作废弹窗
    showDropDialog () {
      if (this.batchList.length === 0) return this.$message.error('请选择要进行作废的数据')
      this.exceptionDialogFlag = true
    },
    showBatchDialog () {
      this.batchDialogFlag = true
      this.batchObj.dataTime = formatDate(new Date(), 'yyyy-MM-dd')
    },
    // 作废
    async discardData () {
      if (!this.exceptionObj.exceptionTypeName || this.exceptionObj.exceptionTypeName === '') return this.$message.warning('请先选择异常类型')
      this.batchList.forEach(item => {
        item.validFlag = '0'
        item.exceptionTypeNo = this.exceptionObj.exceptionTypeNo
        item.exceptionTypeName = this.exceptionObj.exceptionTypeName
        item.memo = this.exceptionObj.memo,
          item.meterOperator = userName
      })
      const data = await this.axios.post(`pass/systemBase/v1/meterworkgrabactuals/batchUpdateGrab`, [...this.batchList], { individualType: 'json' })
      if (data.status === 'succeed') {
        this.queryData()
        this.$message.success('作废成功')
        this.closeDialog();
      } else {
        this.$message.error(data.message)
      }
    },
    // 启用
    enableData (row) {
      if (this.batchList.length === 0) return this.$message.error('请选择要进行启用的数据')
      this.batchList.forEach(item => {
        item.validFlag = '1'
        item.meterOperator = userName
      })
      this.$confirm('确认要启用选中数据吗？').then(async () => {
        const data = await this.axios.post(`pass/systemBase/v1/meterworkgrabactuals/batchUpdateGrab`, [...this.batchList], { individualType: 'json' })
        if (data.status === 'succeed') {
          this.queryData()
          this.$message.success('启用成功')
          this.baseData = { ...this.baseDataCache }
        } else {
          this.$message.error(data.message)
        }
      }).catch(() => { })
    },
    selectChange (list) {
      this.batchList = [...list]
      let sList = []
      if (this.ctrlFlag) {
        this.ctrlFlag = false
        list.forEach(item => {
          let index = this.dataList.findIndex(iItem => iItem.actualNo === item.actualNo)
          if (index >= 0) {
            sList.push(index)
          }
        })
        // 数据起始index
        let list1 = this.dataList.slice(sList[0], sList[sList.length - 1] + 1)
        list1.forEach(item => {
          this.$refs.dataTable.toggleRowSelection(item, true)
        })
      }
    },
    exceptionChange (val, flag) {
      let item = this.exceptionList.find(o => o.baseName === val)
      if (item) {
        if (flag === 'edit') {
          this.baseData.exceptionTypeNo = item.baseCode
        } else if (flag === 'batch') {
          this.batchObj.exceptionTypeNo = item.baseCode
        } else {
          this.exceptionObj.exceptionTypeNo = item.baseCode
        }
      }
    },
    // 批量新增数据
    async batchInsertData () {
      this.$refs.batchData.validate(async valid => {
        if (!valid) return this.$message.error('请规范填写数据')
        // 检验批次是否为空
        if (!this.banci || this.banci === '') return this.$message.error('班次不能为空')
        // 数据通过校验后，根据填写的insertNum复制数据
        // 将重量转换为吨
        let grossWeight = this.batchObj.grossWeight - 0 > 0 ? this.floatComputed1(this.batchObj.grossWeight, 1000, '*') : 0
        let tareWeight = this.batchObj.tareWeight - 0 > 0 ? this.floatComputed1(this.batchObj.tareWeight, 1000, '*') : 0
        let netWeight = this.batchObj.netWeight - 0 > 0 ? this.floatComputed1(this.batchObj.netWeight, 1000, '*') : 0
        let wetWeight = this.batchObj.wetWeight - 0 > 0 ? this.floatComputed1(this.batchObj.wetWeight, 1000, '*') : 0
        this.batchObj.dataTime = this.batchObj.dataTime + this.banci
        let paramList = []
        if (!(/^(-?\d+)(\.\d+)?$/.test(this.insertNum)) || this.insertNum - 0 === 0) return this.$message.error('新增条数需要为大于0的数值！！！')
        for (let h = 0; h < this.insertNum; h++) {
          paramList.push({ ...this.batchObj, grossWeight, tareWeight, netWeight, wetWeight })
        }
        const data = await this.axios.post('pass/systemBase/v1/meterworkgrabactuals/batchInsertGrab', [...paramList], { individualType: 'json' })
        if (data.status === 'succeed') {
          this.queryData()
          this.$message.success('新增成功')
          this.closeBatDialog()
        } else {
          this.$message.error(data.message)
        }
      })
    },
    closeDialog () {
      this.exceptionDialogFlag = false
      this.exceptionObj = { ...this.exceptionObjCache }
      this.baseData = { ...this.baseDataCache }
    },
    // 行点击
    rowChange (row) {
      let grossWeight = row.grossWeight - 0 > 0 ? this.floatComputed1(row.grossWeight, 1000, '/') : 0
      let tareWeight = row.tareWeight - 0 > 0 ? this.floatComputed1(row.tareWeight, 1000, '/') : 0
      let netWeight = row.netWeight - 0 > 0 ? this.floatComputed1(row.netWeight, 1000, '/') : 0
      let wetWeight = row.wetWeight - 0 > 0 ? this.floatComputed1(row.wetWeight, 1000, '/') : 0
      let wgtObj = { ...row, grossWeight, tareWeight, netWeight, wetWeight }
      this.baseData = { ...wgtObj }
    },
    // 行选中
    handleSelectionChange () {

    },
    // 动态设置行样式
    tableRowClassName ({
      row,
      rowIndex
    }) {
      if (row.valueFlag === '0') {
        return 'error-row2';
      } else if (row.valueFlag === '1') {
        return 'wait-row'
      }
      return '';
    },
    // 时间改变回调
    timeChange (val, name, model) {
      let time = ''
      if (val) {
        time = formatDate(val, 'yyyy-MM-dd')
      }
      model === 'search' ? this.pageForm[name] = time : model === 'edit' ? this.baseData[name] = time : this.batchObj[name] = time
    },
    // 时间改变回调
    dateChange (val, name, model) {
      let time = ''
      if (val) {
        time = formatDate(val, 'yyyy-MM-dd')
      }
      this.batchObj[name] = time
    },
    // 分页大小变化
    pageSizeChange (size) {
      this.pageForm.pageSize = size
      this.queryData()
    },
    // 分页跳转
    pageCurrentChange (pageNum) {
      this.pageForm.pageNum = pageNum
      this.queryData(0)
    }
  },
  watch: {
    'baseData.grossWeight': {
      handler: function (gross) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(gross - 0)) {
          this.baseData.grossWeight = 0
          return this.$message.error('毛重需要为数值')
        }
        if (gross - 0 !== 0) {
          this.baseData.netWeight = this.floatComputed1(gross - 0, this.baseData.tareWeight - 0, '-')
        }
      }
    },
    'baseData.tareWeight': {
      handler: function (tare) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(tare - 0)) {
          this.baseData.tareWeight = 0
          return this.$message.error('毛重需要为数值')
        }
        if (tare - 0 !== 0) {
          this.baseData.netWeight = this.floatComputed1(this.baseData.grossWeight - 0, tare - 0, '-')
        }
      }
    },
    'baseData.netWeight': {
      handler: async function (net) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(net - 0)) {
          this.baseData.tareWeight = 0
          return this.$message.error('净重需要为数值')
        }
        if (net - 0 !== 0) {
          // 如果填入重量大于50吨进行提示
          if (net - 0 > 50) {
            let flag = await this.$confirm('当前输入净重大于50吨，确定进行新增吗？').catch(() => 'cancel')
            if (flag === 'cancel') {
              this.baseData.grossWeight = 0
              this.baseData.netWeight = 0
              this.baseData.wetWeight = 0
            }
          }
          // 干重等于净重减去(净重减去扣杂)乘以水分
          let otherWgt = 0, waterWgt = 0
          // 扣杂是否填入
          if (reg.test(this.baseData.koZa - 0) && Number(this.baseData.koZa) !== 0) {
            otherWgt = this.floatComputed1(net, this.baseData.koZa, '-')
          } else {
            otherWgt = net
          }
          this.baseData.wetWeight = otherWgt
          // 水分是否填入, 未填入大于0的水分值不进行计算干重
          if (reg.test(this.baseData.water - 0) && Number(this.baseData.water) !== 0) {
            let waterPercent = this.floatComputed1(this.baseData.water, 100, '/')
            waterWgt = this.floatComputed1(net, waterPercent, '*').toFixed(2)
            this.baseData.dryWeight = this.floatComputed1(otherWgt, waterWgt, '-')
          }
        }
      }
    },
    'baseData.water': {
      handler: function (water) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(water - 0)) {
          this.baseData.tareWeight = 0
          return this.$message.error('水分需要为数值')
        }
        if (this.baseData.netWeight && this.baseData.netWeight - 0 !== 0) {
          // 干重等于净重减去(净重减去扣杂)乘以水分
          let otherWgt = 0, waterWgt = 0
          // 扣杂是否填入
          if (reg.test(this.baseData.koZa - 0) && Number(this.baseData.koZa) !== 0) {
            otherWgt = this.floatComputed1(this.baseData.netWeight, this.baseData.koZa, '-')
          } else {
            otherWgt = this.baseData.netWeight
          }
          this.baseData.wetWeight = otherWgt
          // 水分是否填入, 未填入大于0的水分值不进行计算干重
          if (reg.test(this.baseData.water - 0) && Number(this.baseData.water) !== 0) {
            let waterPercent = this.floatComputed1(this.baseData.water, 100, '/')
            waterWgt = this.floatComputed1(this.baseData.netWeight, waterPercent, '*').toFixed(2)
            this.baseData.dryWeight = this.floatComputed1(otherWgt, waterWgt, '-')
          }
        }
      }
    },
    'batchObj.grossWeight': {
      handler: function (gross) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(gross - 0)) {
          this.batchObj.grossWeight = 0
          return this.$message.error('毛重需要为数值')
        }
        if (gross - 0 !== 0) {
          this.batchObj.netWeight = this.floatComputed1(gross - 0, this.batchObj.tareWeight - 0, '-')
        }
      }
    },
    'batchObj.tareWeight': {
      handler: function (tare) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(tare - 0)) {
          this.batchObj.tareWeight = 0
          return this.$message.error('毛重需要为数值')
        }
        if (tare - 0 !== 0) {
          this.batchObj.netWeight = this.floatComputed1(this.batchObj.grossWeight - 0, tare - 0, '-')
        }
      }
    },
    'batchObj.netWeight': {
      handler: async function (net) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(net - 0)) {
          this.batchObj.tareWeight = 0
          return this.$message.error('净重需要为数值')
        }
        if (net - 0 !== 0) {
          // 如果填入重量大于50吨进行提示
          if (net - 0 > 50) {
            let flag = await this.$confirm('当前输入净重大于50吨，确定进行新增吗？').catch(() => 'cancel')
            if (flag === 'cancel') {
              this.batchObj.grossWeight = 0
              this.batchObj.netWeight = 0
              this.batchObj.wetWeight = 0
            }
          }
          // 干重等于净重减去(净重减去扣杂)乘以水分
          let otherWgt = 0, waterWgt = 0
          // 扣杂是否填入
          if (reg.test(this.batchObj.koZa - 0) && Number(this.batchObj.koZa) !== 0) {
            otherWgt = this.floatComputed1(net, this.batchObj.koZa, '-')
          } else {
            otherWgt = net
          }
          this.batchObj.wetWeight = otherWgt
          // 水分是否填入, 未填入大于0的水分值不进行计算干重
          if (reg.test(this.batchObj.water - 0) && Number(this.batchObj.water) !== 0) {
            let waterPercent = this.floatComputed1(this.batchObj.water, 100, '/')
            waterWgt = this.floatComputed1(net, waterPercent, '*').toFixed(2)
            this.batchObj.dryWeight = this.floatComputed1(otherWgt, waterWgt, '-')
          }
        }
      }
    },
    'batchObj.water': {
      handler: function (water) {
        let reg = /^(-?\d+)(\.\d+)?$/
        if (!reg.test(water - 0)) {
          this.batchObj.tareWeight = 0
          return this.$message.error('水分需要为数值')
        }
        if (this.batchObj.netWeight && this.batchObj.netWeight - 0 !== 0) {
          // 干重等于净重减去(净重减去扣杂)乘以水分
          let otherWgt = 0, waterWgt = 0
          // 扣杂是否填入
          if (reg.test(this.batchObj.koZa - 0) && Number(this.batchObj.koZa) !== 0) {
            otherWgt = this.floatComputed1(this.batchObj.netWeight, this.batchObj.koZa, '-')
          } else {
            otherWgt = this.batchObj.netWeight
          }
          this.batchObj.wetWeight = otherWgt
          // 水分是否填入, 未填入大于0的水分值不进行计算干重
          if (reg.test(this.batchObj.water - 0) && Number(this.batchObj.water) !== 0) {
            let waterPercent = this.floatComputed1(this.batchObj.water, 100, '/')
            waterWgt = this.floatComputed1(this.batchObj.netWeight, waterPercent, '*').toFixed(2)
            this.batchObj.dryWeight = this.floatComputed1(otherWgt, waterWgt, '-')
          }
        }
      }
    }
  }
}
</script>
<style>
.product_wrap_xb .el-table td {
  padding: 1px !important;
}

.grab_edit_wrap .el-input--suffix .el-input__inner {
  padding: 8px !important;
}

.weight_wrap {
  display: inline-block;
  text-align: left;
  font-weight: 500;
  font-size: 19px;
}

.exception_wrap .el-dialog__footer {
  text-align: center;
}

.grab_edit_wrap .el-icon-date {
  display: none;
}
</style>