import axios from '@/libs/api.request'
import Main from '@/components/main'
/*/!**
 * 获取登陆用户信息
 *!/
export const getCurrentUser = (fn) => {
  fetch("/v1/symanage/user").then(response=>{
    fn(response.result);
  }).catch(err=>{
    console.log(err);
  });
}

/!**
 * 获取系统列表
 * @param fn
 *!/
const _systems=[];
export function getSystems(fn){
  if(_systems&&_systems.length>0){
    fn(_systems);
    return;
  }
  fetch("/v1/rbac/resource/system").then(response=>{
    fn(response.result);
  }).catch(err=>{
    console.log(err);
  });
}

/!**
 * 获取当前系统菜单
 * @param fn
 *!/
const _menus={};
export const getMenus = (system, fn) => {
  if(_menus[system]&&_menus[system]['models']){
    fn(_menus[system]);
    return;
  }
  fetch("/v1/rbac/resource/menu/"+system).then(response=>{
    let data = response.result;
    let menus = [];
   /!* menus.push({//添加首页
      path: '/',
      name: '_home',
      redirect: '/home',
      component: Main,
      meta: {
        hideInMenu: true,
        notCache: true
      },
      children: [
        {
          path: '/home',
          name: 'home',
          meta: {
            hideInMenu: true,
            title: '首页',
            notCache: true,
            icon: 'md-home'
          },
          component: () => import('@/view/single-page/home')
        }
      ]
    });*!/
    for (let i = 0; i < data.length; i++) {
      let children = [];
      for (let j = 0; j < data[i].menus.length; j++) {
        children.push({
          path: '/'+data[i].menus[j].id,
          name : data[i].menus[j].id,
          meta : {
            icon:data[i].menus[j].icon,
            title:data[i].menus[j].name,
          },
          component: () => import('@/view/'+data[i].menus[j].href+'.vue')
          //component: require('@/view/'+data[i].menus[j].href+'.vue')
        });
      }
      menus.push({
        path : '/'+ data[i].id,
        name : data[i].id,
        meta : {
          icon:data[i].icon,
          title:data[i].name,
        },
        component: () => import('@/components/main'),
        //component: require('@/components/main'),
        children : children
      });
    }
    /!*menus.push({//添加401页面
      path: '/401',
      name: 'error_401',
      meta: {
        hideInMenu: true
      },
      component: () => import('@/view/error-page/401.vue')
    });
    menus.push({
      path: '/500',
      name: 'error_500',
      meta: {
        hideInMenu: true
      },
      component: () => import('@/view/error-page/500.vue')
    });
    menus.push({
      path: '*',
      name: 'error_404',
      meta: {
        hideInMenu: true
      },
      component: () => import('@/view/error-page/404.vue')
    });*!/
    _menus[system] = menus;
    fn(_menus[system])
  }).catch(err=>{
    console.log(err);
  })
}*/

export const get = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: url,
        method: 'get',
        params: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const post = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: url,
        method: 'post',
        data: params,
      })
      .then((response) => {
        resolve(response.data)
      })
      .catch((err) => {
        reject(err)
      })
  })
}

export const patch = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: url,
        method: 'patch',
        data: params,
      })
      .then(
        (response) => {
          resolve(response.data)
        },
        (err) => {
          reject(err)
        }
      )
  })
}

export const put = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: url,
        method: 'put',
        data: params,
      })
      .then(
        (response) => {
          resolve(response.data)
        },
        (err) => {
          reject(err)
        }
      )
  })
}

export const remove = (url, params = {}) => {
  return new Promise((resolve, reject) => {
    axios
      .request({
        url: url,
        method: 'POST',
        params: params,
      })
      .then(
        (response) => {
          resolve(response.data)
        },
        (err) => {
          reject(err)
        }
      )
  })
}
