import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {environment} from '../../../environments/environment';
import {ObjectData} from '../../utils/objectData.service';

@Injectable({
  providedIn: 'root'
})
export class MenuService {

  constructor(private http: HttpClient) {}

  /**
   * 查询菜单树
   * @param roleId
   * @param callBack
   */
  public queryMenuTree(callBack:any) {
    this.http.post(environment.baseUrl + 'secMenu/queryMenuTree?dt='+new Date().getTime(), null).subscribe(data => {
      callBack(data);
    });
  }

  /**
   * 查询菜单
   * @param roleId
   * @param callBack
   */
  public queryMenuList(callBack:any) {
    let params = {
      time: new Date().getTime()
    };
    this.http.post(environment.baseUrl + 'secMenu/queryMenuList?' + ObjectData.objectByString(params), null).subscribe(data => {
      callBack(data);
    });
  }

  /**
   * 分配菜单
   * @param params
   * @param callBack
   */
  public assignMenu(params: any, callBack:any) {
    params.time = new Date().getTime();
    this.http.post(environment.baseUrl + 'secMenu/assignMenu', params).subscribe(data => {
      callBack(data);
    });
  }

  /**
   * 删除菜单
   * @param params
   * @param callBack
   */
  public delMenu(params: any, callBack:any) {
    params.time = new Date().getTime();
    this.http.post(environment.baseUrl + 'secMenu/delMenu', params).subscribe(data => {
      callBack(data);
    });
  }

  /**
   * 编辑菜单
   * @param params
   * @param callBack
   */
  public editMenu(params: any, callBack:any) {
    params.time = new Date().getTime();
    this.http.post(environment.baseUrl + 'secMenu/editMenu', params).subscribe(data => {
      callBack(data);
    });
  }
  /**
   * 查询角色菜单权限数组
   * @param roleId
   * @param callBack
   */
  public queryRoleMenuArray(roleId: number, callBack:any) {
    const param = {
      roleId: roleId,
      t: new Date().getTime() // 时间戳避免缓存
    }
    this.http.get(environment.baseUrl + 'secMenu/queryRoleMenuArray?' + ObjectData.objectByString(param)).subscribe(data => {
      callBack(data);
    });
  }
}
