import Vue from 'vue'

let MagGetSaleParam = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.MagGetSaleParam = Vue.prototype.$MagGetSaleParam = MagGetSaleParam
  },
  gasbrands: [],
  prices: [],
  adjustables: [],
  inputtors : [],
  smallareas:[],
  async initinputtor(){
    let res = await Vue.resetpost('api/af-revenue/sql/getAlluserRes', {data:{
      condition:" 1=1"
    }}, {newly:true, resolveMsg: null, rejectMsg: '获取用户出错！！！'})
    if(res.data){
      MagGetSaleParam.inputtors = res.data;
    }
  },
  async getsmallarea(filiale){
    let smallareas=[]
    let condition=`f_small_area is not null and f_small_area <>''  group by f_small_area`
    if(filiale){
      condition=`f_filialeid='${filiale}' and f_small_area is not null and f_small_area <>''  group by f_small_area`
    }
    let res = await Vue.resetpost('api/af-revenue/sql/manage_singleTable', {data:{
    items: 'f_small_area', tablename: 't_user_address', orderitem: 'f_small_area DESC',condition:condition
      }}, {newly:true,resolveMsg: null, rejectMsg: '获取区域出错！！！'})
    if(res.data){
      res.data.forEach((item) => {
        var param={
          label:item.f_small_area,
          value:item.f_small_area
        }
        smallareas.push(param)
      })
    }
    return  smallareas
  },
  getresinputtor(orgid){
    let inputtors = []
    if(MagGetSaleParam.inputtors.length>0){
      let arr = MagGetSaleParam.inputtors.filter((res) => {
        if(res.rolestr != null &&  res.rolestr!='' ){
          return res.rolestr.indexOf("抄表员")>-1 && res.f_orgid == orgid
        }
      })

      arr.forEach((res) => {
        inputtors.push({label: res.name, value: res.name})
      })
    }
    return inputtors;
  },
  // 开启卡监听
  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 = []
        if (Array.isArray(item.gasmodel)) {
          console.log('isArray', item.gasmodel)
          item.gasmodel.forEach((item, index) => {
            gasmodel[index] = {}
            gasmodel[index].label = item.f_meter_style + item.f_type
            gasmodel[index].value = item
          })
        } else {
          console.error("gasmodel is not an array or is undefined");
        }
        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_orgid === 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
  },
  getPrices() {
    return MagGetSaleParam.prices
  },
  getPrice (object) {
    if (MagGetSaleParam.prices.length === 0) {
      MagGetSaleParam.organizeData(Vue.$appdata.saleParams)
    }
    let result = []
    console.log('获取价格。。。', MagGetSaleParam.prices)
    if (object){
      // 做筛选
      MagGetSaleParam.prices.forEach((item) => {
        if (item.f_state === '有效' &&
          item.f_price_type === object.f_price_type &&
          item.f_filialeid === object.filter) {
          let value = {
            label: item.f_price_name,
            value: item
          }
          result.push(value)
        }
      })
    }else{
      // 做筛选
      MagGetSaleParam.prices.forEach((item) => {
        if (item.f_state === '有效' &&
          item.f_price_type === object.f_price_type) {
          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
