<?php

use app\models\Restaurant;
use yii\helpers\Html;
use yii\helpers\Url;

/** @var yii\web\View $this */
/** @var app\models\RestaurantSearch $searchModel */
/** @var yii\data\ActiveDataProvider $dataProvider */

$this->title = 'Рестораны';
?>
<div class="restaurant-index">

    <div class="mb-4">
        <h2 class="font-weight-bold text-dark">
            <b>Управление ресторанами</b>
        </h2>
        <p class="text-muted mb-0" style="font-size: 14px;">Список всех зарегистрированных ресторанов</p>
    </div>

    <div class="bg-white rounded-3 mb-4">
        <div class="p-4">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h3 class="font-weight-bold mb-0" style="font-size: 18px;">
                    <b>Все рестораны</b>
                </h3>
            </div>

            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th scope="col" style="font-size: 13px; font-weight: 600; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); padding: 12px 16px; border-bottom: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">Изображение</th>
                            <th scope="col" style="font-size: 13px; font-weight: 600; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); padding: 12px 16px; border-bottom: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">Ресторан</th>
                            <th scope="col" style="font-size: 13px; font-weight: 600; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); padding: 12px 16px; border-bottom: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">Адрес</th>
                            <th scope="col" style="font-size: 13px; font-weight: 600; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); padding: 12px 16px; border-bottom: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">Контакты</th>
                            <th scope="col" style="font-size: 13px; font-weight: 600; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); padding: 12px 16px; border-bottom: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">Документы</th>
                            <th scope="col" style="font-size: 13px; font-weight: 600; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); padding: 12px 16px; border-bottom: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">Статус и действия</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ($dataProvider->getCount() > 0): ?>
                            <?php foreach ($dataProvider->getModels() as $index => $model): ?>
                                <tr style="border-bottom: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">
                                    <td style="padding: 16px;">
                                        <?php if ($model->img): ?>
                                            <a href="<?= Yii::getAlias('@web/restaurant_imgs/') . $model->img ?>" target="_blank" class="d-inline-block">
                                                <img src="<?= Yii::getAlias('@web/restaurant_imgs/') . $model->img ?>" 
                                                     alt="<?= Html::encode($model->name) ?>" 
                                                     style="width: 80px; height: 80px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border-color, rgba(224, 224, 224, 0.56));">
                                            </a>
                                        <?php else: ?>
                                            <div style="width: 80px; height: 80px; background-color: var(--border-color, rgba(224, 224, 224, 0.56)); border-radius: 8px; display: flex; align-items: center; justify-content: center;">
                                                <i class="fas fa-image" style="font-size: 24px; color: var(--text-secondary, rgba(0, 0, 0, 0.6));"></i>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td style="padding: 16px;">
                                        <div style="font-size: 14px; color: var(--text-primary, #000000); font-weight: 500;">
                                            <?= Html::encode($model->name) ?>
                                        </div>
                                        <div style="font-size: 12px; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); margin-top: 4px;">
                                            <?= Html::encode($model->category) ?>
                                        </div>
                                        <?php if ($model->estimation): ?>
                                            <div class="d-flex align-items-center mt-2">
                                                <span style="color: #f59e0b; font-weight: 500; font-size: 13px;">
                                                    <?= number_format($model->estimation, 1) ?>
                                                </span>
                                                <i class="fas fa-star ml-1" style="color: #f59e0b; font-size: 11px;"></i>
                                                <?php if ($model->review_count): ?>
                                                    <span style="font-size: 11px; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); margin-left: 4px;">
                                                        (<?= $model->review_count ?>)
                                                    </span>
                                                <?php endif; ?>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td style="padding: 16px; font-size: 14px; color: var(--text-primary, #000000); max-width: 200px;">
                                        <?= Html::encode($model->address) ?>
                                    </td>
                                    <td style="padding: 16px;">
                                        <div style="font-size: 14px; color: var(--text-primary, #000000);">
                                            <i class="fas fa-phone mr-1" style="font-size: 12px; color: var(--text-secondary, rgba(0, 0, 0, 0.6));"></i>
                                            <?= Html::encode($model->phone) ?>
                                        </div>
                                        <div style="font-size: 12px; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); margin-top: 4px;">
                                            <i class="fas fa-envelope mr-1" style="font-size: 11px;"></i>
                                            <?= Html::encode($model->email) ?>
                                        </div>
                                        <?php if ($model->count_orders): ?>
                                            <div style="font-size: 12px; color: var(--text-secondary, rgba(0, 0, 0, 0.6)); margin-top: 4px;">
                                                <i class="fas fa-shopping-bag mr-1" style="font-size: 11px;"></i>
                                                Заказов: <?= Html::encode($model->count_orders) ?>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td style="padding: 16px;">
                                        <div class="d-flex flex-column gap-1">
                                            <?php if ($model->register_document): ?>
                                                <?= Html::a(
                                                    '<i class="fas fa-file-contract mr-1" style="font-size: 12px;"></i>Регистрация',
                                                    ['@web/restaurant_documents/' . $model->register_document],
                                                    [
                                                        'target' => '_blank',
                                                        'class' => 'btn btn-sm',
                                                        'style' => 'font-size: 12px; text-transform: none; background-color: #dbeafe; color: #1e40af; border: none; border-radius: 4px; padding: 4px 8px; text-align: left; display: flex; align-items: center;',
                                                        'onmouseover' => 'this.style.backgroundColor="#bfdbfe"',
                                                        'onmouseout' => 'this.style.backgroundColor="#dbeafe"',
                                                        'download' => 'register_document_' . $model->id . '.' . pathinfo($model->register_document, PATHINFO_EXTENSION)
                                                    ]
                                                ) ?>
                                            <?php else: ?>
                                                <span class="text-muted" style="font-size: 12px; color: var(--text-secondary, rgba(0, 0, 0, 0.6));">
                                                    <i class="fas fa-file-contract mr-1" style="font-size: 11px;"></i>Нет документа
                                                </span>
                                            <?php endif; ?>
                                            
                                            <?php if ($model->permission_document): ?>
                                                <?= Html::a(
                                                    '<i class="fas fa-file-signature mr-1" style="font-size: 12px;"></i>Разрешение',
                                                    ['@web/restaurant_documents/' . $model->permission_document],
                                                    [
                                                        'target' => '_blank',
                                                        'class' => 'btn btn-sm',
                                                        'style' => 'font-size: 12px; text-transform: none; background-color: #dcfce7; color: #166534; border: none; border-radius: 4px; padding: 4px 8px; text-align: left; display: flex; align-items: center;',
                                                        'onmouseover' => 'this.style.backgroundColor="#bbf7d0"',
                                                        'onmouseout' => 'this.style.backgroundColor="#dcfce7"',
                                                        'download' => 'permission_document_' . $model->id . '.' . pathinfo($model->permission_document, PATHINFO_EXTENSION)
                                                    ]
                                                ) ?>
                                            <?php else: ?>
                                                <span class="text-muted" style="font-size: 12px; color: var(--text-secondary, rgba(0, 0, 0, 0.6));">
                                                    <i class="fas fa-file-signature mr-1" style="font-size: 11px;"></i>Нет документа
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                    </td>
                                    <td style="padding: 16px; min-width: 220px;">
                                        <?php
                                        $statuses = [
                                            0 => 'На проверке',
                                            1 => 'Принят',
                                            2 => 'Отклонен',
                                        ];
                                        
                                        $currentStatus = $statuses[$model->status] ?? 'Неизвестно';
                                        $statusColors = [
                                            0 => '#ffc107',
                                            1 => '#28a745',
                                            2 => '#dc3545',
                                        ];
                                        $color = $statusColors[$model->status] ?? '#6c757d';
                                        $textColor = $model->status == 0 ? '#000000' : '#ffffff';
                                        
                                        $html = '<div style="margin-bottom: 12px;">';
                                        $html .= '<span class="badge" style="background-color: '.$color.'; color: '.$textColor.'; padding: 4px 10px; font-size: 12px; font-weight: 500; border-radius: 4px;">'.$currentStatus.'</span>';
                                        $html .= '</div>';
                                        
                                        $html .= '<div style="margin-bottom: 12px;">';
                                        $html .= '<form method="post" action="'.Url::to(['restaurant/update-status']).'">';
                                        $html .= Html::hiddenInput('id', $model->id);
                                        $html .= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken);
                                        $quickStatuses = [0, 1, 2];
                                        foreach ($quickStatuses as $key) {
                                            if (isset($statuses[$key])) {
                                                $checked = $model->status == $key ? 'checked' : '';
                                                $radioId = 'status_'.$model->id.'_'.$key;
                                                $html .= '<div style="margin: 6px 0; display: flex; align-items: center;">';
                                                $html .= '<input type="radio" id="'.$radioId.'" name="status" value="'.$key.'" '.$checked.' 
                                                         onchange="this.form.submit()" style="margin-right: 8px; cursor: pointer; accent-color: #588157; width: 14px; height: 14px;">';
                                                $html .= '<label for="'.$radioId.'" style="font-size: 13px; cursor: pointer; color: var(--text-primary, #000000); margin: 0;">'.$statuses[$key].'</label>';
                                                $html .= '</div>';
                                            }
                                        }
                                        $html .= '</form>';
                                        $html .= '</div>';
                                        
                                        $html .= '<div class="d-flex gap-2">';
                                        $html .= Html::a(
                                            '<i class="fas fa-trash-alt" style="font-size: 12px;"></i>',
                                            ['delete', 'id' => $model->id],
                                            [
                                                'class' => 'btn btn-sm d-flex align-items-center justify-content-center',
                                                'style' => 'width: 32px; height: 32px; background-color: #fee2e2; color: #dc2626; border: none; border-radius: 6px;',
                                                'title' => 'Удалить',
                                                'data' => [
                                                    'confirm' => 'Вы уверены, что хотите удалить этот ресторан?',
                                                    'method' => 'post',
                                                ],
                                                'onmouseover' => 'this.style.backgroundColor="#fecaca"',
                                                'onmouseout' => 'this.style.backgroundColor="#fee2e2"'
                                            ]
                                        );
                                        $html .= '</div>';
                                        
                                        echo $html;
                                        ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="7" class="text-center py-5">
                                    <i class="fas fa-utensils mb-3" style="font-size: 48px; color: var(--icon-secondary, #9ca3af);"></i>
                                    <p class="text-muted mb-2" style="color: var(--text-secondary, rgba(0, 0, 0, 0.6));">Нет зарегистрированных ресторанов</p>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

            <?php if ($dataProvider->getCount() > 0): ?>
                <div class="d-flex justify-content-between align-items-center mt-4 pt-3 border-top" style="border-color: var(--border-color, rgba(224, 224, 224, 0.56));">
                    <div class="text-muted" style="font-size: 14px; color: var(--text-secondary, rgba(0, 0, 0, 0.6));">
                        Показано <?= $dataProvider->getCount() ?> из <?= $dataProvider->getTotalCount() ?> ресторанов
                    </div>
                    <?php
                    echo \yii\widgets\LinkPager::widget([
                        'pagination' => $dataProvider->pagination,
                        'options' => ['class' => 'pagination mb-0'],
                        'linkOptions' => [
                            'class' => 'page-link',
                            'style' => 'font-size: 14px; border: 1px solid var(--border-color, rgba(224, 224, 224, 0.56)); margin: 0 2px; border-radius: 6px; color: var(--text-primary, #000000);',
                        ],
                        'disabledPageCssClass' => 'page-link disabled',
                        'disabledListItemSubTagOptions' => [
                            'style' => 'color: var(--text-secondary, rgba(0, 0, 0, 0.6));'
                        ],
                        'activePageCssClass' => 'active',
                    ]);
                    ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const radioInputs = document.querySelectorAll('input[type="radio"]');
        radioInputs.forEach(radio => {
            radio.addEventListener('focus', function() {
                this.style.outline = '2px solid #588157';
                this.style.outlineOffset = '2px';
            });
            
            radio.addEventListener('blur', function() {
                this.style.outline = 'none';
            });
        });
    });
</script>