import moment from 'moment'

const initState = {
  dtStart: moment().subtract(1, "day").startOf('day').startOf('minute').format("YYYY-MM-DDTHH:mm:ss"),
  dtFinish: moment().endOf('day').startOf('minute').format("YYYY-MM-DDTHH:mm:ss"),
  imei: "",
  pageState: "await", // await | loading | isLoaded | error
  packsData: [{
    pack_number: 1,
    count: 10,
    pack_dt: new Date(),
  }]
}

const state = {
    ...initState
}

const getters = {
  dtStart:   (state)  => new Date(state.dtStart),
  dtFinish:  (state)  => new Date(state.dtFinish),
  imei:      (state)  => state.imei,
  pageState: (state)  => state.pageState,
  packsData: (state)  => state.packsData,
}

const mutations = {
  setDtStart:   (state, updVal) => state.dtStart = moment(updVal).format("YYYY-MM-DDTHH:mm:ss"),
  setDtFinish:  (state, updVal) => state.dtFinish = moment(updVal).format("YYYY-MM-DDTHH:mm:ss"),
  setImei:      (state, updVal) => {
    console.log(updVal)
    state.imei = updVal
  }
}


const actions = {
  uploadData: ({commit, state}) => {
    setTimeout(() => {
      state.pageState = 'loading'
    }, 500);
    setTimeout(() => {
      state.pageState = 'isLoaded'
    }, 3000);
  }
}

export const store = {
    namespaced: true,
    state,
    getters,
    mutations,
    actions,
}
