import {$post} from '~/tool/axios'
import {useConfig, useSessionToken, useSaveTokenType, useToken} from "~/store";
import $router from "~/router";

const $save_token_type = useSaveTokenType()
const $session_token = useSessionToken()
const $token = useToken()
const $config = useConfig()
const admin_api = 'Admin'
export const yo = async (data) => await $post({url: `${$config.value.api.url}/api/yo`, data}, true)
export const UploadImageAction = async (base64) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Upload/image`, data: {base64}
}, true)
export const ConfigDeleteAction = async (id) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Config/delete`, data: {id}
}, true)
export const ConfigCreateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Config/create`, data
}, true)
export const ConfigUpdateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Config/update`, data
}, true)
export const ConfigListAction = async () => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Config/list`
}, true)
export const AuthUpdateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Auth/update`, data
}, true)
export const AuthCreateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Auth/create`, data
}, true)
export const AuthDeleteAction = async (id) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Auth/delete`, data: {id}
}, true)
export const AuthListAction = async () => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Auth/list`
}, true)
export const AdminDeleteAction = async (ids) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/delete`, data: {ids}
}, true)
export const AdminUpdateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/update`, data
}, true)
export const AdminEditPasswordAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/Edit/password`, data
}, true)
export const AdminCreateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/create`, data
}, true)
export const AdminListAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/list?page=${data.page}`, data
}, true)
export const AuthSelectAction = async () => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Auth/select`
}, true)
export const AdminAuthSelectAction = async () => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/AdminAuth/select`
}, true)
export const AdminAuthListAction = async () => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/AdminAuth/list`
}, true)
export const AdminAuthUpdateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/AdminAuth/update`, data
}, true)
export const AdminAuthDeleteAction = async (ids) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/AdminAuth/delete`, data: {ids}
}, true)
export const AdminAuthCreateAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/AdminAuth/create`, data
}, true)
export const ConfigGetAction = async (label_arr) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Config/get`, data: {label_arr}
}, true)
export const AdminChangeNicknameAction = async (nickname) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/Change/nickname`, data: {nickname}
}, true)
export const AdminChangePasswordAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/Change/password`, data
}, true)
export const AdminLoginAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/login`, data
}, true)
export const AdminMenuAction = async (data) => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/menu`, data
}, true)
export const AdminStatusAction = async () => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/status`
}, false)
export const AdminInfoAction = async () => await $post({
  url: `${$config.value.api.url}/api/${admin_api}/Admin/info`
}, false)

export const $headers = () => {
  let $token
  if ($save_token_type.value === 'local') {
    $token = useToken()
  } else {
    $token = useSessionToken()
  }
  return {
    'Authorization': 'Bearer ' + $token.value
  }
}
export const $image = (path) => {
  const path_ret = ['http://', 'https://', ';base64,']
  for (let i = 0; i < path_ret.length; i++) {
    if (path.indexOf(path_ret[i]) !== -1) {
      return path
    }
  }
  return `${$config.value.api.url}${path}`
}
export const $base64 = (file) => {
  let reader = new FileReader()
  reader.readAsDataURL(file)
  return new Promise(resolve => (reader.onloadend = () => resolve(reader.result)))
}
export const $response = (res, then, next = false) => {
  if (res) {
    if ($config.value.api.login.indexOf(res.code) !== -1) {
      $session_token.value = null
      $token.value = null
      if (!!next) {
        next('/login')
      } else {
        $router.push('/login')
      }
    }
    if (res.code !== $config.value.api.success) return window.$message().error(res.message)
    then()
  }
}
