import {HttpResetClass, trans} from 'vue-client'
import ObjConfig from '../../src/plugins/GerJsonObjectConfig'

export default class Comp {

  SafeCheckType = {}
  SafeCheckTypeArray = []
  DevicesSetConfig = {}
  DevicesSetConfigArray = []
//{name: "年度普检", Civil: "DevicesConfig", isnCivil: "DevicesSetConfig"}
  //要修改的数组
  ChangeArray = []
  //需要修改item的暂存区 例：{lname: "表种类", index: 11, type: "selector", multiple: false, options: Array(4), …}
  // {data: "皮膜", isdefect: false(隐患), isdefault: false(默认)}
  ChangeArea = {}
  //添加元素的类型
  jsona = JSON.stringify(this.SafeCheckType).split('},')
  //控制隐患等级显示
  defect_bool = false
  isdefect_bool = true
  //当item-type为selector时这个值为true显示要修改值得列表
  data_bool = false
  isdata_bool = true
  //控制详情页显示
  detail_page = false
  mainbool = false

  //条件参数
  config_name//该查询文件的名字
  business //业务
  isCivil //是否民用
  data_index = {yewu: '', item: ''}//正在修改得安检项目的index属性
  ConfigChange = ['民用', '非民用']
  RadioButton = ['是', '否']//showModal弹框里得RaidoButton
  isDefect = ['隐患', '非隐患']//是否隐患select内容
  isCheckMust = ['必选', '非必选']//是否隐患select内容
  isDefault = ['默认', '非默认']//是否默认select内容
  itemType = ['列表', '字符', '数字', '图片']
  //添加项目按钮的暂存区
  b_Area = {checkname: '', index: '', checkmust: '', items: [], bool: false}
  keep = {lname: '', index: '', type: '', multiple: ''}
  keep_options = {data: '', isdefect: '非隐患', isdefault: '非默认'}
  //存放未修改的json
  json_str = {}
  selected = null

  async ready () {
    await this.readCondition()
    this.both(this.SafeCheckType)
    console.log('ready++++++++++++++++++=', JSON.stringify(this.safeOne))
    await window.dispatchEvent(new CustomEvent('goRefresh'), {})
  }

  async readCondition () {
    console.log('进入=====请求safeCheckType======')
    let http = new HttpResetClass()
    let response = await http.load('GET', `rs/phonevues/SafecheckType.json`, {}, {
      resolveMsg: null,
      rejectMsg: null
    }).then(res => {
      console.log('======接收到的数据========>' + JSON.stringify(res.data))
      this.SafeCheckType = res.data
    })
    console.log('===safechecktype====>' + JSON.stringify(this.SafeCheckType))
  }

  async readConfig () {
    console.log('进入=====================')
    let http = new HttpResetClass()
    let response = await http.load('GET', `rs/phonevues/` + this.config_name, {}, {
      resolveMsg: null,
      rejectMsg: null
    }).then(res => {
      console.log('==============>' + res.data)
      this.DevicesSetConfig = res.data
    })
    this.DevicesSetConfigArray = ObjConfig.getObjConfig(this.DevicesSetConfig)
    for (let i = 0; i < this.DevicesSetConfigArray.length; i++) {
      this.DevicesSetConfigArray[i].bool = false
    }
    console.log('=======>' + JSON.stringify(this.DevicesSetConfig))
  }

  async writeConfig (obj) {
    let http = new HttpResetClass()
    let response = await http.load('POST', `api/af-safecheck/logic/saveJson_logic`, {
      'data': {
        'jsonStr': JSON.stringify(obj),
        'vueName': '' + this.config_name
      }
    }, {resolveMsg: null, rejectMsg: null})
  }

  //保存按钮点击后事件
  async save_Json (f) {
    console.log('====>修改之前\n', JSON.stringify(this.DevicesSetConfigArray))
    let writejson = this.deepClone(this.DevicesSetConfigArray)
    for (let i = 0; i < writejson.length; i++) {
      if (writejson[i].hasOwnProperty('bool')) delete writejson[i].bool
      for (let j = 0; j < writejson[i].items; j++) {
        if (writejson[i].items[j].type !== 'selector' &&
          writejson[i].items[j].hasOwnProperty('options')) {
          delete writejson[i].items[j].options
        }
      }
    }
    console.log('====>修改之后\n', JSON.stringify(this.DevicesSetConfigArray))
    console.log('====>配置文件修改之后\n', JSON.stringify(this.DevicesSetConfig))

    await this.writeConfig(this.ArrToJson(writejson))
    alert('修改成功')
    console.log('======>arrr转换前\n', JSON.stringify(this.DevicesSetConfigArray))
    console.log('======>arrr转换后\n', JSON.stringify(this.ArrToJson(writejson)))
  }

