import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useStoreStore = defineStore('store', () => {
  const selectedStoreName = ref<string>('未选择门店');
  const selectedStoreId = ref<number | null>(null);

  function setStore(name: string, id: number) {
    selectedStoreName.value = name;
    selectedStoreId.value = id;
    localStorage.setItem('selectedStoreName', name);
    localStorage.setItem('selectedStoreId', id.toString());
  }

  function loadStoreFromLocal() {
    const name = localStorage.getItem('selectedStoreName');
    const id = Number(localStorage.getItem('selectedStoreId'));
    if (name && !isNaN(id)) {
      selectedStoreName.value = name;
      selectedStoreId.value = id;
    }
  }

  return { selectedStoreName, selectedStoreId, setStore, loadStoreFromLocal }
})