import { get, post, patch, put, remove } from './basefun'

/**
 * 获取字典
 * @param fn
 */
var getDict = (id, fn, type) => {
  let _dicts = []
  if (!type && _dicts && _dicts[id]) {
    fn(_dicts[id])
    return
  } else {
    get('api/v1/basedata/dictionary/dicttype/' + id)
      .then((response) => {
        _dicts[id] = response.result
        _dicts[id].forEach(function (item, index) {
          item.value = item.value + ''
        })
        return fn(_dicts[id])
      })
      .catch((err) => {
        console.log(err)
      })
  }
}

export default {
  name: 'dictutil',
  /**
   * 获取字典类型
   * @param fn
   */
  getDicttype: (fn) => {
    let _dicttype = {}
    if (_dicttype['area']) {
      fn(_dicttype)
      return
    } else {
      get('api/v1/basedata/dictionary/dicttype/')
        .then((response) => {
          _dicttype = response.result
          fn(_dicttype)
        })
        .catch((err) => {
          console.log(err)
        })
    }
  },
  getDict: getDict,
  /**
   * 获取字典name
   * @param value
   * @param fn
   */
  getDictname: (id, value, fn) => {
    let name = ''
    return getDict(id, (data) => {
      data.forEach((dict) => {
        if (dict.value == value) {
          name = dict.name
          fn(name)
        }
      })
    })
  },
  /**
   * 获取字典value
   * @param id
   * @param name
   */

  getDictvalue: (id, name) => {
    let value = ''
    getDict(id, (data) => {
      for (let i = 0; i < data.length; i++) {
        if (data[i].name == name) {
          value = data[i].value
        }
      }
    })
    return value
  },
  /**
   * 根据区域获取单位信息
   * @type {string}
   */
  getCompanys: (areaids, fn) => {
    if (this.companys && this.companys.length > 0) {
      fn(this.companys)
    } else {
      post('api/v1/basic/company/getByArea/' + areaids)
        .then((response) => {
          this.companys = response.result
          fn(this.companys)
        })
        .catch((err) => {
          console.log(err)
        })
    }
  },
  /**
   * 刷新单位树
   */
  refreshCompanys: (areaids) => {
    post('api/v1/basic/company/getByArea/' + areaids)
      .then((response) => {
        //GET请求，从后台获取数据
        this.companys = response.result
      })
      .catch((err) => {
        console.log(err)
      })
  },
  /**
   * 获取省份信息
   * @type {Array}
   */
  getProvinces: (fn) => {
    let provinces = []
    if (provinces && provinces.length > 0) {
      fn(provinces)
    } else {
      get('api/v1/basedata/area/childs/0')
        .then((response) => {
          provinces = response.result
          fn(provinces)
        })
        .catch((err) => {
          console.log(err)
        })
    }
  },
  /**
   * 获取市级信息
   * @param provinceid 省份ID
   */
  getCitys: (provinceid, fn) => {
    let citys = []
    if (citys && citys[provinceid]) {
      fn(citys[provinceid])
      return
    }
    get('api/v1/basedata/area/childs/' + provinceid)
      .then((response) => {
        citys[provinceid] = response.result
        fn(citys[provinceid])
      })
      .catch((err) => {
        console.log(err)
      })
  },
  /**
   * 获取县级信息
   * @param cityid 市级ID
   * @param fn
   */
  getCountys: (cityid, fn) => {
    let countys = []
    if (countys && countys[cityid]) {
      fn(countys[cityid])
      return
    }
    get('api/v1/basedata/area/childs/' + cityid)
      .then((response) => {
        //GET请求，从后台获取数据
        countys[cityid] = response.result
        fn(countys[cityid])
      })
      .catch((err) => {
        console.log(err)
      })
  },
}
