<template>
  <div v-for="row in rows" >
    <input type="checkbox" v-model=" piece[row.f_defect_field]" /><label>{{row.f_defect}}</label>
  </div>
  <div v-for="row in rows" >
    <div class="form-group" v-show="piece[row.f_defect_field]">
      <label class="col-xs-4 control-label">{{row.f_device_id ==''?row.f_device_type:''}}{{row.f_defect}}照片：</label>
      <div class="col-xs-8">
        <img-self  @click="takePic(row.f_defect_path ,row.f_device_id == '' ?row.f_device_type:''+ row.f_defect +'照片')"  :src="piece[row.f_defect_path]"  :alt="row.f_device_id =='' ?row.f_device_type:''+row.f_defect+'照片'" width="150" height="200"></img-self>
        &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile(row.f_defect_path, row.f_defect_path)"></span>
      </div>
    </div>
  </div>
</template>
<script>
  import Vue from 'vue'
  import co from 'co'
  import * as Util from '../Util'

  let saveGen =function * (self){
    if(Vue.android){
      let result= self.$androidUtil.bzLogic(`获取隐患信息`, {condition:"f_device_type = '"+self.f_device_type+"'"})
      self.rows = Object.assign({}, self.rows, result.data)
      console.log(self.piece,self.rows)
      if (self.piece == undefined) {
        if( self.$parent.$parent.data.f_global_defects == null ){
          self.$parent.$parent.data.f_global_defects = {}
        }
        self.$set('piece', self.$parent.$parent.data.f_global_defects)
      }
    }else {
      self.$androidUtil.bzLogic(`获取隐患信息`,{condition:"f_device_type = '"+self.f_device_type+"'"}, {
        resolveMsg: null,
        rejectMsg: null
      }).then((res) => {
        self.rows = Object.assign({}, self.rows, res.data.data)
        console.log(self.piece,self.rows)
        if (self.piece == undefined) {
          if( self.$parent.$parent.data.f_global_defects == null ){
            self.$parent.$parent.data.f_global_defects = {}
          }
          self.$set('piece', self.$parent.$parent.data.f_global_defects)
          console.log("piece",self.piece)
        }
      })
    }
    /*self.$watch('piece', function (val) {
      if(self.piece != null) {
        for (let i in self.rows) {
          if (!self.piece[self.rows[i].f_defect_path])
            self.piece[self.rows[i].f_defect_path] = Vue.nopic
        }
      }
    })*/
  }

  export default {
    title: '隐患信息',
    props: ['f_device_type', 'piece'],
    data() {
      return {
        rows: [],
      }
    },
    watch: {
      'f_device_type'() {
        let gen = saveGen(this)
        co(gen)
      }
    },
    methods: {
      cameraCallBack(prop, fileName) {
        console.log(prop + ',' + fileName)
        HostApp.__this__.$set('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
        let fileName
        if (!this.piece[prop] || this.piece[prop].includes("nopic.png")) {
          fileName = Util.guid() + '-' + this.f_device_type + '-' + 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('devices', {}, 'paper-router')
      },

      delfile(prop, fileName) {
        if (fileName == Vue.nopic)
          return
        else {
          HostApp.delfile(fileName)
          this.piece[prop] = Vue.nopic
        }
      }
    },
    ready() {
      if(this.f_device_type != undefined) {
        let gen = saveGen(this)
        co(gen)
      }
    }
  }
</script>
