<template>
  <section>
    <nav-bar :inner="true" title="扫码出库"></nav-bar>
    <section class="content mgt186">
      <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" placeholder="" v-model="val2" disabled>
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">物料</div>
          <div class="fxcol mgl20">
            <input type="text" class="filter-input filter-scan-input" placeholder="" v-model="val3" disabled>
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">待出数</div>
          <div class="fxcol mgl20">
            <input type="text" class="filter-input filter-scan-input" placeholder="" v-model="val4" disabled>
          </div>
        </div>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">已出数</div>
          <div class="fxcol mgl20">
            <input type="text" class="filter-input filter-scan-input" placeholder="" v-model="val5" disabled>
          </div>
        </div>
      </div>
      <div class="grid-wraper">
        <div class="slide">
          <table class="layout-t">
            <tr>
              <th>备件唯一编码</th>
              <th>备件名称</th>
              <th>数量</th>
              <th>单位</th>
              <th>货位</th>
            </tr>
            <tr v-for="e in dataList" :key="e.sparepart_only_id" @click="toCheck(e)" :class="{'checked': e.sparepart_only_id === pkId}">
              <td>{{e.sparepart_only_id}}</td>
              <td>{{e.material_name}}</td>
              <td>{{e.real_qty}}</td>
              <td>{{e.qty_unit_name}}</td>
              <td>{{e.struct_name}}</td>
            </tr>
          </table>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled': !pkId}" @click="del()">删除</button>
      <button class="btn submit-button" :class="{'btn-disabled': dataList.length === 0}" :disabled="disabled1" @click="toSure">确认</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import {queryIODis, confirmDis} from '@config/getData1.js'
export default {
  name: 'ScanOutStore',
  components: {
    NavBar,
    SearchBox
  },
  data () {
    return {
      val1: '',
      val2: this.$store.getters.materObj.bill_code || '',
      val3: this.$store.getters.materObj.material_name || '',
      val4: this.$store.getters.materObj.need_qty || '',
      val5: this.$store.getters.materObj.finish_qty || '',
      dataList: [],
      pkId: '',
      pkObj: {},
      disabled1: false
    }
  },
  methods: {
    handleChange1 (e, type) {
      if (type) {
        this._queryIODis(e)
      }
    },
    toCheck (e) {
      this.pkId = this.pkId === e.sparepart_only_id ? '' : e.sparepart_only_id
      this.pkObj = this.pkId === e.sparepart_only_id ? e : {}
    },
    /** 1.2备品备件扫描 */
    async _queryIODis (e) {
      let res = await queryIODis(e, this.$store.getters.materObj.iostorinvdtl_id)
      if (res.code === '1') {
        if (!this.dataList.length) {
          this.dataList.push(res.content)
        } else {
          if (JSON.stringify(this.dataList).indexOf(JSON.stringify(res.content)) === -1) {
            this.dataList.push(res.content)
          }
        }
      } else {
        this.Dialog(res.desc)
      }
    },
    async _confirmDis () {
      try {
        let res = await confirmDis('1', this.$store.getters.materObj, this.dataList)
        if (res.code === '1') {
          this.toast(res.desc)
          this.$router.push({
            path: '/SparePartOutstore'
          })
        } else {
          this.Dialog(res.desc)
        }
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    del () {
      if (!this.pkId) {
        return
      }
      this.dataList.map((el, i) => {
        if (el.sparepart_only_id === this.pkId) {
          this.dataList.splice(i, 1)
        }
      })
      this.pkId = ''
      this.pkObj = {}
    },
    toSure () {
      this.disabled1 = true
      if (!this.dataList.length) {
        this.disabled1 = false
        return
      }
      this._confirmDis()
    }
  }
}
</script>
