<template>
  <div>
    <form>
      <div class="form-group">
        <label class="col-sm-12 control-label">表前阀</label>
        <div class="col-sm-12" :class="paper.style__">
            <input type="checkbox" v-model="piece.f_valve_meter_normal">正常</input>
            <input type="checkbox" v-model="piece.f_valve_meter_leakage">漏气</input>
            <input type="checkbox" v-model="piece.f_valve_meter_wrapped">包裹</input>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_valve_meter_wrapped">
        <label class="col-sm-12 control-label">表前阀包裹照片：</label>
        <div class="col-sm-4">
          <img-self  @click="takePic('f_meter_wrapped_path', '表前阀包裹照片')"  :src="piece.f_meter_wrapped_path" alt="表前阀包裹照片" width="150" height="200"></img-self>
        </div>
        <div class="col-sm-8">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_meter_wrapped_path', this.piece.f_meter_wrapped_path)"></span>
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-12 control-label">开关状态</label>
        <div class="col-sm-12" :class="paper.style__">
          <radio-group :value.sync="piece.f_valve_meter_on" type="success" buttons="false">
            <radio value="开">开</radio>
            <radio value="关">关</radio>
          </radio-group>
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-12 control-label">自闭阀</label>
        <div class="col-sm-12" :class="paper.style__">
            <input type="checkbox" v-model="piece.f_valve_auto_normal">正常</input>
            <input type="checkbox" v-model="piece.f_valve_auto_leakage">漏气</input>
            <input type="checkbox" v-model="piece.f_valve_auto_wrapped">包裹</input>
            <input type="checkbox" v-model="piece.f_valve_auto_damaged">损坏</input>
            <input type="checkbox" v-model="piece.f_valve_auto_non">未安装</input>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_valve_auto_wrapped">
        <label class="col-sm-12 control-label">自闭阀包裹照片：</label>
        <div class="col-sm-4">
          <img-self  @click="takePic('f_auto_wrapped_path', '自闭阀包裹照片')"  :src="piece.f_auto_wrapped_path" alt="自闭阀包裹照片" width="150" height="200"> </img-self>
        </div>
        <div class="col-sm-8">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_auto_wrapped_path', this.piece.f_auto_wrapped_path)"></span>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_valve_auto_damaged">
        <label class="col-sm-12 control-label">自闭阀损坏照片：</label>
        <div class="col-sm-4">
          <img-self  @click="takePic('f_auto_damaged_path', '自闭阀损坏照片')"  :src="piece.f_auto_damaged_path" alt="自闭阀损坏照片" width="150" height="200"></img-self>
        </div>
        <div class="col-sm-8">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_auto_damaged_path', this.piece.f_auto_damaged_path)"></span>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_valve_auto_non">
        <label class="col-sm-12 control-label">自闭阀未安装照片：</label>
        <div class="col-sm-4">
          <img-self  @click="takePic('f_auto_non_path', '自闭阀未安装照片')"  :src="piece.f_auto_non_path" alt="自闭阀未安装照片" width="150" height="200"> </img-self>
        </div>
        <div class="col-sm-8">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_auto_non_path', this.piece.f_auto_non_path)"></span>
        </div>
      </div>
      <div class="form-group">
        <label class="col-sm-12 control-label">灶前阀</label>
        <div class="col-sm-12" :class="paper.style__">
            <input type="checkbox" v-model="piece.f_valve_cooker_normal">正常</input>
            <input type="checkbox" v-model="piece.f_valve_cooker_leakage">漏气</input>
            <input type="checkbox" v-model="piece.f_valve_cooker_wrapped">包裹</input>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_valve_cooker_wrapped">
        <label class="col-sm-12 control-label">灶前阀包裹照片：</label>
        <div class="col-sm-4">
          <img-self  @click="takePic('f_cooker_wrapped_path', '灶前阀包裹照片')"  :src="piece.f_cooker_wrapped_path" alt="灶前阀包裹照片" width="150" height="200"></img-self>
        </div>
        <div class="col-sm-8">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_cooker_wrapped_path', this.piece.f_cooker_wrapped_path)"></span>
        </div>
      </div>
    </form>
    <a href="#" class="btn btn-primary" @click="goBackValidation">确定</a>
    <div style="height:50px;"></div>
  </div>
