/*
    COPS
*/

#include <YSI_Coding\y_hooks>

#define check_cop                   if(!IsACop(playerid)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not a cop.");
#define check_duty                  if(!Iter_Contains(cops_duty, playerid)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not on duty.");
#define check_cop_car               if(Vehicles[GetPlayerVehicleID(playerid)][vPersonal] > 2 && Vehicles[GetPlayerVehicleID(playerid)][vType] != 7) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not in a cop car.");

#define JAIL_REASON_DEATH           (1)
#define JAIL_REASON_DISCONNECT      (2)
#define JAIL_REASON_ARREST          (3)

new 
    Cuffed[MAX_PLAYERS],
    Somated[MAX_PLAYERS],
    WantedPlayers[MAX_PLAYERS][50];

hook OnGameModeInit()
{
    CreateDynamicPickup(1314, 23, 1568.3859,-1691.6636,5.8906, -1, -1, -1, 100.0, -1, 0);
    CreateDynamic3DTextLabel(""EMBED_SERVER"Arrest point\n"EMBED_WHITE"Type /arrest to arrest a suspect.", 0xFFFFFF99, 1568.3859, -1691.6636, 5.8906, 15.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 15.0, -1, 0);
}

hook OnPlayerDisconnect(playerid, reason)
{
    Iter_Remove(cops_duty, playerid);
    Cuffed[playerid] = 0;
    Somated[playerid] = 0;

    OnSuspectGetsCaught(playerid, JAIL_REASON_DISCONNECT, INVALID_PLAYER_ID);
}

hook OnPlayerSpawn(playerid)
{
    if(Cuffed[playerid])
    {
        Cuffed[playerid] = 0;
        TogglePlayerControllable(playerid, 1);
    }
}

hook OnPlayerDeath(playerid, killerid, reason)
{
    OnSuspectGetsCaught(playerid, JAIL_REASON_DEATH, killerid);
}

CB:OnPlayerJailed(playerid, time)
{
    playerVariables[playerid][pJailTime] = time;

	gString[0] = EOS;
	format(gString, sizeof(gString), "jailed for ~p~%s minutes", ConvertToMinutes(playerVariables[playerid][pJailTime]));
	PlayerTextDrawSetString(playerid, ptd_wanted, gString);
	PlayerTextDrawShow(playerid, ptd_wanted);
	SetPlayerWantedLevel(playerid, 0);

	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "update `server_accounts` set `playerJailTime` = '%d' where `playerID` = '%d'", time, playerVariables[playerid][pSQLID]);
	mysql_tquery(SQL, gQuery, "", "");

    SetPlayerWantedLevel(playerid, 0);
	playerVariables[playerid][pWantedLevel] = 0;

	format(playerVariables[playerid][pCrime1], 48, "no crime");
	format(playerVariables[playerid][pCrime2], 48, "no crime");
	format(playerVariables[playerid][pCrime3], 48, "no crime");

	PlayerTextDrawHide(playerid, ptd_wanted);

	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "update `server_accounts` set `playerWantedLevel` = '0', `playerCrime1` = 'no crime', `playerCrime2` = 'no crime', `playerCrime3` = 'no crime' where `playerID` = '%d'", playerVariables[playerid][pSQLID]);
	mysql_tquery(SQL, gQuery, "", "");
    return 1;
}

