<template>
  <section>
    <nav-bar title="设备维修确认"></nav-bar>
    <section class="content mgt186">
      <div class="filter-wraper">
        <search-box
        label="设备"
        v-model="val1"
        @handleChange="handleChange1"
        :seaShow="false"
        ></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.repair_id" @click="toCheck(e)" :class="{'checked': e.repair_id === pkId}">
              <td @click="toPage(e)">{{e.repair_code}}</td>
            </tr>
          </table>
        </div>
        <div class="slide">
          <table class="layout-t">
            <tr>
              <th>设备编号</th>
              <th>设备名称</th>
              <th>状态</th>
              <th>维修日期</th>
            </tr>
            <tr v-for="e in dataList" :key="e.repair_id" @click="toCheck(e)" :class="{'checked': e.repair_id === pkId}">
              <td>{{e.device_code}}</td>
              <td>{{e.device_name}}</td>
              <td>{{e.status_name}}</td>
              <td>{{e.plan_start_date}}</td>
            </tr>
          </table>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled1" @click="toSure1">通过</button>
      <button class="btn submit-button" :class="{'btn-disabled': pkId === ''}" :disabled="disabled2" @click="toSure2">不通过</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import { queryRepairs, repairOpeate } from '@config/getData2.js'
export default {
  name: 'EquipRepairConfirm',
  components: {
    NavBar,
    SearchBox
  },
  data () {
    return {
      val1: '',
      dataList: [],
      pkId: '',
      pkObj: {},
      disabled1: false,
      disabled2: false
    }
  },
  beforeRouteLeave (to, from, next) {
    if (to.path === '/home' || to.path === '/login') {
      this.$store.dispatch('setKeepAlive', [])
    }
    next()
  },
  activated () {},
  methods: {
    handleChange1 (e, type) {
      if (type) {
        this._queryRepairs(e)
      }
    },
    toCheck (e) {
      this.pkId = this.pkId === e.repair_id ? '' : e.repair_id
      this.pkObj = this.pkId === e.repair_id ? e : {}
    },
    /** 设备查询 */
    async _queryRepairs (e) {
      let res = await queryRepairs(e, '2')
      if (res.code === '1') {
        this.dataList = [...res.content.rows]
      } else {
        this.Dialog(res.desc)
      }
    },
    async _repairOpeate (type) {
      try {
        let res = await repairOpeate(this.pkObj, type)
        if (res.code === '1') {
          this.toast(res.desc)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled1 = false
        this.disabled2 = false
        this.disabled3 = false
      } catch (e) {
        this.disabled1 = false
        this.disabled2 = false
        this.disabled3 = false
      }
    },
    toSure1 () {
      this.disabled1 = true
      if (!this.pkId) {
        this.disabled1 = false
        return
      }
      this._repairOpeate('4')
    },
    toSure2 () {
      this.disabled2 = true
      if (!this.pkId) {
        this.disabled2 = false
        return
      }
      this._repairOpeate('5')
    },
    toPage (e) {
      this.$store.dispatch('materObj', e)
      this.$router.push({
        path: '/MaintainDetails'
      })
    }
  }
}
</script>
