<?php

declare(strict_types=1);

use yii;
use yii\bootstrap\ActiveForm;

// Инициализация
if (
    !yii::$app->user->isGuest
    && yii::$app->user->identity->agnt
) {
    $panel ?? $panel = 'profile_panel_settings_account';
} else {
    $panel ?? $panel = 'profile_panel_settings_account';
}

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

<div id="page_profile" class="container mb-auto">
    <div class="row h-100 py-3">
        <nav class="col-3">
            <?= $sidebar ?? yii::$app->controller->renderPartial('/profile/sidebar') ?>
        </nav>
        <article class="col-9">
            <div class="p-4 rounded">
                <div id="profile_panel_settings" class="profile_panel">
                    <div class="profile_panel_menu mb-3">
                        <?php if (!yii::$app->user->isGuest) : ?>
                            <label class="btn button_white mb-0 mr-2" for="profile_panel_settings_account">Аккаунт</label>
                            <?php if (yii::$app->user->identity->agnt) : ?>
                                <label class="btn button_white mb-0" for="profile_panel_settings_import">Импорт</label>
                            <?php endif ?>
                        <?php endif ?>
                    </div>
                    <div class="profile_panel_content d-flex">
                        <?php if (!yii::$app->user->isGuest) : ?>
                            <input type="radio" id="profile_panel_settings_account" name="main_panel" <?= $panel === 'profile_panel_settings_account' ? 'checked' : null ?> />
                            <div class="col">
                                <h5>Доставка</h5>
                                <div class="dropdown-divider mb-3"></div>
                                <?php $form = ActiveForm::begin([
                                    'id' => 'form_profile_settings',
                                    'action' => false,
                                    'fieldConfig' => [
                                        'template' => '{label}{input}',
                                    ],
                                    'options' => [
                                        'onsubmit' => 'return false;'
                                    ]
                                ]);

                                // Инициализация
                                $model ?? $model = yii::$app->user->identity;
                                $delivery_to_terminal_list ?? $delivery_to_terminal_list = ['Нет данных'];
                                ?>

                                <?= $form->field($model, 'opts[delivery_to_terminal]', ['options' => ['class' => "mb-1"]])
                                    ->dropDownList($delivery_to_terminal_list, [
                                        'onChange' => 'page_profile_settings(this.parentElement.parentElement, \'profile_panel_settings_account\')',
                                        'disabled' => count($delivery_to_terminal_list) <= 1
                                    ])->label('Терминал'); ?>

                                <small class="d-block mb-1">Выберите терминал <b><u>получателя</u></b> для <b>расчёта доставки</b></small>

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

                            <?php if (yii::$app->user->identity->agnt) : ?>
                                <input type="radio" id="profile_panel_settings_import" name="main_panel" <?= $panel === 'profile_panel_settings_import' ? 'checked' : null ?> />
                                <div class="col">
                                    <h5>Параметры 1C</h5>
                                    <div class="dropdown-divider mb-3"></div>
                                    <?php $form = ActiveForm::begin([
                                        'id' => 'form_profile_settings',
                                        'action' => false,
                                        'fieldConfig' => [
                                            'template' => '{label}{input}',
                                        ],
                                        'options' => [
                                            'onsubmit' => 'return false;'
                                        ]
                                    ]);

                                    // Инициализация
                                    $model ?? $model = yii::$app->user->identity;
                                    $import_oem_list ?? $import_oem_list = ['Нет данных'];
                                    ?>

                                    <?= $form->field($model, 'opts[import_supplies_oem]', ['options' => ['class' => "mb-1"]])
                                        ->dropDownList($import_oem_list, [
                                            'onChange' => 'page_profile_settings(this.parentElement.parentElement, \'profile_panel_settings_import\')',
                                            'disabled' => count($import_oem_list) <= 1
                                        ])->label('OEM-номера'); ?>

                                    <small class="d-block mb-1">Выберите поле в котором хранятся <b>ОЕМ-номера</b> и повторите импорт</small>
                                    <small class="d-block">Значения взяты из импортированных товаров</small>

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

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