import {
  iGetList,
  iGetItem,
  iAddItem,
  iUpdateItem,
  getInfo,
  iSaveRole,
  iGetRole,
  iGetItemRole,
  iDeletaItems
} from '@/api/account'

import listResult from '@/model/listResult'

import ROLEKEY from '@/const/roleType'
import { translateLabel } from '@/utils'

// import { translateLabel } from '@/utils'

const getList = async data => listResult(await iGetList(data), x => ({
  id: x.id,
  mobile: x.mobileNumber,
  nickName: x.nickname,
  role: x.roleName,
  roleF: translateLabel(ROLEKEY.ROLETYPE, x.roleName)
}))

const getItem = async params => {
  const result = await iGetItem(params)
  if (result.success) {
    const data = result.data
    result.data = {
      mobile: data.mobileNumber,
      name: data.nickname,
      info: data
    }
  }
  return result
}

const getMyInfo = async params => {
  const result = await getInfo(params)
  if (result.success) {
    const data = result.data
    result.data = {
      mobile: data.mobileNumber,
      name: data.nickname,
      info: data
    }
  }
  return result
}

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

const getItemRole = async params => {
  const result = await iGetItemRole(params)
  if (result.success) {
    const data = result.data
    result.data = data[0].id
  }
  return result
}

const getRole = async() => {
  const result = await iGetRole({
    data: {
      code: null
    }
  })
  if (result.success) {
    const data = result.data
    console.log(data)
    result.data = data.map(x => ({
      value: x.id,
      label: x.name
    }))
  }
  return result
}

const saveRole = async params => await iSaveRole(params)

const deletaItems = async params => await iDeletaItems(params)

export {
  getList,
  getItem,
  getMyInfo,
  saveItem,
  saveRole,
  getRole,
  getItemRole,
  deletaItems
}
