<template>
  <section>
    <nav-bar title="喷雾工序"></nav-bar>
    <section ref="content" class="content" :style="'margin-top: '+(0.96+3 * 0.92)+ 'rem'">
      <div class="filter-wraper">
        <search-box
        label="设备"
        v-model="val1"
        :seaShow="false"
        ></search-box>
        <search-box
        label="工艺指令卡"
        :focused="true"
        v-model="val2"
        @handleChange="handleChange2"
        ></search-box>
        <search-box
        label="托盘"
        :focused="true"
        :seaShow="false"
        v-model="val3"
        ></search-box>
        <search-box
        label="站点"
        :focused="true"
        :seaShow="false"
        v-model="val4"
        ></search-box>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">混合设备</div>
          <div class="fxcol mgl20 visible" >
            <dropdown-menu
            :option="option"
            :active="active"
            :open="open"
            @toggleItem="toggleItem"
            @dropdownMenu="dropdownMenu">
            </dropdown-menu>
          </div>
        </div>
      </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 submit-bar1">
      <button class="btn submit-button" @click="feeding">上料请求</button>
      <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 === ''}" :disabled="disabled2" @click="_issuePW('下发')">下发</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled3" @click="_moveFinish('过料完成')">过料完成</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === '' || val3 === '' || val4 === '' || active === ''}" :disabled="disabled4" @click="_movePWVehicle('下料混合')">下料混合</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" @click="toPrint1">打印桶码</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" @click="groupDisk">组盘</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled5" @click="_sendLK">发送立库</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled1" @click="_updateWorkStatus('99', '结束')">结束</button>
      <i ref="arrow" class="iconfont open_icon" @click="packUp"></i>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import DropdownMenu from '@components/DropdownMenu.vue'
import { submitPackUp } from '@config/mUtils.js'
import {queryMixDevice, queryWorkTask, updateWorkStatus, issuePW, moveFinish, movePWVehicle, sendLK} from '@config/getData2'
export default {
  name: 'ProcessSpray',
  components: {
    NavBar,
    SearchBox,
    DropdownMenu
  },
  data () {
    return {
      val1: '',
      val2: '',
      barCode: '',
      val3: '',
      val4: '',
      option: [],
      active: '',
      open: false,
      dataList: [],
      pkId: '',
      pkObj: {},
      up: false,
      disabled1: false,
      disabled2: false,
      disabled3: false,
      disabled4: false,
      disabled5: false
    }
  },
  created () {
    this._queryMixDevice()
  },
  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.barCode = e
        this._queryWorkTask(e)
      }
    },
    toCheck (e) {
      this.pkId = this.pkId === e.worktask_id ? '' : e.worktask_id
      this.pkObj = this.pkId === e.worktask_id ? e : {}
    },
    packUp () {
      this.up = submitPackUp(this.$refs.submit, this.$refs.arrow, this.$refs.content, 3, this.up)
    },
    toggleItem () {
      if (!this.open) {
        this.open = true
      } else {
        this.open = false
      }
    },
    dropdownMenu (i) {
      this.active = i + ''
      this.open = false
    },
    /** 混合设备查询 */
    async _queryMixDevice () {
      let res = await queryMixDevice()
      if (res.code === '1') {
        this.option = [...res.content]
        this.option.map(el => {
          this.$set(el, 'value', el.id)
          this.$set(el, 'label', el.value)
        })
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 扫描工艺指令卡 */
    async _queryWorkTask (e) {
      let res = await queryWorkTask(e, 'GX003')
      if (res.code === '1') {
        this.val2 = res.worktask_code
        this.dataList = [...res.rows]
      } else {
        this.Dialog(res.desc)
      }
    },
    /** 检查确认、开始、结束 */
    async _updateWorkStatus (type, n) {
      this.disabled1 = true
      if (this.pkId === '') {
        this.disabled1 = false
        return
      }
      try {
        let code = ''
        if (type === '30') {
          code = this.val1
        }
        let res = await updateWorkStatus(this.pkId, type, n, code)
        if (res.code === '1') {
          this.toast(res.desc)
          this._queryWorkTask(this.barCode)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    /** 下发 */
    async _issuePW (n) {
      this.disabled2 = true
      if (this.pkId === '') {
        this.disabled2 = false
        return
      }
      try {
        let res = await issuePW(this.pkId, n)
        if (res.code === '1') {
          this.toast(res.desc)
          this._queryWorkTask(this.barCode)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled2 = false
      } catch (e) {
        this.disabled2 = false
      }
    },
    /** 过料完成 */
    async _moveFinish (n) {
      this.disabled3 = true
      if (this.pkId === '') {
        this.disabled3 = false
        return
      }
      try {
        let res = await moveFinish(this.pkId, n)
        if (res.code === '1') {
          this.toast(res.desc)
          this._queryWorkTask(this.barCode)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled3 = false
      } catch (e) {
        this.disabled3 = false
      }
    },
    /** 下料混合 */
    async _movePWVehicle (n) {
      this.disabled4 = true
      if (this.pkId === '' || this.val3 === '' || this.active === '' || this.val4 === '') {
        this.disabled4 = false
        return
      }
      try {
        let res = await movePWVehicle(this.pkId, this.val1, this.val3, this.option[this.active].value, this.val4, n)
        if (res.code === '1') {
          this.toast(res.desc)
          this._queryWorkTask(this.barCode)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled4 = false
      } catch (e) {
        this.disabled4 = false
      }
    },
    /** 组盘 */
    groupDisk () {
      if (this.pkId) {
        this.$store.dispatch('materObj', this.pkObj)
        this.$router.push({
          path: '/GroupDiskOperateN',
          query: {id: this.pkId, url: 'ProcessSpray'}
        })
      }
    },
    /** 上料请求 */
    feeding () {
      this.$router.push('/SprayFeed')
    },
    /** 发送立库 */
    async _sendLK () {
      this.disabled5 = true
      if (this.pkId === '') {
        this.disabled5 = false
        return
      }
      try {
        let res = await sendLK(this.pkId, this.val3, this.val4, '发送立库')
        if (res.code === '1') {
          this.toast(res.desc)
          this._queryWorkTask(this.barCode)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled5 = false
      } catch (e) {
        this.disabled5 = false
      }
    },
    /** 打印桶码 */
    toPrint1 () {
      if (this.pkId) {
        this.$store.dispatch('materObj1', this.pkObj)
        this.$store.dispatch('setKeepAlive', ['ProcessSpray', 'ChengPinBarrelPrint'])
        this.$router.push({
          path: '/ChengPinBarrelPrint',
          query: {url: 'ProcessSpray'}
        })
      }
    }
  }
}
</script>

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