import Vue from 'vue'

let MagGetSaleParam = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.MagGetSaleParam = Vue.prototype.$MagGetSaleParam = MagGetSaleParam
  },
  gasbrands: [],
  prices: [],
  adjustables: [],

  // 开启卡监听
  organizeData (fromSystem) {
    if(Vue.android){
      console.log('接受System中返回的参数进行组织', fromSystem)
      fromSystem.gasbrands.forEach((item, index) => {
        MagGetSaleParam.gasbrands[index] = {}
        MagGetSaleParam.gasbrands[index].label = item.f_meter_brand
        let gasmodel = []
        item.gasmodel.forEach((item, index) => {
          gasmodel[index] = {}
          gasmodel[index].label = item.f_meter_style + item.f_type
          gasmodel[index].value = item
        })
        item.gasmodel = gasmodel
      MagGetSaleParam.gasbrands[index].value = item
      })
      fromSystem.adjustable.forEach((item, index) => {
        MagGetSaleParam.adjustables[index] = {}
      MagGetSaleParam.adjustables[index].label = item.f_adjustable_name
      MagGetSaleParam.adjustables[index].value = item
      })
      MagGetSaleParam.prices = [...MagGetSaleParam.prices, ...fromSystem.price]
      console.log('参数组织完毕', MagGetSaleParam)
    }
  },
  getAdjustable (val) {
    // 数组去重
    let arr = []
    console.log("调压箱调压箱调压箱调压箱调压箱调压箱调压箱调压箱调压箱")
    console.log(Vue.$appdata.saleParams)
    console.log(MagGetSaleParam.adjustables)
    if (val) {
      MagGetSaleParam.adjustables.forEach((item, next) => {
        if (item.value.f_filialeids === val) {
        arr.push(item)
      }
    })
    } else {
      MagGetSaleParam.adjustables.forEach((item, next) => {
        arr.push(item)
    })
    }
    let hash = {}
    let result = arr.reduce((item, next) => {
      hash[next.label] ? '' : hash[next.label] = true && item.push(next)
      return item
    }, [])
    return Array.from(new Set(result))
  },
  getGasbrand () {
    if (MagGetSaleParam.gasbrands.length === 0) {
      MagGetSaleParam.organizeData(Vue.$appdata.saleParams)
    }
    return MagGetSaleParam.gasbrands
  },
  getPrice () {
    if (MagGetSaleParam.prices.length === 0) {
      MagGetSaleParam.organizeData(Vue.$appdata.saleParams)
    }
    let result = []
    console.log('获取价格。。。', MagGetSaleParam.prices)
    // 做筛选
      MagGetSaleParam.prices.forEach((item) => {
        if (item.f_state === '有效' && Vue.$login.f.f_orgids === item.f_filialeids) {
          console.log('进来了')
          let value = {
            label: item.f_price_name,
            value: item
          }
          result.push(value)
        }
      })
    // 数组去重
    let hash = {}
    result = result.reduce((item, next) => {
      hash[next.label] ? '' : hash[next.label] = true && item.push(next)
      return item
    }, [])
    return Array.from(new Set(result))
  }
}

export default MagGetSaleParam
