package com.zhangmeng.online.exam.ui.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.zhangmeng.online.exam.ui.api.ApiUtils;
import com.zhangmeng.online.exam.ui.module.Role;
import com.zhangmeng.online.exam.ui.module.User;
import com.zhangmeng.online.exam.ui.utils.HttpUtils;
import javafx.beans.property.SimpleStringProperty;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhangmeng.online.exam.ui.api.ApiUtils.API_URL;

/**
 * @author zm
 * @date 2025/3/31 17:04
 * @version: 1.0
 */
public class RoleService {
    public static List<Role> getAllRoles() {

        Map<String, Object> params = new HashMap<>();
        params.put("pageNum", ApiUtils.PAGE_NUM);
        params.put("pageSize", ApiUtils.PAGE_SIZE);

        String userListData = HttpUtils.GET(API_URL + "/role/list",params);
        JSONObject jsonObject = JSON.parseObject(userListData);
        JSONArray data = jsonObject.getJSONArray("data");
        Integer total = jsonObject.getInteger("total");
        List<Role> roles = new ArrayList<>();
        for (Object datum : data) {
            JSONObject rolemap = (JSONObject) datum;

            Role role = new Role();
            role.setId(rolemap.getInteger("id"));
            role.setName(rolemap.getString("name"));
            role.setDescription(rolemap.getString("desc")); // 角色描述
            role.setStatus(rolemap.getString("status"));
            role.setPermissions(new ArrayList<>()); // 角色权限

            roles.add(role);
        }

        return roles;
    }
}
