<?php
/**
 * Admin Import/Export Page
 * Product import/export functionality
 */

$pageTitle = 'Import/Export';

require_once __DIR__ . '/includes/init.php';
require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/../../includes/ImportExport.php';

$importExport = new ImportExport();
$message = '';
$error = '';

// Handle export
if (isset($_GET['action']) && $_GET['action'] === 'export') {
    $importExport->exportProducts();
    exit;
}

// Handle import
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'import') {
    if (isset($_FILES['csv_file']) && $_FILES['csv_file']['error'] === UPLOAD_ERR_OK) {
        $result = $importExport->importProducts($_FILES['csv_file']['tmp_name']);
        if ($result['success']) {
            $message = 'Import reușit! ' . $result['imported'] . ' produse importate, ' . $result['updated'] . ' produse actualizate.';
        } else {
            $error = 'Eroare la import: ' . $result['message'];
        }
    } else {
        $error = 'Eroare la încărcarea fișierului.';
    }
}
?>

<div class="content">
  <div class="px-3 px-xxl-4 py-3 py-lg-4">
    <div class="card mb-3">
      <div class="card-header">
        <h5 class="mb-0">Import/Export Produse</h5>
      </div>
      <div class="card-body">
        <?php if ($message): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
          <?php echo htmlspecialchars($message); ?>
          <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        <?php endif; ?>
        <?php if ($error): ?>
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
          <?php echo htmlspecialchars($error); ?>
          <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
        <?php endif; ?>

        <div class="row g-4">
          <div class="col-md-6">
            <div class="card border">
              <div class="card-body">
                <h6 class="mb-3">Export Produse</h6>
                <p class="fs-10 text-600 mb-3">Exportă toate produsele într-un fișier CSV pentru editare.</p>
                <a href="/admin/import-export?action=export" class="btn btn-primary">
                  <span class="fas fa-download me-1"></span>Exportă CSV
                </a>
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="card border">
              <div class="card-body">
                <h6 class="mb-3">Import Produse</h6>
                <p class="fs-10 text-600 mb-3">Importă produse din fișier CSV. Produsele existente vor fi actualizate pe baza SKU.</p>
                <form method="POST" enctype="multipart/form-data">
                  <input type="hidden" name="action" value="import">
                  <div class="mb-3">
                    <input type="file" class="form-control" name="csv_file" accept=".csv" required>
                  </div>
                  <button type="submit" class="btn btn-primary">
                    <span class="fas fa-upload me-1"></span>Importă CSV
                  </button>
                </form>
              </div>
            </div>
          </div>
        </div>

        <div class="mt-4">
          <h6 class="mb-3">Format CSV</h6>
          <p class="fs-10 text-600 mb-2">Fișierul CSV trebuie să conțină următoarele coloane:</p>
          <div class="table-responsive">
            <table class="table table-sm table-bordered">
              <thead>
                <tr>
                  <th>Coloană</th>
                  <th>Descriere</th>
                  <th>Obligatoriu</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td><code>sku</code></td>
                  <td>Codul unic al produsului (pentru actualizare)</td>
                  <td>Da</td>
                </tr>
                <tr>
                  <td><code>name</code></td>
                  <td>Numele produsului</td>
                  <td>Da</td>
                </tr>
                <tr>
                  <td><code>price</code></td>
                  <td>Prețul produsului</td>
                  <td>Da</td>
                </tr>
                <tr>
                  <td><code>sale_price</code></td>
                  <td>Prețul de vânzare (opțional)</td>
                  <td>Nu</td>
                </tr>
                <tr>
                  <td><code>stock_quantity</code></td>
                  <td>Cantitatea în stoc</td>
                  <td>Da</td>
                </tr>
                <tr>
                  <td><code>category_id</code></td>
                  <td>ID-ul categoriei</td>
                  <td>Nu</td>
                </tr>
                <tr>
                  <td><code>description</code></td>
                  <td>Descrierea produsului</td>
                  <td>Nu</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

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

