<template>
  <section>
    <nav-bar title="设备保养执行-维修工"></nav-bar>
    <section class="content mgt186">
      <div class="filter-wraper">
        <search-box
        label="设备"
        v-model="val1"
        @handleChange="handleChange1"
        ></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.maint_id" @click="toCheck(e)" :class="{'checked': e.maint_id === pkId}">
              <td>{{e.maint_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.maint_id" @click="toCheck(e)" :class="{'checked': e.maint_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 === ''}" @click="toJump">填报</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 { queryMaintenance, maintOpeate } from '@config/getData2.js'
export default {
  name: 'MaintainExecution',
  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 () {
    this._queryMaintenance(this.val1)
  },
  methods: {
    handleChange1 (e, type) {
      if (type) {
        this._queryMaintenance(e)
      }
    },
    toCheck (e) {
      this.pkId = this.pkId === e.maint_id ? '' : e.maint_id
      this.pkObj = this.pkId === e.maint_id ? e : {}
    },
    /** 设备查询 */
    async _queryMaintenance (e) {
      let res = await queryMaintenance(e, '1')
      if (res.code === '1') {
        this.dataList = [...res.content.rows]
      } else {
        this.Dialog(res.desc)
      }
    },
    async _maintOpeate (type) {
      try {
        let res = await maintOpeate(this.pkObj, type)
        if (res.code === '1') {
          this.toast(res.desc)
        } else {
          this.Dialog(res.desc)
        }
        this.disabled1 = false
        this.disabled2 = false
      } catch (e) {
        this.disabled1 = false
        this.disabled2 = false
      }
    },
    toSure1 () {
      this.disabled1 = true
      if (!this.pkId) {
        this.disabled1 = false
        return
      }
      this._maintOpeate('1')
    },
    toSure2 () {
      this.disabled2 = true
      if (!this.pkId) {
        this.disabled2 = false
        return
      }
      this._maintOpeate('2')
    },
    toJump () {
      if (this.pkId) {
        this.$store.dispatch('materObj', this.pkObj)
        this.$router.push({
          path: '/MaintainResults'
        })
      }
    }
  }
}
</script>
