<template>
  <div>
    <validator name='v'>
      <a href="#" class="btn btn-primary" @click="this.$back('items')">&lt;&lt;</a>
      <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
        <div class="col-sm-4 form-group form-input-group" style="width: 20%">
          <label class="col-sm-2 control-lable">审核人：</label>
          <div class="col-sm-8">
            <input readonly='readonly' class="form-control" style="height:34px;margin-top: 8px" type="text" v-model="item.f_approved_by" v-validate:f_approved_by='{ required: true }' />
          </div>
        </div>
        <div class="col-sm-4 form-group form-input-group" style="width: 20%">
          <label class="col-sm-2 control-lable">审核结果：</label>
          <div class="col-sm-8">
            <input readonly='readonly' class="form-control" style="height:34px;margin-top: 8px" type="text" v-model="item.f_approved" v-validate:f_approved='{ required: true }' />
          </div>
        </div>
        <div class="col-sm-4 form-group form-input-group" style="width: 20%">
          <label class="col-sm-2 control-lable">审核时间：</label>
          <div class="col-sm-8">
            <input readonly='readonly' class="form-control" style="height:34px;margin-top: 8px" type="text" v-model="item.f_approved_time"/>
          </div>
        </div>
        <div class="col-sm-4 form-group form-input-group" style="width: 20%">
          <label class="col-sm-2 control-lable">审核备注：</label>
          <div class="col-sm-8">
            <textarea class="form-control" type="text" v-model="item.f_approved_note"></textarea>
          </div>
        </div>
        <div class="col-sm-4 form-group form-input-group" style="width: 20%">
          <button :disabled="(item.f_approved != '未审核' && item.f_approved != '重检')" type="button" class="btn btn-primary col-sm-3" @click="approve">
            <span class="glyphicon glyphicon-ok"></span> 通过
          </button>
          <button :disabled="(item.f_approved != '未审核' && item.f_approved != '重检')" type="button" class="btn btn-primary col-sm-3" @click="disapprove">
            <span class="glyphicon glyphicon-remove"></span> 打回
          </button>
        </div>
      </div>
    </validator>
  </div>

</template>

<script>
  import * as Util from '../Util'
  import co from 'co'
  import Vue from 'vue'

  let syncApprove = function * (self) {
    yield self.$resetpost('rs/logic/ApprovePaper', {data:{item: self.item}}, {resolveMsg: '操作成功！', rejectMsg: '操作失败！'}).then(
      (res)=>{
      if(res.data.code == 200){
      alert("安检单审核通过!")
      self.$back('items')
    }
  }
  )
  }

  let syncDisapprove = function * (self) {
    yield self.$resetpost('rs/logic/DisapprovePaper', {data:{item: self.item}}, {resolveMsg: '操作成功！', rejectMsg: '操作失败！'}).then((res)=> {
      if (res.data.code == 200) {
      alert("安检单已打回!")
      self.$back('items')
    }
  })
  }

  let noPlanDisapprove = function * (self) {
    yield self.$resetpost('rs/logic/NoPlanDisapprove', {data: self.item}, {resolveMsg: '操作成功！', rejectMsg: '操作失败！'}).then((res)=> {
      if (res.data.code == 200) {
      alert("安检单已打回!")
      self.$back('items')
    }
  })
  }
  export default {
    title: '安检单审核',
    props: ['item', 'name'],
    ready () {
      if(this.item.f_approved == '未审核') {
        this.item.f_approved_time = Util.toStandardTimeString()
        this.item.f_approved_by = this.$login.f.name
      }
    },
    methods: {
      approve () {
        this.item.f_approved = '已审核'
        let gen = syncApprove(this)
        return co(gen)
      },
      disapprove () {
        if(this.item.f_no_checkplan == '无计划安检'){
          this.item.f_approved = '打回'
          let gen = noPlanDisapprove(this)
          return co(gen)
        }
        else {
          this.item.f_approved = '打回'
          let gen = syncDisapprove(this)
          return co(gen)
        }
      },
    }
  }

</script>
