<template>
    <div class="order-wraper">
        <div class="search-confirm-wrap">
            <div class="search-wrap">
                <div class="search-item">
                    <div class="search-label">规格</div>
                    <div class="filter_input_wraper">
                        <input type="text" class="filter-input">
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">载具</div>
                    <div class="filter_input_wraper">
                        <input type="text" class="filter-input">
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">重量</div>
                    <div class="filter_input_wraper">
                        <input type="number" class="filter-input">
                    </div>
                </div>
                <div class="search-item_3">
                    <button class="button button--primary" @click="_washQuery">平均分配</button>
                    <button class="button button--primary" :disabled="disabled1" :class="{'button--defalut': checkArr.length === 0}" @click="_washSubmitWash">取消分配</button>
                    <button class="button button--primary" @click="toJump">确认上料</button>
                </div>
            </div>
        </div>
        <div class="grid_wraper">
            <table class="filter-table">
                <thead>
                    <tr>
                        <th>选择</th>
                        <th>序号</th>
                        <th>设备号</th>
                        <th>工单号</th>
                        <th>计划数量</th>
                        <th>生产数量</th>
                        <th>物料规格</th>
                        <th>重量(kg)</th>
                        <th>数量</th>
                        <th>物料编号</th>
                        <th>物料名称</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(e, i) in dataList" :key="i" @click="toRadio(e)">
                        <td>
                          <button class="iconfont select_icon select_square_icon" :class="e.checked ? 'selected_icon' : 'unselect_icon'"></button>
                        </td>
                        <td>{{ e.device_code }}</td>
                        <td>{{e.material_code}}</td>
                        <td>{{e.material_spec}}</td>
                        <td>{{e.deviceinstor_weight | unitskg}}</td>
                        <td>{{ e.deviceinstor_qty | numeric(3)  }}</td>
                        <td>{{ e.task_code }}</td>
                        <td>{{ e.update_time }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</template>

<script>
import {dictAll, washSpecList, washQuery, washSubmitWash} from '@config/getData2.js'
export default {
  name: 'cleaningloading',
  data () {
    return {
      options1: [],
      value1: '',
      options2: [],
      value2: '',
      dataList: [],
      checkArr: [],
      disabled1: false
    }
  },
  created () {
    this._dictAll()
    this._washSpecList()
  },
  methods: {
    // 车间下拉框
    async _dictAll () {
      let res = await dictAll()
      if (res.code === 200) {
        this.options1 = [...res.content]
        this.value1 = this.options1[0].value
        this._washQuery()
      }
    },
    // 规格下拉框
    async _washSpecList () {
      let res = await washSpecList()
      if (res.code === 200) {
        this.options2 = [...res.content]
      }
    },
    // grid
    async _washQuery () {
      let res = await washQuery(this.value1, this.value2)
      if (res.code === 200) {
        this.checkArr = []
        res.content.map(el => {
          this.$set(el, 'checked', false)
        })
        this.dataList = [...res.content]
      }
    },
    // 确认上料
    async _washSubmitWash () {
      this.disabled1 = true
      if (this.checkArr.length === 0) {
        this.disabled1 = false
        return
      }
      try {
        let arr = []
        this.checkArr.map(el => {
          arr.push(el.device_code)
        })
        let res = await washSubmitWash(arr)
        if (res.code === 200) {
          this.toast(res.msg)
          this._washQuery()
        }
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    toJump () {
      this.$router.push('/manpour')
    },
    toRadio (e) {
      e.checked = !e.checked
      this.checkArr = this.dataList.filter(i => { return i.checked === true })
    }
  }
}
</script>

<style lang="stylus" scoped>
.grid_wraper
    height calc(100% - 1.1rem)
</style>
