<template>
  <section>
    <nav-bar title="新料其他入库组盘"></nav-bar>
    <section class="content mgt186 mgb70">
      <div class="filter-wraper">
        <search-box
        label="载具码"
        v-model="val1"
        @handleChange="handleChange1"
        ></search-box>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">关联单据</div>
          <div class="fxcol mgl20">
            <input type="text" class="filter-input filter-scan-input" v-model="val3" disabled>
          </div>
          <div class="mgl20">
            <button class="btn" @click="searchList">选择</button>
          </div>
        </div>
        <search-box
        label="桶码"
        :focused='true'
        v-model="val2"
        @handleChange="handleChange2"
        ></search-box>
      </div>
      <div class="grid-wraper">
        <div class="left_fixed">
          <table class="layout-t left_layout_t">
            <tr>
            <th>桶码</th>
            </tr>
            <tr v-for="e in dataList" :key="e.bucketunique" @click="toCheck(e)" :class="{'checked': e.bucketunique === pkId}">
              <td>{{e.bucketunique}}</td>
            </tr>
          </table>
        </div>
        <div class="slide">
          <table class="layout-t">
            <tr>
              <th>物料编码</th>
              <th>物料名称</th>
              <th>批次</th>
              <th>重量kg</th>
            </tr>
            <tr v-for="e in dataList" :key="e.bucketunique" @click="toCheck(e)" :class="{'checked': e.bucketunique === pkId}">
              <td>{{e.material_code}}</td>
              <td>{{e.material_name}}</td>
              <td>{{e.pcsn}}</td>
              <td>{{e.storage_qty | numeric(3)}}</td>
            </tr>
          </table>
        </div>
      </div>
    </section>
    <section class="calc_value_wraper">
      <div class="bottom-filter-tip">
        <div class="filter-label txtjustify">总重量</div>
        <div class="fxcol mgl20">
          <input type="text" class="filter-input filter-scan-input" v-model="val4">
        </div>
        <div class="mgl20">KG</div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled': this.pkId === ''}" @click="deleteList">删除行</button>
      <button class="btn submit-button" :disabled="disabled" :class="{'btn-disabled': dataList.length === 0}" @click="toSure">确认组盘</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import {checkVehicle, checkBucket, confirmVehicleOther} from '@config/getData2.js'
import {accAdd} from '@config/mUtils.js'
export default {
  name: 'NewMaterInStoreGroup',
  components: {
    NavBar,
    SearchBox
  },
  data () {
    return {
      val1: '',
      vehicle: {},
      val2: '',
      val3: '',
      val4: '',
      dataList: [],
      pkId: '',
      pkObj: '',
      disabled: false
    }
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/home' || to.path === '/login') {
      this.$store.dispatch('setKeepAlive', [])
    }
    next()
  },
  activated () {
    this.val3 = this.$store.getters.materObj.source_bill_code
  },
  methods: {
    handleChange1 (e, type) {
      if (type) {
        this._checkVehicle(e, '1')
      }
    },
    handleChange2 (e, type) {
      if (this.val3 === '' || this.val3 === undefined) {
        this.toast('请先关联单据')
        return
      }
      if (type) {
        this._checkBucket(e, '1')
      }
    },
    /** 查找关联单据 */
    async searchList () {
      this.$router.push({
        path: '/SearchRelatedBill2'
      })
    },
    toCheck (e) {
      this.pkId = this.pkId === e.bucketunique ? '' : e.bucketunique
      this.pkObj = this.pkId === e.bucketunique ? e : {}
    },
    /** 删除行 */
    deleteList () {
      if (this.pkId !== '') {
        this.dataList.map((el, i) => {
          if (el.bucketunique === this.pkId) {
            this.dataList.splice(i, 1)
          }
        })
        this.val4 = '0.000'
        this.dataList.map(el => {
          this.val4 = accAdd(this.val4, el.storage_qty)
        })
        this.val4 = Number(this.val4).toFixed(3) + ''
        this.pkId = ''
        this.pkObj = {}
      }
    },
    /** 确认组盘 */
    toSure () {
      this.disabled = true
      if (this.dataList.length === 0) {
        this.disabled = false
        return
      }
      this._confirmVehicleOther()
    },
    /** 查询载具码 */
    async _checkVehicle (code, type) {
      let res = await checkVehicle(code, type)
      if (res.code === '1') {
        this.vehicle = res.vehicle_jo
        this.val1 = res.vehicle_jo.storagevehicle_code
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 查询桶信息 */
    async _checkBucket (code, type) {
      let res = await checkBucket(code, type)
      if (res.code === '1') {
        if (this.$store.getters.materObj.material_id === res.bucket_jo.material_id && this.$store.getters.materObj.pcsn === res.bucket_jo.pcsn) {
          if (this.dataList.length) {
            let arr = this.dataList.filter(el => { return el.bucketunique === res.bucket_jo.bucketunique })
            if (arr.length) {
              this.toast('桶码已存在')
            } else {
              this.dataList.push(Object.assign({}, res.bucket_jo, {bill: this.$store.getters.materObj}))
            }
          } else {
            this.dataList.push(Object.assign({}, res.bucket_jo, {bill: this.$store.getters.materObj}))
          }
        } else {
          this.toast('当前物料标识和批次与单据信息不一致')
        }
        this.val4 = '0.000'
        this.dataList.map(el => {
          this.val4 = accAdd(this.val4, el.storage_qty)
        })
        this.val4 = Number(this.val4).toFixed(3) + ''
      } else {
        this.Dialog(res.desc)
      }
    },
    async _confirmVehicleOther () {
      try {
        let bill = this.$store.getters.materObj !== '' ? this.$store.getters.materObj : {}
        let res = await confirmVehicleOther('1', this.vehicle, this.dataList, '', bill)
        if (res.code === '1') {
          this.toast(res.desc)
          Object.assign(this.$data, this.$options.data())
          this.$store.dispatch('materObj', '')
        } else {
          this.Dialog(res.desc)
        }
        this.disabled = false
      } catch (e) {
        this.disabled = false
      }
    }
  }
}
</script>
