/**
 * long description for the file
 *
 * @summary 角色/权限管理
 * @author wu <308822989@qq.com>
 *
 * Created at     : 2018-08-01 18:01:57
 * Last modified  : 2018-08-29 09:13:13
 */

import axios from '@/config/axios.js';
import { proPath } from '@/config/config.js';
import { getCookie } from '@/utils/util.js';

const state = {

}

const getters = {
}

const mutations = {

}
const actions = {
  //  角色树列表
  list ({ commit, state }, obj) {
    let companyId = window.top.localStorage.getItem('companyId') ? window.top.localStorage.getItem('companyId') : JSON.parse(window.top.localStorage.getItem('companys'))[0].id;
    let res = (obj && obj.companyId && obj.appId) ? axios.get(proPath + 'v1/sysroles/getRolesUser?companyId=' + obj.companyId + '&appId=' + obj.appId) : axios.get(proPath + 'v1/sysroles/getRolesUser?companyId=' + companyId);
    return res;
  },
  //  删除
  delRoles ({ commit, state }, idArr) {
    if (!(idArr && idArr.length > 0)) {
      return false;
    }
    let arr = [];
    for (let item of idArr) {
      arr.push(item.id);
    }
    idArr = arr.join(',');
    let res = axios.delete(proPath + 'v1/sysroles/' + idArr);
    return res;
  },
  //  添加
  addRoles ({ commit, state }, form) {
    let res = axios.post(proPath + 'v1/sysroles/', form);
    return res;
  },
  //  修改
  editRoles ({ commit, state }, obj) {
    let res = axios.put(proPath + 'v1/sysroles/' + obj.id, obj.form);
    return res;
  },
  // //  岗位查找用户列表
  // userList ({ commit, state }, postId) {
  //   let res = axios.get('v1/sysusers/getUsersByPostId/' + postId);
  //   return res;
  // }
  //  数据权限列表（角色授权显示）
  dataRolesList1 ({ commit, state }, form) {
    let res = axios.get(proPath + 'v1/sysdatablepolicys/daTablePolicyUserId', {
      params: form
    });
    return res;
  },
  // 数据权限列表（数据权限管理显示）
  dataRolesList2 ({ commit, state }, form) {
    let res = axios.get(proPath + 'v1/sysdatablepolicys/', {
      params: form
    });
    return res;
  },
  //  用户查询菜单选中项
  sysauthorizes ({ commit, state }, form) {
    let res = axios.get(proPath + 'v1/sysauthorizes/', {
      params: form
    });
    return res;
  },
  //  设置用户菜单数据权限
  setSysauthorizes ({ commit, state }, obj) {
    let res = axios.post(proPath + 'v1/sysauthorizes/batchInsert', obj);
    return res;
  },
  //  机构列表（角色授权显示）
  dataOrgsList ({ commit, state }, form) {
    let res = axios.get(proPath + 'v1/sysorgroles/', {
      params: form
    });
    return res;
  },
  //  设置机构权限
  setSysauthorizes2 ({ commit, state }, obj) {
    let res = axios.post(proPath + 'v1/sysorgroles/batchInsert', obj);
    return res;
  },
  // 可访问菜单列表
  canAccessMenuList ({ commit, state }, obj) {
    let res = axios.get(proPath + 'v1/sysorgauthorizes/getOrganVisitMenu?orgId=' + obj.orgId + '&appId=' + obj.appId);
    return res;
  },
  // 可授权菜单列表
  canAuthMenuList ({ commit, state }, obj) {
    let res = axios.get(proPath + 'v1/sysorgauthorizes/getOrgCanAuthorizesMenu?orgId=' + obj.orgId + '&appId=' + obj.appId);
    return res;
  },
  // 获取当前登录账号下相关授权权限
  userCanAuthMenuList ({ commit, state }, obj) {
    let res = axios.get(proPath + 'v1/sysorgauthorizes/getMineCanAuthorizes?companyId=' + top.localStorage.getItem('companyId') + '&appId=' + obj.appId);
    return res;
  },
  userCanAuthOrgList ({ commit, state }) {
    let res = axios.get(proPath + 'v1/sysorgs/findMineManageOrg?companyId=' + top.localStorage.getItem('companyId'));
    return res;
  },
  // 批量授权
  batchAuthMenu ({ commit, state }, obj) {
    var param = {
      orgId: obj.orgId,
      authorizes: obj.authorizes,
      type: obj.type
    };
    let res = axios.post(proPath + 'v1/sysorgauthorizes/batchInsert', param, {
      diy: true,
      headers: {
        'Content-Type': 'application/json;charset=UTF-8'
      }
    });
    return res;
  },
  deleteOrgMenu ({ commit, state }, obj) {
     // 删除菜单权限
     let res = axios.delete(proPath + 'v1/sysorgauthorizes/' + obj.id);
     return res;
  }
}

//
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