CB:OnSuspectGetsCaught(suspect, type, cop_)
{
    if(!playerVariables[suspect][pWantedLevel]) return 1;

    new price = playerVariables[suspect][pWantedLevel], time = playerVariables[suspect][pWantedLevel] * 150;
    if(type == JAIL_REASON_DEATH || type == JAIL_REASON_DISCONNECT)
    {
        price *= 1000;
        time *= 2;

        new cops[128], total_cops;
        foreach(new cop : cops_duty)
        {
            if(IsPlayerStreamedIn(cop, suspect))
            {
                if(GetDistanceBetweenPlayers(cop, suspect) <= 30)
                {
                    GameTextForPlayer(cop, "Running suspect bonus!", 5000, 1);

                    SCM(cop, COLOR_YELLOW, "Ai primit $%s bonus pentru prinderea suspectului %s.", FormatNumber(price), GetName(suspect));
                    GivePlayerCash(cop, price);
                    PlayerPlaySound(cop, 1058, 0.0, 0.0, 0.0);

                    format(cops, 128, "%s%s ", cops, GetName(cop));
                    total_cops++;
                    GiveRaportPoints(cop, 0, 1);
                }
            }
        }

        if(total_cops > 0)
        {
            OnPlayerJailed(suspect, time);
        }

        SendNearbyMessage(suspect, COLOR_PURPLE, 30.0, "* %s is now in jail thanks to %s", GetName(suspect), cops);

        for(new x = 1; x <= 2; x++) SendGroupMessage(x, COLOR_D, "Dispatch: %s has been killed by %s and will be in jail for %d seconds, over.", GetName(suspect), GetName(cop_), time);
    }
    
    else
    {
        price *= 500;
        GameTextForPlayer(cop_, "Running suspect bonus!", 5000, 1);

        SCM(cop_, COLOR_YELLOW, "Ai primit $%s bonus pentru arestarea suspectului %s.", FormatNumber(price), GetName(suspect));
        GivePlayerCash(cop_, price);
        PlayerPlaySound(cop_, 1058, 0.0, 0.0, 0.0);

        GiveRaportPoints(cop_, 0, 2);

        for(new x = 1; x <= 2; x++) SendGroupMessage(x, COLOR_D, "Dispatch: %s has been arrested by %s and will be in jail for %d seconds, over.", GetName(suspect), GetName(cop_), time);

        OnPlayerJailed(suspect, time);
    }
    
    return 1;
}

stock GiveDutyWeapons(playerid)
{
    if(Iter_Contains(cops_duty, playerid))
    {
        GivePlayerWeapon(playerid, 24, 100);
        GivePlayerWeapon(playerid, 31, 250);
        GivePlayerWeapon(playerid, 29, 500);
        GivePlayerWeapon(playerid, 41, 500);

        SetPlayerArmourEx(playerid, 100);
        SetPlayerHealthEx(playerid, 100);
    }

    return 1;
}

stock IsACop(playerid)
{
    new group = playerVariables[playerid][pGroup];
    if(groupVariables[group][gType] != 1) return 0;
    return 1;
}

CMD:duty(playerid, params[])
{
    check_cop

    if(GetPlayerVirtualWorld(playerid) == 40001 || GetPlayerVirtualWorld(playerid) == 20001 || GetPlayerVirtualWorld(playerid) == 20002 || (GetPlayerVirtualWorld(playerid) >= 10001 && GetPlayerVirtualWorld(playerid) <= 11000))
    {
        if(Iter_Contains(cops_duty, playerid))
        {
            Iter_Remove(cops_duty, playerid);

            ResetPlayerWeapons(playerid);
            SendNearbyMessage(playerid, COLOR_PURPLE, 20.0, "* Cop %s puts his badge and weapons in his locker.", GetName(playerid));
        }

        else
        {
            Iter_Add(cops_duty, playerid);
            GiveDutyWeapons(playerid);

            SendNearbyMessage(playerid, COLOR_PURPLE, 20.0, "* Cop %s took his badge and weapons from his locker.", GetName(playerid));
        }
    }
    return 1;
}

