import Vue from 'vue'
import GetSaleParam from './MagGetSaleParams'

let loadParamGen = async function (f) {
  if (GetSaleParam.gasbrands.length === 0 && GetSaleParam.prices.length === 0 && GetSaleParam.adjustables.length === 0 ) {
    let getParams = await Vue.resetpost('rs/logic/getSaleInitData', {f_filialeids: Vue.$login.f.f_orgids}, {resolveMsg: null, rejectMsg: '获取营收参数出错！！！'})
    if (getParams.data) {
      MagLoadParams.organizeData(getParams.data)
    }
  }
}

let MagLoadParams = {

  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.MagLoadParams = Vue.prototype.$MagLoadParams = MagLoadParams
  },

  async loadParam () {
    await loadParamGen()
  },

  // 开启卡监听
  organizeData (fromSystem) {
    fromSystem.gasbrands.forEach((item, index) => {
      GetSaleParam.gasbrands[index] = {}
      GetSaleParam.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
      GetSaleParam.gasbrands[index].value = item
    })

    fromSystem.adjustable.forEach((item, index) => {
      GetSaleParam.adjustables[index] = {}
      GetSaleParam.adjustables[index].label = item.f_adjustable_name
      GetSaleParam.adjustables[index].value = item
    })
    GetSaleParam.prices = [...GetSaleParam.prices, ...fromSystem.price]
    console.log('参数组织完毕', GetSaleParam)
  }

}

export default MagLoadParams
