<template>
  <section>
    <nav-bar :inner="true" title="查找托盘"></nav-bar>
    <section class="content mgt186" v-infinite-scroll="loadMore" infinite-scroll-disabled="busy" infinite-scroll-distance="0" infinite-scroll-immediate-check="false">
      <div class="filter-wraper">
        <search-box
        label="桶码"
        v-model="val1"
        @handleChange="handleChange"
        ></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.id" @click="toCheck(e)" :class="{'checked': e.id === pkId}">
              <td>{{e.storagevehicle_code}}</td>
            </tr>
          </table>
        </div>
        <div class="slide">
          <table class="layout-t">
            <tr>
              <th>总桶数</th>
              <th>软废编码</th>
              <th>桶数</th>
              <th>重量kg</th>
              <th>货位</th>
            </tr>
            <tr v-for="e in dataList" :key="e.id" @click="toCheck(e)" :class="{'checked': e.id === pkId}">
              <td>{{e.bucket_num | numeric(3)}}</td>
              <td>{{e.material_code}}</td>
              <td>{{e.material_num | numeric(3)}}</td>
              <td>{{e.total_qty | numeric(3)}}</td>
              <td>{{e.struct_code}}</td>
            </tr>
          </table>
        </div>
      </div>
      <div class="loading-tips">{{desc}}</div>
    </section>
    <section class="calc_value_wraper">
      <div class="bottom-filter-tip">
        <div class="filter-label txtjustify">出库点</div>
        <div class="fxcol mgl20 visible" >
          <dropdown-menu
          :option="option"
          :active="active"
          :open="open"
          :up="true"
          @toggleItem="toggleItem"
          @dropdownMenu="dropdownMenu">
          </dropdown-menu>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :disabled="disabled1" :class="{'btn-disabled': pkId === '' || active === ''}" @click="toSure1">取半满托盘</button>
      <button class="btn submit-button" :disabled="disabled2" :class="{'btn-disabled': active === ''}" @click="toSure2">取空托盘</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import DropdownMenu from '@components/DropdownMenu.vue'
import {findVehicle, queryOutPoint, askVehicle, needVehicle} from '@config/getData2.js'
export default {
  name: 'SearchPallet',
  components: {
    NavBar,
    SearchBox,
    DropdownMenu
  },
  data () {
    return {
      val1: '',
      option: [],
      active: '',
      open: false,
      dataList: [],
      pkId: '',
      pkObj: {},
      page: 1,
      size: '10',
      busy: false,
      desc: '',
      disabled1: false,
      disabled2: false
    }
  },
  created () {
    this._queryOutPoint()
  },
  methods: {
    handleChange (e, type) {
      if (type) {
        this._findVehicle()
      }
    },
    toggleItem () {
      if (!this.open) {
        this.open = true
      } else {
        this.open = false
      }
    },
    dropdownMenu (i) {
      this.active = i + ''
      this.open = false
    },
    toCheck (e) {
      this.pkId = this.pkId === e.id ? '' : e.id
      this.pkObj = this.pkId === e.id ? e : {}
    },
    toSure1 () {
      this.disabled1 = true
      if (this.pkId !== '' && this.active !== '') {
        this._askVehicle(this.pkObj, this.option[this.active].point_code)
      }
    },
    toSure2 () {
      this.disabled2 = true
      if (this.active !== '') {
        this._needVehicle(this.option[this.active].point_code)
      }
    },
    async loadMore () {
      this.busy = true
      this.page++
      this.desc = '加载数据中...'
      let res = await findVehicle(this.val1, this.page + '', this.size)
      if (res.code === '1') {
        this.dataList = [...this.dataList, ...res.vehicle_rows]
        if (res.vehicle_rows.length < 10) {
          this.busy = true
          this.desc = '已加载全部数据'
        } else {
          this.busy = false
        }
      } else {
        this.Dialog(res.desc)
        this.desc = res.desc
      }
    },
    async _findVehicle () {
      this.page = 1
      this.busy = false
      this.desc = ''
      let res = await findVehicle(this.val1, this.page + '', this.size)
      if (res.code === '1') {
        this.dataList = []
        this.dataList = [...res.vehicle_rows]
        if (res.vehicle_rows.length < 10) {
          this.busy = true
          this.desc = '已加载全部数据'
        }
      } else {
        this.Dialog(res.desc)
        this.desc = res.desc
      }
    },
    async _queryOutPoint () {
      let res = await queryOutPoint()
      if (res.code === '1') {
        this.option = [...res.point_rows]
        this.option.map(el => {
          this.$set(el, 'value', el.point_code)
          this.$set(el, 'label', el.point_code)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 取出托盘 */
    async _askVehicle (obj, code) {
      try {
        let res = await askVehicle(obj, code)
        if (res.code === '1') {
          this.toast(res.desc)
          this.$router.back()
        } else {
          this.Dialog(res.desc)
        }
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    /** 取空托盘 */
    async _needVehicle (code) {
      try {
        let res = await needVehicle(code)
        if (res.code === '1') {
          this.toast(res.desc)
          this.$router.back()
        } else {
          this.Dialog(res.desc)
        }
        this.disabled2 = false
      } catch (e) {
        this.disabled2 = false
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
>>>.content
  height calc(100% - 2.96rem)
  overflow-y auto
</style>
