import { createStore } from 'vuex'
import {showDialog} from "vant";
import router from "../router";

export default createStore({
  state: {
    loading: false,
    screenSize: {
      width: 0,
      height: 0,
    },
    patientCards: [],
    homePageDoctors: [],
    appointmentInfo: {},
    currentExamDate: null,
    examDateRange: [],
    examIndexArray: [],
    covidExamIdCard: null,
    covidExamIndexes: [],
    currentBook: {},
    yjReqNo: null,
    physicalExamNextPage: 1,
    physicalExamListFinished: false,
    physicalExamIndexes: [],
    currentTjid: null,
    appointmentManagementActiveTab: 'unpaid'
  },

  mutations: {
    SET_LOADING: (state, payload) => (state.loading = payload),
    SET_SCREENSIZE: (state, payload) => (state.screenSize = payload),
    SET_PATIENTCARDS: (state, payload) => (state.patientCards = payload),
    SET_HOMEPAGEDOCTORS: (state, payload) => (state.homePageDoctors = payload),
    SET_APPOINTMENTINFO: (state, payload) => (state.appointmentInfo = payload),
    SET_CURRENTEXAMDATE: (state, payload) => (state.currentExamDate = payload),
    SET_EXAMDATERANGE: (state, payload) => (state.examDateRange = payload),
    SET_EXAMINDEXARRAY: (state, payload) => (state.examIndexArray = payload),
    SET_COVIDEXAMIDCARD: (state, payload) => (state.covidExamIdCard = payload),
    SET_COVIDEXAMINDEXES: (state, payload) => (state.covidExamIndexes = payload),
    SET_CURRENTBOOK: (state, payload) => (state.currentBook = payload),
    SET_YJREQNO: (state, payload) => (state.yjReqNo = payload),
    SET_PHYSICALEXAMNEXTPAGE: (state, payload) => (state.physicalExamNextPage = payload),
    SET_PHYSICALEXAMLISTFINISHED: (state, payload) => (state.physicalExamListFinished = payload),
    SET_PHYSICALEXAMINDEXES: (state, payload) => (state.physicalExamIndexes = payload),
    SET_CURRENTTJID: (state, payload) => (state.currentTjid = payload),
    SET_APPOINTMENTMANAGEMENTACTIVETAB: (state, payload) => {state.appointmentManagementActiveTab = payload}
  },

  getters: {
    patientCards(state) {
      return state.patientCards
    },

    alreadyHasCard(state) {
      return state.patientCards.length > 0
    },

    onlyOneCard(state) {
      return state.patientCards.length === 1
    },

    getDefaultCard(state) {
      if (state.patientCards.length === 1) {
        return state.patientCards[0]
      }
      for (let i = 0; i < state.patientCards.length; i ++) {
        if (state.patientCards[i].isDefault === 1) {
          return state.patientCards[i]
        }
      }
      return {}
    },

    hasCertainPatientId: (state) => (patientId) => {
      const index = state.patientCards.findIndex((card) => {
        return card['patientId'] === patientId
      })
      return index !== -1
    },

    getCertainCard: (state) => (patientId) => {
      for (let i = 0; i < state.patientCards.length; i ++) {
        if (state.patientCards[i].patientId === patientId) {
          return state.patientCards[i]
        }
      }
      return {}
    }
  },

  actions: {
    storePatientCards({commit}, payload) {
      if (payload['cards'].length === 0) {
        showDialog({
          title: '提示',
          message: '您的就诊人为空，请前往添加。'
        }).then(() => {
          router.push('/myPatientIdCards')
        });
      } else {
        payload['cards'].forEach(item => {
          item['value'] = item['patientId']
        });
        localStorage.setItem('patientCards', JSON.stringify(payload['cards']))
        commit('SET_PATIENTCARDS', payload['cards'])
      }
    },

    storeHomepageDoctors({ commit }, payload) {
      commit('SET_HOMEPAGEDOCTORS', payload['doctorList'])
    },

    storeAppointmentInfo({ commit }, payload) {
      commit('SET_APPOINTMENTINFO', payload['appointmentInfo'])
    },

    storeExamDateRange({ commit }, payload) {
      commit('SET_EXAMDATERANGE', payload['dateRange'])
    },

    storeCurrentBookItem({ commit }, payload) {
      commit('SET_CURRENTBOOK', payload['bookItem'])
    },
  },
})
