<template>
  <div class="order-wraper">
      <div class="wrap-filter-buttons">
          <div class="wrap-filters">
              <div class="filter_item">
                  <div class="filter_label">工单日期</div>
                  <div class="filter-input-wrap">
                      <el-date-picker
                        v-model="value1"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
                      </el-date-picker>
                  </div>
              </div>
              <div class="filter_item">
                  <div class="filter_label filter_label_z2">设备</div>
                  <div class="filter-input-wrap filter-input-wrap_z2">
                    <el-select v-model="value" filterable placeholder="请选择">
                      <el-option
                          v-for="item in options"
                          :key="item.device_code"
                          :label="item.device_name"
                          :value="item.device_code">
                      </el-option>
                    </el-select>
                  </div>
              </div>
              <div class="filter_item">
                  <div class="filter_label filter_label_z3">关键字</div>
                  <div class="filter-input-wrap filter-input-wrap_z3">
                  <input type="text" class="filter-input filter-input_1" v-model="keyValue" placeholder="工单号、物料编码">
                  <i v-show="closeIcon1" class="iconfont close_icon" @click="clearData(1)"></i>
                  </div>
              </div>
          </div>
          <div class="wrap-buttons">
              <button class="button button--primary" @click="getDatas">查询</button>
              <button class="button button--primary" :class="{'button--defalut': pkId === ''}" :disabled="disabled1" @click="showDialog">残次品报工</button>
          </div>
      </div>
      <div class="grid_wraper">
          <table class="filter-table">
              <tr>
                  <!-- <th width="4%"></th> -->
                  <th width="8%">工单日期</th>
                  <th width="8%">工单号</th>
                  <th width="7%">设备</th>
                  <th width="5%">状态</th>
                  <th width="9%">物料名称</th>
                  <th width="8%">物料规格</th>
                  <th width="5%">工序</th>
                  <th width="8%">工单数量</th>
                  <th width="8%">实际数量</th>
                  <th width="8%">报废数量</th>
                  <th width="8%">报修数量</th>
                  <th width="9%">开始时间</th>
                  <th width="9%">开始时间</th>
              </tr>
              <tr v-for="e in dataList" :key="e.workorder_id" :class="{'selected_icon': pkId === e.workorder_id}" @click="toRadio(e)">
                  <!-- <td>
                    <button class="iconfont select_icon" :class="{'selected_icon': pkId === e.workorder_id}" @click="toRadio(e)"></button>
                  </td> -->
                  <td>{{e.create_time}}</td>
                  <td>{{e.workorder_code}}</td>
                  <td>{{e.device_code}}</td>
                  <td>{{e.workorder_status_name}}</td>
                  <td>{{ e.material_name }}</td>
                  <td>{{e.material_spec}}</td>
                  <td>{{e.workprocedure_name}}</td>
                  <td>{{e.plan_qty}}</td>
                  <td>{{ e.real_qty }}</td>
                  <td>{{e.nok_qty}}</td>
                  <td>{{e.repare_qty}}</td>
                  <td>{{ e.realproducestart_date }}</td>
                  <td>{{ e.realproduceend_date }}</td>
              </tr>
          </table>
      </div>
      <jxDialog
        ref="child"
        title="请输入数量"
        @toSure="toSureDialog"
        >
        <div class="form_wraper">
          <div class="form">
            <div class="form_item">
              <div class="form_item__label">报废数量</div>
              <div class="form_item__content">
                <input type="number" class="form_item__input" v-model="nokQty">
              </div>
            </div>
            <div class="form_item">
              <div class="form_item__label">报修数量</div>
              <div class="form_item__content">
                <input type="number" class="form_item__input" v-model="repareQty">
              </div>
            </div>
          </div>
        </div>
      </jxDialog>
  </div>
</template>

<script>
import {dateFtt} from '@config/utils.js'
import jxDialog from '@components/dialog.vue'
import { deviceList, getTable, unqualReport, orderStatus } from '../../../config/getData2.js'
export default {
  components: {
    jxDialog
  },
  data () {
    return {
      value1: [],
      options: [],
      value: '',
      keyValue: '',
      disabled1: false,
      dataList: [],
      pkId: '',
      pkObj: {},
      nokQty: '',
      repareQty: '',
      status: []
    }
  },
  computed: {
    closeIcon1 () {
      return this.keyValue !== ''
    }
  },
  watch: {
    keyValue () {
      this.debouncedgetDatas()
    }
  },
  created () {
    this._deviceList()
    this.debouncedgetDatas = this.debounce(this.getDatas, 500)
    this._orderStatus()
  },
  methods: {
    debounce (fn, delay = 500) {
      let timer = null
      return function () {
        if (timer) {
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          fn.apply(this, arguments)
          timer = null
        }, delay)
      }
    },
    async _deviceList () {
      let res = await deviceList()
      if (res.code === 200) {
        this.options = [...res.content]
      }
    },
    async _orderStatus () {
      let res = await orderStatus()
      this.status = [...res]
      this.getDatas()
    },
    async getDatas () {
      let res = await getTable(this.value, this.keyValue, this.value1 !== null ? dateFtt(this.value1[0]) : '', this.value1 !== null ? dateFtt(this.value1[1]) : '')
      if (res.code === 200) {
        res.content.map(el => {
          this.status.map(e => {
            if (e.value === Number(el.workorder_status)) {
              this.$set(el, 'workorder_status_name', e.label)
            }
          })
        })
        this.dataList = [...res.content]
      }
      this.dataList = [...res.content]
    },
    clearData (e) {
      switch (e) {
        case 1:
          this.keyValue = ''
          break
      }
    },
    toRadio (e) {
      this.pkId = this.pkId === e.workorder_id ? '' : e.workorder_id
      this.pkObj = this.pkId === e.workorder_id ? e : {}
    },
    showDialog () {
      if (!this.pkId) {
        this.toast('请选择一行')
        return
      }
      this.nokQty = ''
      this.repareQty = ''
      this.$refs.child.active = true
    },
    toSureDialog () {
      this._unqualReport()
    },
    // 残次品报工
    async _unqualReport () {
      this.$refs.child.disabled = true
      this.disabled1 = true
      if (!this.pkId) {
        this.toast('请选择一行')
        this.disabled1 = false
        return
      }
      try {
        let res = await unqualReport(this.pkId, this.nokQty, this.repareQty)
        this.toast(res.message)
        this.disabled1 = false
        this.pkId = ''
        this.pkObj = {}
        this.$refs.child.active = false
        this.$refs.child.disabled = false
        this.getDatas()
      } catch (e) {
        this.disabled1 = false
        this.$refs.child.active = false
        this.$refs.child.disabled = false
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.wrap-filters
  width calc(100% - 160px)
.wrap-buttons
  width 160px
.filter_label_z2
  width 32px
.filter-input-wrap_z2
  width calc(100% - 32px)
.filter_label_z3
  width 43px
.filter-input-wrap_z3
  width calc(100% - 43px)
.filter_item
  &:nth-child(1)
    width 37%
  &:nth-child(2)
    width calc(30% - 10px)
  &:nth-child(3)
    width calc(33% - 10px)
.filter-input_1
  padding-right 30px
.close_icon
  width 20px
  height 20px
  font-size 15px
  line-height 20px
  top 5px
  right 10px
</style>
