import Vue from 'vue'
import { employeeQueryList } from '@/api/expenseManagement'
import { getLookUpData, listLookUp } from '@/api/data'
import {
  SIDEBAR_TYPE,
  DEFAULT_THEME,
  DEFAULT_LAYOUT_MODE,
  DEFAULT_COLOR,
  DEFAULT_COLOR_WEAK,
  DEFAULT_FIXED_HEADER,
  DEFAULT_FIXED_SIDEMENU,
  DEFAULT_FIXED_HEADER_HIDDEN,
  DEFAULT_CONTENT_WIDTH_TYPE,
  DEFAULT_MULTI_TAB
} from '@/store/mutation-types'

const app = {
  state: {
    sidebar: true,
    device: 'desktop',
    theme: '',
    layout: '',
    contentWidth: '',
    fixedHeader: false,
    fixSiderbar: false,
    autoHideHeader: false,
    color: null,
    weak: false,
    multiTab: true,
    isNewTab: false,
    isNewSubTab: false,
    updateList: false,
    winHeight: 0, //  窗口高度
    loadingStatus: false,
    closeTabPages: [], // 已关闭的页面
    employeeList: [], // 申请人列表
    defectiveReturnReason: [], // 不良品申请退货列表
    goodReturnReason: [], // 良品申请退货列表
    bookReason: [], // 收款事由
    priceLeveList: [], // 价格等级
    tempBillOfData: null, // 临时存储收货单数据
    authMenusList: [], // 权限菜单数据
    priceAuthOptions: [], // 价格权限选项
    curActionPermission: '', // 当前激活状态的权限码
    showPdfPrintView: false, // pdf 打印预览
    pdfUrl: '',
    productBrandAllList: null, // 产品品牌列表
    productTypeAllList: null, // 产品分类列表
    warehouseAuthList: null, // 有权限的仓库列表
    warehouseAllList: null, // 无权限所有仓库列表
    defWarehouse: null, // 默认仓库
    isWarehouse: false, // 仓库管理权限
    isShowCustomerSearch: false, // 客服查询权限
    interFaceList: [
      // 销售单编辑、下推、审核
      {
        a: 'sales/findBySn',
        b: [
          'sales/auditPass',
          'sales/auditReject',
          'sales/auditPush',
        ]
      },
      {
        a: 'sales/findBaseBySn',
        b: [
          'sales/submit',
          'sales/detail/insert',
          'sales/detail/delete',
          'sales/detail/updateWarehouse',
          'sales/detail/updateQty',
          'sales/detail/delete/batch',
          'sales/detail/delete/all',
          'sales/detail/disablePromo',
          'sales/detail/enablePromo',
          'sales/detail/changePromo',
          'sales/detail/addPromo',
          'sales/detail/updateBatch',
          'sales/detail/insertBatch',
          'sales/detail/cancelAll',
          'sales/detail/cancleOfBatch',
          'sales/detail/pushOfBatch',
          'dispatch/detail/deleteBatch',
          'dispatch/detail/updateQty',
          'dispatch/pushDown',
          'sales/detail/batchTransferOfPurchaseAmount'
        ],
        timeStr: ''
      },
      // 销售退货
      {
        a: 'salesReturn/findBySn',
        b: [
          'salesReturn/submitVerify',
          'salesReturn/submit',
          'salesReturn/againCreate',
          'salesReturn/receive',
          'salesReturn/check',
          'salesReturn/detail/insert',
          'salesReturn/detail/batchInsert',
          'salesReturn/detail/delete',
          'salesReturn/detail/updateReason',
          'salesReturn/detail/updateBackStockQty',
          'salesReturn/detail/updateQty',
          'salesReturn/detail/setReturnReason',
          'salesReturn/detail/updateReceiveQty',
          'salesReturn/detail/updateBatchGoodQty',
          'salesReturn/detail/updateBatchBackStockQty',
          'salesReturn/detail/receiveBatch',
          'salesReturn/detail/updateByCustomerService',
          'salesReturn/customerServiceConfirm',
          'salesReturn/detail/setReturnPrice',
          'salesReturn/detail/deleteByCustomerService',
          'salesReturn/detail/deleteByReceiver',
          'salesReturn/detail/insertByReceive',
          'salesReturn/detail/insertByCustomerService',
          'salesReturn/detail/updateAllReason'
        ],
        timeStr: ''
      },
      // 散件入库
      {
        a: 'spareParts/queryBySn',
        b: [
          'spareParts/saveDetail',
          'spareParts/deleteDetail',
          'spareParts/detail/parseProducts',
          'spareParts/detail/batchInsert',
          'spareParts/queryDetailPageCount'
        ],
        timeStr: ''
      },
      // 采购退货
      {
        a: 'sparePartsReturn/queryBySn',
        b: [
          'sparePartsReturn/submit',
          'sparePartsReturn/detail/delete',
          'sparePartsReturn/detail/save',
          'sparePartsReturn/detail/saveBatch'
        ],
        timeStr: ''
      },
      // 采购入库
      {
        a: 'purchase/queryBySn',
        b: [
          'purchase/saveDetail',
          'purchase/deleteDetail',
          'purchase/detail/batchInsert',
          'purchase/setSupplierInfo',
          'purchase/submit'
        ],
        timeStr: ''
      },
      // 调拨
      {
        a: 'allocateBill/findBySn',
        b: [
          'allocateBill/submit',
          'allocateBill/save',
          'allocateBill/detail/updateBatchDepartment',
          'allocateBill/detail/delete',
          'allocateBill/detail/deleteAll',
          'allocateBill/detail/save',
          'allocateBill/detail/parseProducts',
          'allocateBill/detail/batchInsert',
          ''
        ],
        timeStr: ''
      },
      // 调拨退货
      {
        a: 'allocateReturn/queryBySn',
        b: [
          'allocateReturn/submit',
          'allocateReturn/check',
          'allocReturnDetail/check',
          'allocReturnDetail/delete',
          'allocReturnDetail/insert',
          'allocReturnDetail/insertBatch',
          'allocReturnDetail/update'
        ],
        timeStr: ''
      },
      // 发货单
      {
        a: 'sendBill/findBySn',
        b: [
          'sendBill/update',
          'sendBill/updateIssueRemark',
          'sendBill/updateTransportInfo'
        ],
        timeStr: ''
      },
      // 提货单
      {
        a: 'pickUp/queryBySn',
        b: [
          'pickUp/submit',
          'pickUp/save'
        ]
      }
    ]
  },
  mutations: {
    SET_SIDEBAR_TYPE: (state, type) => {
      state.sidebar = type
      Vue.ls.set(SIDEBAR_TYPE, type)
    },
    CLOSE_SIDEBAR: (state) => {
      Vue.ls.set(SIDEBAR_TYPE, true)
      state.sidebar = false
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    TOGGLE_THEME: (state, theme) => {
      // setStore('_DEFAULT_THEME', theme)
      Vue.ls.set(DEFAULT_THEME, theme)
      state.theme = theme
    },
    TOGGLE_LAYOUT_MODE: (state, layout) => {
      Vue.ls.set(DEFAULT_LAYOUT_MODE, layout)
      state.layout = layout
    },
    TOGGLE_FIXED_HEADER: (state, fixed) => {
      Vue.ls.set(DEFAULT_FIXED_HEADER, fixed)
      state.fixedHeader = fixed
    },
    TOGGLE_FIXED_SIDERBAR: (state, fixed) => {
      Vue.ls.set(DEFAULT_FIXED_SIDEMENU, fixed)
      state.fixSiderbar = fixed
    },
    TOGGLE_FIXED_HEADER_HIDDEN: (state, show) => {
      Vue.ls.set(DEFAULT_FIXED_HEADER_HIDDEN, show)
      state.autoHideHeader = show
    },
    TOGGLE_CONTENT_WIDTH: (state, type) => {
      Vue.ls.set(DEFAULT_CONTENT_WIDTH_TYPE, type)
      state.contentWidth = type
    },
    TOGGLE_COLOR: (state, color) => {
      Vue.ls.set(DEFAULT_COLOR, color)
      state.color = color
    },
    TOGGLE_WEAK: (state, flag) => {
      Vue.ls.set(DEFAULT_COLOR_WEAK, flag)
      state.weak = flag
    },
    TOGGLE_MULTI_TAB: (state, bool) => {
      Vue.ls.set(DEFAULT_MULTI_TAB, bool)
      state.multiTab = bool
    },
    SET_WIN_HEIGHT: (state, val) => {
      state.winHeight = val
    },
    SET_loadingStatus: (state, val) => {
      state.loadingStatus = val
    },
    SET_employeeList: (state, val) => {
      state.employeeList = val
    },
    SET_returnReason: (state, obj) => {
      state[obj.key] = obj.val
    },
    SET_showPdfPrint: (state, val) => {
      state.showPdfPrintView = val
    },
    SET_PdfUrl: (state, val) => {
      state.pdfUrl = val
    }
  },
  actions: {
    setSidebar ({ commit }, type) {
      commit('SET_SIDEBAR_TYPE', type)
    },
    CloseSidebar ({ commit }) {
      commit('CLOSE_SIDEBAR')
    },
    ToggleDevice ({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    ToggleTheme ({ commit }, theme) {
      commit('TOGGLE_THEME', theme)
    },
    ToggleLayoutMode ({ commit }, mode) {
      commit('TOGGLE_LAYOUT_MODE', mode)
    },
    ToggleFixedHeader ({ commit }, fixedHeader) {
      if (!fixedHeader) {
        commit('TOGGLE_FIXED_HEADER_HIDDEN', false)
      }
      commit('TOGGLE_FIXED_HEADER', fixedHeader)
    },
    ToggleFixSiderbar ({ commit }, fixSiderbar) {
      commit('TOGGLE_FIXED_SIDERBAR', fixSiderbar)
    },
    ToggleFixedHeaderHidden ({ commit }, show) {
      commit('TOGGLE_FIXED_HEADER_HIDDEN', show)
    },
    ToggleContentWidth ({ commit }, type) {
      commit('TOGGLE_CONTENT_WIDTH', type)
    },
    ToggleColor ({ commit }, color) {
      commit('TOGGLE_COLOR', color)
    },
    ToggleWeak ({ commit }, weakFlag) {
      commit('TOGGLE_WEAK', weakFlag)
    },
    ToggleMultiTab ({ commit }, bool) {
      commit('TOGGLE_MULTI_TAB', bool)
    },
    SetWinHeight ({ commit }, val) {
      commit('SET_WIN_HEIGHT', val)
    },
    GetEmployeeList ({ commit }, val) {
      employeeQueryList({}).then(res => {
        if (res.status == 200) {
          commit('SET_employeeList', res.data)
        } else {
          commit('SET_employeeList', [])
        }
      })
    },
    getLookUpDataByCode ({ commit }, params) {
      getLookUpData({
        pageNo: 1,
        pageSize: 1000,
        lookupCode: params.code,
        isEnable: params.isEnable
      }).then(res => {
        if (res.status == 200) {
          commit('SET_returnReason', { key: params.key, val: res.data.list })
        }
      })
    }
  }
}

export default app
