import axios from 'axios';
import { createStore } from 'vuex';
import {
  ADMIN_GET_SITE_SETTING,
  ADMIN_LAB_KIT_ADD_API,
  ADMIN_LAB_KIT_DELETE_API,
  ADMIN_LAB_KIT_LIST_API,
  ADMIN_LAB_KIT_UPDATE_API,
  ADMIN_LOGIN_DETAIL,
  ADMIN_PATIENT_DETAIL_API,
  ADMIN_PATIENT_PROFILE_API,
  ADMIN_PROVIDER_DETAIL_API,
  ADMIN_UPDATE_PASSWORD,
  ADMIN_UPDATE_SITE_SETTING,
  APPOINTMENT_DETAILS_API,
  LABS_DELETE_API,
  LABS_LIST_API,
  LABS_UPDATE_API,
  MEDICINE_ADD_QUESTIONERIES_EXCEL_API,
  MEDICINE_DELETE_API,
  MEDICINE_LIST_API,
  MEDICINE_SAVE_API,
  MEDICINE_UPDATE_API,
  MEETING_NOTES_API,
  MEETING_PRESCREPTIONS_API,
  PATIENT_DELETE_API,
  PATIENT_FILTER_LIST_API,
  PATIENT_LABKIT_LIST_API,
  PATIENT_LABKIT_STATUS_UPDATE_API,
  PATIENT_LIST_API,
  PATIENT_MEETING_LIST_API,
  PATIENT_PRESCRIPTION_STATUS_UPDATE_API,
  PATIENT_UPDATE_API,
  PROFILE_UPDATE_API,
  PROVIDER_DELETE_API,
  PROVIDER_LIST_API,
  PROVIDER_MEETING_LIST_API,
  PROVIDER_UPDATE_API,
  SUBCRIPTIONS_LIST_API
} from './constants.js';


