import { CommonDynamicSelect } from "@/api/modules/common";
import { useUserStore } from "@/stores/modules/user";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { useGlobalStore } from "@/stores/modules/global";

/**
 * @description 检查权限
 * @param argno 权限值
 */
export async function f_power_ind(argno: number) {
  const globalStore = useGlobalStore();
  const { CheckPower } = useAuthButtons();

  let lb_ifpower = globalStore.isSuper || CheckPower(argno);

  if (lb_ifpower) {
    return true;
  }

  let _params = {
    dsname: "web_power_ind",
    queryparams: {
      argno: [argno]
    }
  };
  let ret = await CommonDynamicSelect(_params);

  let li_grade = 0;
  let ll_parentid = 0;
  let ls_treename_p = "";
  let ls_treename = "";

  if (ret.datatable.length) {
    li_grade = Number(ret.datatable[0].grade);
    ll_parentid = Number(ret.datatable[0].parentid);
    ls_treename_p = ret.datatable[0].treename_p;
    ls_treename = ret.datatable[0].treename;
  } else {
    throw new Error("查询权限信息失败");
  }
  let arg_msg = "";

  if (lb_ifpower) {
    // if (sys_ifreg === 1 && sys_ifnewserver === 1) {
    //   if (sys_user_grade < li_grade) {
    //     let ls_grade_user = '';
    //     switch (sys_user_grade) {
    //       case 0:
    //         ls_grade_user = '公共[0]';
    //         break;
    //       case 1:
    //         ls_grade_user = '执行[1]';
    //         break;
    //       case 2:
    //         ls_grade_user = '计划[2]';
    //         break;
    //       case 3:
    //         ls_grade_user = '策略[3]';
    //         break;
    //       default:
    //         ls_grade_user = '未知等级';
    //         break;
    //     }
    //     let ls_grade_pow = '';
    //     switch (li_grade) {
    //       case 0:
    //         ls_grade_pow = '公共[0]';
    //         break;
    //       case 1:
    //         ls_grade_pow = '执行[1]';
    //         break;
    //       case 2:
    //         ls_grade_pow = '计划[2]';
    //         break;
    //       case 3:
    //         ls_grade_pow = '策略[3]';
    //         break;
    //       default:
    //         ls_grade_pow = '未知等级';
    //         break;
    //     }
    //     arg_msg = `你当前用户的等级为:${ls_grade_user},不能执行权限等级为:${ls_grade_pow}的操作`;
    //     return false;
    //   }
    // }
  } else {
    if (ll_parentid > 0) {
      throw new Error(`你没有【${ls_treename_p}-${ls_treename}】的使用权限!`);
    } else {
      throw new Error(`你没有【${ls_treename}】的使用权限!`);
    }
  }

  return true;
}
