import Vue from 'vue'
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,
  DEFAULT_FONT_SIZE
} from '@/store/mutation-types'

const app = {
  state: {
    sidebar: true,
    device: 'desktop',
    theme: '',
    fontSize:'small',
    layout: '',
    contentWidth: '',
    fixedHeader: false,
    fixSiderbar: false,
    autoHideHeader: false,
    color: null,
    weak: false,
    multiTab: true,
    nowRoute: '', // 当前路由
    isShowVideo: false, // 是否显示视频弹窗
    isNewTab: false,
    closeTabPages: [], // 已关闭的页面
    isNewSubTab: false,
    updateList: false,
    loadingStatus: false,
    winHeight: 0 ,//  窗口高度
    isLastDayForMonth: false, // 是否是当月最后一天
    showPdfPrintView: false, // pdf 打印预览
    pdfPrintList: [] ,// 打印的pdf 文件
    showSelectPrint: false, // 选择打印机
    printTaskName: '', // 打印任务名称
    printTaskID: '', // 打印任务id列表
    printLoading: false, // 正在打印中
    printUseing: '0', // 选择打印机的用途 0 设置打印机，1打印时
    printSettingType: '', // 设置打印机类型，0 针式，1 喷墨
    printDefNeedle: undefined,// 默认针式打印机
    printDefInk: undefined, // 默认喷墨打印机
    returnReason: [], // 申请退货列表
  },
  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) => {
      Vue.ls.set(DEFAULT_THEME, theme)
      localStorage.setItem(DEFAULT_THEME,theme)
      state.theme = theme
    },
    TOGGLE_FONTSIZE:(state, size) => {
      localStorage.setItem(DEFAULT_FONT_SIZE,size)
      state.fontSize = size
      document.body.className = size
    },
    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
    },
    // 当前路由
    TOGGLE_NOW_ROUTER: (state, bool) => {
      state.nowRoute = bool
    },
    // 是否显示视频弹窗
    IS_ShOW_VIDEO: (state, bool) => {
      state.isShowVideo = bool
    },
    SET_WIN_HEIGHT: (state, val) => {
      state.winHeight = val
    },
    SET_loadingStatus: (state, val) => {
      state.loadingStatus = val
    },
    // 打印
    SET_pdfPrintList: (state, val) => {
      state.pdfPrintList = val
    },
    SET_showPdfPrint: (state, val) => {
      state.showPdfPrintView = val && val.length > 0
    },
    SET_showSelectPrint: (state, val) => {
      state.showSelectPrint = val
    },
    SET_printTaskName: (state, val) => {
      state.printTaskName = val
    },
    SET_printTaskID: (state, val) => {
      state.printTaskID = val
    },
    SET_printLoading: (state, val) => {
      state.printLoading = val
    },
    SET_printDefNeedle: (state, val) => {
      state.printDefNeedle = val
      localStorage.setItem('DEFAULT_PRINT_NEEDLE',val)
    },
    SET_printDefInk: (state, val) => {
      state.printDefInk = val
      localStorage.setItem('DEFAULT_PRINT_INK',val)
    },
    SET_printUseing: (state, val) => {
      state.printUseing = val
    },
    SET_printSettingType: (state, val) => {
      state.printSettingType = val
    },
    SET_returnReason:(state, val) => {
      state.returnReason = 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)
    },
    ToggleFontSize({
      commit
    }, size) {
      commit('TOGGLE_FONTSIZE', size)
    },
    TogglePrintDefNeedle({
      commit
    }, str) {
      commit('SET_printDefNeedle', str)
    },
    TogglePrintDefInk({
      commit
    }, str) {
      commit('SET_printDefInk', str)
    },
    TogglePrintUseing({
      commit
    }, str) {
      commit('SET_printUseing', str)
    },
    TogglePrintSettingType({
      commit
    }, str) {
      commit('SET_printSettingType', str)
    },
    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)
    },
    getReturnReasonData({ commit }, code, isEnable) {
      getLookUpData({
        pageNo: 1,
        pageSize: 1000,
        lookupCode: code,
        isEnable: isEnable ? 1 : undefined
      }).then(res => {
        if (res.status == 200) {
          commit('SET_returnReason', res.data.list)
        }else{
          commit('SET_returnReason', [])
        }
      })
    },
  }
}

export default app
