<?php
$object = $request['object'];
$type = UserType($object['user']);
if ($type === 'direct') {
    switch ($request['header-type']) {
        case 'edit':
            echo($DLA->DoCommand("Update app_notification SET nTitle='{$object['title']}', nContent='{$object['content']}', nMode='{$object['mode']}' WHERE ID='{$object['ID']}'") ? 1 : 0);
            break;
        case 'add':
            echo($DLA->DoCommand("INSERT INTO app_notification (nTitle, nContent, nMode) VALUES ('{$object['title']}','{$object['content']}','{$object['mode']}')") ? 1 : 0);
            break;
        case 'load':
            $select = $DLA->Select("SELECT nContent as 'content' , nTitle as 'name', ID, IsActive as 'active' FROM app_notification WHERE nMode='{$object['mode']}'");
            if ($select) {
                $counter = 0;
                $temp = [];
                for ($i = 0; $i < count($select); $i += 1) {
                    $row = $select[$i];
                    $counter += 1;
                    $temp[] = array(
                        "counter" => $counter,
                        "content" => $row['content'],
                        "name" => $row['name'],
                        "type" => ($object['mode'] === '0' ? 'پیام های عمومی' : 'راهنما های عمومی'),
                        "nCode" => 'n-' . $row['ID'],
                        "ID" => $row['ID'],
                        "active" => $row['active'],
                        "option-btn" => Option($counter - 1, $row['ID'], ($row['active'] === '1' ? true : false))
                    );
                }
                echo json_encode($temp, JSON_UNESCAPED_UNICODE);
            } else {
                echo "[]";
            }
            break;
        case 'delete':
            echo($DLA->DoCommand("DELETE FROM app_notification WHERE ID='{$object['ID']}'") ? 1 : 0);
            break;
        case 'active':
            echo($DLA->DoCommand("UPDATE app_notification SET IsActive='{$object['show']}' WHERE ID='{$object['ID']}'") ? 1 : 0);
            break;
        default:
            break;
    }
} else {
    echo 0;
}

function Option($index, $ID, $f)
{
    $html = '';
    $html .= ' <div class="btn-group-vertical " role="group" aria-label="Vertical button group">';
    $html .= '<button class="btn btn-xs btn-danger waves-effect" data-mod="notification" data-action="delete-item" data-id="' . $ID . '">';
    $html .= '<i class="material-icons">delete_forever</i>';
    $html .= '</button>';
    if ($f == 0) {
        $html .= '<button data-index="' . $index . '" rel="tooltip" title="فعال/غیرفعال" class="btn btn-xs waves-effect btn-warning" data-mod="notification" data-action="show-item" data-id="' . $ID . '" data-value="1">';
        $html .= '<i class="material-icons">visibility_off</i>';
    } else {
        $html .= '<button rel="tooltip" title="فعال/غیرفعال" class="btn btn-xs waves-effect btn-info" data-mod="notification" data-action="show-item" data-id="' . $ID . '" data-value="0">';
        $html .= '<i class="material-icons">visibility</i>';
    }
    $html .= '</button>';
    $html .= '<button data-index="' . $index . '" class="btn btn-xs btn-success waves-effect" data-mod="notification" data-action="edit-item" data-id="' . $ID . '">';
    $html .= '<i class="material-icons">mode_edit</i>';
    $html .= '</button>';
    $html .= '</div>';
    return $html;
}