  //查询信息被改变
  query_info (v, i, f) {
    if (i === 1) {
      this.business = v
      console.log('====>', v)
    } else {
      this.isCivil = v
      console.log('====>', v)
    }
    f()
  }

  //查询按钮被点击
  async query_config (f) {
    console.log(this.SafeCheckType[this.business][this.isCivil])
    await this.configName()
    await this.readConfig()
    console.log(this.config_name)
    await this.configtrue(f)
    await window.dispatchEvent(new CustomEvent('goRefresh'), {})
    console.log('我刷新了哦')
    console.log(this.mainbool)
    f()
  }

  configtrue (f) {
    if (this.DevicesSetConfig != null) this.mainbool = true
    f()
  }

  configName () {
    this.config_name = this.SafeCheckType[this.business][this.isCivil] + '.json'
  }

//==================外层list操作

  //点击安检项目后事件
  async bChoose (index, f) {
    this.data_index.yewu = index
    this.ChangeArray = this.DevicesSetConfigArray[index].items
    console.log('======展开的数组是=====》' + JSON.stringify(this.ChangeArray))
    for (let i = 0; i < this.DevicesSetConfigArray.length; i++) {
      if (i === index) continue  //遍历到所点击item跳出循环
      this.DevicesSetConfigArray[i].bool = false//所有的展开状态设置为false
      console.log(i)
    }
    this.DevicesSetConfigArray[index].bool = !this.DevicesSetConfigArray[index].bool//所点击得item展开状态切换
    await window.dispatchEvent(new CustomEvent('goRefresh'), {})
    console.log()
    f()
  }

  //点击添加项目后事件
  addb (f) {
    this.b_Area.checkmust=""
    this.b_Area.checkname=""
    this.detail_page = true
    this.selected = 'addb'
    f()
  }

  //添加项目/修改项目名字被改变
  addb_name (v, f) {
    console.log('====>', v)
    this.b_Area.checkname = v
    f()
  }

  //添加项目/修改项目必选被改变
  addb_isMust (v, f) {
    console.log('====>', v)
    this.b_Area.checkmust = v
    f()
  }

  //确认添加
   addb_true (f) {
    this.b_Area.index = this.DevicesSetConfigArray.length + 1
    this.b_Area.checkmust = this.b_Area.checkmust === '必选'
    this.DevicesSetConfigArray.push(this.deepClone(this.b_Area))
    console.log('==修改后当前业务信息==>', JSON.stringify(this.DevicesSetConfigArray))
    this.b_Area = this.deepClone({checkname: '', index: '', checkmust: '', items: [], bool: false})
    // this.detail_page = false
     this.selected = 'addSuccess'
    window.dispatchEvent(new CustomEvent('goRefresh'), {})
    f()
    console.log('刷新了哦')
  }

  //取消添加
  addb_off (f) {
    this.b_Area = this.deepClone({checkname: '', index: '', checkmust: '', items: [], bool: false})
    console.log('====')
    f()
  }

  //点击删除项目事件bDel
  bDel (index, f) {
    if (this.DevicesSetConfigArray[index].checkname === '燃气表') {
      this.detail_page = true
      this.selected = 'defaultPage'
      f()
      return
    }
    this.DevicesSetConfigArray.splice(index, 1)
    f()
  }

  //点击修改项目后事件 内容改变事件用的添加项目得方法
  bChange (index, f) {
    this.bChoose(index, f)
    this.DevicesSetConfigArray[index].bool = true //所点击得item展开状态切换
    this.detail_page = true
    if (this.DevicesSetConfigArray[index].checkname === '燃气表') {
      this.selected = 'defaultPage'
      f()
      return
    }
    this.b_Area.checkname = this.DevicesSetConfigArray[index].checkname
    this.b_Area.checkmust = '非必选'
    if (this.DevicesSetConfigArray[index].hasOwnProperty('checkmust')) {
      if (this.DevicesSetConfigArray[index].checkmust === false) this.b_Area.checkmust = '非必选'
      if (this.DevicesSetConfigArray[index].checkmust === true) this.b_Area.checkmust = '必选'
    }
    this.selected = 'bDetail'
    f()
  }

