<?php
require_once 'check_auth.php';

$current_status = isset($_GET['status']) ? $_GET['status'] : 'all';
$search = isset($_GET['search']) ? cleanInput($_GET['search']) : '';

$allowed_statuses = ['all', 'pending', 'approved', 'shipped', 'delivered'];
if (!in_array($current_status, $allowed_statuses)) {
    $current_status = 'all';
}

$where = [];
$params = [];

if ($current_status != 'all') {
    $where[] = "status = ?";
    $params[] = $current_status;
}

if (!empty($search)) {
    $where[] = "(order_no LIKE ? OR customer_name LIKE ? OR customer_phone LIKE ?)";
    $searchTerm = "%$search%";
    $params[] = $searchTerm;
    $params[] = $searchTerm;
    $params[] = $searchTerm;
}

$whereSql = !empty($where) ? ' WHERE ' . implode(' AND ', $where) : '';

try {
    $countStmt = $pdo->prepare("SELECT COUNT(*) as total FROM orders" . $whereSql);
    $countStmt->execute($params);
    $total = $countStmt->fetch()['total'];
    
    $stmt = $pdo->prepare("SELECT * FROM orders" . $whereSql . " ORDER BY created_at DESC LIMIT 500");
    $stmt->execute($params);
    $orders = $stmt->fetchAll();
    
    $statsStmt = $pdo->query("SELECT status, COUNT(*) as count FROM orders GROUP BY status");
    $stats = [];
    foreach ($statsStmt->fetchAll() as $row) {
        $stats[$row['status']] = $row['count'];
    }
    $all_count = array_sum($stats);
    
} catch(PDOException $e) {
    die("Sorgu hatası: " . $e->getMessage());
}

