// 检斤记录
<template>
  <div class="container">
    <el-row class="toptitle">
      <el-col :span="12">
        <h2 class="bg-left title">检斤记录</h2>
      </el-col>
      <el-col :span="12" class="bg-right">
        <el-button type="primary" size="small" @click="returnsales()">
          <img width="6" height="10" style="vertical-align: bottom; margin-right: 3px"
            src="../../../public/img/lujing.png" alt="" />返回
        </el-button>
      </el-col>
    </el-row>
    <div class="center">
      <el-row>
        <el-col :span="12">
          <el-button :type="warehousingType == 1?'primary':''" size="small" @click="changeType(1)"
            class="btn_css btn_topcss">贸易入库</el-button>
          <el-button :type="warehousingType == 3?'primary':''" size="small" @click="changeType(3)"
            class="btn_css btn_topcss">收购入库</el-button>
        </el-col>
        <el-col :span="12" class="bg-right">
          <div style="display:flex">
            <el-select v-model="warehouseName" placeholder="请选择仓库名" @change="warehouseNameChange">
              <el-option v-for="item in warehouseList" :key="item.id" :label="item.warehouseName"
                :value="item.warehouseName"></el-option>
            </el-select>
            <el-input :placeholder="warehousingType == 1 ? '可按业务编号、合同编号/移库编号、车牌号查找':'可按业务编号、车牌号、客户名查找'" clearable
              v-model="searchKeyWord" class="search_css"></el-input>
            <!-- <el-input placeholder="可按业务编号、车牌号、客户名查找" clearable v-model="searchKeyWord"></el-input> -->
            <el-button type="primary" size="small" @click="lookup">查找</el-button>
          </div>
        </el-col>
      </el-row>
      <el-button :type="choice == ''?'primary':''" size="small" @click="screen()" class="btn_css">全部</el-button>
      <el-button :type="choice == 2?'primary':''" size="small" @click="screen(2)" class="btn_css">已称毛重</el-button>
      <el-button :type="choice == 3?'primary':''" size="small" @click="screen(3)" class="btn_css">已称皮重</el-button>
      <el-button size="small" @click="print('',1)">批量打印</el-button>
      <el-table class="forData" :data="weighingRecordList" style="width: 100%; margin-top: 20px" height="780"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="80">
          <template scope="scope">
            <span v-if="scope.$index < 9">0{{ scope.$index + 1 }}</span>
            <span v-else>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="number" label="业务编号"></el-table-column>
        <el-table-column prop="contractNo" label="合同编号/移库编号" v-if="warehousingType == 1" width="130">
          <template scope="scope">
            <span v-if="scope.row.contractNo">{{scope.row.contractNo}}</span>
            <span v-else>{{scope.row.moveTaskNo}}</span>
          </template>

        </el-table-column>
        <!--//moveTaskNo -->
        <el-table-column prop="customer" label="客户" v-if="warehousingType == 3"></el-table-column>
        <el-table-column prop="carNumber" label="车牌号"></el-table-column>
        <el-table-column prop="goodsName" label="货名"></el-table-column>
        <el-table-column prop="grossWeight" label="毛重（公斤）" width="100"></el-table-column>
        <el-table-column prop="tare" label="皮重（公斤）" width="100"></el-table-column>
        <el-table-column prop="buckleMiscellaneous" label="扣重（公斤）" width="100"></el-table-column>
        <el-table-column prop="netWeight" label="净重（公斤）" width="100"></el-table-column>
        <el-table-column prop="secretaryWeigher" label="司称员"></el-table-column>
        <el-table-column prop="grossDate" label="检斤时间"></el-table-column>
        <el-table-column prop="status" label="状态"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <!-- <el-button size="small" @click="print(scope.row)">打印</el-button> -->
            <el-button @click="print(scope.row)" v-if="scope.row.status == '已称皮重' || scope.row.status == '已称毛重'"
              v-hasPermission="`acquisitionManagement.acquisitionWeight.print`">打印</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="text-align: center; padding: 10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-size="deptCircularPage.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="deptBudgetTotal">
      </el-pagination>
    </div>
    <div class="mask" v-show="isShowPrintType"></div>
    <div class="print-type" v-show="isShowPrintType">
      <div class="print-type-content">
        <div class="print-type-title">请选择打印纸张类型</div>
        <div class="print-type-checkbox">
          <!-- <el-radio-group v-model="radio">
            <el-radio label="A4" class="label-width" @change="selectType">A4</el-radio>
            <el-radio label="A5" class="label-width" @change="selectType">A5</el-radio>
          </el-radio-group> -->
        </div>

        <div class="shdw-style" v-if="checked||(!ddchecked&&!checked)">
          <div>收货单位：</div>
          <!-- <div>{{companyName}}</div> -->
          <el-select v-model="companyName" placeholder="请选择" style="width: 75%;">
            <el-option v-for="item in companyNameOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div v-if="warehouseName=='鲅鱼圈金信库'||warehouseName=='白城内陆港'||warehouseName=='哈尔滨依兰库'">
          <div class="print-type-ew">
            <el-checkbox v-model="checked" @change="changeEw">打印额外票据</el-checkbox>
            <el-checkbox v-model="ddchecked" @change="changeEwDD">单独打印额外票据</el-checkbox>
          </div>
          <div class="ew-print" v-if="printSeparately">
            <div class="row">
              <div>额外客户姓名：</div>
              <!-- <div>{{companyName}}</div> -->
              <el-select v-model="ewCustomer" placeholder="请选择" style="width: 75%;">
                <el-option v-for="item in ewCustomerOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
            <div class="row">
              <div>额外收货单位：</div>
              <!-- <div>{{companyNameEW}}</div> -->
              <el-select v-model="companyNameEW" placeholder="请选择" style="width: 75%;">
                <el-option v-for="item in companyNameEWOptions" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
      </div>
      <div class="bottom-btn">
        <el-button @click="typePrintClick">确定</el-button>
        <el-button @click="typePrintCannelClick">取消</el-button>
      </div>
    </div>
    <el-dialog width="70%" class="table-content" center :visible.sync="isShowPrint" title="粮食检斤单">
      <weightCheckPrint :tableData="dialogData"></weightCheckPrint>
      <div style="text-align:center">
        <el-button type="primary" @click="isShowPrint = false">关闭</el-button>
        <el-button type="primary" @click="printSmall">打印小票</el-button>
        <el-button type="primary" @click="printBig">打印单据</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    selectWarehouseSelf,
    getweighingList,
    getPrint
  } from '@/model/houseSelfCollect/index'
  import weightCheckPrint from './component/weightCheckPrint.vue'
  export default {
    components: {
      weightCheckPrint
    },
    data() {
      return {
        weighingRecordList: [],
        warehouseId: '',
        warehouseName: '',
        // goodsNameList: [],
        // goodsNameAllList: [],
        // binNoList: [],
        // binNumberId: '', //仓位Id
        // warehouseType: '',
        compId: '',
        //分页
        currentPage: 1,
        pageSize: 10,
        deptCircularPage: {},
        deptBudgetTotal: 0,
        warehouseList: [], //仓库
        searchKeyWord: '',
        choice: '',
        warehousingType: 1,
        isShowPrint: false,
        dialogData: {},
        title: '',
        modification: [],
        ewCustomerOptions: [{
          value: '黑龙江利润元粮食贸易有限公司',
          label: '黑龙江利润元粮食贸易有限公司'
        }, {
          value: '黑龙江众利合粮食贸易有限公司',
          label: '黑龙江众利合粮食贸易有限公司'
        }, {
          value: '黑龙江鑫满仓粮食贸易有限公司',
          label: '黑龙江鑫满仓粮食贸易有限公司'
        }, {
          value: '黑龙江中天昊元贸易有限公司',
          label: '黑龙江中天昊元贸易有限公司'
        }],
        companyNameOptions: [{
          value: '黑龙江利润元粮食贸易有限公司',
          label: '黑龙江利润元粮食贸易有限公司'
        }, {
          value: '黑龙江众利合粮食贸易有限公司',
          label: '黑龙江众利合粮食贸易有限公司'
        }, {
          value: '黑龙江鑫满仓粮食贸易有限公司',
          label: '黑龙江鑫满仓粮食贸易有限公司'
        }, {
          value: '黑龙江中天昊元贸易有限公司',
          label: '黑龙江中天昊元贸易有限公司'
        }, ],
        companyNameEWOptions: [{
          value: '厦门建发物产有限公司',
          label: '厦门建发物产有限公司'
        }],
        companyName: '黑龙江利润元粮食贸易有限公司',
        companyNameEW: '厦门建发物产有限公司',
        ewCustomer: '黑龙江利润元粮食贸易有限公司',
        ewradio: '',
        ddchecked: false,
        checked: false,
        isShowPrintType: false,
        dialogDataList: [],
        jjSelectPrintList: [],
        printSeparately: false,
        onePrint: false,
      }
    },
    activated() {
      //仓库
      selectWarehouseSelf({
          compId: localStorage.getItem('ws-pf_compId'),
        })
        .toPromise()
        .then((response) => {
          this.warehouseList = response
        })
      this.compId = localStorage.getItem('ws-pf_compId')
      this.warehousingType = this.$route.query.inWarehouseType
      this.warehouseName = this.$route.query.warehouseName //仓库名
      this.getList()
    },
    methods: {
      typePrintCannelClick() {
        this.onePrint = false //单个打印标识
        this.dialogDataList = [] //打个打印取消时数组至空
        this.isShowPrintType = false
      },
      handleSelectionChange(val) {
        this.modification = val;
      },
      // batchPrint(){
      //   if(this.modification.length == 0){
      //     this.$message.error("请选择批量打印的条目！")
      //     return
      //   }
      //   // this.isShowPrintType = true
      // },
      changeEw(e) {
        this.ddchecked = false
        if (e) {
          this.printSeparately = true
        } else {
          this.printSeparately = false
        }
      },
      changeEwDD(e) {
        this.checked = false
        if (e) {
          this.printSeparately = true
        } else {
          this.printSeparately = false
        }
      },
      lookup() {
        this.getList()
      },
      changeType(val) {
        this.warehousingType = val
        this.getList()
      },
      screen(num) {
        this.choice = num
        this.getList()
      },
      returnsales() {
        this.$router.go(-1)
      },
      async typePrintClick(type) {
        if (this.onePrint) { //判断是否是单个打印
          getPrint({
              id: this.idPrint
            })
            .toPromise()
            .then((response) => {
              this.dialogDataList = [] //点击确认时先清空要打印的数组
              this.dialogData = response
              if (response.compName == '黑龙江秋收有限公司') {
                this.dialogData.compName = '黑龙江中天昊元贸易有限公司'
              }
              if (this.checked) {
                this.dialogDataList.push(this.dialogData)
                var _datas = JSON.stringify(this.dialogData)
                _datas = JSON.parse(_datas)
                _datas.compName = this.companyNameEW;
                _datas.customer = this.ewCustomer
                this.dialogDataList.push(_datas)
              } else if (this.ddchecked) {
                _datas = this.dialogData
                _datas.compName = this.companyNameEW;
                _datas.customer = this.ewCustomer
                this.dialogDataList.push(_datas)
              } else {
                this.dialogData.companyName = this.companyName
                this.dialogDataList.push(this.dialogData)
              }
              this.printBig('', this.radio)
            })
            .catch((response) => {
              // EventBus.$emit('error', this.$t('showMessage.asteriskRequired'))
            })
        } else {
          //   if (this.isExcelPrint) {
          //   this.printBig(2, this.radio)
          //   this.isExcelPrint = false
          // } else {
          this.jjSelectPrintList = []
          let index1 = 0;
          for (let i = 0; i < this.modification.length; i++) {
            await getPrint({
                id: this.modification[i].id
              })
              .toPromise()
              .then((response) => {
                index1++;
                response.compName = this.companyName
                if (response.compName == '黑龙江秋收有限公司') {
                  response.compName = '黑龙江中天昊元贸易有限公司'
                }
                this.jjSelectPrintList.push(response)
                if (index1 == this.modification.length) {
                  this.printBig(1, this.radio)
                }
              })
              .catch((response) => {})
          }
          // }
        }
      },
      makeEWjjSelectPrintList() {
        let _selectPrint_EWjjSelectPrintList = JSON.parse(JSON.stringify(this.jjSelectPrintList));
        for (let i = 0; i < _selectPrint_EWjjSelectPrintList.length; i++) {
          let _data = _selectPrint_EWjjSelectPrintList[i];
          _data.compName = this.companyNameEW;
          _data.customer = this.ewCustomer
        }
        localStorage.setItem('selectPrint_EWjjSelectPrintList', JSON.stringify(_selectPrint_EWjjSelectPrintList))
      },
      check(name, type) {
        if (type == 1) {
          for (var _tmp = 0; _tmp < this.ewCustomerOptions.length; _tmp++) {
            if (this.ewCustomerOptions[_tmp].label == name) {
              return false
            }
          }
          return true
        } else if (type == 2) {
          for (var _tmp = 0; _tmp < this.companyNameEWOptions.length; _tmp++) {
            if (this.companyNameEWOptions[_tmp].label == name) {
              return false
            }
          }
          return true

        }

      },
      print(row, type, pageType) {
        //打印
        if (type == 1) { //
          if (this.modification.length == 0) {
            this.$message.error('请选择批量打印的条目！')
            return
          }
          for (let _num = 0; _num < this.modification.length; _num++) {
            if (this.modification[_num].serviceManagementType == 1 && (this.warehouseName == '鲅鱼圈金信库' || this
                .warehouseName == '白城内陆港'|| this.warehouseName == '哈尔滨依兰库')) {
              if (this.modification[_num].seller && this.modification[_num].buyer) {
                let cang = {}
                cang.value = this.modification[_num].seller
                cang.label = this.modification[_num].seller
                if (this.check(this.modification[_num].seller, 1)) {
                  this.ewCustomerOptions.push(cang)
                }
                let cang1 = {}
                cang1.value = this.modification[_num].buyer
                cang1.label = this.modification[_num].buyer
                if (this.check(this.modification[_num].buyer, 2)) {
                  this.companyNameEWOptions.push(cang1)
                }
              }
            }
          }
          this.isShowPrintType = true
        } else {
          if (row.serviceManagementType == 1 && (this.warehouseName == '鲅鱼圈金信库' || this.warehouseName == '白城内陆港'|| this.warehouseName == '哈尔滨依兰库')) {
            if (row.seller && row.buyer) {
              let cang = {}
              cang.value = row.seller
              cang.label = row.seller
              if (this.check(row.seller, 1)) {
                this.ewCustomerOptions.push(cang)
              }
              let cang1 = {}
              cang1.value = row.buyer
              cang1.label = row.buyer
              if (this.check(row.buyer, 2)) {
                this.companyNameEWOptions.push(cang1)
              }
            }
          }
          this.idPrint = row.id
          this.isShowPrintType = true
          this.onePrint = true //单个打印标识
        }

      },
      printSmall() {
        window.open('../../../../static/weightCheck.html?type=1&tableData=' + JSON.stringify(this.dialogData))
      },
      printBig(type, radioType) {
        // 打印额外
        if (this.checked || this.ddchecked) {
          this.makeEWjjSelectPrintList()
          //  this.selectEW = true;
        }
        if (type == 1) {
          localStorage.setItem('selectPrint_jjSelectPrintList', JSON.stringify(this.jjSelectPrintList))
          window.open('../../../../static/weightCheck.html?type=2&radioType=' + radioType + '&selectEW=' + this
            .checked + '&selectEWDD=' + this.ddchecked)
        } else if (type == 2) {
          localStorage.setItem('selectPrint_jjSelectPrintList', JSON.stringify(this.excelPrintData))
          window.open('../../../../static/weightCheck.html?type=2&radioType=' + radioType)
        } else {
          console.log(this.dialogDataList)
          window.open('../../../../static/weightCheck.html?type=2&tableData=' + JSON.stringify(this.dialogDataList))
        }
      },
      warehouseNameChange() {
        this.getList()
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        console.log(`当前页: ${val}`)
        this.getList()
      },
      getList() {
        // 列表
        getweighingList({
            compId: localStorage.getItem('ws-pf_compId'),
            currentPage: this.currentPage,
            pageSize: this.pageSize,
            searchKeyWord: this.searchKeyWord,
            warehouseName: this.warehouseName,
            managementType: 1,
            serviceManagementType: this.warehousingType,
            statusType: this.choice
          })
          .toPromise()
          .then((response) => {
            this.weighingRecordList = response.records
            this.deptBudgetTotal = response.total
          })
          .catch((response) => {
            // EventBus.$emit('error', this.$t('showMessage.asteriskRequired'))
          })
      },

    },
  }
