<template>
  <section>
    <nav-bar :inner="true" title="保养结果填报"></nav-bar>
    <section class="content mgt186">
      <div class="filter-wraper">
        <search-box
        label="设备"
        v-model="val1"
        @handleChange="handleChange1"
        ></search-box>
        <div class="bottom-filter-tip">
          <div class="filter-label txtjustify">保养单</div>
          <div class="fxcol mgl20">
            <input type="text" class="filter-input filter-scan-input" placeholder="" v-model="val2">
          </div>
        </div>
      </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_dtl_id">
              <td>
                <for-dropdown-menu
                :value="e.isfinish"
                :id="e.maint_dtl_id"
                @getValue="getValue">
                </for-dropdown-menu>
              </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_dtl_id" @click="toCheck(e)" :class="{'checked': e.maint_dtl_id === pkId}">
              <td>{{e.maint_item_name}}</td>
              <td>{{e.item_level}}</td>
              <td>{{e.contents}}</td>
              <td>{{e.requirement}}</td>
            </tr>
          </table>
        </div>
      </div>
    </section>
    <section class="submit-bar">
      <button class="btn submit-button" :class="{'btn-disabled': dataList.length === 0}" :disabled="disabled1" @click="toSure">确认</button>
      <button class="btn submit-button" @click="toCancle">取消</button>
    </section>
  </section>
</template>

<script>
import NavBar from '@components/NavBar.vue'
import SearchBox from '@components/SearchBox.vue'
import ForDropdownMenu from '@components/ForDropdownMenu.vue'
import {queryMaintenanceDtl, dtlConfirm} from '@config/getData2.js'
export default {
  name: 'MaintainResults',
  components: {
    NavBar,
    SearchBox,
    ForDropdownMenu
  },
  data () {
    return {
      val1: JSON.stringify(this.$store.getters.materObj) !== '{}' ? this.$store.getters.materObj.device_code : '',
      val2: JSON.stringify(this.$store.getters.materObj) !== '{}' ? this.$store.getters.materObj.maint_code : '',
      dataList: [],
      pkId: '',
      pkObj: {},
      disabled1: false
    }
  },
  mounted () {
    this._queryMaintenanceDtl()
  },
  methods: {
    handleChange1 (e, type) {
      if (type) {
        console.log(e)
      }
    },
    toCheck (e) {
      this.pkId = this.pkId === e.maint_dtl_id ? '' : e.maint_dtl_id
      this.pkObj = this.pkId === e.maint_dtl_id ? e : {}
    },
    getValue (p) {
      this.dataList.map(el => {
        if (el.maint_dtl_id === p[0]) {
          el.isfinish = p[1]
        }
      })
    },
    /** 明细查询 */
    async _queryMaintenanceDtl () {
      let res = await queryMaintenanceDtl(this.$store.getters.materObj)
      if (res.code === '1') {
        this.dataList = [...res.content.rows]
      } else {
        this.Dialog(res.desc)
      }
    },
    async _dtlConfirm () {
      try {
        let res = await dtlConfirm(this.dataList)
        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
      }
    },
    toSure () {
      this.disabled1 = true
      if (!this.dataList.length) {
        this.disabled1 = false
        return
      }
      this._dtlConfirm()
    },
    toCancle () {
    }
  }
}
</script>

<style lang="stylus" scoped>
.mgb140
  margin-bottom 1.4rem
</style>
