<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_2">
                    <button class="button button--primary" @click="_KzTasks">查询</button>
                    <button class="button button--primary" :disabled="disabled3" :class="{'button--defalut': pkId === ''}" @click="_kzresend">下发</button>
                    <button class="button button--primary" :disabled="disabled1" :class="{'button--defalut': pkId === ''}" @click="toSure1('1')">强制完成</button>
                    <button class="button button--primary" :disabled="disabled2" :class="{'button--defalut': pkId === ''}" @click="toSure2('2')">强制取消</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>上料重量(g)</th>
                        <th>分配重量</th>
                        <th>任务状态</th>
                        <th>备注</th>
                        <th>执行步骤</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(e, i) in dataList" :key="e.task_id" :class="{'selected_icon': pkId === e.task_id}" @click="toRadio(e)">
                        <td>{{ i+1 }}</td>
                        <td>{{ e.create_time }}</td>
                        <td>{{ e.task_code }}</td>
                        <td>{{e.point_code1}}</td>
                        <td>{{e.point_code3}}</td>
                        <td>{{e.material_spec}}</td>
                        <td>{{e.material_qty | numeric}}</td>
                        <td>{{ e.qtyArr }}</td>
                        <td>{{ ['生成', '任务异常', '确定终点', '起点和终点确认', '下发', '运行', '完成', '取消'][Number(e.task_status) - 1] }}</td>
                        <td>{{ e.remark }}</td>
                        <td>{{ e.task_step }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <jxDialog
          ref="child"
          title="提示"
          :type="type"
          @toSure="toSureDialog"
          @toCancle="toCancle"
          >
          <div v-show="type === '1'" class="form_wraper">是否强制完成该任务？</div>
          <div v-show="type === '2'" class="form_wraper">是否强制取消该任务？</div>
        </jxDialog>
    </div>
</template>

<script>
import jxDialog from '@components/dialog.vue'
import {dictAll, KzTasks, operation, kzresend} from '@config/getData2.js'
export default {
  components: {
    jxDialog
  },
  data () {
    return {
      options1: [],
      value1: '',
      dataList: [],
      pkId: '',
      pkObj: {},
      disabled1: false,
      disabled2: false,
      disabled3: false,
      type: ''
    }
  },
  created () {
    this._dictAll()
  },
  methods: {
    // 车间下拉框
    async _dictAll () {
      let res = await dictAll()
      if (res.code === 200) {
        this.options1 = [...res.content]
        this.value1 = this.options1[0].value
        this._KzTasks()
      }
    },
    // grid
    async _KzTasks () {
      let res = await KzTasks(this.value1)
      if (res.code === 200) {
        this.pkId = ''
        this.pkObj = {}
        this.dataList = [...res.content]
      }
    },
    toRadio (e) {
      this.pkId = this.pkId === e.task_id ? '' : e.task_id
      this.pkObj = this.pkId === e.task_id ? e : {}
    },
    toSure1 (type) {
      this.disabled1 = true
      if (this.pkId === '') {
        this.disabled1 = false
        return
      }
      this.type = type
      this.$refs.child.active = true
    },
    toSure2 (type) {
      this.disabled2 = true
      if (this.pkId === '') {
        this.disabled2 = false
        return
      }
      this.type = type
      this.$refs.child.active = true
    },
    toSureDialog (type) {
      switch (type) {
        case '1':
          this._operation('forceFinish')
          break
        case '2':
          this._operation('cancel')
          break
      }
    },
    toCancle () {
      this.disabled1 = false
      this.disabled2 = false
    },
    async _operation (type) {
      try {
        let res = await operation(this.pkId, type)
        if (res.code === 200) {
          this.toast(res.msg)
          this._KzTasks()
        }
        this.disabled1 = false
        this.disabled2 = false
      } catch (e) {
        this.disabled1 = false
        this.disabled2 = false
        this.$refs.child.active = false
      }
    },
    // 重新下发
    async _kzresend () {
      if (this.pkId === '') {
        this.disabled3 = false
        return
      }
      try {
        let res = await kzresend(this.pkId)
        if (res.code === 200) {
          this.toast(res.msg)
          this._KzTasks()
        }
        this.disabled3 = false
      } catch (e) {
        this.disabled3 = false
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
</style>
