import { getGroupInfo, selectDeptFlage } from "@/api/system/dept";

import store from '@/store'
/**
 * 油品，根据油品名查类型
 */
export async function queryPageStatus(allowJiBieArr = [], type) { 
  // type 1 积分 2 电子卡 3 优惠券 4 标签 5 退款
  

  //返回值  0 页面初始状态 1 此页面不对此用户不开放 2 权限不在此页面 3 发生错误 4 页面在此页面，但未开启功能 5 正常显示 
  const currentJiBie = store.state.user.jiBie
  //不符合jiBie
  if(!allowJiBieArr.includes(currentJiBie)){
    return 1;
  }
  // 符合jiBie 不要求type
  if(!type){
    return 5
  }

  if(currentJiBie == 0){
    return 1
  }

  const confData = await selectDeptFlage();
  if (confData.code != 200) {
    return 3
  }
  let integralRuleFlag = 0
  let integralFlag = 0
  let cardRuleFlag = 0
  let cardEnabledFlag = 0
  let couponFlag = 0
  let couponEnabledFlag = 0
  let labelFlag = 0
  let refundExaminStatus = 0

console.log('confData',confData);

  integralRuleFlag = confData.data.groupDept.integralRuleFlag; // 0 不共享 1 共享
  cardRuleFlag = confData.data.groupDept.cardRuleFlag; // 0 不共享 1 共享
  couponFlag = confData.data.groupDept.couponFlag; // 0 不共享 1 共享
  refundExaminStatus =  confData.data.groupDept.refundExaminStatus; // 0站点 1集团

  if(integralRuleFlag == 1){
    integralFlag = confData.data.groupDept.integralFlag; // 开启 不开启
  }else{
    if(currentJiBie == 2){
      integralFlag = confData.data.userDept.integralFlag
    }
  }


  if(cardRuleFlag == 1){
    cardEnabledFlag = confData.data.groupDept.cardEnabledFlag; 
  }else{
    if(currentJiBie == 2){
      cardEnabledFlag = confData.data.userDept.cardEnabledFlag
    }
  }


  if(couponFlag == 1){
    couponEnabledFlag = confData.data.groupDept.couponEnabledFlag; 
  }else{
    if(currentJiBie == 2){
      couponEnabledFlag = confData.data.userDept.couponEnabledFlag
    }
  }

  if(currentJiBie == 2){
    labelFlag =  confData.data.userDept.labelFlag;
  }
  
  
  if (currentJiBie == 1) { // 集团 
    if (type == 1) {
      if (integralRuleFlag == 1) {
        if (integralFlag == 1) {
          return 5
        }
        return 4
      } else {
        return 2
      }

    } else if (type == 2) {
      if (cardRuleFlag == 1 ) { 
        if (cardEnabledFlag == 1) {
          return 5
        }
        return 4
      } else {
        return 2
      }
    } else if (type == 3) {
      if (couponFlag == 1 ) {

        if (couponEnabledFlag == 1) {
          return 5
        }
        return 4
      } else {
        return 2
      }
    } else if (type == 4) {
      return 2
    } else if (type == 5) {
      if(refundExaminStatus == '1'){
        return 5
      }else if(refundExaminStatus == '0'){
        return 4
      }
      return 3 
    }
  } else if (currentJiBie == 2) { // 站点
    if (type == 1) {
      if (integralRuleFlag != 1) {
        if (integralFlag == 1) {
          return 5
        }
        return 4
      } else {
        return 2
      }

    } else if (type == 2) {
      if (cardRuleFlag != 1) { 
        if (cardEnabledFlag == 1) {
          return 5
        }
        return 4
      } else {
        return 2
      }
    } else if (type == 3) {
      if (couponFlag != 1) { 
        if (couponEnabledFlag == 1) {
          return 5
        }
        return 4
      } else {
        return 2
      }
    } else if (type == 4) {
      if (labelFlag != 1) { 
        return 4
      } else {
        return 5
      }
    } else if (type == 5) {
      if(refundExaminStatus == '0'){
        return 5
      }else if(refundExaminStatus == '1'){
        return 4
      }
      return 3 
    }
  }
}