<?php

use api\classes\API;
use api\classes\API_platforms;
use bin\php\Classes\formBuilder;
use bin\php\Classes\pageNavbar;

if (!defined('APP_INIT')) {
    exit;
}
# IDE Section

# Includes Section
include_once($_SERVER['DOCUMENT_ROOT'] . '/bin/php/Classes/pageNavbar.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/bin/php/Classes/formBuilder.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/api/classes/API.php');
include_once($_SERVER['DOCUMENT_ROOT'] . '/api/classes/API_platforms.php');

# Check permissions
$API = new API();
if (!$API->checkPermissions('admin-platforms', 'RW', true)) {
    echo 'error 401 unauthorized';
    exit;
}

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['form'] = true;
$jsScriptLoadData['breadCrumbs'] = true;

# PageClasses Setup
$pageNavbar = new pageNavbar(true);
$pageNavbar->AddHTMLButton('<a href="?add="><button class="btn btn-primary"><i class="fas fa-plus text-success"></i>' . __('add_platform') . '</button></a>');

$formBuilder = new formBuilder('edit_platform', '<i class="fas fa-edit"></i>', '/platforms/',);
$formBuilder->submitButtonText = __('save');
$formBuilder->submitButtonIcon = '<i class="fas fa-save"></i>';

# Retrieve Information for the page
$platform_uuid = $_GET['edit'];
$_GET['platform_uuid'] = $platform_uuid;
$API_platforms = new API_platforms();
$_GET['builder'] = [1 => ['where' => [0 => 'platform_uuid', 1 => $platform_uuid]]];
$requiredFields = ['platform_uuid' => ['type' => 'uuid']];
$API_platforms->validateData($requiredFields);
$platform_data = $API_platforms->getPlatforms()[0];

# Set breadcrumb data
array_push($GLOBALS['breadCrumbArray'], array('display' => __('platforms'), 'href' => '/platforms/'));
array_push($GLOBALS['breadCrumbArray'], array('display' => __('edit_platform'), 'href' => ''));

# Start page output
if ($platform_data) {
    $pageNavbar->outPutNavbar();
    $formBuilder->startForm();
    ?>

    <form id="FormValidation" enctype="multipart/form-data" method="post" action="/api/v1/platforms">
        <input type="hidden" name="platform_uuid" value="<?php echo $platform_data["platform_uuid"] ?>">
        <input type="hidden" name="_method" value="PUT">
        <div class="card-body">
            <div class="form-group form-show-validation row">
                <label for="platform_name" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('platform_name') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="text" class="form-control" id="platform_name" name="platform_name" placeholder="" value="<?php echo $platform_data['platform_name'] ?>" required>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="platform_slugify" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('platform_slugify') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="text" class="form-control" id="platform_slugify" name="platform_slugify" placeholder="" value="<?php echo $platform_data['platform_slugify'] ?>" required disabled>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="platform_enabled" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('platform_enabled') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <div class="select2-input">
                        <select id="platform_enabled" name="platform_enabled" class="form-control" required>
                            <option value="1" <?php echo(($platform_data['platform_enabled'] == 1) ? 'selected' : '') ?>><?php echo __('enabled') ?></option>
                            <option value="0" <?php echo(($platform_data['platform_enabled'] == 0) ? 'selected' : '') ?>><?php echo __('disabled') ?></option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="platform_description" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('platform_description') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <textarea class="form-control" id="platform_description" name="platform_description" rows="5"><?php echo $platform_data['platform_description'] ?></textarea>
                </div>
            </div>

            <div class="form-group form-show-validation row">
                <label for="platform_image" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('platform_image') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <div class="input-file input-file-image">
                        <img class="img-upload-preview" width="200" src="data:image/png;base64, <?php echo $platform_data['platform_image'] ?>" alt="preview">
                        <input type="file" class="form-control form-control-file" id="platform_image" name="platform_image" accept="image/png">
                        <label for="platform_image" class="label-input-file btn btn-black btn-round">
                            <span class="btn-label"><i class="fa fa-file-image"></i></span>
                            <?php echo __('upload_image') ?>
                        </label>
                    </div>
                </div>
            </div>
        </div>
        <?php
        $formBuilder->formFooter();
        ?>
    </form>
    <?php
    $formBuilder->endForm();
} else {

    echo "No Platform found with uuid " . htmlspecialchars($_GET['edit'], ENT_QUOTES, 'UTF-8');
}