<template>
  <div v-for="dev in devices" style="width: 95%;margin: auto" >
    <div class="flex panel panel-default well" style="width: 100%;display: inline-block">
      <div class="panel-body bg-info">
        <div class="text-center">
          <label class="control-label" style="font-weight: bold">{{dev.device}}<span  v-if="dev.checkmust">*</span>{{'['+paper['device'+ dev.n].length+']'}}</label>
          <span :class="style__" class="glyphicon glyphicon-plus" @click="addDevice(dev)" v-if="dev.device!='燃气表' && role != 'androidview'"></span>
          <ul class="list-unstyled panel-ul">
            <li
              :id="'vc-list-'+attach+'-'+$index"
              v-for="row in paper['device'+ dev.n]"
              @click="select(row)">
              <!--v-bind:class="{ 'bg-success': isSelected(row) }"-->
              <div partial class="panel panel-default well" style="padding: 0px">
                <div class="panel-body inbg">
                  <div class="row form-group">
                    <div class="row">
                      <span class="li-font" v-for="items in row.f_items" v-if=" items.f_item_value && items.f_item_value != '' ">
                        {{items.f_item_name}}:{{items.f_item_value}}&nbsp;&nbsp;&nbsp;
                      </span>
                      <div class="text-right">
                        <img style="width: 20px" src="../../assets/remove.png" :class="style__" @click="removeDevice(dev,row)" v-if="dev.device!='燃气表' &&(role != 'androidview' || upload_state !='已传') "></img>
                        <img style="width: 20px" src="../../assets/edit.png" @click="editDevice(dev,$index)"></img>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <partial name="default"></partial>
            </li>
          </ul>

        <!--<div v-for="row in paper['device'+ dev.n]">-->
          <!--{{$index+1}}.-->
          <!--<span v-for="items in row.f_items" v-if="items.f_item_value != ''">{{items.f_item_name}}:{{items.f_item_value}}；&nbsp;</span>-->

        <!--</div>-->
      </div>
    </div>
    <!--    <table class="table table-bordered">
          <tr v-for="dev in devices">
            <td>
              <table class="table">
                <thead class="thead-inverse">
                <th colspan="2">
                  {{dev.device}} {{'['+paper['device'+ dev.n].length+']'}}&nbsp;&nbsp;&nbsp;&nbsp;<span :class="style__" class="glyphicon glyphicon-plus" @click="addDevice(dev)"></span>
                </th>
                </thead>
                <tbody v-for="row in paper['device'+ dev.n]">
                <tr>
                  <td>{{$index+1}}.</td>
                  <td>
                    &nbsp;&nbsp;&nbsp;&nbsp;<span :class="style__" class="glyphicon glyphicon-remove" @click="removeDevice(dev,row)"></span>
                    &nbsp;&nbsp;&nbsp;&nbsp;<span class="glyphicon glyphicon-th-list" @click="editDevice(dev,$index)"></span>
                  </td>
                </tr>
                </tbody>
              </table>
            </td>
          </tr>
        </table>-->

  </div>
  <div v-if='hasDevice' style="width: 95%;margin: auto" >
    <div class="flex panel panel-default well" style="width: 100%;display: inline-block">
      <div class="panel-body bg-info">
        <div class="text-center">
          <label class="control-label" style="font-weight: bold">安检结果统计</label>
        </div>
        <ul class="list-unstyled panel-ul">
          <li >
            <div partial class="panel panel-default well" style="padding: 0px">
              <div class="panel-body inbg">
                <p style="margin-bottom: 5px">
                   <span class="li-font">
                       安检结果:{{result}}
                      </span>
                </p>
                <div class="row form-group" v-for="deviceCollect in deviceCollects">
                  <div class="row" >
                      <span class="li-font">
                        {{deviceCollect.name}}:{{deviceCollect.value}}&nbsp;&nbsp;&nbsp;
                      </span>
                  </div>
                  <div class="row" v-if="deviceCollect.value == '有隐患'">
                    <lable>
                      隐患:
                    </lable>
                    <span class="li-font" v-for="val in deviceCollect.devices">
                        {{val}}&nbsp;&nbsp;&nbsp;
                      </span>
                  </div>
                </div>
              </div>
            </div>
            <partial name="default"></partial>
          </li>
        </ul>

      </div>
    </div>


  </div>

  <div class="row app-input text-center" v-if="false">
    <label class="control-label text-right">
      <input type="checkbox"
             @change="showrepairchange($event)"
             :checked = "ifchecked"
      >
      是否维修
    </label>
    <v-select :value="model.serviceacitivity[0].f_reciever" :value-single="true"
              :options='repairers' placeholder='请选择维修员'
              close-on-select v-on:change="repiarmancg" ></v-select>
  </div>
  <devices-item :show.sync="showDevice" :role="role" :operation="operation" :config="config" :device="dev" :idx="idx" v-if="showDevice"></devices-item>



