import {nextTick, onActivated, ref, watch} from "vue";
import {useRootStore} from "@/stores/useStore";


export function usePagination(callback) {


 const pageSize = ref(10)
 const pageIndex = ref(1)
 const totalCount = ref(0)
 const tableData = ref([

 ])
 const refEl = ref(null)
 const initSearch = () => {
  nextTick(() => {
   refEl.value.initPage()
  })
 }
 const handleSizeChange = (pageIndex1,pageSize1) => {
  pageSize.value = pageSize1
  pageIndex.value = pageIndex1
  console.log('handleSizeChange','handleSizeChange')
  callback()
 }
 return {
  pageSize,
  pageIndex,
  totalCount,
  tableData,
  initSearch,
  handleSizeChange,
  refEl
 }


}
export function useClickRouterToHome() {
 function addHomeCount() {
  store.toHomeCounter++
 }
 const operationType = ref('')
 const currentOpObj = ref(null)
 const store = useRootStore()
 function changeOperation(operation,currentObj2) {
  console.log('assad',currentOpObj,operation)
  operationType.value = operation
  currentOpObj.value = (currentObj2 && Object.keys(currentObj2).length > 0)?currentObj2: ''

 }
 watch(() => store.toHomeCounter,(val) => {
  currentOpObj.value = {}
  operationType.value = ''
 })
 return {
  operationType,
  currentOpObj,
  changeOperation
 }
}
export function useDetailOrEdit() {
 const operationType = ref('')
 const currentOpObj = ref(null)

 function changeOperation(operation,currentObj2) {
  console.log('assad',currentOpObj)
  operationType.value = operation
  currentOpObj.value = currentObj2 || ''


 }
 return {
  operationType,
  changeOperation,
  currentOpObj
 }
}
export function useActiveGetTableData(callback){
 onActivated(callback)
}
