<template>
  <modal class="search_area" :show.sync="$parent.$parent.$parent.showModal" v-ref:modal>
    <div slot="modal-header" class="modal-header">
      <h4 class="modal-title ">
          选择隐患 <p style="font-size: 12px;">（检索同时存在多种隐患时点击“和”，检索具有其中一种隐患时点击“或”）</p>
      </h4>
    </div>
    <div slot="modal-body" class="modal-body">
      <div v-for="device in f_items">
        <label class = "text-left">{{device.type}}</label>
        <span  v-show="!device.showDe" @click="showDevice(device)" class="glyphicon glyphicon-chevron-left"></span>
        <span v-show="device.showDe" @click="showDevice(device)" class="glyphicon glyphicon-chevron-down"></span>
        <div v-for="item in device.team" v-show="device.showDe" class=" col-xs-12" style="left: 100px">
            <input type="checkbox" v-model="item.check" @click="checkClick(item)">{{item.str}}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <span v-show="item.and" @click="addAnd(item)" disabled="item.able1"><a>和</a></span>&nbsp;&nbsp;&nbsp;
            <span v-show="item.or" @click="addOr(item)" disabled="item.able2"><a>或</a></span>&nbsp;&nbsp;&nbsp;
        </div>
      </div>
    </div>
    <div slot="modal-footer" class="modal-footer">
      <button  class="button_new button_spacing" @click="remove">清空</button>
      <button  class="button_search button_spacing" @click="$parent.$parent.$parent.ok">确认</button>
      <button  class="button_clear button_spacing" @click="$parent.$parent.$parent.cancel">取消</button>
    </div>
  </modal>

</template>

<script>
  import { PagedList } from 'vue-client'
  import Vue from 'vue'
  import co from 'co'
  import * as Util from '../Util'

  //获取json配置文件
  let asyncReady = async function(self) {
    // 获取配置信息
    try {
      let res = await Vue.resetget(`/phone/rs/vue`,{data: {}},{resolveMsg: null, rejectMsg: null})
      if( res.data.safecheck.DevicesConfig) {
        self.config = res.data.safecheck.DevicesConfig

        for(let device in self.config) {
          self.f_items.push({type: device, showDe:false,  team: []})
        }

        for( let every in self.f_items){
        for(let device in self.config) {
          if( self.f_items[every].type == device) {
            for (let item in self.config[device].items) {
              if (self.config[device].items[item]) {
                for (let result in  self.config[device].items[item].options) {
                  if (device == '其他') {
                    if (self.config[device].items[item].options[result].isdefect == true) {
                      self.f_items[every].team.push({
                        str: item + self.config[device].items[item].options[result].data + '',
                        check: false,
                        and: true,
                        or: true,
                        able1: true,
                        able2: true
                      })
                    }
                  } else {
                    if (self.config[device].items[item].options[result].isdefect == true) {
                      self.f_items[every].team.push({
                        str: device + item + self.config[device].items[item].options[result].data + '',
                        check: false,
                        check: false,
                        and: true,
                        or: true,
                        able2: true
                      })
                    }
                  }
                }
              }
            }
          }
          }
        }
      }
    } catch (error) {
      // 忽略704,文件找不到异常R
      if (error.status !== 704) {
        throw error
      }
    }
  }

  export default {
    title: '安检单查看',
    data () {
      return {
        config: {},
        condition:'',
        f_items:[]
      }
    },
    methods: {
      readConfig () {
        // Vue.$login = {jwt: this.$login.f.id}
        let gen = asyncReady(this)
        return co(gen)
      },
      addAnd(con){
        if(con.check) {
          con.able1 = true
          con.or = false
          if (this.condition != '') {
            this.condition += ' and f_defect_content like \'%' + con.str + '%\''
          }else{
            this.condition += ' f_defect_content like \'%' + con.str + '%\''
          }
        }
      },
      addOr(con){
        if(con.check) {
          con.and = false
          con.able2 = true
          if (this.condition != '') {
           this.condition += ' or f_defect_content like \'%' + con.str + '%\''
           }else{
             this.condition += ' f_defect_content like \'%' + con.str + '%\''
         }
        }
      },
      showDevice(team){
        for( let it in this.f_items){
          if(this.f_items[it].type == team.type)
            this.f_items[it].showDe = !team.showDe
        }
      },
      checkClick(con){
        if(con.check) {
          if (con.and) {
            this.condition = this.condition.replace(' and f_defect_content like \'%' + con.str + '%\'', "")
            this.condition = this.condition.replace(' f_defect_content like \'%' + con.str + '%\' and', "")
            this.condition = this.condition.replace(' f_defect_content like \'%' + con.str + '%\' or', "")
            this.condition = this.condition.replace(' f_defect_content like \'%' + con.str + '%\'', "")
          }else if (con.or) {
            this.condition = this.condition.replace(' or f_defect_content like \'%' + con.str + '%\'', "")
            this.condition = this.condition.replace(' f_defect_content like \'%' + con.str + '%\' and', "")
            this.condition = this.condition.replace(' f_defect_content like \'%' + con.str + '%\' or', "")
            this.condition = this.condition.replace(' f_defect_content like \'%' + con.str + '%\'', "")
          }
        }else{
          if (con.and && !con.or) {
            if (this.condition != '') {
              this.condition += ' and f_defect_content like \'%' + con.str + '%\''
            }else{
              this.condition += ' f_defect_content like \'%' + con.str + '%\''
            }
          }else if (con.or && !con.and) {
            if (this.condition != '') {
              this.condition += ' or f_defect_content like \'%' + con.str + '%\''
            }else{
              this.condition += ' f_defect_content like \'%' + con.str + '%\''
            }
          }
        }
      },
      remove(con){
        this.condition =''
        for ( let te in  this.f_items){
          for ( let it in  this.f_items[te].team) {
            this.f_items[te].team[it].check = false
            this.f_items[te].team[it].and = true
            this.f_items[te].team[it].or = true
            this.f_items[te].team[it].able1 = false
            this.f_items[te].team[it].able2 = false
          }
        }
      }
    },
    ready(){
      this.readConfig()
    },
  }
</script>
