<?php

use yii\bootstrap\ActiveForm;
use yii\helpers\Html;

use app\models\Product;

?>

<link href="/css/pages/product.css" rel="stylesheet">

<div id="page_product" class="container mb-auto">
    <div class="row py-3">
        <article class="col-12">
            <div class="p-3 d-flex flex-column rounded">
                <div id="product_slider" class="row px-3 profile_panel">
                    <div class="col-1 product_slider_preview p-0 pr-3 mb-3 d-flex flex-column">
                        <?php
                        // Инициализация
                        $covr_not_found = true;

                        // Проверка на то, что обложка установлена
                        foreach (!empty($model['imgs']) && is_array($model['imgs']) ? $model['imgs'] : [] as $image) {
                            if ($image['covr'] ?? false) {
                                $covr_not_found = false;
                            }
                        }

                        foreach (!empty($model['imgs']) && is_array($model['imgs']) ? $model['imgs'] : [] as $key => $image) {
                            // Перебор изображений для генерации обложек

                            // Инициализация
                            $h150 = $image['h150'] ?? '/img/covers/h150/product.png';

                            if (($image['covr'] ?? false) || ($covr_not_found && $key === 0)) {
                                echo <<<HTML
                                        <label class="p-0 mb-2 rounded unselectable active" for="product_slider_image_$key" onclick="return product_image_choose('$key', this.parentElement);">
                                HTML;
                            } else {
                                echo <<<HTML
                                        <label class="p-0 mb-2 rounded unselectable" for="product_slider_image_$key" onclick="return product_image_choose('$key', this.parentElement);">
                                HTML;
                            }

                            echo <<<HTML
                                    <img class="img-fluid" src="$h150"/>
                                </label>
                            HTML;
                        }
                        ?>

                        <?php if (
                            !yii::$app->user->isGuest
                            && (yii::$app->user->identity->type === 'administrator'
                                || yii::$app->user->identity->type === 'moderator')
                        ) : ?>
                            <input id="product_slider_image_new" type="file" class="d-none" onchange="return product_panel_images_write('<?= $model['catn'] ?>', this);" multiple />
                            <label class="p-0 mb-2 button_write unselectable" for="product_slider_image_new" role="button">
                                <img class="img-fluid rounded" src="/img/covers/h150/product_new.png" />
                            </label>
                        <?php endif ?>
                    </div>
                    <div class="product_slider_image">
                        <?php

                        // Инициализация
                        $checked = '';

                        foreach (!empty($model['imgs']) && is_array($model['imgs']) ? $model['imgs'] : []  as $key => $image) {
                            // Перебор изображений для генерации полных версий

                            // Инициализация
                            $name = $image['name'] ?? 'Без названия';
                            $orig = $image['orig'] ?? '/img/covers/product.png';

                            if (($image['covr'] ?? false) || ($covr_not_found && $key === 0)) {
                                // Если это изображение является обложкой или обложка не найдена

                                // Реинициализация
                                $checked = 'checked';
                            }

                            // Генерация изображения
                            if (
                                !yii::$app->user->isGuest
                                && (yii::$app->user->identity->type === 'administrator'
                                    || yii::$app->user->identity->type === 'moderator')
                            ) {
                                $catn = $model['catn'];

                                echo <<<HTML
                                    <input type="radio" id="product_slider_image_$key" name="slider" $checked/>
                                    <div class="col p-0 unselectable">
                                        <img class="mb-3 img-fluid w-100 rounded" src="$orig"/>
                                        <div class="d-flex justify-content-center">
                                            <button class="flex-grow-1 mr-3 btn button_blue button_clean" onclick="return product_panel_images_cover_write('$catn', '$key');">Сделать основной обложкой</button>
                                            <button class="btn button_red button_clean" onclick="return product_panel_images_delete('$catn', '$key');">Удалить</button>
                                        </div>
                                    </div>
                                HTML;
                            } else {
                                echo <<<HTML
                                    <input type="radio" id="product_slider_image_$key" name="slider" $checked/>
                                    <div class="col p-0 unselectable">
                                        <img class="img-fluid rounded" src="$orig"/>
                                    </div>
                                HTML;
                            }

                            // Деинициализация
                            $checked = '';
                        }
                        ?>
                    </div>
                    <div class="col ml-4 d-flex flex-column">
                        <div class="row mb-1">
                            <?php if (
                                !yii::$app->user->isGuest
                                && (yii::$app->user->identity->type === 'administrator'
                                    || yii::$app->user->identity->type === 'moderator')
                            ) : ?>
                                <h3 id="title_<?= $model['catn'] ?>" class="my-auto pointer-event" role="button" onclick="return product_panel_title_edit('<?= $model['catn'] ?>', this);"><?= $model['name'] ?? 'Без названия' ?></h3>
                            <?php else : ?>
                                <h3 id="title_<?= $model['catn'] ?>" class="my-auto"><?= $model['name'] ?? 'Без названия' ?></h3>
                            <?php endif ?>
                        </div>

                        <div class="row mb-2">
                            <?php if (
                                !yii::$app->user->isGuest
                                && (yii::$app->user->identity->type === 'administrator'
                                    || yii::$app->user->identity->type === 'moderator')
                            ) : ?>
                                <h6 id="catn_<?= $model['catn'] ?>" class="mr-auto my-auto pointer-event" role="button" onclick="return product_panel_catn_edit('<?= $model['catn'] ?>', this, true);">
                                    <?= $model['catn'] ?? '' ?>
                                    <!-- <small class="d-flex">
                                        <a class="text-dark my-auto ml-3" title="Редактировать" role="button" onclick="return product_panel_edit('<?= $model['catn'] ?>');">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a class="text-dark my-auto ml-2" title="Удалить" role="button" onclick="return product_panel_delete('<?= $model['catn'] ?>');">
                                            <i class="fas fa-trash-alt"></i>
                                        </a>
                                    </small> -->
                                </h6>
                                <h6 id="prod_<?= $model['catn'] ?>" class="my-0">
                                    <?= $model['prod'] ?? 'Неизвестно' ?>
                                </h6>
                            <?php else : ?>
                                <h6 id="catn_<?= $model['catn'] ?>" class="mr-auto my-0">
                                    <?= $model['catn'] ?>
                                </h6>
                                <h6 id="prod_<?= $model['catn'] ?>" class="my-0">
                                    <?= $model['prod'] ?? 'Неизвестно' ?>
                                </h6>
                            <?php endif ?>
                        </div>

                        <div class="dropdown-divider px-0 mb-2"></div>

                        <!-- Информация о товаре -->

                        <?php if (
                            !yii::$app->user->isGuest
                            && (yii::$app->user->identity->type === 'administrator'
                                || yii::$app->user->identity->type === 'moderator')
                        ) : ?>
                            <section id="prod_<?= $model['catn'] ?>_info" class="col">
                                <!-- Габариты -->
                                <p class="form-control-sm p-0 h-auto">
                                    <b>Габариты:</b><span id="prod_<?= $model['catn'] ?>_dmns_x" class="ml-1 pointer-event" role="button" onclick="return product_panel_dimensions_edit('<?= $model['catn'] ?>', this, 'x');"><?= empty($model['dmns']['x']) ? '0' : $model['dmns']['x'] ?></span><span class="mr-1">см</span><small>x</small><span id="prod_<?= $model['catn'] ?>_dmns_y" class="ml-1 pointer-event" role="button" onclick="return product_panel_dimensions_edit('<?= $model['catn'] ?>', this, 'y');"><?= empty($model['dmns']['y']) ? '0' : $model['dmns']['y'] ?></span><span class="mr-1">см</span><small>x</small><span id="prod_<?= $model['catn'] ?>_dmns_z" class="ml-1 pointer-event" role="button" onclick="return product_panel_dimensions_edit('<?= $model['catn'] ?>', this, 'z');"><?= empty($model['dmns']['z']) ? '0' : $model['dmns']['z'] ?></span><span class="mr-1">см</span>
                                </p>
                                <!-- Вес -->
                                <p class="form-control-sm p-0 h-auto">
                                    <b>Вес:</b><span id="prod_<?= $model['catn'] ?>_dmns_x" class="ml-1 pointer-event" role="button" onclick="return product_panel_weight_edit('<?= $model['catn'] ?>', this);"><?= empty($model['wght']) ? '0' : $model['wght'] ?></span><span class="mr-1">г</span>
                                </p>
                            </section>
                        <?php else : ?>
                            <section id="prod_<?= $model['catn'] ?>_info" class="col mb-1">
                                <!-- Габариты -->
                                <p class="form-control-sm p-0 h-auto">
                                    <b>Габариты:</b><span id="prod_<?= $model['catn'] ?>_dmns_x" class="ml-1"><?= empty($model['dmns']['x']) ? '0' : $model['dmns']['x'] ?></span><span class="mr-1">см</span><small>x</small><span id="prod_<?= $model['catn'] ?>_dmns_y" class="ml-1"><?= empty($model['dmns']['y']) ? '0' : $model['dmns']['y'] ?></span><span class="mr-1">см</span><small>x</small><span id="prod_<?= $model['catn'] ?>_dmns_z" class="ml-1"><?= empty($model['dmns']['z']) ? '0' : $model['dmns']['z'] ?></span><span class="mr-1">см</span>
                                </p>
                                <!-- Вес -->
                                <p class="form-control-sm p-0 h-auto">
                                    <b>Вес:</b><span id="prod_<?= $model['catn'] ?>_dmns_x" class="ml-1"><?= empty($model['wght']) ? '0' : $model['wght'] ?></span>г
                                </p>
                            </section>
                        <?php endif ?>

                        <div class="dropdown-divider px-0 mb-2"></div>

                        <!-- Описание -->

                        <div class="row mb-3 h-100 product_panel d-flex flex-column">
                            <?php if (
                                !yii::$app->user->isGuest
                                && (yii::$app->user->identity->type === 'administrator'
                                    || yii::$app->user->identity->type === 'moderator')
                            ) : ?>
                                <p id="description_<?= $model['catn'] ?>" class="mt-0 ml-0 text-break pointer-event product_description" role="button" onclick="return product_panel_description_edit('<?= $model['catn'] ?>', this);"><?= $model['dscr'] ?? 'Без описания' ?></p>
                            <?php else : ?>
                                <p id="description_<?= $model['catn'] ?>" class="mt-0 ml-0 text-break product_description"><?= $model['name'] ?? 'Без описания' ?></p>
                            <?php endif ?>
                            <p class="mt-0">
                                <?php
                                // foreach ($model['catn'] ?? [] as $catn) {
                                //     echo <<<HTML
                                //         $catn
                                //     HTML;
                                // }
                                ?>
                            </p>
                        </div>
                        <?php
                        $form = ActiveForm::begin([
                            'id' => 'form_product_cart',
                            'action' => false,
                            'fieldConfig' => [
                                'template' => '{input}',
                            ],
                            'options' => [
                                'onsubmit' => 'return false;',
                                'class' => 'row mt-auto'
                            ]
                        ]);

                        // Просто для теста
                        $model = new Product();
                        ?>

                        <?php ActiveForm::end(); ?>
                    </div>
                </div>

                <!-- <div class="row mt-3 mx-0">
                    <p class="ml-0">Время для повышения релевантности в поисковиках</p>
                    <time class="ml-auto"></time>
                </div> -->
            </div>
        </article>
    </div>
</div>

<script src="/js/product.js" defer></script>
<?php if (
    !yii::$app->user->isGuest
    && (yii::$app->user->identity->type === 'administrator'
        || yii::$app->user->identity->type === 'moderator')
) : ?>
    <script src="/js/product_panel.js" defer></script>
<?php endif ?>
