/*
 * @Author: your name
 * @Date: 2021-10-14 17:17:53
 * @LastEditTime: 2022-05-18 15:25:20
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \Foshan4A\src\store\modules\app.js
 */
import Cookies from 'js-cookie'

const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus')
      ? !!+Cookies.get('sidebarStatus')
      : true,
    withoutAnimation: false,
    expand: false,
  },
  device: 'desktop',
  dialog: Cookies.get('dialogStatus') === 'true',
  pwdflag: false,
  outflag: false,
  outcheck: false,
  systemSet: Cookies.get('systemSet') != null ? Cookies.get('systemSet') : null,
  queryForm: JSON.parse(sessionStorage.getItem('queryForm')) ?? null,
  passengerDialogFlag: false,
  passengerQueryParams: {
    flightNO: '',
    flightDate: '',
    passengerName: '',
  },
  abnormalBaggageDialogFlag: false,
  abnormalBaggageQueryParams: {
    flightNO: '',
    flightDate: '',
    bagSN: '',
    fileNumber: ''
  }
}
const mutations = {
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  TOGGLE_DIALOG: (state, dialog) => {
    state.dialog = dialog
    Cookies.set('dialogStatus', dialog)
  },
  TOGGLE_PWDFLAG: (state, pwdflag) => {
    state.pwdflag = pwdflag
  },
  TOGGLE_OUTFLAG: (state, outflag) => {
    state.outflag = outflag
  },
  TOGGLE_OUTCHECK: (state, outcheck) => {
    state.outcheck = outcheck
  },
  SYSTEM_SET: (state, systemSet) => {
    state.systemSet = systemSet
    Cookies.set('systemSet', systemSet)
  },
  SET_QUERY_FORM(state, form) {
    state.queryForm = form
    sessionStorage.setItem('queryForm', JSON.stringify(form))
  },
  EXPAND_SIDEBAR: (state, expandFlag) => {
    state.sidebar.expand = expandFlag
  },
  TOGGLE_PASSENGER_DIALOG_FLAG(state, flag) {
    state.passengerDialogFlag = flag
  },
  SET_PASSENGER_QUERY_PARAMS(state, params) {
    state.passengerQueryParams = params
  },
  TOGGLE_ABNORMAL_BAGGAGE_DIALOG_FLAG(state, flag) {
    state.abnormalBaggageDialogFlag = flag
  },
  SET_ABNORMAL_BAGGAGE_QUERY_PARAMS(state, params) {
    state.abnormalBaggageQueryParams = params
  }
}

const actions = {
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  toggleDialog({ commit }, dialog) {
    commit('TOGGLE_DIALOG', dialog)
  },
  togglePwdflag({ commit }, pwdflag) {
    commit('TOGGLE_PWDFLAG', pwdflag)
  },
  toggleOutflag({ commit }, outflag) {
    commit('TOGGLE_OUTFLAG', outflag)
  },
  toggleOutcheck({ commit }, outcheck) {
    commit('TOGGLE_OUTCHECK', outcheck)
  },
  getSystemSet({ commit }, systemSet) {
    commit('SYSTEM_SET', systemSet)
  },
  setQueryForm({ commit }, form) {
    commit('SET_QUERY_FORM', form)
  },
  expandSidebar({ commit }, expandFlag) {
    commit('EXPAND_SIDEBAR', expandFlag)
  },
  togglePassengerDialogFlag({ commit }, flag) {
    commit('TOGGLE_PASSENGER_DIALOG_FLAG', flag)
  },
  setPassengerQueryParams({ commit }, params) {
    commit('SET_PASSENGER_QUERY_PARAMS', params)
  },
  toggleAbnormalBaggageDialogFlag({ commit }, flag) {
    commit('TOGGLE_ABNORMAL_BAGGAGE_DIALOG_FLAG', flag)
  },
  setAbnormalBaggageQueryParams({ commit }, params) {
    commit('SET_ABNORMAL_BAGGAGE_QUERY_PARAMS', params)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
