<?php

if ($request['object'] == 'load') {
    $select = null;

    $select = $DLA->Select("Select ID, gName, gIcon, gActive, gMaster, (SELECT count(*) FROM `app_acceptor_list` WHERE `sGuild`=app_guild_list.ID) as count From app_guild_list ORDER BY gName");
    $array = array(
        "error" => 0,
        "id" => null,
        "name" => null,
        "url" => null,
        'count' => 0,
        "img" => null,
        "show" => null,
        "master" => null,
        "button-edit" => null,
        "index" => null,
        "counter" => null
    );
    $empty = array();
    if ($select) {
        $counter = 0;
        for ($i = 0; $i < count($select); $i += 1) {
            $row = $select[$i];
            $array['error'] = 0;
            $array['id'] = $row['ID'];
            $array['name'] = $row['gName'];
            $array['url'] = $row['gIcon'];
            $array['count'] = $row['count'];
            $array['img'] = '<a href="javascript:void(0);" class="thumbnail"><img style="width: 90px!important;" src="' . (is_image($row['gIcon']) ? $row['gIcon'] : defaultImageUrl) . '" class="img-responsive"></a>';
            $array['show'] = $row['gActive'];
            $array['master'] = $row['gMaster'];
            $array['button-edit'] = LoadOptionEdit($row['ID'], $row['gActive'], $counter);
            $array['index'] = $counter;
            $array['counter'] = ($counter + 1);
            $empty[] = $array;
            $counter += 1;
        }
    } else {
        $array['error'] = 1;
        $empty[] = $array;
    }
    echo json_encode($empty, JSON_UNESCAPED_UNICODE);
} elseif ($request['object'] == 'guild-master-control') {
    $r = $request['r'];
    switch ($r) {
        case 'load':

            $select = null;
            $select = $DLA->Select("Select ID, gName, gActive, gTabIndex From app_guild_list WHERE gMaster ='1' ");
            $mainObject = array(
                'error' => 0,
                "list" => array(),
                "non-list" => array()
            );
            $empty = array();
            if ($select) {
                for ($i = 0; $i < count($select); $i += 1) {
                    $row = $select[$i];
                    $array = array(
                        "error" => 0,
                        "cat_id" => null,
                        "cat_index" => null,
                        "cat_name" => null,
                        "cat_active" => null
                    );

                    $counter += 1;
                    $array['error'] = 0;
                    $array['cat_id'] = $row['ID'];
                    $array['cat_name'] = $row['gName'];
                    $array['cat_active'] = $row['gActive'];
                    $array['cat_index'] = $row['gTabIndex'];

                    $empty[] = $array;
                }
            }
            if ($counter > 0) {
                $mainObject['list'] = bubbleSort($empty, 'cat_index', true);
            }
            echo json_encode($mainObject, JSON_UNESCAPED_UNICODE);
            break;
        case 'update':
            $flag = 1;
            $string = json_decode($request['string'], true);
            $tabCounter = 0;
            if (count($string) > 0) {
                for ($i = 0; $i < count($string); $i += 1) {
                    $tabCounter += 1;
                    $masterID = $string[$i]['id'];
                    if (count($string[$i]) == 2) {
                        $itemCounter = 0;
                        for ($ii = 0; $ii < count($string[$i]['children']); $ii += 1) {
                            $itemCounter += 1;
                            $id = $string[$i]['children'][$ii]['id'];
                            if (!$DLA->DoCommand("UPDATE app_guild_list SET gMaster='0',gMasterID='{$masterID}',gTabIndex='{$itemCounter}',gMasterLive='1' WHERE ID='{$id}'"))
                                $flag = 0;
                        }
                    }
                    if (!$DLA->DoCommand("UPDATE app_guild_list SET gMaster='1',gMasterID='0',gTabIndex='{$tabCounter}',gMasterLive='1' WHERE ID='{$masterID}'"))
                        $flag = 0;
                }
                echo $flag;
            }

            break;
        default:
            break;
    }
}

function LoadOptionEdit($ID, $f, $index)
{
    $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="category" 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="category" 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="category" 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="category" data-action="edit-item" data-id="' . $ID . '">';
    $html .= '<i class="material-icons">mode_edit</i>';
    $html .= '</button>';
    $html .= '</div>';
    return $html;
}