import {
  iGetList,
  iGetItem,
  iGetDoctorWithArticleNum,
  iUpdateItem,
  iAddItem,
  iGetArticleList,
  iDeleteDoctorInArticle,
  iDeleteDoctorsInArticle,
  iChangeShowStatus,
  iGetDeptList,
  iGetDoctortList,
  iDeleteDoctor,
  iDeleteDoctors
} from '@/api/doctor'

import listResult from '@/model/listResult'

import CARRIERKEY from '@/const/carrierKey'

import { translateLabel } from '@/utils'

const getList = async data => listResult(await iGetList(data), x => ({
  id: x.id,
  hospital: x.hospital,
  dept: x.dept,
  doctorName: x.name,
  articleNum: x.used ? x.used : 0,
  isShow: x.isShow,
  showStatus: x.isShow ? '展示中' : '未展示',
  headUrl: `${process.env.VUE_APP_BASE_IMG}${x.headUrl}`
}))

// const setChannels = data => {
//   const result = []
//   data.channels.forEach((x, index) => {
//     result.push(x)
//     if (data.channelsIsTop[index]) {
//       result.push(`${x}_top`)
//     }
//   })
//   return result
// }

const getItem = async params => {
  const result = await iGetItem(params)
  if (result.success) {
    const data = result.data
    result.data = {
      id: data.id,
      name: data.name,
      hospital: data.hospital,
      dept: data.dept,
      desc: data.description,
      headUrl: [{ url: `${process.env.VUE_APP_BASE_IMG}${data.headUrl}` }],
      isTop: data.isTop,
      tagOptions: data.title ? data.title.split(',') : []
      // channels: setChannels(data)
    }
  }
  return result
}

const getDoctorWithArticleNum = async params => await iGetDoctorWithArticleNum(params)

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

const getArticleList = async data => listResult(await iGetArticleList(data), x => ({
  id: x.id,
  contentType: translateLabel(CARRIERKEY.CARRIER, x.contentType),
  title: x.title,
  isReleaseF: x.isRelease ? '已发布' : '未发布',
  isFeatureF: x.isFeature ? '是' : '否'
}))

const deleteDoctorInArticle = async params => await iDeleteDoctorInArticle(params)

const deleteDoctorsInArticle = async params => await iDeleteDoctorsInArticle(params)

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

const getDeptList = async params => await iGetDeptList(params)

const deleteDoctor = async params => await iDeleteDoctor(params)

const deleteDoctors = async params => await iDeleteDoctors(params)

const getDoctortList = async params => {
  const result = await iGetDoctortList(params)
  const data = result.data
  if (result.success) {
    result.data = data.map(x => ({
      value: x.id,
      label: x.name
    }))
  }
  return result
}
export {
  getList,
  getItem,
  getDoctorWithArticleNum,
  saveItem,
  getArticleList,
  deleteDoctorInArticle,
  deleteDoctorsInArticle,
  changeShowStatus,
  getDeptList,
  getDoctortList,
  deleteDoctor,
  deleteDoctors
}
