import { Map, fromJS, List } from 'immutable'
export * from './goodsModal.js'
export * from './customModal.js'

export function validateState(state = Map({
  gmfEmail: { state: 'success', message: '' },
  gmfMobile: { state: 'success', message: '' },
  tmpSalePrice: { state: 'success', message: '' },
  kce: { state: 'success', message: '' },
  fymxList: [],
}), action) {
  switch (action.type) {
    case 'update':
      return state.sfs(fromJS(action.data))
    case 'update_goods':
      if (!state.gf(["fymxList", action.index])) {
        // 避免有些行的校验值是null或者undefined，这里做预设
        state = state.sf(["fymxList", action.index], {})
      }
      Object.keys(action.data).forEach((key, index) => {
        state = state.sf(["fymxList", action.index, key], action.data[key])
      })
      return state
    default:
      return state
  }
}

// 用作初始化并开票后重置使用的初始对象
let initFormObj = {
  isEmail: false,
  isPhone: false,
  fpqqlsh: "",
  hsbz: true, // 默认含税
  ghdwmc: undefined,
  ghdwsbh: "",
  ghdwdzdh: "",
  ghdwyhzh: "",
  gmfEmail: "",
  gmfMobile: "",
  hsslbs: "0", // 征税方式默认 0：普通征税
  hjje: "", // 合计金额
  jshj: "", // 价税合计
  kplx: "0",
  hjse: "",
  bz: "",
  kce: "", // 扣除额，差额征税用
  fymxList: [],
  tmpJe: "", // 不含税金额
  tmpDj: "", // 不含税单价
  tmpJshj: "", // 价税合计大写
  tmpSalePrice: "", // 含税销售额，差额征税用
  tmpHsje: "", // 含税金额
  tmpHsdj: "", // 含税单价
}
export function formObj(state = Map(initFormObj), action) {
  switch (action.type) {
    case 'reset':
      return state.sfs(fromJS(initFormObj))
    case 'update':
      return state.sfs(fromJS(action.data))
    case 'update_goods':
      Object.keys(action.data).forEach((key, index) => {
        state = state.sf(["fymxList", action.index, key], action.data[key])
      })
      return state
    default:
      return state
  }
}

export function tempState(state = Map({
  showCustomModal: false,
  showGoodsModal: false,
  showBalanceModal: false,
  showLimitModal: false,
  showOutdateModal: false,
  edit: "",
  editIndex: null,
  editInfo: {}, // 差额征税，点选后缓存的商品信息
  myInitData: {}, // 销售方初始化数据
  rateList: [],
  searchCustomerList: []
}), action) {
  switch (action.type) {
    case 'setMyInitData':
      return state.sf(['myInitData'], action.data)
    case 'setRateList':
      return state.sf(['rateList'], action.data)
    case 'setLimitModal':
      return state.sf(['showLimitModal'], action.data)
    case 'setOutdateModal':
      return state.sf(['showOutdateModal'], action.data)
    case 'setCustomModal':
      return state.sf(['showCustomModal'], action.data)
    case 'setGoodsModal':
      return state.sf(['showGoodsModal'], action.data)
    case 'setBalanceModal':
      return state.sf(['showBalanceModal'], action.data)
    case 'setEditId':
      return state.sf(['editId'], action.data)
    case 'setEditIndex':
      return state.sf(['editIndex'], action.data)
    case 'setEditInfo':
      return state.sf(['editInfo'], action.data)
    case 'setDefaultRate':
      return state.sf(['defaultRate'], action.data)
    case 'setSearchCustomerList':
      return state.sf(['searchCustomerList'], action.data)
    default:
      return state
  }
}
