<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_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>
      <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_cooker_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_terminal_normal">正常</input>
            <input type="checkbox" v-model="piece.f_valve_terminal_leakage">漏气</input>
            <input type="checkbox" v-model="piece.f_valve_terminal_wrapped">包裹</input>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_valve_terminal_wrapped">
        <label class="col-sm-12 control-label">截止阀包裹照片：</label>
        <div class="col-sm-4">
          <img-self  @click="takePic('f_terminal_wrapped_path', '截止阀包裹照片')"  :src="piece.f_terminal_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_terminal_wrapped_path', this.piece.f_terminal_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__">
            <input type="checkbox" v-model="piece.f_valve_magnetic_normal">正常</input>
            <input type="checkbox" v-model="piece.f_valve_magnetic_leakage">漏气</input>
            <input type="checkbox" v-model="piece.f_valve_magnetic_wrapped">包裹</input>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_valve_magnetic_wrapped">
        <label class="col-sm-12 control-label">电磁阀包裹照片：</label>
        <div class="col-sm-4">
          <img-self  @click="takePic('f_magnetic_wrapped_path', '电磁阀包裹照片')"  :src="piece.f_magnetic_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_magnetic_wrapped_path', this.piece.f_magnetic_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,
      piece:{f_valve_meter_normal:'',f_valve_meter_leakage:'',f_valve_meter_wrapped:'',f_valve_meter_on:'',f_valve_cooker_normal:'',f_valve_cooker_leakage:'',f_valve_magnetic_normal:'',
        f_valve_cooker_wrapped:'',f_valve_cooker_on:'',f_valve_terminal_normal:'',f_valve_terminal_leakage:'',f_valve_terminal_wrapped:'',f_valve_magnetic_leakage:'',f_valve_magnetic_wrapped:'',
        f_meter_wrapped_path:'',f_cooker_wrapped_path:'',f_terminal_wrapped_path:'',f_magnetic_wrapped_path:''}
    }
  },
  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_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)
    })
    if(!this.piece.f_terminal_wrapped_path)
      this.$set('piece.f_terminal_wrapped_path', Vue.nopic)
    this.$watch("piece.f_valve_terminal_wrapped", function(val){
      if(!val)
        this.$set('piece.f_terminal_wrapped_path', Vue.nopic)
    })
    if(!this.piece.f_magnetic_wrapped_path)
      this.$set('piece.f_magnetic_wrapped_path', Vue.nopic)
    this.$watch("piece.f_valve_magnetic_wrapped", function(val){
      if(!val)
        this.$set('piece.f_magnetic_wrapped_path', Vue.nopic)
    })
  },
  methods: {
    cameraCallBack(prop, fileName) {
      console.log(prop + ',' + fileName)
      HostApp.__this__.piece[prop] = fileName + '?' + Math.random()
      console.log(HostApp.__this__.piece[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-noncivil', {}, 'paper-router')
    },
    delfile(prop, fileName) {
      if(fileName == Vue.nopic)
        return
      else {
        HostApp.delfile(fileName)
        this.piece[prop] = Vue.nopic
      }
    }
  }
}
</script>