</template>

<script>
import Vue from 'vue'
import * as Util from '../Util'
import RadioGroup from 'vue-client/src/vue-strap/src/radioGroup'
import Radio from 'vue-client/src/vue-strap/src/radioBtn'

export default {
  title: '阀门',
  data() {
    return {
      paper: this.$parent.$parent.data
    }
  },
  props: ['idx'],
  components: {
    RadioGroup,
    Radio
  },
  ready() {
    this.$set('piece', this.paper.f_valves[this.idx])
    if(!this.piece.f_meter_wrapped_path)
      this.$set('piece.f_meter_wrapped_path', Vue.nopic)
    this.$watch("piece.f_valve_meter_wrapped", function(val){
      if(!val)
        this.$set('piece.f_meter_wrapped_path', Vue.nopic)
    })
    if(!this.piece.f_auto_wrapped_path)
      this.$set('piece.f_auto_wrapped_path', Vue.nopic)
    this.$watch("piece.f_valve_auto_wrapped", function(val){
      if(!val)
        this.$set('piece.f_auto_wrapped_path', Vue.nopic)
    })
    if(!this.piece.f_auto_damaged_path)
      this.$set('piece.f_auto_damaged_path', Vue.nopic)
    this.$watch("piece.f_valve_auto_damaged", function(val){
      if(!val)
        this.$set('piece.f_auto_damaged_path', Vue.nopic)
    })
    if(!this.piece.f_auto_non_path)
      this.$set('piece.f_auto_non_path', Vue.nopic)
    this.$watch("piece.f_valve_auto_non", function(val){
      if(!val)
        this.$set('piece.f_auto_non_path', Vue.nopic)
    })
    if(!this.piece.f_cooker_wrapped_path)
      this.$set('piece.f_cooker_wrapped_path', Vue.nopic)
    this.$watch("piece.f_valve_cooker_wrapped", function(val){
      if(!val)
        this.$set('piece.f_cooker_wrapped_path', Vue.nopic)
    })
  },
  methods: {
    cameraCallBack(prop, fileName) {
      console.log(prop + ',' + fileName)
      //HostApp.__this__.$set(prop, fileName) doesnt work
      HostApp.__this__.piece[prop] = fileName + '?' + Math.random()
      console.log(HostApp.__this__.meter[prop])
      HostApp.__callback__ = null
      HostApp.__this__ = null
    },
    takePic(prop, title) {
      HostApp.__callback__ = this.cameraCallBack
      HostApp.__this__ = this
      console.log('this.piece[prop]:' + this.piece[prop])
      let fileName
      if(!this.piece[prop] || this.piece[prop].includes("nopic.png")) {
        fileName = Util.guid() +'-valve-' + this.idx + '-' + prop + '.jpg'
      } else {
        fileName = Util.getFileName(this.piece[prop])
      }
      HostApp._open_a_page({type: 'boomerang',
        page: 'com.aofeng.hybrid.android.peripheral.CameraActivity',
        param: {file: fileName, requestCode: 111, callback:'javascript:HostApp.__callback__("' + prop +'", "%s");', watermark: title + '\t时间：'+ Util.toStandardTimeString() + '\t' + Vue.user.name}
      })
    },
    goBackValidation() {
      this.$goto('paper-device', {}, 'paper-router')
    },
    delfile(prop, fileName) {
      if(fileName == Vue.nopic)
        return
      else {
        HostApp.delfile(fileName)
        this.piece[prop] = Vue.nopic
      }
    }
  }
}
</script>
