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: {},
    examDateRange: [],
    examIndexArrayMap: {},
    currentExamIndex: {},
    testIndexArrayMap: [],
    pathologyIndexArrayMap: [],
    electroIndexArrayMap: [],
    currentPathologyIndex: {},
    currentElectroIndex: {},
    currentBook: {},
    yjReqNo: null,
    physicalExamNextPage: 1,
    physicalExamListFinished: false,
    physicalExamIndexes: [],
    appointmentManagementActiveTab: 'unpaid',
    createOrderRequest: {},
  },

  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 {}
    },

    getCreateOrderRequest(state) {
      return state.createOrderRequest
    },

    getCurrentExamIndex(state) {
      return state.currentExamIndex
    },

    getCurrentPathologyIndex(state) {
      return state.currentPathologyIndex
    },

    getCurrentElectroIndex(state) {
      return state.currentElectroIndex
    }
  },
  
  mutations: {
    SET_LOADING: (state, payload) => (state.loading = payload),
    SET_SCREEN_SIZE: (state, payload) => (state.screenSize = payload),
    SET_PATIENT_CARDS: (state, payload) => (state.patientCards = payload),
    SET_HOMEPAGE_DOCTORS: (state, payload) => (state.homePageDoctors = payload),
    SET_APPOINTMENT_INFO: (state, payload) => (state.appointmentInfo = payload),
    SET_EXAM_DATE_RANGE: (state, payload) => (state.examDateRange = payload),
    PUSH_EXAM_INDEX_ARRAY: (state, payload) => (state.examIndexArrayMap[payload.id] = payload.val),
    SET_CURRENT_EXAM_INDEX: (state, payload) => (state.currentExamIndex = payload),
    SET_CURRENT_PATHOLOGY_INDEX: (state, payload) => (state.currentPathologyIndex = payload),
    SET_CURRENT_ELECTRO_INDEX: (state, payload) => (state.currentElectroIndex = payload),
    PUSH_TEST_INDEX_ARRAY: (state, payload) => (state.testIndexArrayMap[payload.id] = payload.val),
    PUSH_PATHOLOGY_INDEX_ARRAY: (state, payload) => (state.pathologyIndexArrayMap[payload.id] = payload.val),
    PUSH_ELECTRO_INDEX_ARRAY: (state, payload) => (state.electroIndexArrayMap[payload.id] = payload.val),
    SET_CURRENT_BOOK: (state, payload) => (state.currentBook = payload),
    SET_YJ_REQ_NO: (state, payload) => (state.yjReqNo = payload),
    SET_PHYSICAL_EXAM_NEXT_PAGE: (state, payload) => (state.physicalExamNextPage = payload),
    SET_PHYSICAL_EXAM_LIST_FINISHED: (state, payload) => (state.physicalExamListFinished = payload),
    SET_PHYSICAL_EXAM_INDEXES: (state, payload) => (state.physicalExamIndexes = payload),
    SET_APPOINTMENT_MANAGEMENT_ACTIVE_TAB: (state, payload) => {state.appointmentManagementActiveTab = payload},
    CREATE_ORDER_REQUEST: (state, payload) => (state.createOrderRequest = payload),
  },


  actions: {
    storeScreenSize({ commit }, payload) {
      commit('SET_SCREEN_SIZE', payload['screenSize'])
    },
    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_PATIENT_CARDS', payload['cards'])
      }
    },
    storePatientCardsOnly({ commit }, payload) {
      commit('SET_PATIENT_CARDS', payload['cards'])
    },
    storeHomepageDoctors({ commit }, payload) {
      commit('SET_HOMEPAGE_DOCTORS', payload['doctorList'])
    },

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

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

    storeExamIndexArray({ commit }, payload) {
      commit('PUSH_EXAM_INDEX_ARRAY', payload)
    },

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

    storeCreateOrderRequest({ commit }, payload) {
      commit('CREATE_ORDER_REQUEST', payload['createOrderRequest'])
    },

    storeCurrentExamIndex({ commit }, payload) {
      commit('SET_CURRENT_EXAM_INDEX', payload['currentExamIndex'])
    },

    storeCurrentPathologyIndex({ commit }, payload) {
      commit('SET_CURRENT_PATHOLOGY_INDEX', payload['currentPathologyIndex'])
    },

    storeCurrentElectroIndex({ commit }, payload) {
      commit('SET_CURRENT_ELECTRO_INDEX', payload['currentElectroIndex'])
    },

    storeTestIndexArray({commit}, payload) {
      commit('PUSH_TEST_INDEX_ARRAY', payload)
    },
    
    storePathologyIndexArray({ commit }, payload) {
      commit('PUSH_PATHOLOGY_INDEX_ARRAY', payload)
    },
    
    storeElectroIndexArray({ commit }, payload) {
      commit('PUSH_ELECTRO_INDEX_ARRAY', payload)
    },

    storeYjReqNo({commit}, payload) {
      commit('SET_YJ_REQ_NO', payload['yjReqNo'])
    },

    storePhysicalExamNextPage({commit}, payload) {
      commit('SET_PHYSICAL_EXAM_NEXT_PAGE', payload['physicalExamNextPage'])
    },

    storePhysicalExamListFinished({commit}, payload) {
      commit('SET_PHYSICAL_EXAM_LIST_FINISHED', payload['physicalExamListFinished'])
    },

    storePhysicalExamIndexes({commit}, payload) {
      commit('SET_PHYSICAL_EXAM_INDEXES', payload['physicalExamIndexes'])
    },

    storeAppointmentManagementActiveTab({commit}, payload) {
      commit('SET_APPOINTMENT_MANAGEMENT_ACTIVE_TAB', payload['appointmentManagementActiveTab'])
    },
  },
})
