import Cookies from 'js-cookie'
import { Notification } from 'element-ui'
import { getBatchList } from '@/api/batch'
import { getConfigProject } from '@/api/project'
import { getLimitGroupList } from '@/api/systemSetting/limitGroup'
import { getScenic } from '@/api/ticketSetting/scenic'
import { getSalesChannel } from '@/api/ticketSetting/channel'
import { getTicketPriceStragegy, getTicketTag } from '@/api/ticketSetting/ticket'
import { getTicketProduct } from '@/api/ticketSetting/ticketProduct'
import { getPayWay } from '@/api/orderSetting/order'
import { getAdvPosition } from '@/api/adv'

const app = {
  getters: {
    refundMinDays (state) {
      return state.projectConfig.refund_latest_day ?? -30
    }
  },
  state: {
    projectConfig: {
      refund_latest_day: -30 // 提前可退天数最小值
    },
    appStatus: {
      status: 1,
      process: 0
    },
    client: null,
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      withoutAnimation: false
    },
    device: 'desktop',
    language: Cookies.get('language') || 'zh',
    ticketTagLoading: false,
    tagList: [],
    scenicList: [],
    salesChannel: [],
    batchList: [],
    ticketTypeList: [],
    greyList: [],
    goodsGroupList: [],
    scenic_type: '',
    limitGroupList: [],
    ticketProductLoading: false,
    ticketProductList: [],
    strategies: '',
    payWayList: []
  },
  mutations: {
    SET_PROJECT_CONFIG: (state, data) => {
      state.projectConfig = data
    },
    SET_AD_POSITION: (state, data) => {
      state.adPostionList = data
    },
    SET_PAY_WAY: (state, data) => {
      state.payWayList = data
    },
    SET_SALES_CHANNEL: (state, data) => {
      state.salesChannel = data
    },
    SET_SCENIC_LIST: (state, data) => {
      state.scenicList = data
    },
    SET_BATCH_LIST: (state, data) => {
      state.batchList = data
    },
    SET_TICKET_TYPE_LIST: (state, data) => {
      state.ticketTypeList = data
    },
    SET_GREY_LIST: (state, data) => {
      state.greyList = data
    },
    SET_GOODS_GROUP_LIST: (state, data) => {
      state.goodsGroupList = data
    },
    SET_SCENIC_TYPE: (state, data) => {
      state.scenic_type = data
    },
    SET_LIMIT_GROUP_LIST: (state, data) => {
      state.limitGroupList = data
    },
    SET_TICKET_PRODUCT_LOADING: (state, data) => {
      state.ticketProductLoading = data
    },
    SET_TICKET_PRODUCT: (state, data) => {
      state.ticketProductList = data
    },
    SET_TICKET_TAG_LOADING: (state, data) => {
      state.ticketTagLoading = data
    },
    SET_TICKET_TAG: (state, data) => {
      state.tagList = data
    },
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SET_LANGUAGE: (state, language) => {
      state.language = language
      Cookies.set('language', language)
    },
    SHOW_ROOM: (state, room) => {
      state.dialogVisible = true
      state.globleRoom = room
    },
    HIDE_ROOM: (state) => {
      state.dialogVisible = false
    },
    SHOW_WXNOTIFY: (state) => {
      // if (state.wxNoify.visible) return
      // console.log(Notification)
      state.wxNoify = Notification.info({
        iconClass: 'iconfont icon-auth',
        title: '提醒',
        dangerouslyUseHTMLString: true,
        customClass: 'authNotify',
        duration: 0,
        message: `有新的待授权用户需处理`,
        position: 'bottom-right'
      })
    },
    UPDATE_APP: (state, data) => {
      state.appStatus = data
    },
    SET_STRATEGIES: (state, data) => {
      state.strategies = data
    }
  },
  actions: {
    // 获取项目配置项
    getProjectConfig ({ commit }) {
      getConfigProject().then(res => {
        commit('SET_PROJECT_CONFIG', res.extraInfo)
      })
    },
    getAdvPosition ({ commit }) {
      const params = {
        pageNum: 0,
        pageSize: 0,
        type: 'WX',
        status: ['active']
      }

      getAdvPosition(params).then(res => {
        commit('SET_AD_POSITION', res.extraInfo.list)
      })
    },
    getPayWay ({ commit }) {
      getPayWay().then(res => {
        // console.log(res)
        commit('SET_PAY_WAY', res.extraInfo)
      })
    },
    getSalesChannel ({ commit }) {
      getSalesChannel().then(res => {
        // console.log(res)
        commit('SET_SALES_CHANNEL', res.extraInfo.list)
      })
    },
    getBatchList ({ commit }) {
      getBatchList({ pageSize: 0, pageNum: 0 }).then(res => {
        commit('SET_BATCH_LIST', res.extraInfo.list)
      })
    },
    getScenicType ({ commit }) {
      getConfigProject().then(res => {
        commit('SET_SCENIC_TYPE', res.extraInfo.scenic_type)
      })
    },
    getScenicList ({ commit }) {
      getScenic({ pageNum: 0, pageSize: 0 }).then(res => {
        const getSenicList = res.extraInfo.list.filter(e => e.id !== 9999)
        commit('SET_SCENIC_LIST', getSenicList)
      })
    },
    getTicketProduct ({ commit }) {
      commit('SET_TICKET_PRODUCT_LOADING', true)

      getTicketProduct({ pageNum: 0, pageSize: 0 }).then(res => {
        commit('SET_TICKET_PRODUCT', res.extraInfo.list)
      }).finally(() => {
        commit('SET_TICKET_PRODUCT_LOADING', false)
      })
    },
    getTicketTag ({ commit }) {
      commit('SET_TICKET_TAG_LOADING', true)

      getTicketTag({}).then(res => {
        commit('SET_TICKET_TAG', res.extraInfo.list)
      }).finally(() => {
        commit('SET_TICKET_TAG_LOADING', false)
      })
    },
    getTicketPriceStragegy ({ commit }) {
      const stragegiesMap = {
        PERIOD: '日期段价格',
        WEEKDAY: '周价格',
        BASIC: '日常价格'
      }
      getTicketPriceStragegy().then(res => {
        const strategies = res.extraInfo.strategies.map(i => stragegiesMap[i]).join(' > ')
        commit('SET_STRATEGIES', strategies)
      })
    },
    getLimitGroupList ({ commit }) {
      const params = {
        pageNum: 0,
        pageSize: 0,
        total: 0,
        status: ['active', 'inactive']
      }
      getLimitGroupList(params).then(res => {
        const limitGroup = res.extraInfo.list
        commit('SET_LIMIT_GROUP_LIST', limitGroup)
      })
    },
    updateApp ({ commit }, data) {
      commit('UPDATE_APP', data)
    },
    toggleSideBar ({ commit }) {
      commit('TOGGLE_SIDEBAR')
    },
    closeSideBar ({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    toggleDevice ({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    setLanguage ({ commit }, language) {
      commit('SET_LANGUAGE', language)
    },
    showRoom ({ commit }, room) {
      commit('SHOW_ROOM', room)
    },
    hideRoom ({ commit }) {
      commit('HIDE_ROOM')
    }
  }
}

export default app