export default createStore({
  state: {
    isLoading: false,
    isErrorMsg: false,
    isSuccessMsg: false,
    patientList:[],
    patientMeetingList: [],
    patientLabKitList:[],
    providerMeetingList:[],
    providersList:[],
    labsList:[],
    subcriptions:[],
    patientLabKitStatus:'',
    patientPrescriptionStatus:'',
    singlePatientAppointment: null,
    patientPrescription:null,
    patientNotes: null,
    medicineList: [],
    questioneriesList: [],
    adminDetail: null,
    siteSetting: null,
    showMessage: null,
    timeout: null,
    checkLoginExpire: false,
    labKitList: [],
    patientDetail: null,
    providerDetail:null,
    patientAnswers: null,
  },
  mutations: {
    setLoading(state, payload) {
      console.log('payload');
      state.isLoading = payload
    },
    setCheckLoginExpire(state, payload) {
      console.log('payload');
        state.checkLoginExpire = payload
    },
    setErrorMsg(state, payload) {
      console.log('payload');
       clearTimeout(state.timeout)
      state.isErrorMsg = payload
       state.timeout = setTimeout(() => {
      state.isErrorMsg = false
    }, 5000)
    },
    setSuccessMsg(state, payload) {
      console.log('payload');
        clearTimeout(state.timeout)
      state.isSuccessMsg = payload
       state.timeout = setTimeout(() => {
      state.isSuccessMsg = false
    }, 5000)
    },
     setShowMsg(state, payload) {
      console.log('payload');
       state.showMessage = payload
       
    },
    setSubcriptions(state, payload) {
      state.subcriptions = payload
    },  
     setPatientDetail(state, payload) {
      console.log('payload');
        state.patientDetail = payload
    },
      setProviderDetail(state, payload) {
      console.log('payload');
        state.providerDetail = payload
    },
      setProviderMeetingList(state, payload) {
      console.log('payload');
        state.providerMeetingList = payload
    },
      
    setPtientList(state, payload) {
      state.patientList = payload
    },
    setPtientMeetingList(state, payload) {
      state.patientMeetingList = payload
    },
    setPtientMeetingList(state, payload) {
      state.patientMeetingList = payload
    },
    setPatientLabKitList(state, payload) {
      state.patientLabKitList = payload
    },
    setProvidersList(state, payload) {
      state.providersList = payload
    },
    setLabsList(state, payload) {
      state.labsList = payload
    },
    setLabKitList(state, payload) {
      state.labKitList = payload
    },
    setPrescription(state, payload) {
      state.patientPrescription = payload
    },
    setSinglePatientAppointment(state, payload) {
      state.singlePatientAppointment = payload
    },
    setPatientNotes(state, payload) {
      state.patientNotes = payload
    },
     setMedicineList(state, payload) {
      state.medicineList = payload
    },
    setQuestionersList(state, payload) {
      state.questioneriesList = payload
    },
    setAdminDetail(state, payload) {
      state.adminDetail = payload
    },
    setSiteSetting(state, payload) {
      state.siteSetting = payload
    },
    setPatientLabKitStatus(state, payload) {
      state.patientLabKitStatus = payload
    },
    setPatientPrescriptionStatus(state, payload) {
      state.patientPrescriptionStatus = payload
    },
    setPatientAnswers(state, payload) {
      state.patientAnswers = payload
    },
    
    
    
  },
  actions: {
    
    async updateIsLoading({ commit }, payload) {
        commit('setLoading', payload)
    },
    async updateCheckToken({ commit }, payload) {
        commit('setCheckLoginExpire', payload)
    },
    async patientList({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(PATIENT_LIST_API, {
        plan:  'all' ,
        gender: 'all',
        state: 'all',
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Response:', response.data.patients);
            let dataArray =[]
            for (let data of response.data.patients) {
              let dataObject = {}
              dataObject.name = data.first_name + ' ' + data.last_name
              dataObject.first_name = data.first_name 
              dataObject.last_name =  data.last_name
              dataObject.email = data.email
              dataObject.dob = data.dob
              dataObject.phone_no = data.phone_no
              dataObject.avatar = '',
              dataObject.id = data.id,
              dataArray.push(dataObject)
            }
            console.log(dataArray)
            commit('setPtientList',dataArray)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async PatientFilter({ commit,state }, payload) {
      commit('setLoading', true)
      await axios.post(PATIENT_FILTER_LIST_API, {
        plan: payload.plan ? payload.plan: 'all' ,
        gender: payload.gender ? payload.gender: 'all',
        state: payload.state? payload.state :'all',
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response Patient:', response.data.patients);
            let dataArray =[]
            for (let data of response.data.patients) {
              let dataObject = {}
              dataObject.name = data.first_name + ' ' + data.last_name
              dataObject.first_name = data.first_name 
              dataObject.last_name =  data.last_name
              dataObject.email = data.email
              dataObject.dob = data.dob
              dataObject.phone_no = data.phone_no
              dataObject.avatar = '',
              dataObject.id = data.id,
              dataArray.push(dataObject)
            }
            console.log(dataArray)
            commit('setPtientList',dataArray)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async getSubcriptions ({commit,state},payload){
      commit('setLoading', true)
      await axios.post(SUBCRIPTIONS_LIST_API, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response Subcriptions:', response.data);
            commit('setSubcriptions',response.data);
          })
          .catch(error => {
            commit('setLoading', false)
            commit('setPrescription',null)
            console.error('Error:', error);
          });
    },
    async patientMeetingList({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(PATIENT_MEETING_LIST_API+payload.id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Meeting Response:', response.data.patients);
            commit('setPtientMeetingList',response.data.patients);

           })
           .catch(error => {
             commit('setLoading', false)
             console.error('Error:', error);
           });
    },
    async providerMeetingList({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(PROVIDER_MEETING_LIST_API+payload.id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Meeting Response:', response.data.patients);
            commit('setProviderMeetingList',response.data.patients);

           })
           .catch(error => {
             commit('setLoading', false)
             console.error('Error:', error);
           });
    },
    async patientUpdate({ commit }, payload) {
      commit('setLoading', true)
      await axios.post(PATIENT_UPDATE_API+payload.id, {
        first_name: payload.first_name,
        last_name: payload.last_name,
        email: payload.email,
        phone_no: payload.phone_no,
        dob: payload.dob,
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async patientDelete({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(PATIENT_DELETE_API+payload.id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async providersList({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(PROVIDER_LIST_API, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Response:', response.data.patients);
            let dataArray =[]
            for (let data of response.data.patients) {
              let dataObject = {}
              dataObject.name = data.first_name + ' ' + data.last_name
              dataObject.first_name = data.first_name 
              dataObject.last_name =  data.last_name
              dataObject.email = data.email
              // dataObject.dob = data.dob
              dataObject.phone_no = data.phone_number
              dataObject.avatar = '',
              dataObject.id = data.id,
              dataArray.push(dataObject)
            }
            console.log(dataArray)
            commit('setProvidersList',dataArray)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async providerUpdate({ commit }, payload) {
      commit('setLoading', true)
      await axios.post(PROVIDER_UPDATE_API+payload.id, {
        first_name: payload.first_name,
        last_name: payload.last_name,
        email: payload.email,
        phone_number: payload.phone_no,
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
              if (response.data.message && response.status === 200) {
                  commit('setSuccessMsg', true)
                  console.log('Response:', response.data.message);
                  commit('setShowMsg', response.data.message)
              }
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async providerDelete({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(PROVIDER_DELETE_API+payload.id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async labsList({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(LABS_LIST_API, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Response:', response.data);
            let dataArray =[]
            for (let data of response.data.patients) {
              let dataObject = {}
              dataObject.name = data.name
              dataObject.address = data.address 
              dataObject.slot_date =  data.slot_date
              dataObject.slot_time =  data.slot_time
              dataObject.appointment_id = data.appointment_id
              dataObject.booking_time = data.booking_time
              dataObject.contact_no = data.contact_no
              dataObject.avatar = '',
              dataObject.id = data.id,
              dataArray.push(dataObject)
            }
            console.log(dataArray)
            commit('setLabsList',dataArray)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async labUpdate({ commit }, payload) {
      commit('setLoading', true)
      await axios.post(LABS_UPDATE_API+payload.id, {
        name: payload.name,
        address: payload.address,
        contact_no: payload.contact_no,
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async labDelete({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(LABS_DELETE_API+payload.id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async patientLabKitList ({commit,state},payload){
      commit('setLoading', true)
      await axios.post(PATIENT_LABKIT_LIST_API+payload.patient_id ,{}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
    })  .then(response => {
              commit('setLoading', false)
              console.log('Response Labkit:', response.data);
              commit('setPatientLabKitList',response.data)
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async updateLabkitListStatus ({commit,state},payload){
      commit('setLoading', true)
      await axios.post(PATIENT_LABKIT_STATUS_UPDATE_API+payload.cart_id ,{
          status:payload.status
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
    })  .then(response => {
              commit('setLoading', false)
              commit('setPatientLabKitStatus',response.data);
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async updatePrescriptionStatus ({commit,state},payload){
      commit('setLoading', true)
      await axios.post(PATIENT_PRESCRIPTION_STATUS_UPDATE_API+payload.prescription_id ,{
          status:payload.status
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
    })  .then(response => {
              commit('setLoading', false)
              commit('setPatientPrescriptionStatus',response.data);
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async getAppointmentByIdAgent ({commit,state},payload){
      commit('setLoading', true)
      await axios.post(APPOINTMENT_DETAILS_API+payload.patient_id + '/' + payload.appointment_id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
    })  .then(response => {
              commit('setLoading', false)
              commit('setSinglePatientAppointment',response.data.data)
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async getPrescriptions ({commit,state},payload){
      commit('setLoading', true)
      await axios.post(MEETING_PRESCREPTIONS_API+payload.patient_id + '/' + payload.appointment_id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
      commit('setLoading', false)
      let doctor={}
      let itemsPrescriptions=[]
                for (let data of response.data) {
                  let dataObject = {}
                  dataObject.first_name = data.patient.first_name
                  dataObject.last_name = data.patient.last_name
                  dataObject.gender = data.patient.gender
                  dataObject.dob = data.patient.dob
                  dataObject.patient_address = data.patient.address+' '+ data.patient.city+' '+ data.patient.state +' ' +data.patient.country
                  dataObject.name = data.prescription.name
                  dataObject.brand = data.prescription.brand
                  dataObject.from = data.prescription.from
                  dataObject.direction_quantity = data.prescription.direction_quantity
                  dataObject.dosage = data.prescription.dosage
                  dataObject.quantity = data.prescription.quantity
                  dataObject.refill_quantity = data.prescription.refill_quantity
                  dataObject.actions = ''
                  dataObject.id = data.prescription.id
                  dataObject.comments = data.comments
                  dataObject.direction_one = data.direction_one
                  dataObject.direction_two = data.direction_two
                  dataObject.doctor = data.telemedPro
                  dataObject.status = data.status
                  dataObject.date = data.created_at
                  dataObject.agent_sign = data.telemedPro.name
                  dataObject.medical_license_number = data.telemedPro.medical_license_number
                  itemsPrescriptions.push(dataObject)
                  
                }
              console.log('Response Notes:', response.data);
      
              commit('setPrescription',itemsPrescriptions)
          })
          .catch(error => {
            commit('setLoading', false)
            commit('setPrescription',null)
            console.error('Error:', error);
          });
    },
    async getHistoryPatientNotes ({commit,state},payload){
      commit('setLoading', true)
      await axios.post(MEETING_NOTES_API+payload.patient_id + '/' + payload.appointment_id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
    })  .then(response => {
              commit('setLoading', false)
              commit('setPatientNotes',response.data.data)
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
     async medicineList({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(MEDICINE_LIST_API, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Response:', response.data);
            let dataArray =[]
            // for (let data of response.data.medicines) {
            //   let dataObject = {}
            //   dataObject.title = data.title
            //   dataObject.slug = data.slug 
            //   dataObject.price =  data.price
            //   dataObject.slot_time =  data.slot_time
            //   dataObject.appointment_id = data.appointment_id
            //   dataObject.booking_time = data.booking_time
            //   dataObject.contact_no = data.contact_no
            //   dataObject.avatar = '',
            //   dataObject.id = data.id,
            //   dataArray.push(dataObject)
            // }
            console.log(dataArray)
            commit('setMedicineList',response.data.medicines)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async medicineUpdate({ commit }, payload) {
      commit('setLoading', true)
      await axios.post(MEDICINE_UPDATE_API+payload.id, {
        title: payload.title,
        slug: payload.slug,
        list_one_title: payload.list_one_title,
        list_sub_title: payload.list_sub_title,
        list_two_title: payload.list_two_title,
        price: payload.price,
        currency: payload.currency,
        excel: payload.excel,
        image:payload.image
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
            if (response.data.message=='success' && response.status === 200) {
              commit('setSuccessMsg', true)
              console.log('Response:', response.data.message);
              commit('setShowMsg', 'Successfully Updated')
            }
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async medicineDelete({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(MEDICINE_DELETE_API+payload.id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
            if (response.data.message && response.status === 200) {
              commit('setSuccessMsg', true)
              console.log('Response:', response.data.message);
              commit('setShowMsg', 'Successfully Deleted')
            }
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async questioneriesListExcel({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(MEDICINE_ADD_QUESTIONERIES_EXCEL_API, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Response:', response.data);
        
            commit('setQuestionersList',response.data.medicines)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async medicineAdd({ commit }, payload) {
      commit('setLoading', true)
      // const formData = new FormData()
      //   formData.append('title', defaultItem.value.title)
      //     formData.append('slug', defaultItem.value.slug)
      //    formData.append('list_one_title', defaultItem.value.list_one_title)
      //   formData.append('list_sub_title', defaultItem.value.list_sub_title)
      //    formData.append('list_two_title', defaultItem.value.list_two_title)
      //     formData.append('price', defaultItem.value.price)
      //     formData.append('currency', currencySign.value)
      
      // formData.append('image', imageBase64.value)
      await axios.post(MEDICINE_SAVE_API, {
        title: payload.title,
        slug: payload.slug,
        list_one_title: payload.list_one_title,
        list_sub_title: payload.list_sub_title,
        list_two_title: payload.list_two_title,
        price: payload.price,
        currency: payload.currency,
        excel: payload.excel,
        image:payload.image
      }, {
        headers: {
          'Content-Type': 'multipart/form-data',
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
            if (response.data.message=='success' && response.status === 200) {
              commit('setSuccessMsg', true)
              console.log('Response:', response.data.message);
              commit('setShowMsg', 'Successfully Added')
            }
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async adminDetial({ commit }, payload) {
      commit('setLoading', true)
     
      await axios.post(ADMIN_LOGIN_DETAIL, {}, {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
        console.log('Response:', response.data);
           commit('setAdminDetail',response.data.admin_details)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async siteSetting({ commit }, payload) {
      commit('setLoading', true)
     
      await axios.post(ADMIN_GET_SITE_SETTING, {}, {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
        console.log('Response:', response.data.settings_data);
           commit('setSiteSetting',response.data.settings_data)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
     async siteSettingUpdate({ commit }, payload) {
      commit('setLoading', true)
     
      await axios.post(ADMIN_UPDATE_SITE_SETTING+payload.id, {
              plan_main_title: payload.plan_main_title,
              plan_description:  payload.plan_description,
              header_title: payload.header_title,
              footer_text: payload.footer_text,
              domain_name: payload.domain_name,
              logo:payload.logo,
              favicon:payload.favicon//imageBase64.value
      }, {
        headers: {
          'Content-Type': 'multipart/form-data',
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
            if (response.data.msg && response.status === 200) {
              commit('setSuccessMsg', true)
              console.log('Response:', response.data.message);
              commit('setShowMsg', response.data.msg)
            }
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async adminPasswordUpadate({ commit }, payload) {
      commit('setLoading', true)
     
      await axios.post(ADMIN_UPDATE_PASSWORD, {
              password: payload.password,
              new_password :  payload.new_password ,
              confirm_password :  payload.confirm_password ,
      }, {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
      if (response.data.status == 'error') {
          commit('setErrorMsg', true)
           console.log('Response:', response.data.msg);
           commit('setShowMsg', response.data.msg)
         
          }
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async profileUpdate({ commit }, payload) {
      commit('setLoading', true)
      await axios.post(PROFILE_UPDATE_API, {
        first_name: payload.name,
        last_name:payload.last_name,
        phone_no: payload.phone_no,
        image:payload.image
      }, {
        headers: {
           'Content-Type': 'multipart/form-data',
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async checkLogin({ commit }, payload) {
      //commit('setLoading', true)
     
      await axios.post(ADMIN_LOGIN_DETAIL, {}, {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            //commit('setLoading', false)
        console.log('Response:', response.data);
      
  
          })
          .catch(error => {
           //commit('setLoading', false)
            console.error('Error:', error);
            if (error.response && error.response.status === 401 && error.response.data.error === 'Token has expired') {
                 commit('setCheckLoginExpire', true) 
            }
          });
    },
     async labsKitList({ commit }, payload) {
      commit('setLoading', true)
  
      await axios.post(ADMIN_LAB_KIT_LIST_API, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
              commit('setLoading', false)
            console.log('Response:', response.data);
          
           
           
            commit('setLabKitList',response.data.labkit)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async labKitUpdate({ commit }, payload) {
      commit('setLoading', true)
      await axios.post(ADMIN_LAB_KIT_UPDATE_API+payload.id, {
        name: payload.name,
        amount: payload.amount,
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
          if (response.data.message && response.status === 200) {
                  commit('setSuccessMsg', true)
                  console.log('Response:', response.data.message);
                  commit('setShowMsg', response.data.message)
              }
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async labKitAdd({ commit }, payload) {
      commit('setLoading', true)
      await axios.post(ADMIN_LAB_KIT_ADD_API, {
        name: payload.name,
        amount: payload.amount,
      }, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
        console.log('Response:', response.data);
        if (response.data.message && response.status === 200) {
          commit('setSuccessMsg', true)
           console.log('Response:', response.data.message);
           commit('setShowMsg', response.data.message)
        }
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async labKitDelete({ commit }, payload) {
      commit('setLoading', true)
      console.log(localStorage.getItem('admin_access_token'))
      await axios.post(ADMIN_LAB_KIT_DELETE_API+payload.id, {}, {
        headers: {
            'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data);
            if (response.data.message && response.status === 200) {
              commit('setSuccessMsg', true)
              console.log('Response:', response.data.message);
              commit('setShowMsg', response.data.message)
            }
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async patientDetial({ commit }, payload) {
      commit('setLoading', true)
     
      await axios.post(ADMIN_PATIENT_DETAIL_API+payload.id, {}, {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
        console.log('Response:', response.data);
           commit('setPatientDetail',response.data)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async providerDetial({ commit }, payload) {
      commit('setLoading', true)
     
      await axios.post(ADMIN_PROVIDER_DETAIL_API+payload.id, {}, {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
      })  .then(response => {
            commit('setLoading', false)
        console.log('Response:', response.data);
           commit('setProviderDetail',response.data)
  
          })
          .catch(error => {
            commit('setLoading', false)
            console.error('Error:', error);
          });
    },
    async getAgentQuestionsAnswers ({commit},payload){
      commit('setLoading', true)
      await axios.post(ADMIN_PATIENT_PROFILE_API+payload.patient_id, {}, {
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('admin_access_token')}`,
        }
    })
        .then(response => {
            commit('setLoading', false)
            console.log('Response:', response.data.data);
            commit('setPatientAnswers', response.data.data)
        })
        .catch(error => {
            commit('setLoading', false)
            console.log('Error:', error);
        });
      
    },
  },
  getters: {
    getIsLoading(state){
      return state.isLoading
    },
     getErrorMsg(state) {
        return state.isErrorMsg
    },
    getSuccessMsg(state) {
      console.log('payload');
        return state.isSuccessMsg
    },
     getShowMsg(state) {
      console.log('payload');
        return state.showMessage 
    },
    
    getSubcriptions(state){
      return state.subcriptions
    },
    getPatientList(state){
      return state.patientList
    },
    getPatientMeetingList(state){
      return state.patientMeetingList
    },
    getProviderMeetingList(state){
      return state.providerMeetingList
    },
    getPatientLabKitList(state){
      return state.patientLabKitList
    },
    getPatientLabKitStatus(state){
      return state.patientLabKitStatus
    },
    getPatientPrescriptionStatus(state){
      return state.patientPrescriptionStatus
    },
    getProvidersList(state){
      return state.providersList
    },
    getLabsList(state){
      return state.labsList
    },
    getSinglePatientAppointment(state){
      return state.singlePatientAppointment
    },
    getPrescriptionList(state){
      return state.patientPrescription
    },
    getPatientNotes(state){
      return state.patientNotes
    },
    getMedcineList(state){
      return state.medicineList
    },
    getQuestioneriesList(state){
      return state.questioneriesList
    },
     getAdminDetail(state){
      return state.adminDetail
    },
    getSiteSetting(state){
      return state.siteSetting
    },
    getCheckLoginExpire(state){
      return state.checkLoginExpire
    },
    getLabKitList(state) {
      return state.labKitList
    },
     getPatientDetail(state) {
      return state.patientDetail
    },
      getProviderDetail(state) {
      return state.providerDetail
    },
    getPatientAnswers(state){
      return state.patientAnswers
    },
  }
})
