import {
  iGetList,
  iGetItem,
  iGetShowNum,
  iUpdateItem,
  iAddItem,
  iDeleteBanner,
  iDeleteBanners,
  iChangeShowStatus
} from '@/api/banner'

import listResult from '@/model/listResult'

const getList = async data => listResult(await iGetList(data), x => ({
  id: x.id,
  sort: x.sort.toString(),
  title: x.title,
  url: x.url,
  imgUrl: process.env.VUE_APP_BASE_IMG + x.imgUrl,
  isTop: x.isTop,
  isTopF: x.isTop ? '已置顶' : '未置顶',
  isShow: x.isShow,
  isShowF: x.isShow ? '已展示' : '未展示'
}))

const getItem = async params => {
  const result = await iGetItem(params)
  if (result.success) {
    const data = result.data
    result.data = {
      name: data.title,
      url: data.url,
      imgFile: [{ url: `${process.env.VUE_APP_BASE_IMG}${data.imgUrl}` }], // [{ url: data.imgUrl }],
      isTop: data.isTop,
      isShow: data.isShow,
      sort: data.sort
    }
  }
  return result
}

const saveItem = async({ id, ...params }) => {
  if (id) {
    return await iUpdateItem(id, params)
  } else {
    return await iAddItem(params)
  }
}

const getShowNum = async params => await iGetShowNum(params)

const deleteBanner = async params => await iDeleteBanner(params)

const deleteBanners = async params => await iDeleteBanners(params)

const changeShowStatus = async params => await iChangeShowStatus(params)

export {
  getList,
  getItem,
  getShowNum,
  saveItem,
  deleteBanner,
  deleteBanners,
  changeShowStatus
}