  //确认修改事件
   b_true (f) {
    this.DevicesSetConfigArray[this.data_index.yewu].checkname = this.deepClone(this.b_Area.checkname)
    this.DevicesSetConfigArray[this.data_index.yewu].checkmust = this.b_Area.checkname === '必选'
    console.log('==修改后当前业务信息==>', JSON.stringify(this.DevicesSetConfigArray[this.data_index.yewu]))
    // this.detail_page = false //关闭动态组件
    this.b_Area = JSON.parse(JSON.stringify({checkname: '', index: '', checkmust: '', items: [], bool: false}))
    // await this.bChoose(this.data_index.yewu, f)
     this.selected = 'alterSuccess'
    window.dispatchEvent(new CustomEvent('goRefresh'), {})
    f()
  }

  //取消修改事件
  b_off (f) {
    console.log('==取消修改后当前业务信息==>', JSON.stringify(this.DevicesSetConfigArray[this.data_index.yewu]))
    // this.detail_page = false //关闭动态组件
    this.b_Area = JSON.parse(JSON.stringify({checkname: '', index: '', checkmust: '', items: [], bool: false}))
    f()
  }

  //点击添加内容后事件
  bAdd (index, f) {
    this.bChoose(index, f)
    this.data_bool = false
    this.DevicesSetConfigArray[index].bool = true //所点击得item展开状态切换
    this.detail_page = true
    this.selected = 'addDetail'
    this.ChangeArea = this.deepClone(this.keep)
    f()
  }

//删除指定item
  delItem (index, f) {
    console.log('删除节点===begin===>' + index)
    console.log('===before====>' + JSON.stringify(this.ChangeArray))
    this.ChangeArray.splice(index, 1)
    console.log('===after====>' + JSON.stringify(this.ChangeArray))
    for (let i in this.DevicesSetConfigArray) {
      if (this.DevicesSetConfigArray[i].index == this.data_index) {
        this.DevicesSetConfigArray[i].items = this.ChangeArray
      }
    }
    console.log('===yewu_after====>' + JSON.stringify(this.DevicesSetConfigArray))
    f()
  }

//==================外层list添加内容按钮事件
  //确定添加方法
  //{"lname":"是否漏气","index":11,"type":"selector","multiple":false,"options":
  // [{"data":"是","isdefect":true,"isdefault":false},{"data":"否","isdefect":false,"isdefault":false}]}
  async add_true (f) {
    this.data_bool = false
    if (this.DevicesSetConfigArray[this.data_index.yewu].items.length === 0) {
      console.log('是最后一个了')
      this.ChangeArea.index = 1
    } else {
      this.ChangeArea.index = this.ChangeArray[this.ChangeArray.length - 1].index + 1
    }
    console.log('====keep=>\n' + JSON.stringify(this.ChangeArea))
    let json = this.state_tr(this.deepClone(this.ChangeArea))
    console.log('====json=>\n' + JSON.stringify(json))
    this.ChangeArray.push(json)
    console.log('=====>\n' + JSON.stringify(this.ChangeArray))
    console.log('=====>刷新过后')
    this.selected = 'addSuccess'
    await window.dispatchEvent(new CustomEvent('goRefresh'), {})
    f()
    console.log('刷新了哦')
  }

  //添加取消方法
  async add_off () {
    this.data_bool = false
    // this.ChangeArray = this.deepClone(this.keep)
    this.ChangeArea = {}
    console.log('===添加取消===', JSON.stringify(this.ChangeArray))
    await window.dispatchEvent(new CustomEvent('goRefresh'), {})
  }

