<template>
  <div class="auto">
    <div style="width: 100%;height: 36px;background-color: #f1f8ff">
      <div style="width: 8px;height: 100%;background-color: #6aa6e2;margin-right: 20px;float: left"></div>
      <div style="width: auto;height:100%;float: left;"><p class="title-font" style="margin-top: 5px">安检选项：</p></div>
    </div>
    <div v-for="dev in devices" style="width: 100%;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>-->
          </div>
          <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'"></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>
      </div>


    </div>
    <!--  <div v-if='hasDevice' style="width: 100%;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-itemm :show.sync="showDevice" :role="role" :operation="operation" :config="config" :device="dev" :idx="idx" v-if="showDevice"></devices-itemm>-->


</div>
</template>

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

  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: {DevicesItemm},
    title: '安检项界面',
    data () {
      return {
        // selectrepair : this.$parent.data.safecheckrepair,
        // ifchecked: this.$parent.data.whetherrepair === 1,
        // showrepair: this.$parent.data.whetherrepair === 1,
        showrepair: false,
        paper: this.$parent.data,
        devices: this.$parent.devices,
        config: this.$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.role,
        hasDevice:false,
        Url:this.$androidUtil.getProxyUrl(),
        result:'正常',
        picture:[],
        abc:[]
      }
    },
    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.data.whetherrepair = 1
        } else {
          this.showrepair = false
          this.$parent.data.whetherrepair = 0
          this.$parent.data.safecheckrepair = ''
        }
      },
      changeRepairman (val) {
        if (val.length !== 0) {
          this.$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}/rs/sql/tel_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 = '正常'
        console.log('paper:',this.paper)
        let codata = JSON.parse(JSON.stringify(this.paper))
        console.log('codata内容：',codata)
        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 items = this.paper[device][item].f_items[i]
                console.log('items.f_is_defect')
                console.log(items.f_is_defect)
                if((items.f_is_defect == true || items.f_is_defect == 'true') && items.f_item_value != ''){
                  checkResult = '有隐患'
                  this.result = '有隐患'
                  deviceInfos.push(`${items.f_item_name}-${items.f_item_value}`)
                }

                if(items.type == 'selector'){
                  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(){
      this.devicesCollect()
    },
    events:{
      'finishChoose'(val){
        console.log('已选择安检设备')
        this.hasDevice = true
        this.picture = val
        this.devicesCollect()
      }
    },
    watch :{
      'picture'(val){
        for (let row of val){
          this.abc.push(row)
        }
        this.$dispatch('picture', this.abc)
      },
      'result'(val){
        this.$dispatch('result', val)
      },
      'deviceCollects'(val){
        this.$dispatch('deviceCollects', val)
      },
      'hasDevice'(val){
        this.$dispatch('hasDevice', val)
      }
    }
  }
</script>