</template>

<script>
  import Vue from 'vue'
  import { HttpResetClass } from 'vue-client'
  import co from 'co'
  import * as Util from '../Util'
  import DevicesItem from "./DevicesItem";

  let getRepairs = async function(self,url) {
    // 获取配置信息
    try {
      let val = {source: 'this.getParentByType($department$).getChildByName($维修员$).getUsers()', userid: `${Vue.$login.jwt}`}
      let repairs =[]
      let res = await Vue.resetpost(`${url}/rs/search`,{data: val},{resolveMsg: null, rejectMsg: null})
      res.data.forEach((item) => {
        repairs.push({label: item.name, value: item})
      })
      self.f_repair_person = repairs
    } catch (error) {
      console.log('error',error)
    }
  }

  export default {
    components: {DevicesItem},
    title: '安检项界面',
    data () {
      return {
        // selectrepair : this.$parent.$parent.data.safecheckrepair,
        // ifchecked: this.$parent.$parent.data.whetherrepair === 1,
        // showrepair: this.$parent.$parent.data.whetherrepair === 1,
        showrepair: false,
        paper: this.$parent.$parent.data,
        devices: this.$parent.$parent.devices,
        config: this.$parent.$parent.config,
        showDevice:false,
        dev:{},
        operation:'',
        idx:'',
        safeCheckToRepair:'',
        showCheckToRepair:false,
        userid: '',
        // userid: this.$login.f.id,
        source:
        'dep=this.getParentByType($organization$).getSpecialResByType($department$),' +
        'tool.getFullTree(dep.where(row.getChildByName($派单员$) != null))',
        isback:false,
        defectDevice:{},
        deviceCollect:{},
        deviceCollects:[],
        callObj:null,
        user:{},
        role:this.$parent.$parent.role,
        upload_state: this.$parent.$parent.item.f_upload_state,
        hasDevice:false,
        Url:this.$androidUtil.getProxyUrl(),
        result:'正常'
      }
    },
    methods: {
      addDevice (dev) {
        this.operation = 'add'
        this.paper['device' + dev.n].push({})
        this.dev = dev
        console.log('length', this.paper['device' + dev.n])
        this.idx=this.paper['device' + dev.n].length - 1
        this.showDevice = true
        // this.$goto('device-item', {config: this.config, device: dev, idx: this.paper['device' + dev.n].length - 1}, 'paper-router')
      },
      editDevice (dev, idx) {
        this.operation = 'edit'
        console.log('dev'+JSON.stringify(dev))
        console.log('idx'+idx)
        this.dev = dev
        this.idx = idx
        this.showDevice = true
        // this.$goto('device-item', {config: this.config, device: dev, idx: idx}, 'paper-router')
      },
      removeDevice (dev, row) {
        this.paper['device' + dev.n].$remove(row)
        this.devicesCollect()
      },
      showrepairchange (event) {
        if (event.srcElement.checked) {
          this.showrepair = true
          this.$parent.$parent.data.whetherrepair = 1
        } else {
          this.showrepair = false
          this.$parent.$parent.data.whetherrepair = 0
          this.$parent.$parent.data.safecheckrepair = ''
        }
      },
      changeRepairman (val) {
        if (val.length !== 0) {
          this.$parent.$parent.data.safecheckrepair = val
        }
      },
      readRepair () {
        let url = this.$androidUtil.getProxyUrl()
        let gen = getRepairs(this,url)
        return co(gen)
      },
      repiarmancg(val){
        if(val){
          console.log(val)
          this.model.serviceacitivity[0].f_reciever = val.name
          let http1 = new HttpResetClass()
          http1.load('POST',`${this.Url}/api/af-safecheck/sql/safe_singleTable_OrderBy`, {data: {
              items: 'f_user_telephone',
              tablename: 't_user',
              condition: `name  = '${val}'`,
              orderitem: 'id'
            }}, {resolveMsg: null, rejectMsg: null}).then((ress) => {
            this.model.f_repairman_phone = ress.data[0].f_user_telephone
          })
        }
      },
        devicesCollect(){
          this.deviceCollects = []
          let checkResult = '正常'
          this.result = '正常'
          let codata = JSON.parse(JSON.stringify(this.paper))
          for(let device in this.paper){
            let f_device_type
            if(device.startsWith("device")){
              for(let item in this.paper[device]) {
                if(this.paper[device].length > 1 ){
                  let j = Number.parseInt(item)+1
                  f_device_type = `${this.paper[device][item].f_device_type}${j}`
                }else{
                  f_device_type = this.paper[device][item].f_device_type
                }
        // console.log('codata[device][item]=>',codata[device][item])
            let deviceInfos = []
            for(let i in this.paper[device][item].f_items){
              let defects = ''
              let items = this.paper[device][item].f_items[i]
              if (items.type === 'selector' && items.f_item_value != null && items.f_item_value instanceof Array){
                for (let i in items.f_item_value) {
                  let f_item_value = items.f_item_value[i]

                  console.log('f_item_value => ' + JSON.stringify(f_item_value))
                  console.log('f_device_type => ' + JSON.stringify(f_device_type))
                  console.log('this.paper[device][item] => ' + JSON.stringify(this.paper[device][item]))
                  console.log('this.config[this.paper[device][item].f_device_type] => ' + JSON.stringify(this.config[this.paper[device][item].f_device_type]))

                  let options = this.config[this.paper[device][item].f_device_type].items[items.f_item_name].options
                  for (let option in options) {
                    if (options[option].data == f_item_value) {
                      if (options[option].isdefect == 'true' || options[option].isdefect == true) {
                        checkResult = '有隐患'
                        this.result = '有隐患'
                        defects=`${defects}${options[option].data}/`
                        // let level = options[option].level
                      }
                    }
                  }
                  console.log('defects=====>'+JSON.stringify(defects))
                }
                if(defects != ''){
                  deviceInfos.push(`${items.f_item_name}${defects.substring(0, defects.length - 1)}`)
                }
              }
              console.log('deviceInfos========>'+JSON.stringify(deviceInfos))
              console.log('items.f_is_defect')
              console.log(items.f_is_defect)
              if (items.type === 'selector') {
                items.selected = []
              } else if (items.type === 'inputSelector') {
                items.selected = ''
              }
            }


              console.log('deviceCollect',this.deviceCollect)
              this.$set('deviceCollect.name',f_device_type)
              this.$set('deviceCollect.value',checkResult)
              this.$set('deviceCollect.devices',deviceInfos)
              this.deviceCollects.push(JSON.parse(JSON.stringify(this.deviceCollect)))
              checkResult = '正常'
              this.deviceCollect = {}
            }
          }
        }
        if(this.deviceCollects.length>0){
          this.hasDevice = true
        }
      }
    },
    ready(){
      if(!this.$parent.$parent.showBack){
        this.$parent.$parent.showBack= true
      }
      this.devicesCollect()
    },
    events:{
      'finishChoose'(){
        console.log('已选择安检设备')
        this.hasDevice = true
        this.devicesCollect()
      }
    }
  }
</script>
