<?php
$object = $request['object'];
$type = UserType($object['user']);
if ($type === 'direct') {
    switch ($request['header-type']) {
        case 'load':
            $select = $DLA->Select("SELECT * FROM `app_request_list` WHERE `arlRead`='" . ($object['mode'] === 'read' ? 1 : 0) . "'");
            if ($select) {
                $counter = 0;
                $temp = [];
                foreach ($select as $row) {
                    $counter += 1;
                    $temp[] = array(
                        "counter" => $counter,
                        "ID" => $row['ID'],
                        "msg-code" => 'r-' . $row['ID'],
                        "full-name" => $row['arlFName'] . ' ' . $row['arlLName'],
                        "state" => "<div class='btn btn-default waves-effect'>" . ($object['mode'] === 'read' ? "برسی شده" : "در انتظار برسی") . "</div>",
                        "type" => "<div class='btn btn-default waves-effect'>" . ($row['arlMod'] === 'client' ? "درخواست عضویت" : "درخواست پذیرندگی") . "</div>",
                        "option" => Option($counter - 1, $row['ID'])
                    );
                }
                echo json_encode($temp, JSON_UNESCAPED_UNICODE);
            } else {
                echo "[]";
            }
            break;
        case 'delete':
            echo($DLA->DoCommand("DELETE FROM `app_request_list` WHERE `ID`='{$object['ID']}'") ? "1" : "0");
            break;
        case 'deactive':
            echo($DLA->DoCommand("UPDATE `app_request_list` SET `arlRead`='1' WHERE `ID`='{$object['ID']}'") ? "1" : "0");
            break;
        default:
            break;
    }
} else {
    echo 0;
}

function Option($index, $ID)
{
    $html = '';
    $html .= ' <div class="btn-group-vertical " role="group" aria-label="Vertical button group">';
    $html .= '<button class="btn btn-danger waves-effect" data-mod="request" data-action="delete-item" data-id="' . $ID . '">';
    $html .= '<i class="material-icons">delete_forever</i>';
    $html .= '</button>';
    $html .= '<button data-index="' . $index . '" class="btn btn-success waves-effect" data-mod="request" data-action="read-item" data-id="' . $ID . '">';
    $html .= '<i class="material-icons">open_in_browser</i>';
    $html .= '</button>';
    $html .= '</div>';
    return $html;
}