</script>
<style lang="scss" scoped>
  .toptitle {
    background: #f6f7fc;
  }

  .bg-left {
    padding-left: 30px;
  }

  .bg-right {
    text-align: right;
    padding: 16px 20px;
  }

  .top-title {
    width: 50%;
    display: flex;
  }

  .title {
    position: relative;
  }

  .title::before {
    content: '';
    display: inline-block;
    width: 5px;
    height: 30px;
    background: #5473e8;
    position: absolute;
    left: 0;
  }

  .container {
    overflow: scroll;
    height: 93vh;
  }

  .center {
    margin: auto;
  }

  .centertitle {
    display: flex;
    margin: 10px 0;
  }

  .temporary {
    line-height: 80px;
    font-size: 20px;
    color: #abaaaa;
  }

  /deep/.forData .cell {
    text-align: center;
  }

  //弹出框
  .eject-warehouse {
    font-size: 20px;
    margin-left: 15px;
    font-weight: 600;
  }

  .table_css {
    margin: 20px auto;

    tr {
      height: 50px;
    }

    .table_item {
      min-width: 70px;
    }

    td {
      text-align: center;
      font-size: 18px;
      font-weight: 600;
    }

    .el-select {
      width: 90%;
    }
  }

  .btn_topcss {
    margin-top: 15px;

  }

  .btn_css {
    width: 90px;
    margin-left: 10px;
  }

  .search_css {
    margin: 0 20px;
  }

  .mask {
    background: black;
    width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;
    z-index: 99;
    opacity: 0.3;
  }

  .print-type-title {
    text-align: center;
    font-size: 18px;
    margin: 20px;
  }

  .bottom-btn {
    display: flex;
    margin-top: 20px;
    justify-content: space-around;
  }

  .print-type-checkbox {
    // padding-left: 20px;
  }

  .print-type {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    width: 440px;
    height: 320px;
    background: white;
    border-radius: 10px;
    z-index: 999;
  }

  /deep/.el-radio-group {
    width: 100%;
    display: flex;
  }

  /deep/.el-radio__label {
    font-size: 20px;
  }

  /deep/.el-radio__inner {
    // background: red;
    height: 20px;
    width: 20px;
  }

  .label-width {
    width: 100%;
    text-align: center;
    font-size: 20px;
  }

  .show-excel {
    opacity: 0;
  }

  /deep/.base_header_layout .find.el-button--primary {
    width: 30px;
    border-top-left-radius: 0px;
    border-bottom-left-radius: 0px;
  }

  /deep/.base_header_layout .findinput input {
    border-top-right-radius: 0px;
    border-bottom-right-radius: 0px;
  }

  .ew-print {
    padding: 0 20px;
    margin-top: 10px;

    // display: flex;
    // justify-content: space-between;
    // align-content: center;
    .row {
      display: flex;
      margin-top: 10px;
      align-items: center;
    }
  }

  .sh-address {
    display: flex;
  }

  .print-type-ew {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;

    /deep/.el-radio__label {
      font-size: 14px;
    }

    margin-top: 10px;
  }

  /deep/.el-table .cell {
    text-align: center;
  }

  .shdw-style {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    margin-top: 20px;
  }
</style>
