<template>
  <section>
    <nav-bar title="压团工序"></nav-bar>
    <section ref="content" class="content mgt186">
      <div class="filter-wraper">
        <search-box
        label="设备"
        v-model="val1"
        ></search-box>
        <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.worktask_id" @click="toCheck(e)" :class="{'checked': e.worktask_id === pkId}">
              <td>{{e.device_code}}</td>
            </tr>
          </table>
        </div>
        <div class="slide">
          <table class="layout-t">
            <tr>
              <th>工令号</th>
              <th>工令任务号</th>
              <th>工序</th>
              <th>产品编码</th>
              <th>批次</th>
              <th>重量kg</th>
              <th>状态</th>
            </tr>
            <tr v-for="e in dataList" :key="e.worktask_id" @click="toCheck(e)" :class="{'checked': e.worktask_id === pkId}">
              <td>{{e.workorder_code}}</td>
              <td>{{e.worktask_code}}</td>
              <td>{{e.workprocedure_name}}</td>
              <td>{{e.material_code}}</td>
              <td>{{e.pcsn}}</td>
              <td>{{e.workorder_qty | numeric(3)}}</td>
              <td>{{e.status_name}}</td>
            </tr>
          </table>
        </div>
      </div>
    </section>
    <section ref="submit" class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled1" @click="_updateWorkStatus('20', '检查确认')">检查确认</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled1" @click="_updateWorkStatus('30', '开始')">开始</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" @click="ResultEntry">工序质检</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled1" @click="_updateWorkStatus('99', '结束')">结束</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import {queryWorkTask, updateWorkStatus} from '@config/getData2'
export default {
  name: 'ProcessYatuan',
  components: {
    NavBar,
    SearchBox
  },
  data () {
    return {
      val1: '',
      val2: '',
      dataList: [],
      pkId: '',
      pkObj: {},
      disabled1: false
    }
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/home' || to.path === '/login') {
      this.$store.dispatch('setKeepAlive', [])
    }
    next()
  },
  activated () {
    if (this.barCode) {
      this._queryWorkTask(this.barCode)
    }
  },
  methods: {
    handleChange2 (e, type) {
      if (type) {
        this._queryWorkTask(e)
      }
    },
    toCheck (e) {
      this.pkId = this.pkId === e.worktask_id ? '' : e.worktask_id
      this.pkObj = this.pkId === e.worktask_id ? e : {}
    },
    /** 扫描工艺指令卡 */
    async _queryWorkTask (e) {
      let res = await queryWorkTask(e, 'GX008')
      if (res.code === '1') {
        this.val2 = res.content.worktask_code
        this.dataList = [...res.content.rows]
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 检查确认 */
    async _updateWorkStatus (type, n) {
      this.disabled1 = true
      if (this.pkId === '') {
        this.disabled1 = false
        return
      }
      try {
        let res = await updateWorkStatus(this.pkId, type, n)
        if (res.code === '1') {
          this.toast(res.desc)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    /** 工序质检 */
    ResultEntry () {
      if (this.pkId) {
        this.$store.dispatch('materObj1', this.pkObj)
        this.$router.push({
          path: '/ResultEntry',
          query: {url: 'ProcessYatuan'}
        })
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
>>>.filter-label
  width 1.8rem
</style>