  //######  以下事件与修改组件方法通用  ######
  //名称输入框值被修改事件 -> 修改组件 modal_input
  //属性下拉框修改后触发 -> 修改组件 modal_type
  //展示options区域值被修改后更新 -> 修改组件 modal_list
  //数据展示列表删除点击后 从暂存区移除数据 -> 修改组件 modal_list_del
  //添加按钮被点击  把暂存区的option添加到keep暂存区 -> 修改组件 modal_add
  //是否多选 是否多选切换后更新暂存区的值 -> 修改组件 RadioCli
  //删除指定item  -> 修改组件  modal_list_del
  //options添加模块数据修改  stat.Call($modal_optionsChange$,e.Value,3)
  //添加隐患等级 -> 修改组件  stat.Call($level$,e.Value)
//需要修改item的暂存区 例：{lname: "表种类", index: 11, type: "selector", multiple: false, options: Array(4), …}
// {data: "皮膜", isdefect: false(隐患), isdefault: false(默认)}
//=============================================修改弹窗里得方法
  //修改按钮点击后事件
//修改按钮点击后事件
  changeClick (field, index, f) {
    field.type === 'selector' ? this.data_bool = true : this.data_bool = false
    this.isdata_bool = !this.data_bool
    this.detail_page = true
    this.defect_bool = false
    this.isdefect_bool = true
    this.selected = 'alterDetail'
    this.json_str = JSON.parse(JSON.stringify(field))
    this.data_index.item = index
    console.log('====正在操作下标为', index, '得item=====')
    this.ChangeArea = JSON.parse(JSON.stringify(this.state_tt(field)))
    console.log('==正在修改的行内容为====>' + JSON.stringify(this.ChangeArea))
    console.log('==复制内容为====>' + JSON.stringify(this.json_str))
    field.type === '列表' ? this.data_bool = true : this.data_bool = false
    this.isdata_bool = !this.data_bool
    f()
  }

  //确认修改方法
  async pop_up (f) {
    if(confirm("确认修改吗？")){
      this.ChangeArray[this.data_index.item] = this.deepClone(this.state_tr(this.deepClone(this.ChangeArea)))
      console.log('===确认后>', JSON.stringify(this.ChangeArea))
    }
    this.selected = 'alterSuccess'
    await window.dispatchEvent(new CustomEvent('goRefresh'), {})
    f()
    console.log('刷新了哦')
  }

  //修改取消方法stat.data.detail_page
  pop_up_off (f) {
    this.data_bool = this.json_str.type === 'selector' || this.json_str.type === '列表'
    this.isdata_bool = !this.data_bool
    this.ChangeArea = JSON.parse(JSON.stringify(this.state_tt(this.json_str)))
    console.log('===确认后>', JSON.stringify(this.ChangeArea))
    console.log('===修改取消  返回原始值===')
    f()
  }

  //修改弹窗输入框内容修改事件
  modal_input (cont, f) {
    this.ChangeArea.lname = cont
    console.log(cont, '=======area', this.ChangeArea.lname)
    f()
  }

  //属性下拉框修改后触发
  modal_type (type, f) {
    console.log('before=====>', JSON.stringify(this.ChangeArea))
    if (this.ChangeArea.type !== '列表' && type === '列表') {
      this.ChangeArea.multiple = '否'
      if (!this.ChangeArea.hasOwnProperty('options')) this.ChangeArea.options = []
    }
    if (this.ChangeArea.type === '列表' && type !== '列表') {
      this.ChangeArea.type = type
      delete this.ChangeArea.multiple
    }
    this.ChangeArea.type = type
    this.data_bool = this.ChangeArea.type === '列表'
    this.isdata_bool = !this.data_bool
    console.log(type, 'after=======', JSON.stringify(this.ChangeArea))
    f()
  }

  //List里得信息被修改被修改
  modal_list (v, index, i, f) {
    if (i === 1) {
      console.log(v, '=======before\n', this.ChangeArea.options[index].data)
      this.ChangeArea.options[index].data = v
      console.log(v, '=======after\n', this.ChangeArea.options[index].data)
    } else if (i === 2) {
      console.log('====before\n' + this.ChangeArea.options[index].isdefect)
      this.ChangeArea.options[index].isdefect = v
      console.log('====after\n' + this.ChangeArea.options[index].isdefect)
    } else if (i === 3) {
      console.log('====before\n' + this.ChangeArea.options[index].isdefault)
      this.ChangeArea.options[index].isdefault = v
      console.log('====after\n' + this.ChangeArea.options[index].isdefault)
    }
    f()
  }