$page_title = 'Sipariş Yönetimi';
?>
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $page_title; ?> - Max Power Admin</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css" rel="stylesheet">
<style>
body { background-color: #f5f7fa; }
.sidebar {
    background: linear-gradient(180deg, #2c3e50 0%, #34495e 100%);
    min-height: 100vh;
}
.sidebar .nav-link {
    color: rgba(255,255,255,0.85);
    padding: 12px 20px;
    border-radius: 8px;
    margin: 4px 0;
}
.sidebar .nav-link:hover, .sidebar .nav-link.active {
    background: rgba(255,255,255,0.1);
    color: #fff;
}
.stat-card {
    border: none;
    border-radius: 12px;
    transition: transform 0.2s;
}
.stat-card:hover { transform: translateY(-3px); }
.stat-icon {
    width: 50px; height: 50px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}
.table-card { border: none; border-radius: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
.table thead { background: #f8f9fa; }
.table thead th {
    border: none;
    padding: 15px;
    font-weight: 600;
    color: #495057;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.table tbody td { padding: 15px; vertical-align: middle; }
.table tbody tr:hover { background-color: #f8f9fa; }
.status-badge {
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 600;
}
.btn-action { padding: 5px 10px; font-size: 0.8rem; border-radius: 6px; }
.filter-btn {
    border-radius: 25px;
    padding: 8px 20px;
    font-weight: 500;
    transition: all 0.2s;
}
.filter-btn.active {
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.topbar {
    background: #fff;
    border-bottom: 1px solid #eee;
    padding: 15px 0;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
    <div class="col-md-2 col-lg-2 px-0 sidebar">
        <div class="p-4 text-center border-bottom border-secondary">
            <h5 class="text-white mb-0 fw-bold"><i class="fas fa-bolt me-2 text-warning"></i>Max Power</h5>
            <p class="text-white-50 small mb-0 mt-1">Admin Paneli</p>
        </div>
        <nav class="nav flex-column p-3">
            <a href="index.php" class="nav-link active">
                <i class="fas fa-list-alt me-2"></i> Siparişler
            </a>
            <a href="ads.php" class="nav-link">
                <i class="fas fa-ad me-2"></i> Reklam Pikselleri
            </a>
            <a href="logout.php" class="nav-link mt-3" style="color:#ff6b6b;">
                <i class="fas fa-sign-out-alt me-2"></i> Çıkış
            </a>
        </nav>
    </div>
    
    <div class="col-md-10 col-lg-10 px-0">
        <div class="topbar px-4">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h4 class="mb-0 fw-bold"><i class="fas fa-shopping-bag me-2 text-primary"></i>Sipariş Yönetimi</h4>
                    <p class="text-muted mb-0 small">Hoş geldiniz, <strong><?php echo $_SESSION['admin_username']; ?></strong></p>
                </div>
                <div class="d-flex align-items-center gap-2">
                    <span class="text-muted small me-3">
                        <i class="fas fa-clock me-1"></i><?php echo date('d.m.Y H:i'); ?>
                    </span>
                </div>
            </div>
        </div>
        
        <div class="p-4">
            <div class="row g-4 mb-4">
                <div class="col-md-3 col-sm-6">
                    <a href="?status=all" class="text-decoration-none">
                        <div class="card stat-card p-3 <?php echo $current_status == 'all' ? 'border-2 border-primary' : ''; ?>">
                            <div class="d-flex align-items-center">
                                <div class="stat-icon bg-primary bg-opacity-10 text-primary">
                                    <i class="fas fa-shopping-cart"></i>
                                </div>
                                <div class="ms-3">
                                    <h6 class="text-muted mb-1 small">Toplam</h6>
                                    <h3 class="mb-0 fw-bold"><?php echo $all_count; ?></h3>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
                <div class="col-md-3 col-sm-6">
                    <a href="?status=pending" class="text-decoration-none">
                        <div class="card stat-card p-3 <?php echo $current_status == 'pending' ? 'border-2 border-warning' : ''; ?>">
                            <div class="d-flex align-items-center">
                                <div class="stat-icon bg-warning bg-opacity-10 text-warning">
                                    <i class="fas fa-clock"></i>
                                </div>
                                <div class="ms-3">
                                    <h6 class="text-muted mb-1 small">Onay Bekleyen</h6>
                                    <h3 class="mb-0 fw-bold text-warning"><?php echo isset($stats['pending']) ? $stats['pending'] : 0; ?></h3>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
                <div class="col-md-3 col-sm-6">
                    <a href="?status=approved" class="text-decoration-none">
                        <div class="card stat-card p-3 <?php echo $current_status == 'approved' ? 'border-2 border-info' : ''; ?>">
                            <div class="d-flex align-items-center">
                                <div class="stat-icon bg-info bg-opacity-10 text-info">
                                    <i class="fas fa-check-circle"></i>
                                </div>
                                <div class="ms-3">
                                    <h6 class="text-muted mb-1 small">Onaylanan</h6>
                                    <h3 class="mb-0 fw-bold text-info"><?php echo isset($stats['approved']) ? $stats['approved'] : 0; ?></h3>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
                <div class="col-md-3 col-sm-6">
                    <a href="?status=shipped" class="text-decoration-none">
                        <div class="card stat-card p-3 <?php echo $current_status == 'shipped' ? 'border-2 border-primary' : ''; ?>">
                            <div class="d-flex align-items-center">
                                <div class="stat-icon bg-primary bg-opacity-10 text-primary">
                                    <i class="fas fa-truck"></i>
                                </div>
                                <div class="ms-3">
                                    <h6 class="text-muted mb-1 small">Kargoda</h6>
                                    <h3 class="mb-0 fw-bold text-primary"><?php echo isset($stats['shipped']) ? $stats['shipped'] : 0; ?></h3>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
                <div class="col-md-3 col-sm-6">
                    <a href="?status=delivered" class="text-decoration-none">
                        <div class="card stat-card p-3 <?php echo $current_status == 'delivered' ? 'border-2 border-success' : ''; ?>">
                            <div class="d-flex align-items-center">
                                <div class="stat-icon bg-success bg-opacity-10 text-success">
                                    <i class="fas fa-home"></i>
                                </div>
                                <div class="ms-3">
                                    <h6 class="text-muted mb-1 small">Teslim Edilen</h6>
                                    <h3 class="mb-0 fw-bold text-success"><?php echo isset($stats['delivered']) ? $stats['delivered'] : 0; ?></h3>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
            </div>
            
            <div class="card table-card mb-4">
                <div class="card-body p-4">
                    <div class="row g-3 mb-4">
                        <div class="col-md-6">
                            <div class="d-flex flex-wrap gap-2">
                                <a href="?status=all" class="btn filter-btn <?php echo $current_status == 'all' ? 'btn-dark' : 'btn-outline-secondary'; ?>">
                                    <i class="fas fa-layer-group me-1"></i> Tümü (<?php echo $all_count; ?>)
                                </a>
                                <a href="?status=pending" class="btn filter-btn <?php echo $current_status == 'pending' ? 'btn-warning text-white' : 'btn-outline-warning'; ?>">
                                    <i class="fas fa-clock me-1"></i> Onay Bekleyen (<?php echo isset($stats['pending']) ? $stats['pending'] : 0; ?>)
                                </a>
                                <a href="?status=approved" class="btn filter-btn <?php echo $current_status == 'approved' ? 'btn-info text-white' : 'btn-outline-info'; ?>">
                                    <i class="fas fa-check me-1"></i> Onaylanan (<?php echo isset($stats['approved']) ? $stats['approved'] : 0; ?>)
                                </a>
                                <a href="?status=shipped" class="btn filter-btn <?php echo $current_status == 'shipped' ? 'btn-primary' : 'btn-outline-primary'; ?>">
                                    <i class="fas fa-truck me-1"></i> Kargoda (<?php echo isset($stats['shipped']) ? $stats['shipped'] : 0; ?>)
                                </a>
                                <a href="?status=delivered" class="btn filter-btn <?php echo $current_status == 'delivered' ? 'btn-success' : 'btn-outline-success'; ?>">
                                    <i class="fas fa-check-double me-1"></i> Teslim Edilen (<?php echo isset($stats['delivered']) ? $stats['delivered'] : 0; ?>)
                                </a>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="d-flex gap-2 justify-content-md-end flex-wrap">
                                <div class="position-relative" style="width: 250px;">
                                    <form method="GET" class="d-flex">
                                        <input type="hidden" name="status" value="<?php echo $current_status; ?>">
                                        <input type="text" name="search" value="<?php echo $search; ?>" class="form-control" placeholder="Sipariş no / ad / telefon ara...">
                                        <button type="submit" class="btn btn-outline-secondary ms-2"><i class="fas fa-search"></i></button>
                                    </form>
                                </div>
                                <div class="btn-group">
                                    <button type="button" class="btn btn-success dropdown-toggle" data-bs-toggle="dropdown">
                                        <i class="fas fa-file-excel me-1"></i> Excel İndir
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li><a class="dropdown-item" href="export.php?type=all&status=<?php echo $current_status; ?>&search=<?php echo urlencode($search); ?>"><i class="fas fa-download me-2"></i> Bu Listeyi İndir</a></li>
                                        <li><hr class="dropdown-divider"></li>
                                        <li><a class="dropdown-item" href="export.php?type=all"><i class="fas fa-download me-2"></i> Tüm Siparişler</a></li>
                                        <li><a class="dropdown-item" href="export.php?type=pending"><i class="fas fa-download me-2"></i> Onay Bekleyenler</a></li>
                                        <li><a class="dropdown-item" href="export.php?type=approved"><i class="fas fa-download me-2"></i> Onaylananlar</a></li>
                                        <li><a class="dropdown-item" href="export.php?type=shipped"><i class="fas fa-download me-2"></i> Kargodakiler</a></li>
                                        <li><a class="dropdown-item" href="export.php?type=delivered"><i class="fas fa-download me-2"></i> Teslim Edilenler</a></li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <?php if (empty($orders)): ?>
                        <div class="text-center py-5">
                            <i class="fas fa-inbox fa-4x text-muted mb-3"></i>
                            <h5 class="text-muted">Henüz sipariş bulunmuyor</h5>
                        </div>
                    <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-hover mb-0 align-middle">
                            <thead>
                                <tr>
                                    <th>Sipariş No</th>
                                    <th>Tarih</th>
                                    <th>Müşteri</th>
                                    <th>Telefon</th>
                                    <th>Şehir</th>
                                    <th>Ürün</th>
                                    <th>Tutar</th>
                                    <th>Ödeme</th>
                                    <th>Durum</th>
                                    <th>İşlem</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($orders as $order): ?>
                                <tr>
                                    <td>
                                        <span class="fw-bold text-primary"><?php echo $order['order_no']; ?></span>
                                    </td>
                                    <td>
                                        <small class="text-muted"><?php echo date('d.m.Y', strtotime($order['created_at'])); ?><br><?php echo date('H:i', strtotime($order['created_at'])); ?></small>
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <div class="bg-primary bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-2" style="width:36px;height:36px;">
                                                <i class="fas fa-user text-primary small"></i>
                                            </div>
                                            <span class="fw-semibold"><?php echo $order['customer_name']; ?></span>
                                        </div>
                                    </td>
                                    <td>
                                        <a href="tel:<?php echo $order['customer_phone']; ?>" class="text-dark text-decoration-none">
                                            <i class="fas fa-phone text-success me-1 small"></i><?php echo $order['customer_phone']; ?>
                                        </a>
                                    </td>
                                    <td>
                                        <i class="fas fa-map-marker-alt text-danger me-1 small"></i><?php echo $order['customer_city']; ?>
                                    </td>
                                    <td>
                                        <small class="text-muted"><?php echo $order['product_name']; ?></small>
                                    </td>
                                    <td>
                                        <span class="fw-bold text-success"><?php echo number_format($order['product_price'], 2, ',', '.'); ?> ₺</span>
                                    </td>
                                    <td>
                                        <?php echo getPaymentBadge($order['payment_type']); ?>
                                    </td>
                                    <td>
                                        <?php echo getStatusBadge($order['status']); ?>
                                    </td>
                                    <td>
                                        <a href="order.php?id=<?php echo $order['id']; ?>" class="btn btn-action btn-primary" title="Görüntüle / Düzenle">
                                            <i class="fas fa-eye me-1"></i>Detay
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
