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 app_name = 'Admin'
let url = ''
const urlPick = () => {
  if ($config.value.api.url.length > 0) {
    url = $config.value.api.url[0].url
    for (let i in $config.value.api.url) {
      if (!!$config.value.api.url[i].active) {
        url = $config.value.api.url[i].url
        break
      }
    }
  }
}
urlPick()
export const AdminResetPasswordAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/reset_password`,
  data
}, true)
export const ConfigCreateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Config/create`,
  data
}, true)
export const ConfigUpdateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Config/update`,
  data
}, true)
export const ConfigDeleteAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Config/delete`,
  data
}, true)
export const ConfigListAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Config/list`,
  data
}, true)
export const AdminAuthSelectAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuth/select`,
  data
}, true)
export const AdminAuthCreateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuth/create`,
  data
}, true)
export const AdminAuthUpdateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuth/update`,
  data
}, true)
export const AdminAuthDeleteAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuth/delete`,
  data
}, true)
export const AdminAuthListAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuth/list`,
  data
}, true)
export const RequestLogTxtAction = async (data) => await $post({
  url: `${url}/api/${app_name}/RequestLog/txt`,
  data
}, true)
export const RequestLogListAction = async (data) => await $post({
  url: `${url}/api/${app_name}/RequestLog/list`,
  data
}, true)
export const UploadListAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Upload/list`,
  data
}, true)
export const UploadDeleteAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Upload/delete`,
  data
}, true)
export const UploadSearchAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Upload/search`,
  data
}, true)
export const AdminCreateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/create`,
  data
}, true)
export const AdminUpdateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/update`,
  data
}, true)
export const AdminDeleteAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/delete`,
  data
}, true)
export const AdminListAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/list?page=${data.page}`,
  data
}, true)
export const AdminAuthGroupSelectAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuthGroup/select`,
  data
}, true)
export const AdminAuthGroupCreateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuthGroup/create`,
  data
}, true)
export const AdminAuthGroupUpdateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuthGroup/update`,
  data
}, true)
export const AdminAuthGroupDeleteAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuthGroup/delete`,
  data
}, true)
export const AdminAuthGroupListAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuthGroup/list`,
  data
}, true)
export const AdminAuthChooseAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuth/choose`,
  data
}, true)
export const UploadImageAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Upload/image`,
  data
}, true)
export const AdminUpdateSelfAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/update_self`,
  data
}, true)
export const AdminAuthMenuAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAuth/menu`,
  data
}, true)
export const AdminAccountChangePasswordAction = async (data) => await $post({
  url: `${url}/api/${app_name}/AdminAccount/change_password`,
  data
}, true)
export const AdminQuitAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/quit`,
  data
}, true)
export const AdminInfoAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/info`,
  data
}, true)
export const AdminStatusAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/status`,
  data
}, true)
export const AdminLoginAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Admin/login`,
  data
}, true)

export const ImageCaptchaCreateAction = async (data) => await $post({
  url: `${url}/api/${app_name}/ImageCaptcha/create`,
  data
}, false)
export const ConfigGetAction = async (data) => await $post({
  url: `${url}/api/${app_name}/Config/get?client=admin`,
  data
}, true)
export const yo = async (data) => await $post({url: `${url}/api/yo`, data}, true)
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 `${url}${path}`
}
export const $base64 = async (file) => {
  let reader = new FileReader()
  reader.readAsDataURL(file)
  return await new Promise(resolve => (reader.onloadend = () => resolve(reader.result)))
}
export const $response = (res, then, next = false) => {
  if (res) {
    if ($config.value.api.error_code.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_code) return window.$message().error(res.message)
    then()
  }
}
