<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">
                        <el-select v-model="value1" filterable clearable placeholder="请选择">
                            <el-option
                              v-for="item in options1"
                              :key="item.value"
                              :label="item.label"
                              :value="item.value">
                          </el-option>
                        </el-select>
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">下料重量</div>
                    <div class="filter_input_wraper">
                        <input type="number" class="filter-input" v-model="weight">
                    </div>
                </div>
                <div class="search-item">
                    <div class="search-label">料框条码</div>
                    <div class="filter_input_wraper">
                        <input type="text" class="filter-input" v-model="vechile_code">
                    </div>
                </div>
                <div class="search-item_3">
                    <button class="button button--primary" @click="_washWashTasks">查询</button>
                    <button class="button button--primary" :disabled="disabled1" :class="{'button--defalut': value1 === ''}" @click="_washWashVechileInfo">获取信息</button>
                    <button class="button button--primary" :disabled="disabled2" :class="{'button--defalut': pkId === ''}" @click="_washWashFinish('1')">确认下料</button>
                    <button class="button button--primary" :disabled="disabled3" :class="{'button--defalut': pkId === ''}" @click="_washWashTaskFinish('2')">强制完成</button>
                    <button class="button button--primary" :disabled="disabled5" @click="_bcpInCallVehicle">呼叫空框</button>
                    <button class="button button--primary" :disabled="disabled4" :class="{'button--defalut': pkId === ''}" @click="_washWashQzFinish('3')">强制下料</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>上料重量(kg)</th>
                        <th>物料名称</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="e in dataList" :key="e.task_id" :class="{'selected_icon': pkId === e.task_id}" @click="toRadio(e)">
                        <td>{{ e.sep_on }}</td>
                        <td>{{ e.create_time }}</td>
                        <td>{{e.task_code}}</td>
                        <td>{{e.material_code}}</td>
                        <td>{{e.material_spec}}</td>
                        <td>{{e.material_qty | unitskg}}</td>
                        <td>{{ e.material_name }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <jxDialog
          ref="child"
          title="提示"
          :type="type"
          @toSure="toSureDialog"
          @toCancle="toCancle"
          >
          <div class="form_wraper">当前任务号不是第一个任务是否确认下料？</div>
        </jxDialog>
    </div>
</template>

<script>
import jxDialog from '@components/dialog.vue'
import {dictAll, washWashTasks, washWashVechileInfo, washWashFinish, washWashTaskFinish, washWashQzFinish, bcpInCallVehicle} from '@config/getData2.js'
export default {
  components: {
    jxDialog
  },
  data () {
    return {
      options1: [],
      value1: '',
      weight: '',
      vechile_code: '',
      dataList: [],
      sep_on: '',
      pkId: '',
      pkObj: {},
      type: '',
      disabled1: false,
      disabled2: false,
      disabled3: false,
      disabled4: false,
      disabled5: false
    }
  },
  created () {
    this._dictAll()
  },
  methods: {
    // 车间下拉框
    async _dictAll () {
      let res = await dictAll()
      if (res.code === 200) {
        this.options1 = [...res.content]
        this.value1 = this.options1[0].value
      }
    },
    // grid
    async _washWashTasks () {
      let res = await washWashTasks(this.value1)
      if (res.code === 200) {
        this.pkId = ''
        this.pkObj = {}
        this.dataList = [...res.content]
        if (this.dataList.length > 0) {
          this.sep_on = this.dataList[0].sep_on
        }
      }
    },
    toRadio (e) {
      this.pkId = this.pkId === e.task_id ? '' : e.task_id
      this.pkObj = this.pkId === e.task_id ? e : {}
    },
    // 获取信息
    async _washWashVechileInfo () {
      this.disabled1 = true
      if (this.value1 === '') {
        this.disabled1 = false
        return
      }
      try {
        let res = await washWashVechileInfo(this.value1)
        if (res.code === 200) {
          if (res.content.length > 0) {
            this.weight = res.content[0].weight
            this.vechile_code = res.content[0].vechile_code
          }
        }
        this.disabled1 = false
      } catch (e) {
        this.disabled1 = false
      }
    },
    // 确认下料
    async _washWashFinish (type) {
      this.disabled2 = true
      if (this.pkId === '') {
        this.disabled2 = false
        return
      }
      if (this.pkObj.sep_on !== this.sep_on) {
        this.type = type
        this.$refs.child.active = true
        return
      }
      this.__washWashFinish()
    },
    async __washWashFinish () {
      try {
        let res = await washWashFinish(this.pkId, this.weight, this.vechile_code)
        if (res.code === 200) {
          this.toast(res.msg)
          this._washWashTasks()
        }
        this.disabled2 = false
      } catch (e) {
        this.disabled2 = false
        this.$refs.child.active = false
      }
    },
    // 强制完成
    async _washWashTaskFinish (type) {
      this.disabled3 = true
      if (this.pkId === '') {
        this.disabled3 = false
        return
      }
      if (this.pkObj.sep_on !== this.sep_on) {
        this.type = type
        this.$refs.child.active = true
        return
      }
      this.__washWashTaskFinish()
    },
    async __washWashTaskFinish () {
      try {
        let res = await washWashTaskFinish(this.pkId)
        if (res.code === 200) {
          this.toast(res.msg)
          this._washWashTasks()
        }
        this.disabled3 = false
      } catch (e) {
        this.disabled3 = false
        this.$refs.child.active = false
      }
    },
    // 强制下料
    async _washWashQzFinish (type) {
      this.disabled4 = true
      if (this.pkId === '') {
        this.disabled4 = false
        return
      }
      if (this.pkObj.sep_on !== this.sep_on) {
        this.type = type
        this.$refs.child.active = true
        return
      }
      this.__washWashQzFinish()
    },
    async __washWashQzFinish () {
      try {
        let res = await washWashQzFinish(this.pkId, this.weight, this.vechile_code)
        if (res.code === 200) {
          this.toast(res.msg)
          this._washWashTasks()
        }
        this.disabled4 = false
      } catch (e) {
        this.disabled4 = false
        this.$refs.child.active = false
      }
    },
    toSureDialog (type) {
      switch (type) {
        case '1':
          this.__washWashFinish()
          break
        case '2':
          this.__washWashTaskFinish()
          break
        case '3':
          this.__washWashQzFinish()
      }
    },
    toCancle () {
      this.disabled2 = false
      this.disabled3 = false
      this.disabled4 = false
    },
    async _bcpInCallVehicle () {
      this.disabled5 = true
      try {
        let res = await bcpInCallVehicle()
        if (res.code === 200) {
          this.toast(res.msg)
        }
        this.disabled5 = false
      } catch (e) {
        this.disabled5 = false
      }
    }
  }
}
</script>

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