<?php
/**
 * Admin Customer Details Page
 * Detailed view of a single customer
 */

$pageTitle = 'Detalii client';

require_once __DIR__ . '/includes/init.php';
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/../../includes/User.php';
require_once __DIR__ . '/../../includes/Order.php';

$db = Database::getInstance();
$user = new User();
$order = new Order();

$userId = intval($_GET['user_id'] ?? 0);
if (!$userId) {
    header('Location: /admin/clienti');
    exit;
}

// Get customer details
$customer = $user->getUserById($userId);
if (!$customer) {
    header('Location: /admin/clienti');
    exit;
}

// Get customer orders
$customerOrders = $order->getUserOrders($userId, 1, 10);

// Get customer addresses
$addresses = $user->getUserAddresses($userId);

// Get statistics
$totalOrders = $db->fetchOne("SELECT COUNT(*) as count FROM orders WHERE user_id = ?", [$userId])['count'] ?? 0;
$totalSpent = $db->fetchOne("SELECT SUM(total) as total FROM orders WHERE user_id = ? AND order_status != 'canceled'", [$userId])['total'] ?? 0;
?>

<div class="content">
  <div class="px-3 px-xxl-4 py-3 py-lg-4">
    <div class="card mb-3">
      <div class="card-header">
        <div class="row">
          <div class="col">
            <h5 class="mb-2">
              <?php echo htmlspecialchars($customer['first_name'] . ' ' . $customer['last_name']); ?>
              <small class="text-600">(<a href="mailto:<?php echo htmlspecialchars($customer['email']); ?>"><?php echo htmlspecialchars($customer['email']); ?></a>)</small>
            </h5>
          </div>
          <div class="col-auto">
            <h6 class="text-uppercase text-600">Client</h6>
          </div>
        </div>
      </div>
      <div class="card-body border-top">
        <div class="row g-3">
          <div class="col-md-3">
            <div class="border rounded p-3 text-center">
              <h6 class="text-600 mb-1">Comenzi</h6>
              <h3 class="mb-0"><?php echo number_format($totalOrders); ?></h3>
            </div>
          </div>
          <div class="col-md-3">
            <div class="border rounded p-3 text-center">
              <h6 class="text-600 mb-1">Total cheltuit</h6>
              <h3 class="mb-0"><?php echo number_format($totalSpent, 2); ?> RON</h3>
            </div>
          </div>
          <div class="col-md-3">
            <div class="border rounded p-3 text-center">
              <h6 class="text-600 mb-1">Puncte fidelitate</h6>
              <h3 class="mb-0"><?php echo number_format($customer['loyalty_points'] ?? 0); ?></h3>
            </div>
          </div>
          <div class="col-md-3">
            <div class="border rounded p-3 text-center">
              <h6 class="text-600 mb-1">Data înregistrare</h6>
              <h6 class="mb-0"><?php echo date('d.m.Y', strtotime($customer['created_at'])); ?></h6>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="card mb-3">
      <div class="card-header">
        <h5 class="mb-0">Informații cont</h5>
      </div>
      <div class="card-body bg-body-tertiary border-top">
        <div class="row">
          <div class="col-lg-6">
            <h6 class="fw-semi-bold ls mb-3 text-uppercase">Informații personale</h6>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">Nume complet</p>
              </div>
              <div class="col"><?php echo htmlspecialchars($customer['first_name'] . ' ' . $customer['last_name']); ?></div>
            </div>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">Email</p>
              </div>
              <div class="col"><a href="mailto:<?php echo htmlspecialchars($customer['email']); ?>"><?php echo htmlspecialchars($customer['email']); ?></a></div>
            </div>
            <?php if ($customer['phone']): ?>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">Telefon</p>
              </div>
              <div class="col"><a href="tel:<?php echo htmlspecialchars($customer['phone']); ?>"><?php echo htmlspecialchars($customer['phone']); ?></a></div>
            </div>
            <?php endif; ?>
            <?php if ($customer['cnp']): ?>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">CNP</p>
              </div>
              <div class="col"><?php echo htmlspecialchars($customer['cnp']); ?></div>
            </div>
            <?php endif; ?>
          </div>
          <div class="col-lg-6 mt-4 mt-lg-0">
            <h6 class="fw-semi-bold ls mb-3 text-uppercase">Informații companie</h6>
            <?php if ($customer['company_name']): ?>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">Nume companie</p>
              </div>
              <div class="col"><?php echo htmlspecialchars($customer['company_name']); ?></div>
            </div>
            <?php endif; ?>
            <?php if ($customer['cui']): ?>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">CUI</p>
              </div>
              <div class="col"><?php echo htmlspecialchars($customer['cui']); ?></div>
            </div>
            <?php endif; ?>
            <?php if ($customer['reg_com']): ?>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">Nr. Reg. Com.</p>
              </div>
              <div class="col"><?php echo htmlspecialchars($customer['reg_com']); ?></div>
            </div>
            <?php endif; ?>
            <?php if ($customer['bank_account']): ?>
            <div class="row mb-2">
              <div class="col-5 col-sm-4">
                <p class="fw-semi-bold mb-1">Cont bancar</p>
              </div>
              <div class="col"><?php echo htmlspecialchars($customer['bank_account']); ?></div>
            </div>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>

    <?php if (!empty($addresses)): ?>
    <div class="card mb-3">
      <div class="card-header">
        <h5 class="mb-0">Adrese</h5>
      </div>
      <div class="card-body border-top">
        <div class="row">
          <?php foreach ($addresses as $address): ?>
          <div class="col-md-6 mb-3">
            <div class="border rounded p-3">
              <h6 class="mb-2">
                <?php echo htmlspecialchars($address['first_name'] . ' ' . $address['last_name']); ?>
                <?php if ($address['is_default']): ?>
                <span class="badge badge-subtle-info">Implicită</span>
                <?php endif; ?>
              </h6>
              <p class="mb-1 fs-10">
                <?php echo htmlspecialchars($address['address_line1']); ?><br>
                <?php if ($address['address_line2']): ?>
                <?php echo htmlspecialchars($address['address_line2']); ?><br>
                <?php endif; ?>
                <?php echo htmlspecialchars($address['city'] . ', ' . $address['county'] . ' ' . $address['postcode']); ?><br>
                <?php echo htmlspecialchars($address['country']); ?>
              </p>
              <?php if ($address['phone']): ?>
              <p class="mb-0 fs-10"><strong>Telefon: </strong><?php echo htmlspecialchars($address['phone']); ?></p>
              <?php endif; ?>
            </div>
          </div>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
    <?php endif; ?>

    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Comenzi</h5>
      </div>
      <div class="card-body border-top p-0">
        <div class="table-responsive">
          <table class="table table-sm mb-0">
            <thead class="bg-body-tertiary">
              <tr>
                <th>Număr comandă</th>
                <th>Data</th>
                <th>Total</th>
                <th>Status</th>
                <th>Acțiuni</th>
              </tr>
            </thead>
            <tbody>
              <?php if (empty($customerOrders)): ?>
              <tr>
                <td colspan="5" class="text-center py-4">Nu există comenzi.</td>
              </tr>
              <?php else: ?>
              <?php foreach ($customerOrders as $ord): ?>
              <tr>
                <td><a href="/admin/comenzi/<?php echo $ord['id']; ?>"><?php echo htmlspecialchars($ord['order_number']); ?></a></td>
                <td><?php echo date('d.m.Y H:i', strtotime($ord['created_at'])); ?></td>
                <td><?php echo number_format($ord['total'], 2); ?> RON</td>
                <td>
                  <span class="badge badge-subtle-<?php 
                    echo $ord['order_status'] === 'delivered' ? 'success' : 
                        ($ord['order_status'] === 'pending' ? 'warning' : 
                        ($ord['order_status'] === 'canceled' ? 'danger' : 'primary')); 
                  ?>">
                    <?php 
                    $statusText = [
                      'pending' => 'În așteptare',
                      'processing' => 'În procesare',
                      'shipped' => 'Expediată',
                      'delivered' => 'Livrată',
                      'canceled' => 'Anulată'
                    ];
                    echo $statusText[$ord['order_status']] ?? $ord['order_status'];
                    ?>
                  </span>
                </td>
                <td>
                  <a class="btn btn-sm btn-falcon-default" href="/admin/comenzi/<?php echo $ord['id']; ?>">
                    <span class="fas fa-eye"></span>
                  </a>
                </td>
              </tr>
              <?php endforeach; ?>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>

