<?php

/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 7/9/2018
 * Time: 5:10 AM
 */

$object = $request['object'];
switch ($request['header-type']) {
    case 'app-notification-load':
        $query = "SELECT * FROM app_broadcast_push_notification";
        $select = $DLA->Select($query);
        $temp = [];
        $counter = 1;
        if ($select) {
            for ($i = 0; $i < count($select); $i += 1) {
                $row = $select[$i];
                $temp[] = array(
                    "counter" => $counter,
                    "data" => array(
                        "counter" => $counter,
                        "id" => $row['ID'],
                        "msg_id" => $row['bn_msg_id'],
                        "active" => $row['bn_active'],
                        "time_create" => $row['bn_time_create'],
                        "message" => $row['bn_message'],
                        "title" => $row['bn_title'],
                        "subtitle" => $row['bn_subtitle'],
                        "ticker" => $row['bn_ticker'],
                        "vibrate" => $row['bn_vibrate'],
                        "sound" => $row['bn_sound'],
                        "largeicon" => $row['bn_largeicon'],
                        "smallicon" => $row['bn_smallicon'],
                        "vibrate_array" => $row['bn_vibrate_array'],
                        "request_code" => $row['bn_request_code'],
                        "request_flag" => $row['bn_request_flag'],
                        "number" => $row['bn_number'],
                        "going" => $row['bn_going'],
                        "auto_cancel" => $row['bn_auto_cancel'],
                        "package" => $row['bn_package'],
                        "parameter" => $row['bn_parameter'],),
                    "title-btn" => '<button class="btn btn-default ">' . $row['bn_title'] . '</button>',
                    "see-btn" => '<button class="btn ' . ((int)$row['bn_visit'] > 0 ? "bg-green" : 'btn-danger') . ' btn-lg btn-block waves-effect" type="button"><span class="badge">' . $row['bn_visit'] . '</span></button>',
                    "state-btn" => Active(($counter - 1), $row['ID'], $row['bn_active']),
                    "key-btn" => Keys(($counter - 1), $row['ID'], $row['bn_msg_id']),
                    "option-btn" => Option(($counter - 1), $row['ID'])

                );
                $counter += 1;
            }
        }
        echo json_encode($temp, JSON_UNESCAPED_UNICODE);
        break;
    case 'app-notification-change-key':
        if (strlen($object['id']) > 0) {
            $newID = uniqid();
            if ($DLA->DoCommand("UPDATE app_broadcast_push_notification SET bn_msg_id='{$newID}' WHERE ID='{$object['id']}'")) {
                echo json_encode(array('state' => 0, 'key' => $newID), JSON_UNESCAPED_UNICODE);
            } else {
                echo json_encode(array('state' => 1, 'key' => 0), JSON_UNESCAPED_UNICODE);
            }
        } else {
            echo json_encode(array('state' => 1, 'key' => 0), JSON_UNESCAPED_UNICODE);
        }
        break;
    case 'app-notification-active':
        $query = "UPDATE app_broadcast_push_notification SET bn_active=" . $object['show'] . " WHERE ID=" . $object['id'];
        echo($DLA->DoCommand($query) ? 1 : 0);
        break;
    case 'app-notification-delete':
        $query = "DELETE FROM app_broadcast_push_notification where ID='{$object['id']}'";
        echo($DLA->DoCommand($query) ? 1 : 0);
        break;
    case 'app-notification-add':
        $number = uniqid();
        $query = "INSERT INTO app_broadcast_push_notification 
          (
          bn_msg_id, bn_active, bn_message, 
          bn_title, bn_subtitle, bn_ticker, bn_vibrate,
          bn_sound, bn_largeicon, bn_smallicon, bn_vibrate_array, 
          bn_request_code, bn_request_flag, bn_number, bn_going, 
          bn_auto_cancel, bn_package, bn_parameter, bn_visit
          ) VALUES (
          '{$number}','0','{$object['bn_message']}',
          '{$object['bn_title']}','{$object['bn_subtitle']}','{$object['bn_ticker']}','1',
          '1','{$object['bn_largeIcon']}','icon','{$object['bn_vibrate_array']}',
          '{$object['bn_request_code']}','{$object['bn_request_flag']}','{$object['bn_number']}','{$object['bn_going']}',
          '{$object['bn_auto_cancel']}','{$object['bn_package']}','{$object['bn_parameter']}','0')";
        echo($DLA->DoCommand($query) ? 1 : 0);
        break;
    case 'app-notification-edit':
        $query = "UPDATE app_broadcast_push_notification SET ";
        $query .= " bn_message='{$object['bn_message']}',
            bn_title='{$object['bn_title']}',
            bn_subtitle='{$object['bn_subtitle']}',
            bn_ticker='{$object['bn_ticker']}',
            bn_vibrate='1',
            bn_sound='1', 
            bn_largeicon='{$object['bn_largeIcon']}', 
            bn_smallicon='icon', 
            bn_vibrate_array='{$object['bn_vibrate_array']}', 
            bn_request_code='{$object['bn_request_code']}',
            bn_request_flag='{$object['bn_request_flag']}',
            bn_number='{$object['bn_number']}',
            bn_going='{$object['bn_going']}', 
            bn_auto_cancel='{$object['bn_auto_cancel']}', 
            bn_package='{$object['bn_package']}', 
            bn_parameter='{$object['bn_parameter']}' ";
        $query .= "WHERE ID='{$object['bn_EditID']}'";
        echo($DLA->DoCommand($query) ? 1 : 0);
        break;
    default:
        break;
}

function Option($index, $ID)
{
    $html = '';
    $html .= ' <div class="btn-group " role="group" aria-label="button group">';
    $html .= '<button data-index="' . $index . '" class="btn btn-md btn-danger waves-effect" data-mod="app-notification-control" 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-md btn-success waves-effect" data-mod="app-notification-control" data-action="edit-item" data-id="' . $ID . '">';
    $html .= '<i class="material-icons">edit</i>';
    $html .= '</button>';
    $html .= '</div>';

    return $html;
}

function Active($index, $ID, $f)
{
    $html = '';
    $html .= '</button>';
    if ($f == 0) {
        $html .= '<button data-index="' . $index . '" rel="tooltip" title="فعال/غیرفعال" class="btn btn-md waves-effect btn-warning" data-mod="app-notification-control" data-action="show-item" data-id="' . $ID . '" data-value="1">';
        $html .= '<i class="material-icons">visibility_off</i>';
    } else {
        $html .= '<button data-index="' . $index . '" rel="tooltip" title="فعال/غیرفعال" class="btn btn-md waves-effect btn-info" data-mod="app-notification-control" data-action="show-item" data-id="' . $ID . '" data-value="0">';
        $html .= '<i class="material-icons">visibility</i>';
    }
    $html .= '</button>';
    return $html;

}

function Keys($index, $ID, $msgID)
{
    $html = '<button class="btn btn-success btn-lg waves-effect" data-action="change-key-item" data-mod="app-notification-control" data-id="' . $ID . '" data-index="' . $index . '"><code>' . $msgID . '</code></button>';
    return $html;
}
