<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 7/9/2018
 * Time: 5:10 AM
 */
$object = $request['object'];
switch ($request['header-type']) {
    case 'comment-load':
        $temp = [];
        $counter = 1;
        $select = $DLA->Select("SELECT * FROM `app_comment_system`");
        if ($select) {
            for ($i = 0; $i < count($select); $i += 1) {
                $row = $select[$i];
                $temp[] = array(
                    "counter" => $counter,
                    "text" => CreateText($counter - 1),
                    "message" => $row['Message'],
                    "left-data" => DateDifferent(ConvertDataToFormat($row['Date']), TimeNowG()),
                    "send-date" => ConvertToJalali($row['Date'])['date'],
                    "communicate" => $row['Communicate'],
                    "subject" => $row['Subject'],
                    "sender" => $row['FullName'],
                    "code" => $row['ID'],
                    "Option" => Option($row['ID'], ($counter - 1), false)
                );
                $counter += 1;
            }
        }
        echo json_encode($temp, JSON_UNESCAPED_UNICODE);
        break;
    case 'comment-delete':
        echo($DLA->DoCommand("DELETE FROM `app_comment_system` WHERE `ID`='{$object['id']}'") ? "1" : "0");
        break;
    default:
        break;
}

function Option($ID, $index, $mode)
{
    $html = '';
    $html .= ' <div class="btn-group-vertical " role="group" aria-label="Vertical button group">';
    if ($mode) {
        $html .= '<button data-index="' . $index . '" class="btn btn-xs btn-success waves-effect" data-mod="commend" data-action="accept-item" data-table="' . ($mode ? "ExpectingTable" : "AcceptingTable") . '" data-id="' . $ID . '">';
        $html .= '<i class="material-icons">assignment_turned_in</i>';
        $html .= '</button>';
    }
    $html .= '<button data-index="' . $index . '" class="btn btn-xs btn-danger waves-effect" data-mod="commend" data-action="delete-item" data-table="' . ($mode ? "ExpectingTable" : "AcceptingTable") . '" data-id="' . $ID . '">';
    $html .= '<i class="material-icons">delete</i>';
    $html .= '</button>';
    $html .= '</div>';
    return $html;
}


function CreateText($index)
{//
    return "<button data-mod='commend' data-index='$index' data-action='model-item' class='btn btn-default waves-effect'>نمایش پیام</button>";
}