<?php
$object = $request['object'];
switch ($request['header-type']) {


    case 'app-load-slide':
        $query = "SELECT `ID`,`TextTag`,`isActive` FROM `app_main_panel_item`";
        $select = $DLA->Select($query);
        $temp = [];
        $counter = 0;
        if ($select) {
            foreach ($select as $row) {
                $counter += 1;
                $temp[] = array(
                    "id" => $row['ID'],
                    'count' => $counter,
                    'main-text' => $row['TextTag'],
                    'active' => $row['isActive'],
                    'button-edit' => LoadOptionEditSlide($row['ID'], $row['isActive'], $counter)
                );
            }
        }
        echo json_encode($temp, JSON_UNESCAPED_UNICODE);
        break;
    case 'app-load-child-slide':
        $query = "SELECT `content` FROM `app_main_panel_item` WHERE `ID`=" . $object['id'];
        $select = $DLA->Select($query);
        $temp = [];
        $counter = 0;
        if ($select AND count($select) == 1) {
            $jsonString = $select[0]['content'];
            if (strlen($jsonString) > 0) {
                $jsonArray = json_decode($jsonString, true);
                $counter = 0;

                foreach ($jsonArray as $Item) {
                    $item = $DLA->Select("SELECT `ID`,`sPhoto`,`sName` FROM `app_acceptor_list` WHERE `ID`='{$Item["id"]}'");
                    if ($item) {
                        $single = $item[0];
                        $img = is_image(convertStringToImageArray($single['sPhoto'], true));
                        $temp[] = array(
                            "ID" => $single["ID"],
                            "index" => $counter + 1,
                            "state" => $img,
                            "name" => (strlen($single['sName']) === 0 ? "بی نام" : $single['sName']),
                            "url" => ($img ? convertStringToImageArray($single['sPhoto'], true) : defaultImage),
                            "error" => ($single['ID'] == null ? 1 : 0)
                        );
                        $counter += 1;
                    }
                }
            }
        }
        echo json_encode($temp, JSON_UNESCAPED_UNICODE);
        break;
    case 'app-add-chile-slide':
        $callback = array('error' => 0);
        $temp = [];
        $select = $DLA->Select("SELECT `ID` FROM `app_acceptor_list` WHERE `ID`='{$object['id']}'");
        if ($select and count($select) === 1) {
            $select = $DLA->Select("SELECT `ID` FROM `app_main_panel_item` where `ID`='{$object['from']}'");
            if ($select and count($select) === 1) {
                $select = $DLA->Select("SELECT `content` FROM `app_main_panel_item` where `ID`='{$object['from']}'");

                if ($select) {
                    if (count($select) === 1) {
                        $select = $select[0]['content'];
                        $jsonArray = json_decode($select, true);
                        $childArray = array();
                        for ($j = 0; $j < count($jsonArray); $j += 1) {
                            if ($jsonArray[$j]["id"] !== $object['id']) {
                                $temp[] = array('id' => $jsonArray[$j]["id"]);
                            } else {
                                $callback['error'] = 200;
                            }
                        }
                    }
                }
                if ($callback['error'] === 0) {
                    $temp[] = array('id' => $object['id']);
                    $json = json_encode($temp, JSON_UNESCAPED_UNICODE);
                    $query = "UPDATE `app_main_panel_item` SET `content`= '{$json}' WHERE `ID`=" . $object['from'];
                    if ($DLA->DoCommand($query)) {
                        $callback['error'] = 0;
                    } else {
                        $callback['error'] = 1;
                    }
                }
            } else {
                $callback['error'] = 300;
            }
        } else {
            $callback['error'] = 100;
        }
        echo json_encode($callback, JSON_UNESCAPED_UNICODE);
        break;
    case 'app-delete-chile-slide':
        $query = "SELECT `content` FROM `app_main_panel_item` WHERE `ID`=" . $object['from'];
        $select = $DLA->Select($query);
        $temp = [];
        $counter = 0;
        $flag = false;
        if ($select) {
            if (count($select) === 1) {
                $jsonArray = json_decode($select[0]['content'], true);
                if (count($jsonArray) > 0) {
                    foreach ($jsonArray as $item) {
                        if ($item["id"] !== $object['id'])
                            $temp[] = array('id' => $item["id"]);
                    }
                    $json = json_encode($temp, JSON_UNESCAPED_UNICODE);
                    $query = "UPDATE `app_main_panel_item` SET `content`= '{$json}' WHERE `ID`=" . $object['from'];
                    $flag = $DLA->DoCommand($query);
                }
            }
        }
        echo($flag ? "1" : '0');
        break;
    case 'app-active-slide':
        echo($DLA->DoCommand("UPDATE `app_main_panel_item` SET `isActive`='{$object['show']}' WHERE `ID`='{$object['id']}'"));
        break;

}


function LoadOptionEditSlide($ID, $f, $index)
{
    $html = '';
    $html .= ' <div class="btn-group-vertical " role="group" aria-label="Vertical button group">';
    if ($f == 0) {
        $html .= '<button data-index="' . $index . '" rel="tooltip" title="فعال/غیرفعال" class="btn btn-md waves-effect btn-warning" data-mod="app-slide-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-slide-control" data-action="show-item" data-id="' . $ID . '" data-value="0">';
        $html .= '<i class="material-icons">visibility</i>';
    }
    $html .= '</button>';
    $html .= '<button data-index="' . $index . '" data-type="edit-slide" class="btn btn-md btn-info waves-effect" data-mod="app-slide-control" data-action="edit-item" data-id="' . $ID . '">';
    $html .= 'ویرایش محتوا';
    $html .= '</button>';

    $html .= '</div>';
    return $html;
}