  //删除list元素
  modal_list_del (index, f) {
    console.log('==>删除options前\n' + JSON.stringify(this.ChangeArea.options))
    this.ChangeArea.options.splice(index, 1)
    console.log('==>删除options后\n' + JSON.stringify(this.ChangeArea.options))
    f()
  }

  //添加信息模块信息被修改
  modal_optionsChange (v, i, f) {
    if (i === 1) {
      console.log('=====>before', JSON.stringify(this.keep_options))
      this.keep_options.data = v
      console.log('=====>after', JSON.stringify(this.keep_options))
    } else if (i === 2) {
      console.log('=====>before', JSON.stringify(this.keep_options))
      this.defect_bool = v === '隐患'
      this.isdefect_bool = !this.defect_bool
      this.keep_options.isdefect = v
      console.log('=====>after', JSON.stringify(this.keep_options))
    } else {
      console.log('=====>before', JSON.stringify(this.keep_options))
      this.keep_options.isdefault = v
      console.log('=====>after', JSON.stringify(this.keep_options))
    }
    f()
  }

  //隐患等级被修改 stat.Call($level$,e.Value)
  level (v) {
    this.keep_options.level = v
  }

  //弹窗里得添加按钮被点击
  modal_add (f) {
    console.log('添加前暂存区===》', JSON.stringify(this.ChangeArea))
    this.ChangeArea.options.push(this.deepClone(this.keep_options))
    console.log('添加后暂存区===》', JSON.stringify(this.ChangeArea))
    this.keep_options = this.deepClone({data: '', isdefect: '非隐患', isdefault: '非默认'})
    f()
  }

  //选择是否多选
  RadioCli (v) {
    console.log(v, '===before====>', JSON.stringify(this.ChangeArea))
    this.ChangeArea.multiple = v
    console.log(v, '===after====>', JSON.stringify(this.ChangeArea))
  }

//====json处理方法==========================================

  both (obj) {
    for (var key in obj) {
      this.SafeCheckTypeArray.push(key)
    }
  }

  //item里得isDefect ，isDefect，布尔值转换为字符 multiple
  state_tt (json) {
    if (json.hasOwnProperty('multiple')) {
      if (json.multiple === true) json.multiple = '是'
      if (json.multiple === false) json.multiple = '否'
    }
    if (json.type === 'selector') json.type = '列表'
    if (json.type === 'string') json.type = '字符'
    if (json.type === 'number') json.type = '数字'
    if (json.type === 'picture') json.type = '图片'
    if (null != json.options) {
      for (let i = 0; i < json.options.length; i++) {
        json.options[i].isdefect = json.options[i].isdefect === true||json.options[i].isdefect === '隐患' ? '隐患' : '非隐患'
        json.options[i].isdefault = json.options[i].isdefault === true || json.options[i].isdefault === '默认'? '默认' : '非默认'
      }
    }
    return json
  }

  //item把值转换
  state_tr (json) {
    if (json.hasOwnProperty('multiple') && json.multiple === '是') json.multiple = true
    if (json.hasOwnProperty('multiple') && json.multiple === '否') json.multiple = false
    if (json.type === '列表') json.type = 'selector'
    if (json.type === '字符') json.type = 'string'
    if (json.type === '数字') json.type = 'number'
    if (json.type === '图片') json.type = 'picture'
    if (json.type === 'selector') {
      for (let i = 0; i < json.options.length; i++) {
        json.options[i].isdefect = json.options[i].isdefect === '隐患'
        json.options[i].isdefault = json.options[i].isdefault === '默认'
      }
    }
    return json
  }

  //文哥方法转数组后转成json
  ArrToJson (arr) {
    let json = {}
    for (let i = 0; i < arr.length; i++) {
      const name = arr[i].checkname
      delete arr[i].checkname
      json[name] = arr[i]
      const itemArea = this.ItemArrToJson(json[name].items)
      delete json[name].items
      json[name]['items'] = itemArea
    }
    return json
  }

  //item数组转换为item对象
  ItemArrToJson (arr) {
    let iteming = {}
    for (let i = 0; i < arr.length; i++) {
      const item_name = arr[i].lname
      delete arr[i].lname
      iteming[item_name] = arr[i]
    }
    return iteming
  }

  //深拷贝json
  deepClone (obj) {
    return JSON.parse(JSON.stringify(obj))
  }

  flush (f) {
    f()
  }
}
