<?php
use yii\helpers\Html;
use yii\helpers\Url;
use app\models\MenuItem;

if (!isset($ordersInProgress)) {
    $ordersInProgress = 0;
}

$kitchenLoadPercent = min(100, ($ordersInProgress / 5) * 100);
$kitchenLoadText = 'Низкая';
if ($ordersInProgress >= 4) {
    $kitchenLoadText = 'Высокая';
} elseif ($ordersInProgress >= 2) {
    $kitchenLoadText = 'Средняя';
}

$loadColor = '#16a34a';
if ($ordersInProgress >= 4) {
    $loadColor = '#dc2626'; 
} elseif ($ordersInProgress >= 2) {
    $loadColor = '#bc6c25';
}
?>

<div class="mb-4">
    <h2 class="font-weight-bold text-dark mb-0">
        <b>Панель управления - <?= Html::encode($model->name) ?></b>
    </h2>
</div>

<div class="row mb-4">
   <div class="col-md-3 col-sm-6 mb-3">
        <div class="bg-white rounded-3 p-3 h-100">
            <div class="d-flex align-items-center h-100">
                <div class="me-3">
                    <div class="rounded-3 p-2 d-flex align-items-center justify-content-center orders-icon-container" 
                         style="width: 50px; height: 50px; background-color: var(--orders-bg,rgba(219, 234, 254, 0));">
                        <i class="fas fa-shopping-bag" style="font-size: 20px; color: var(--orders-icon, #2563eb);"></i>
                    </div>
                </div>
                <div class="flex-grow-1">
                    <div class="text-muted mb-1" style="font-size: 13px;">Сегодня заказов</div>
                    <div class="font-weight-bold mb-0" style="font-size: 22px;">
                        <b><?= Html::encode($todayOrders) ?></b>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-3 col-sm-6 mb-3">
        <div class="bg-white rounded-3 p-3 h-100">
            <div class="d-flex align-items-center h-100">
                <div class="me-3">
                    <div class="rounded-3 p-2 d-flex align-items-center justify-content-center revenue-icon-container" 
                         style="width: 50px; height: 50px; background-color: var(--revenue-bg, #dcfce7);">
                        <i class="fas fa-ruble-sign" style="font-size: 20px; color: var(--revenue-icon, #16a34a);"></i>
                    </div>
                </div>
                <div class="flex-grow-1">
                    <div class="text-muted mb-1" style="font-size: 13px;">Выручка</div>
                    <div class="font-weight-bold mb-0" style="font-size: 22px;">
                        <b><?= number_format($todayRevenue, 0, '', ' ') ?> ₽</b>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-3 col-sm-6 mb-3">
        <div class="bg-white rounded-3 p-3 h-100">
            <div class="d-flex align-items-center h-100">
                <div class="me-3">
                    <div class="rounded-3 p-2 d-flex align-items-center justify-content-center rating-icon-container" 
                         style="width: 50px; height: 50px; background-color: var(--rating-bg, #fef9c3);">
                        <i class="fas fa-star" style="font-size: 20px; color: var(--rating-icon, #ca8a04);"></i>
                    </div>
                </div>
                <div class="flex-grow-1">
                    <div class="text-muted mb-1" style="font-size: 13px;">Рейтинг</div>
                    <div class="font-weight-bold mb-0" style="font-size: 22px;">
                        <b><?= $model->estimation ? number_format($model->estimation, 1) : '0.0' ?></b>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-3 col-sm-6 mb-3">
        <div class="bg-white rounded-3 p-3 h-100">
            <div class="d-flex align-items-center h-100">
                <div class="me-3">
                    <div class="rounded-3 p-2 d-flex align-items-center justify-content-center time-icon-container" 
                         style="width: 50px; height: 50px; background-color: var(--time-bg, #f3e8ff);">
                        <i class="fas fa-clock" style="font-size: 20px; color: var(--time-icon, #9333ea);"></i>
                    </div>
                </div>
                <div class="flex-grow-1">
                    <div class="text-muted mb-1" style="font-size: 13px;">Среднее время</div>
                    <div class="font-weight-bold mb-0" style="font-size: 22px;">
                        <b><?= Html::encode($model->average_time ?? 0) ?> мин</b>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-lg-8 mb-4">
        <div class="bg-white rounded-3">
            <div class="p-4">
                <h3 class="font-weight-bold mb-3" style="font-size: 20px;"><b>Текущие заказы</b></h3>
                
                <?php if ($currentOrders): ?>
                    <?php foreach ($currentOrders as $order): ?>
                        <?php 
                        $status = $order->status;
                        ?>
                        
                        <div class="border rounded-3 p-3 mb-3">
                            <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center">
                                <div class="mb-2 mb-md-0">
                                    <h5 class="font-weight-bold mb-1" style="font-size: 16px;">
                                        <b>Заказ #<?= Html::encode($order->id) ?></b>
                                    </h5>
                                    <?php 
                                    $orderDetails = [];
                                    if ($order->order_details) {
                                        $details = json_decode($order->order_details, true);
                                        if (is_array($details) && !empty($details)) {
                                            $itemNames = [];
                                            foreach ($details as $item) {
                                                if (isset($item['name'])) {
                                                    $itemNames[] = $item['name'] . ' (x' . ($item['quantity'] ?? 1) . ')';
                                                }
                                            }
                                            if (!empty($itemNames)) {
                                                echo '<p class="text-muted mb-1" style="font-size: 14px; color: var(--text-secondary, rgba(255, 255, 255, 0.6));">';
                                                echo Html::encode(implode(', ', $itemNames));
                                                echo '</p>';
                                            }
                                        }
                                    }
                                    ?>
                                    <p class="mb-0" style="font-size: 13px; color: var(rgb(255, 255, 255), rgb(255, 255, 255));">
                                        <i class="fas fa-clock mr-1" style="margin-right: 5px;"></i>
                                        <?php 
                                        $cookingTime = 0;
                                        if ($order->order_details) {
                                            $details = json_decode($order->order_details, true);
                                            if (is_array($details)) {
                                                foreach ($details as $item) {
                                                    $dish = MenuItem::findOne($item['dish_id'] ?? 0);
                                                    if ($dish && $dish->cooking_time) {
                                                        $cookingTime = max($cookingTime, $dish->cooking_time);
                                                    }
                                                }
                                            }
                                        }
                                        
                                        if ($status == 'processing' || $status == 'preparing'): ?>
                                            <?= $cookingTime ?> мин
                                        <?php elseif ($status == 'ready'): ?>
                                            Готово
                                        <?php elseif ($status == 'completed'): ?>
                                            Завершен
                                        <?php else: ?>
                                            <?= $cookingTime ?> мин
                                        <?php endif; ?>
                                    </p>
                                </div>
                                
                                <?php if (in_array($status, ['new', 'processing', 'preparing'])): ?>
                                    <?php 
                                    $buttonText = '';
                                    $buttonColor = '#588157';
                                    
                                    if ($status == 'new') {
                                        $buttonText = 'Принять в работу';
                                        $buttonColor = '#588157';
                                    } elseif ($status == 'processing' || $status == 'preparing') {
                                        $buttonText = 'Готово';
                                        $buttonColor = '#bc6c25';
                                    }
                                    ?>
                                    
                                    <form action="<?= Url::to(['site/update-order-status']) ?>" method="post" style="display: inline;">
                                        <input type="hidden" name="<?= Yii::$app->request->csrfParam ?>" value="<?= Yii::$app->request->csrfToken ?>">
                                        <input type="hidden" name="orderId" value="<?= $order->id ?>">
                                        <button type="submit" 
                                                style="font-size: 13px; background-color: <?= $buttonColor ?>; color: white; border: none; border-radius: 8px; padding: 8px 16px; cursor: pointer; align-self: flex-start;">
                                            <?= $buttonText ?>
                                        </button>
                                    </form>
                                <?php elseif ($status == 'ready'): ?>
                                    <span style="font-size: 13px; 
                                                background-color: var(--courier-badge-bg, rgb(216, 255, 230)); 
                                                color: var(--courier-badge-text, #166534); 
                                                border: 1px solid var(--courier-badge-border, transparent); 
                                                border-radius: 8px; 
                                                padding: 8px 16px; 
                                                align-self: flex-start;
                                                margin-top: 15px">
                                        <?= $order->delivery_type === 'delivery' ? 'Ожидает курьера' : 'Готово к выдаче' ?>
                                    </span>
                                <?php else: ?>
                                    <span style="font-size: 13px; background-color: rgba(68, 128, 92, 0.1); color: #14532d; border: 1px solid rgba(25, 135, 84, 0.25); border-radius: 8px; padding: 8px 16px; align-self: flex-start;">
                                        Завершен
                                    </span>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <div class="border rounded-3 p-3 text-center">
                        <p class="text-muted mb-0">Нет текущих заказов</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="bg-white rounded-3 mb-4">
            <div class="p-4">
                <h3 class="font-weight-bold mb-3" style="font-size: 15px;"><b>Быстрые действия</b></h3>
                <div class="d-grid gap-3">
                    <?= Html::a(
                        '<i class="fas fa-utensils mr-2" style="font-size: 16px; margin-right: 10px"></i>Управление меню',
                        ['/site/menu-management'],
                        [
                            'class' => 'btn',
                            'style' => 'font-size: 14px; text-transform: none; background-color: #588157; color: white; border: none; border-radius: 8px; padding: 10px 16px; text-align: left; display: flex; align-items: center;',
                            'onmouseover' => 'this.style.backgroundColor="rgb(53, 126, 52)"',
                            'onmouseout' => 'this.style.backgroundColor="#588157"'
                        ]
                    ) ?>
                    
                    <?= Html::a(
                        '<i class="fas fa-chart-bar mr-2" style="font-size: 16px; margin-right: 10px"></i>Отчеты',
                        ['/reports/index'],
                        [
                            'class' => 'btn',
                            'style' => 'font-size: 14px; background-color: #bc6c25; text-transform: none; color: white; border: none; border-radius: 8px; padding: 10px 16px; text-align: left; display: flex; align-items: center;',
                            'onmouseover' => 'this.style.backgroundColor="rgb(209, 85, 2)"',
                            'onmouseout' => 'this.style.backgroundColor="#bc6c25"'
                        ]
                    ) ?>
                    
                    <?= Html::a(
                        '<i class="fas fa-cog mr-2" style="font-size: 16px; margin-right: 10px"></i>Настройки',
                        ['/site/restaurant-settings'],
                        [
                            'class' => 'btn',
                            'style' => 'text-transform: none; font-size: 14px; background-color: var(--settings-btn-bg, #374151); color: var(--settings-btn-text, white); border: none; border-radius: 8px; padding: 10px 16px; text-align: left; display: flex; align-items: center;',
                            'onmouseover' => 'this.style.backgroundColor="var(--settings-btn-hover, rgb(73, 85, 105))"',
                            'onmouseout' => 'this.style.backgroundColor="var(--settings-btn-bg, #374151)"'
                        ]
                    ) ?>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-3" style="margin-bottom: 20px">
            <div class="p-4">
                <h3 class="font-weight-bold mb-3" style="font-size: 15px;"><b>Статус кухни</b></h3>
                <div class="mb-3 d-flex justify-content-between">
                    <span class="text-muted" style="font-size: 14px; color: var(rgb(255, 255, 255), rgb(255, 255, 255));">Загрузка кухни</span>
                    <span class="font-weight-bold" style="font-size: 14px; color: <?= $loadColor ?>;">
                        <b><?= $kitchenLoadText ?></b>
                    </span>  
                </div>
                <div class="progress mb-3 rounded-2" style="height: 8px; background-color: var(--progress-bg, rgba(0, 0, 0, 0.1));">
                    <div class="rounded-2" role="progressbar" 
                         style="width: <?= $kitchenLoadPercent ?>%; height: 100%; background-color: <?= $loadColor ?>"></div>
                </div>
                <p class="mb-0" style="font-size: 13px; color: var(rgb(255, 255, 255), rgba(219, 219, 219, 0.6));">
                    В работе <?= $ordersInProgress ?> заказ(ов) из 5 возможных
                </p>
            </div>
        </div>
    </div>
</div>