CMD:su(playerid, params[])
{
    check_cop
    check_duty

    new player, wanted, reason[32];
    if(sscanf(params, "uds[32]", player, wanted, reason))
    {
        return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/su [player id/name] [wanted] [reason]");
    }

    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Player not connected.");
    if(IsACop(player)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You can't use this command on cops.");
    if(player == playerid) return 1;
    if(wanted < 1 || wanted > 6) return SCM(playerid, COLOR_GREY, "Invalid wanted.");

    SetPlayerWantedLevelEx(player, wanted + playerVariables[playerid][pWantedLevel], reason);
    SCM(player, COLOR_DARKRED, "You have commited a crime - reported by: %s - wanted +%d - reason: %s.", GetName(playerid), wanted, reason);

    for(new x = 1; x <= 2; x++) SendGroupMessage(x, COLOR_D, "Dispatch: %s commited a crime (reported by %s) - + %d wanted - reason: %s.", GetName(player), GetName(playerid), wanted, reason);
    return 1;
}

CMD:mdc(playerid, params[])
{
    if(playerVariables[playerid][pAdminLevel] == 0)
    {
        check_cop
    }

    new player;
    if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/mdc [player id/name]");
    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Player not connected.");
    if(playerVariables[player][pWantedLevel] == 0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not chased by the police.");

    SCM(playerid, COLOR_YELLOW, "-- MDC [%s - ID %d] [wanted %d - expires in %s minutes]", GetName(player), player, playerVariables[player][pWantedLevel], ConvertToMinutes(playerVariables[player][pWantedExpire]));
	
	if(!strsame(playerVariables[player][pCrime1], "no crime", true)) SCM(playerid, COLOR_WHITE, playerVariables[player][pCrime1]);
	if(!strsame(playerVariables[player][pCrime2], "no crime", true)) SCM(playerid, COLOR_WHITE, playerVariables[player][pCrime2]);
	if(!strsame(playerVariables[player][pCrime3], "no crime", true)) SCM(playerid, COLOR_WHITE, playerVariables[player][pCrime3]);
    return 1;
}

CMD:mymdc(playerid, params[]) 
{
	if(playerVariables[playerid][pWantedLevel] == 0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not chased by the police.");

	SCM(playerid, COLOR_YELLOW, "-- MDC [%s - ID %d] [wanted %d - expires in %s minutes]", GetName(playerid), playerid, playerVariables[playerid][pWantedLevel], ConvertToMinutes(playerVariables[playerid][pWantedExpire]));
	
	if(!strsame(playerVariables[playerid][pCrime1], "no crime", true)) SCM(playerid, COLOR_WHITE, playerVariables[playerid][pCrime1]);
	if(!strsame(playerVariables[playerid][pCrime2], "no crime", true)) SCM(playerid, COLOR_WHITE, playerVariables[playerid][pCrime2]);
	if(!strsame(playerVariables[playerid][pCrime3], "no crime", true)) SCM(playerid, COLOR_WHITE, playerVariables[playerid][pCrime3]);
	return 1;
}

CMD:aclear(playerid, params[]) 
{
	if(playerVariables[playerid][pAdminLevel] < 1) return SCM(playerid, COLOR_ERROR, "You are not an admin.");

	new player;
	if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/aclear [player id/name]");
	if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"Player not connected.");
	if(playerVariables[player][pWantedLevel] == 0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not chased by the police.");

	SCM(player, COLOR_YELLOW, "Admin %s cleared your wanted level.", GetName(playerid));
	SCM(playerid, COLOR_WARNING, "/aclear: You cleared %s's wanted level.", GetName(player));

	SetPlayerWantedLevel(player, 0);
	playerVariables[player][pWantedLevel] = 0;

	format(playerVariables[player][pCrime1], 48, "no crime");
	format(playerVariables[player][pCrime2], 48, "no crime");
	format(playerVariables[player][pCrime3], 48, "no crime");

	PlayerTextDrawHide(player, ptd_wanted);

	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "update `server_accounts` set `playerWantedLevel` = '0', `playerCrime1` = 'no crime', `playerCrime2` = 'no crime', `playerCrime3` = 'no crime' where `playerID` = '%d'", playerVariables[player][pSQLID]);
	mysql_tquery(SQL, gQuery, "", "");
	return 1;
}

CMD:clear(playerid, params[]) 
{
	check_cop
    check_duty

	new player;
	if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/clear [player id/name]");
	if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"Player not connected.");
	if(playerVariables[player][pWantedLevel] == 0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not chased by the police.");
    if(playerid == player) return 1;

	SCM(player, COLOR_YELLOW, "Cop %s cleared your wanted level.", GetName(playerid));
	SCM(playerid, COLOR_WARNING, "You cleared %s's wanted level.", GetName(player));
    for(new x = 1; x <= 2; x++) SendGroupMessage(x, COLOR_D, "Dispatch: Cop %s cleared %s's wanted level.", GetName(playerid), GetName(player));

	SetPlayerWantedLevel(player, 0);
	playerVariables[player][pWantedLevel] = 0;

	format(playerVariables[player][pCrime1], 48, "no crime");
	format(playerVariables[player][pCrime2], 48, "no crime");
	format(playerVariables[player][pCrime3], 48, "no crime");

	PlayerTextDrawHide(player, ptd_wanted);

	gQuery[0] = EOS;
	mysql_format(SQL, gQuery, sizeof(gQuery), "update `server_accounts` set `playerWantedLevel` = '0', `playerCrime1` = 'no crime', `playerCrime2` = 'no crime', `playerCrime3` = 'no crime' where `playerID` = '%d'", playerVariables[player][pSQLID]);
	mysql_tquery(SQL, gQuery, "", "");
	return 1;
}

CMD:cuff(playerid, params[])
{
    check_cop
    check_cop_car
    check_duty

    new player;
    if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/cuff [player id/name]");
    if(player == playerid) return 1;
    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"Player not connected.");

    if(GetPlayerVehicleID(player) != GetPlayerVehicleID(playerid))
    {
        return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not in your car.");
    }

    if(Cuffed[player])
    {
        return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is already cuffed!");
    }

    Cuffed[player] = 1;
    TogglePlayerControllable(player, 0);

    SendNearbyMessage(playerid, COLOR_PURPLE, 15.0, "* %s cuffed %s.", GetName(playerid), GetName(player));
    return 1;
}

CMD:uncuff(playerid, params[])
{
    if(playerVariables[playerid][pAdminLevel] == 0)
    {
        check_cop
        check_cop_car
        check_duty
    }

    new player;
    if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/uncuff [player id/name]");
    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Player not connected");
    if(!Cuffed[player]) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not cuffed.");
    if(playerid == player) return 1;

    Cuffed[player] = 0;
    TogglePlayerControllable(player, 1);

    SendNearbyMessage(playerid, COLOR_PURPLE, 15.0, "* %s uncuffed %s.", GetName(playerid), GetName(player));
    return 1;
}

CMD:confiscate(playerid, params[])
{
    check_cop
    check_duty

    new player;
    if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/confiscate [player id/name]");
    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Player not connected.");
    if(GetDistanceBetweenPlayers(playerid, player) > 20.0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not near that player.");
    if(playerVariables[player][pDrivingLicense] < gettime()) return SCM(playerid, COLOR_GREY, "That player doesn't have the driving licence.");
    if(playerid == player) return 1;

    new time = gettime() + (120 * 60);
    playerVariables[player][pDrivingLicense] = -time;

    gQuery[0] = EOS;
    mysql_format(SQL, gQuery, sizeof(gQuery), "update `server_accounts` set `playerDrivingLicense` = '%d' where `playerID` = '%d'", playerVariables[player][pDrivingLicense], playerVariables[player][pSQLID]);
    mysql_tquery(SQL, gQuery, "", "");

    new confiscated_time = playerVariables[player][pDrivingLicense] * (-1), year, month, day, hour, minute, second;
    TimestampToDate(confiscated_time, year, month, day, hour, minute, second, 3, 0);

    if(playerVariables[player][pLanguage] == 1) {

        SCM(player, COLOR_DARKRED, "* Your driving license has been confisctated by %s.", GetName(playerid));
        SCM(player, COLOR_DARKRED, "You can get your driving license back on %02d.%02d.%02d, %02d:%02d.", day, month, year, hour, minute);
    }
    else 
    {
        SCM(player, COLOR_DARKRED, "* Licenta ta de condus a fost confiscata de %s.", GetName(playerid));
        SCM(player, COLOR_DARKRED, "Iti poti redobandi permisul de conducere incepand cu data de %02d.%02d.%02d, ora %02d:%02d.", day, month, year, hour, minute);
    }

    SCM(playerid, COLOR_WARNING, "Done! You have confiscated %s's driving license!", GetName(player));

    for(new x = 1; x <= 2; x++) SendGroupMessage(x, COLOR_D, "%s confiscated %s's driving license.", GetName(playerid), GetName(player));
    
    SendNearbyMessage(playerid, COLOR_PURPLE, 15.0, "* %s confiscated %s's driving license.", GetName(playerid), GetName(player));
    GiveRaportPoints(playerid, 1, 1);
    return 1;
}

CMD:m(playerid, params[])
{
    check_cop
    check_duty

    if(isnull(params))
    {
        return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/m(egaphone) [message]");
    }

    Megaphone(playerid, params);
    return 1;
}

CMD:so(playerid, params[])
{
    check_cop
    check_duty
    check_cop_car

    new player;
    if(sscanf(params, "u", player)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/so [player id/name]");
    if(!IsPlayerConnected(player)) return SCM(playerid, COLOR_GREY, "Player not connected.");
    if(GetPVarInt(playerid, "so_delay") > gettime()) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You can't use this command for %d seconds.", GetPVarInt(playerid, "so_delay")-gettime());
    if(GetDistanceBetweenPlayers(playerid, player) > 30.0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not near that player.");

    SetPVarInt(playerid, "so_delay", gettime() + 5);
    Somated[player] = gettime();

    if(GetPlayerState(player) == PLAYER_STATE_DRIVER)
    {
        Megaphone(playerid, "{1560BD}%s{FFFF00}, esti urmarit de politie! Trage pe dreapta imediat!", GetName(player));
    }

    else 
    {
        if(GetPlayerWantedLevel(player) == 0) Megaphone(playerid, "{1560BD}%s{FFFF00}, esti urmarit de politie! Ramai pe loc!", GetName(player));
        else Megaphone(playerid, "{1560BD}%s{FFFF00}, esti urmarit de politie! Te predai?", GetName(player));
    }
    return 1;
}

CMD:arrest(playerid, params[])
{
    check_cop
    check_duty

    new player;
    if(sscanf(params, "u", player))
    {
        return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/arrest [player id/name]");
    }

    if(IsACop(player)) return 1;

    if(GetPlayerVirtualWorld(playerid) != 40001)
    {
        check_cop_car
    
        if(!IsPlayerInVehicle(player, GetPlayerVehicleID(playerid)))
        {
            return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not in your vehicle.");
        }

        if(!IsPlayerInRangeOfPoint(playerid, 15.0, 1568.3859,-1691.6636,5.8906))
        {
            return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not near the arrest point.");
        }
    }
    
    else
    {
        if(GetDistanceBetweenPlayers(playerid, player) < 10.0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You are not near that player.");
    }

    OnSuspectGetsCaught(player, JAIL_REASON_ARREST, playerid);
    GiveRaportPoints(playerid, 0, 1);
    return 1;
}

stock GetWantedPlayers()
{
    new count;
    foreach(new playerid : Player) if(playerVariables[playerid][pWantedLevel] > 0) count++;
    return count;
}

stock Megaphone(playerid, message[], va_args<>)
{   
    new out[200];
    va_format(out, sizeof(out), message, va_start<2>);

    SendNearbyMessage(playerid, COLOR_YELLOW, 100.0, "(megaphone) Politist %s: %s", GetName(playerid), out);
    return 1;
}

CB:DIALOG_WANTED(playerid, dialogid, response, listitem, inputtext[])
{
    if(!response) return 1;
    if(!IsPlayerConnected(WantedPlayers[playerid][listitem])) return 1;
    
    Command_ReProcess(playerid, "find", WantedPlayers[playerid][listitem]);
    return 1;
}

CMD:wanted(playerid, params[])
{
    if(playerVariables[playerid][pAdminLevel] == 0)
    {
        check_cop
    }

    if(GetWantedPlayers() == 0) return SCM(playerid, COLOR_GREY, "Error: "EMBED_WHITE"No wanted players.");

    for(new x; x < 50; x++) WantedPlayers[playerid][x] = INVALID_PLAYER_ID;

    gDialog = "Suspect\tInfo\n";
    foreach(new player : Player)
    {
        if(playerVariables[player][pWantedLevel] > 0)
        {
            format(gDialog, sizeof(gDialog), "%s%s (/id %d)\twanted %d\n", gDialog, GetName(player), player, playerVariables[player][pWantedLevel]);

            for(new x; x < 50; x++)
            {
                if(WantedPlayers[playerid][x] == INVALID_PLAYER_ID) 
                {
                    WantedPlayers[playerid][x] = player;
                    break;
                }
            }
        }
    }

    return ShowDialog(playerid, DIALOG_WANTED, DIALOG_STYLE_TABLIST_HEADERS, ""EMBED_DIALOG"Wanted players", gDialog, "Find", "Exit");
}

/*
    LEADER COMMANDS
*/

CB:DIALOG_MEMBERS(playerid, dialogid, response, listitem, inputtext[])
{
    return 1;
}

CMD:members(playerid, params[])
{
    if(playerVariables[playerid][pGroupRank] < 6) return 1;

    new group = playerVariables[playerid][pGroup];
    inline show_members()
    {
        gDialog = "Rank - Member\tDays in faction\tFaction warns\n";

        new rank, nick[MAX_PLAYER_NAME], days, fw;
        for(new x; x < cache_num_rows(); x++)
        {
            cache_get_value_name_int(x, "playerGroupRank", rank);
            cache_get_value_name(x, "playerName", nick);

            cache_get_value_name_int(x, "playerGroupJoin", days);
            days = (gettime() - days) / 86400;

            cache_get_value_name_int(x, "playerFactionWarns", fw);

            format(gDialog, sizeof(gDialog), "%s[%d] %s\t%d days\t%d/3 FW\n", gDialog, rank, nick, days, fw);
        }

        ShowDialog(playerid, DIALOG_MEMBERS, DIALOG_STYLE_TABLIST_HEADERS, ""EMBED_DIALOG"Faction members", gDialog, "Exit", "");
    }

    gQuery[0] = EOS;
    mysql_format(SQL, gQuery, sizeof(gQuery), "SELECT * FROM `server_accounts` WHERE `playerGroup` = '%d' ORDER BY `playerGroup` DESC", group);
    MySQL_PQueryInline(SQL, using inline show_members, gQuery, "");
    return 1;
}

CMD:changerank(playerid, params[]) 
{
	if(playerVariables[playerid][pGroupRank] < 7) return 1;

    new player, rank;
    if(sscanf(params, "ud", player, rank)) return SCM(playerid, COLOR_GREY, "Syntax: "EMBED_WHITE"/giverank [player id/name] [rank]");
    if(!IsPlayerConnected(player)) return SendClientMessage(playerid, COLOR_GREY, "Player not connected.");
    if(playerVariables[player][pGroup] != playerVariables[playerid][pGroup]) return SendClientMessage(playerid, COLOR_GREY, "Error: "EMBED_WHITE"That player is not in your group.");
    if(playerVariables[player][pGroupRank] == 7) return SendClientMessage(playerid, COLOR_GREY, "Error: "EMBED_WHITE"You can't change a leader's rank.");
    if(rank < 1 || rank > 6) return SendClientMessage(playerid, COLOR_GREY, "Error: "EMBED_WHITE"Invalid rank (1-6).");

    playerVariables[player][pGroupRank] = rank;
    SendGroupMessage(playerVariables[playerid][pGroup], 0x00C2ECFF,  "# Leader %s has changed %s's rank to %d.", GetName(playerid), GetName(player), rank);

    gQuery[0] = EOS;
    mysql_format(SQL, gQuery, sizeof(gQuery), "UPDATE `server_accounts` SET `playerGroupRank` = '%d' WHERE `playerName` = '%e'", rank, GetName(player));
    mysql_tquery(SQL, gQuery, "", "");
    return 1;
}