import { cloneDeep, get } from 'lodash-es'
const InitBaseMethods = (store) => {
  const $checkRes = inject('$checkRes')
  let limit = inject('limit')
  const data = ref([])
  const total = ref(0)
  const searchForm = ref({})
  const form = ref({})
  const b_search = async (query) => {
    const info = { skip: query.skip, limit: query.limit, ...searchForm.value, is_del: '0' }
    const res = await store.query(info)
    if (res.errcode == '0') {
      data.value = res.data
      total.value = res.total
    }
  }
  const b_delete = async (data) => {
    const res = await store.del(data._id)
    if ($checkRes(res, true)) {
      b_search({ skip: 0, limit })
    }
  }
  const b_save = async () => {
    const data = cloneDeep(form.value)
    let res
    if (get(data, '_id')) res = await store.update(data)
    else res = await store.create(data)
    if ($checkRes(res, true)) {
      b_search({ skip: 0, limit })
    }
  }
  return {
    $checkRes,
    limit,
    data,
    total,
    searchForm,
    form,
    b_search,
    b_delete,
    b_save
  }
}

export default InitBaseMethods
