import { randomString } from '@/utils'
import { setTourist } from '@/pages/common'

const group = {
  namespaced: true,
  state: {
    confirmVisible: false,
    formData: {},
    discount: 0,
    // 当前票种
    currentTicket: null,
    // 加入订单前的游客列表
    touristList: [
      {
        idKey: randomString(),
        guestName: '',
        guestPhone: '',
        guestIdentify: '',
        lock: false // 是否可编辑
      }
    ],
    // 订单子项列表
    orderItems: []
  },
  getters: {
    totalCount: state => {
      return state.orderItems.reduce((init, item) => {
        return init + item.count
      }, 0)
    },
    tickets: state => {
      let tickets = state.orderItems.reduce((init, item) => {
        return init.concat(item.tickets)
      }, [])

      // tickets.forEach(item => {
      //   item.discountPrice = +(item.price * state.discount / 10).toFixed(2)
      // })
      console.log('tickets', tickets)
      return tickets
    },
    orderPrice: (state, getters) => {
      return getters.tickets.reduce((init, item) => {
        return init + (item.originalPrice - 0)
      }, 0).toFixed(2)
    },
    finalPrice: (state, getters) => {
      return getters.tickets.reduce((init, item) => {
        return init + (item.price - 0)
      }, 0).toFixed(2)
    }
  },
  mutations: {
    SET_FORM_DATA (state, data) {
      state.formData = data
    },
    SET_DISCOUNT (state, data) {
      state.discount = data
    },
    SET_ORDER_PRICE (state, data) {
      state.orderPrice = data
    },
    SET_TICKET (state, data) {
      state.currentTicket = data
    },
    RESET_TOURIST (state, data) {
      state.touristList = [{
        idKey: randomString(),
        guestName: '',
        guestPhone: '',
        guestIdentify: '',
        lock: false // 是否可编辑
      }]
    },
    UPDATE_TOURIST (state, data) {
      state.touristList = data
    },
    // num 为要改变的游客数，正数为增加，负数为减少；data是读卡器获取的身份证信息
    SET_TOURIST (state, { num, data }) {
      setTourist(state.touristList, num, data)
    },
    ADD_ORDER_ITEM (state, data) {
      state.orderItems.push(data)
    },
    REMOVE_ORDER_ITEM (state, data) {
      const idx = state.orderItems.findIndex(i => i.keyId === data.keyId)
      state.orderItems.splice(idx, 1)
    },
    RESET_ORDER_ITEM (state, data) {
      state.orderItems = []
    },
    SET_CONFIRM_VISIBLE (state, data) {
      state.confirmVisible = data
    }
  },
  actions: {
    setDiscount ({ commit }, data) {
      commit('SET_DISCOUNT', data)
    },
    setFormData ({ commit }, data) {
      commit('SET_FORM_DATA', data)
    },
    setOrderPrice ({ commit }, data) {
      commit('SET_ORDER_PRICE', data)
    },
    setTicket ({ commit }, data) {
      commit('SET_TICKET', data)
    },
    resetTourist ({ commit }, data) {
      commit('RESET_TOURIST', data)
    },
    updateTourist ({ commit }, data) {
      commit('UPDATE_TOURIST', data)
    },
    setTourist ({ commit }, data) {
      commit('SET_TOURIST', data)
    },
    addOrderItem ({ commit }, data) {
      commit('ADD_ORDER_ITEM', data)
    },
    removeOderItem ({ commit }, data) {
      commit('REMOVE_ORDER_ITEM', data)
    },
    resetOrderItem ({ commit }) {
      commit('RESET_ORDER_ITEM')
    },
    setConfirmVisible ({ commit }, data) {
      commit('SET_CONFIRM_VISIBLE', data)
    }
  }
}

export default group
