import { AppDispatch } from '@/store'
import http from '@/utils/http'

/**
 * 库房设置 - 仓格分页列表
 */
export const I1_APIgetList = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get('cms/site/getList', {
      params: {
        searchKey: data.searchKey,
        storageId: data.storageId
      }
    })
    if (res.code === 0) {
      const obj = {
        list: res.data,
        total: res.data.length
      }

      dispatch({ type: 'I1/getList', payload: obj })
    }
  }
}

export const I1_APIgetListPure = (data: { searchKey?: string; storageId: unknown }) => {
  return http.get('cms/site/getList', {
    params: {
      searchKey: data.searchKey,
      storageId: data.storageId
    }
  })
}

/**
 * 库房设置 - 仓格删除
 */
export const I1_APIdel = (id: string | string[]) => {
  const ids = Array.isArray(id) ? id : [id]
  return http.post('/cms/site/site/removes', ids) // 直接传数组
}

/**
 * 库房设置 - 新增仓格
 */
export const I1_APIPositionSave = (data: any) => {
  return http.post('cms/site/site/batchSave', data)
}

/**
 * 库房设置 - 库房分页列表
 */
export const I1_APIwarehouseSimpleList = (data: any): any => {
  return http.post('cms/storage/pageList', data)
}
export const I1_APIwarehouseList = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    const res = await http.post('cms/storage/pageList', data)
    if (res.code === 0) {
      const obj = {
        list: res.data.records,
        total: res.data.total
      }

      dispatch({ type: 'I1/getWarehouseList', payload: obj })
    }
  }
}

/**
 * 库房设置 - 库房删除
 */
export const I1_APIwarehouseDel = (id: string) => {
  return http.get(`cms/storage/remove/${id}`)
}

/**
 * 库房设置 - 新增/修改库房
 */
export const I1_APIwarehouseSave = (data: any) => {
  return http.post('cms/storage/saveEntity', data) // 直接传数组
}
