import { replaceUrlParams, objectGet } from './utils'

const prefix = '/api'
const apis = {
  // 参与人员
  user: {
    list: {
      url: `${prefix}/user`,
      method: 'get',
    }
  },
  // 设置
  setting: {
    // 抽奖设置
    prize: {
      list: {
        url: `${prefix}/setting/prize`,
        method: 'get',
      },
      save: {
        url: `${prefix}/setting/prize`,
        method: 'post',
      }
    }
  },
  // 抽奖
  draw: {
    url: `${prefix}/draw`,
    method: 'post',
  },
  // 奖品维护
  prize: {
    list: {
      url: `${prefix}/prize`,
      method: 'get',
    },
    save: {
      url: `${prefix}/prize`,
      method: 'post',
    },
    update: {
      url: `${prefix}/prize/:PrizeId`,
      method: 'put',
    }
  },
  // 中奖名单
  winner: {
    list: {
      url: `${prefix}/winner`,
      method: 'get',
    },
    // 取消中奖
    cancel: {
      url: `${prefix}/winner/:UserId`,
      method: 'delete',
    }
  },
  // excel 导出
  excel: {
    winner: {
      url: `${prefix}/excel/winner`,
      method: 'get',
    }
  },
  file: {
    upload: {
      url: `${prefix}/file`,
      method: 'post',
    }
  }
}


// getApi
// name is the key, but also can be a.b
// urlparams is option
export default function getApi(name, urlparams) {
  const api = objectGet(apis, name)
  const url = replaceUrlParams(api.url, urlparams)
  return { ...api, url }
}
