import { Injectable } from "@angular/core";
import { MainService } from "./main.service";

@Injectable({
  providedIn: "root",
})
export class ToolService {
  logoUrl = '';
  logoTitle = '';
  faviconUrl = '';

  constructor(private mainService: MainService) {}

  // 获取logoUrl和logoTitle
  getSysNameAndLogoAsync(hosId = ''){
    return this.mainService.getSysNameAndLogo(hosId);
  }

  // 设置logoTitle
  setSysName(logoTitle){
    this.logoTitle = logoTitle;
  }

  // 设置logoUrl
  setLogo(logoUrl){
    this.logoUrl = logoUrl;
  }

  // 设置faviconUrl
  setFavicon(faviconUrl){
    this.faviconUrl = faviconUrl;
  }

  // 获取权限中的院区
  getHospitalList() {
    return (
      JSON.parse(localStorage.getItem("user")).infoPermission.hospitals || []
    );
  }
  // 获取权限中的分组
  getGroupList() {
    return JSON.parse(localStorage.getItem("user")).infoPermission.groups || [];
  }
  // 获取当前院区
  getCurrentHospital() {
    return JSON.parse(localStorage.getItem("user")).user.currentHospital
      ? JSON.parse(localStorage.getItem("user")).user.currentHospital
      : JSON.parse(localStorage.getItem("user")).infoPermission.hospitals[0];
  }
  //获取当前用户科室
  getCurrentUserDept() {
    return JSON.parse(localStorage.getItem("user")).user.dept;
  }
  //获取当前菜单的权限
  initCoopBtns(route) {
    let link;
    let menu: any = JSON.parse(localStorage.getItem("menu")) || []; //菜单
    if (route === "dataDictionary") {
      link = route;
    } else {
      link = route.parent.snapshot.routeConfig.path; //当前路由
    }
    let secondMenus = []; // 二级菜单
    let jurisdiction = []; //当前菜单的权限列表
    let coopBtns: any = {}; // 初始化权限
    menu.forEach((e) => {
      if (e.childrens) {
        e.childrens.forEach((el) => {
          secondMenus.push(el);
        });
      }
    });
    secondMenus.forEach((e) => {
      if (e.link == link) {
        jurisdiction = e.childrens || []; //匹配路由后，取得对应的权限
      }
    });
    jurisdiction.forEach((e) => {
      switch (e.link) {
        case "look":
          coopBtns.look = true; //查看
          break;
        case "add":
          coopBtns.add = true; //新增
          break;
        case "edit":
          coopBtns.edit = true; //编辑
          break;
        case "del":
          coopBtns.del = true; //删除
          break;
        case "reply":
          coopBtns.reply = true; //回复
          break;
        case "mediate":
          coopBtns.mediate = true; //调解
          break;
        case "designate":
          coopBtns.designate = true; //指派
          break;
        case "export":
          coopBtns.export = true; //导出
          break;
        case "isStartUp":
          coopBtns.isStartUp = true; //是否启用
          break;
        case "isTimedStartUp":
          coopBtns.isTimedStartUp = true; //是否定时启用
          break;
        case "copy":
          coopBtns.copy = true; //复制
          break;
        case "generateQRcode":
          coopBtns.generateQRcode = true; //生成二维码
          break;
        case "deleted":
          coopBtns.deleted = true; //已删除
          break;
        case "executeNow":
          coopBtns.executeNow = true; //立即执行
          break;
        case "resetPwd":
          coopBtns.resetPwd = true; //重置密码
          break;
        case "print":
          coopBtns.print = true; //打印
          break;
        case "currentDept":
          coopBtns.currentDept = true; //当前科室工单
          break;
        case "currentUser":
          coopBtns.currentUser = true; //当前人员工单
          break;
        case "allOrders":
          coopBtns.allOrders = true; //全部工单
          break;
        case "batchConversion":
          coopBtns.batchConversion = true; //批量转换
          break;
        case "oneClickConversion":
          coopBtns.oneClickConversion = true; //一键转换
          break;
        case "oneClickRecovery":
          coopBtns.oneClickRecovery = true; //一键恢复
          break;
        case "view":
            coopBtns.view = true; //查看视图
            break;
        case "recount":
          coopBtns.recount = true; //重新统计
          break;
        case "editNum":
          coopBtns.editNum = true; //矫正重量
          break;
      }
    });
    console.log(coopBtns);
    return coopBtns;
  }
  // 获取护士端是否使用科室别名的系统配置
  getDeptDisplay() {
    let postData = {
      idx: 0,
      sum: 1,
      systemConfiguration: { keyconfig: "deptDisplay" },
    };
    return this.mainService.getFetchDataList(
      "simple/data",
      "systemConfiguration",
      postData
    );
  }
}
