<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_plumb_normal">正常</input>
            <input type="checkbox" v-model="piece.f_plumb_leakage">漏气</input>
            <input type="checkbox" v-model="piece.f_plumb_viaroom">穿客厅</input>
            <input type="checkbox" v-model="piece.f_plumb_viabedroom">穿卧室</input>
            <input type="checkbox" v-model="piece.f_plumb_nearappliance">与电器过近</input>
            <input type="checkbox" v-model="piece.f_plumb_wrapped">暗封</input>
            <input type="checkbox" v-model="piece.f_plumb_reroute">私改</input>
            <input type="checkbox" v-model="piece.f_plumb_kitchen_misuse">厨房他用</input>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_plumb_viaroom">
        <label class="col-sm-12 control-label">户内管穿客厅照片：</label>
        <div class="col-sm-4">
          <img-self :class="paper.style__" @click="takePic('f_viaroom_path', '户内管穿客厅照片')"  :src="piece.f_viaroom_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_viaroom_path', this.piece.f_viaroom_path)"></span>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_plumb_viabedroom">
        <label class="col-sm-12 control-label">户内管穿卧室照片：</label>
        <div class="col-sm-4">
          <img-self :class="paper.style__" @click="takePic('f_viabedroom_path', '户内管穿卧室照片')"  :src="piece.f_viabedroom_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_viabedroom_path', this.piece.f_viabedroom_path)"></span>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_plumb_nearappliance">
        <label class="col-sm-12 control-label">与电器过近照片：</label>
        <div class="col-sm-4">
          <img-self :class="paper.style__" @click="takePic('f_plumb_nearappliance_path', '与电器过近照片')"  :src="piece.f_plumb_nearappliance_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_plumb_nearappliance_path', this.piece.f_plumb_nearappliance_path)"></span>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_plumb_wrapped">
        <label class="col-sm-12 control-label">户内管暗封照片：</label>
        <div class="col-sm-4">
          <img-self :class="paper.style__" @click="takePic('f_wrapped_path', '户内管暗封照片')"  :src="piece.f_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_wrapped_path', this.piece.f_wrapped_path)"></span>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_plumb_reroute">
        <label class="col-sm-12 control-label">户内管私改照片：</label>
        <div class="col-sm-4">
          <img-self :class="paper.style__" @click="takePic('f_reroute_path', '户内管私改照片')"  :src="piece.f_reroute_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_reroute_path', this.piece.f_reroute_path)"></span>
        </div>
      </div>
      <div class="form-group" v-show="piece.f_plumb_kitchen_misuse">
        <label class="col-sm-12 control-label">户内管厨房他用照片：</label>
        <div class="col-sm-4">
          <img-self :class="paper.style__" @click="takePic('f_misuse_path', '户内管厨房他用照片')"  :src="piece.f_misuse_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_misuse_path', this.piece.f_misuse_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.vue'
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_inner_plumbs[this.idx])
    if(!this.piece.f_viaroom_path)
      this.$set('piece.f_viaroom_path', Vue.nopic)
    this.$watch("piece.f_plumb_viaroom", function(val){
      if(!val)
        this.$set('piece.f_viaroom_path', Vue.nopic)
    })
    if(!this.piece.f_viabedroom_path)
      this.$set('piece.f_viabedroom_path', Vue.nopic)
    this.$watch("piece.f_plumb_viabedroom", function(val){
      if(!val)
        this.$set('piece.f_viabedroom_path', Vue.nopic)
    })
    if(!this.piece.f_plumb_nearappliance_path)
      this.$set('piece.f_plumb_nearappliance_path', Vue.nopic)
    this.$watch("piece.f_plumb_nearappliance_path", function(val){
      if(!val)
        this.$set('piece.f_plumb_nearappliance_path', Vue.nopic)
    })
    if(!this.piece.f_wrapped_path)
      this.$set('piece.f_wrapped_path', Vue.nopic)
    this.$watch("piece.f_plumb_wrapped", function(val){
      if(!val)
        this.$set('piece.f_wrapped_path', Vue.nopic)
    })
    if(!this.piece.f_reroute_path)
      this.$set('piece.f_reroute_path', Vue.nopic)
    this.$watch("piece.f_plumb_reroute", function(val){
      if(!val)
        this.$set('piece.f_reroute_path', Vue.nopic)
    })
    if(!this.piece.f_misuse_path)
      this.$set('piece.f_misuse_path', Vue.nopic)
    this.$watch("piece.f_plumb_kitchen_misuse", function(val){
      if(!val)
        this.$set('piece.f_misuse_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__.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() +'-ilumb-' + 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>
