<?php

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

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

# Includes Section
require_once "{$_SERVER['DOCUMENT_ROOT']}/bin/php/Classes/pageNavbar.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/bin/php/Classes/formBuilder.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API.php";
require_once "{$_SERVER['DOCUMENT_ROOT']}/api/classes/API_vendors.php";

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

# Page functions

# JS Scripts to load for this page
$jsScriptLoadData['form'] = true;
$jsScriptLoadData['/src/js/sentri/breadCrumbs.js'] = 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_vendor') . '</button></a>');

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

# Retrieve Information for the page
$vendor_uuid = htmlspecialchars($_GET['edit'], ENT_QUOTES, 'UTF-8');
$_GET['vendor_uuid'] = $vendor_uuid;
$API_vendors = new API_vendors();
$_GET['builder'] = [1 => ['where' => [0 => 'vendor_uuid', 1 => $vendor_uuid]]];
$requiredFields = ['vendor_uuid' => ['type' => 'uuid']];
$API_vendors->validateData($requiredFields);
$vendor_data = $API_vendors->getVendors()[0];

# Set breadcrumb data
$_SESSION['breadCrumbArray'][] = ['display' => __('edit_vendor'), 'href' => '/vendors/'];

# Start page output
$pageNavbar->outPutNavbar();
$formBuilder->startForm();
if ($vendor_data) { ?>
    <form id="FormValidation" enctype="multipart/form-data" method="post" action="/api/v1/autop/vendors">
        <input type="hidden" name="vendor_uuid" value="<?php echo $vendor_data["vendor_uuid"] ?>">
        <input type="hidden" name="X-HTTP-Method-Override" value="PUT">
        <div class="card-body">
            <div class="form-group form-show-validation row">
                <label for="vendor_name_edit" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('vendor_name') ?></label>
                <div class="col-lg-9 col-md-12 col-sm-10">
                    <input type="text" class="form-control" id="vendor_name_edit" name="vendor_name" placeholder="" value="<?php echo $vendor_data['vendor_name'] ?>" required>
                </div>
            </div>

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

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

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

            <div class="form-group form-show-validation row">
                <label for="vendor_image" class="col-lg-3 col-md-3 col-sm-4 mt-sm-2"><?php echo __('vendor_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 $vendor_data['vendor_image'] ?>" alt="preview">
                        <input type="file" class="form-control form-control-file" id="vendor_image" name="vendor_image" accept="image/png">
                        <label for="vendor_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 Vendor found with uuid " . htmlspecialchars($_GET['edit'], ENT_QUOTES, 'UTF-8');
}