import request from '@/utils/user_center_request';

export const GetPermissPolicyService = (req) =>{
    const params = new URLSearchParams();
  for (const key in req) {
    // 处理值为数组的情况（如多选参数）
    if (Array.isArray(req[key])) {
      req[key].forEach(value => {
        params.append(key, value); // 数组参数会以 key=val1&key=val2 形式拼接
      });
    } else {
      params.append(key, req[key]); // 普通键值对
    }
  }
    let url = `/permission/get_policy?${params.toString()}`; 
    let token = localStorage.getItem("token") || "";
    return request.get(url,
        {
            headers: {
                'token': token, //token
            }
        }
    )
}

export const UpdatePermissPolicyService = (Data: any) => {
    // 将 Data 转换为 JSON 字符串
    const jsonData = JSON.stringify(Data);
    const token = localStorage.getItem("token") || "";
    
    return request.post('/permission/update_policy', jsonData, {
        headers: {
            'token': token,
            'Content-Type': 'application/json' // 必须指定 JSON 格式的 Content-Type
        }
    });
};


export const AddPermissPolicyService = (Data: any) => {
    const jsonData = JSON.stringify(Data);
    const token = localStorage.getItem("token") || "";
    return request.post('/permission/add_policy', jsonData,{
        headers: {
            'token': token, //token
            'Content-Type': 'application/json' // 必须指定 JSON 格式的 Content-Type
        }
    })
}

export const DelPermissPolicyService = (Data: any) => {
    const params = new URLSearchParams();
    for (let key in Data) {
        params.append(key, Data[key])
    }
    let token = localStorage.getItem("token") || "";
    return request.post('/permission/del_policy', params,{
        headers: {
            'token': token, //token
        }
    })
}
