
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php $view='opps/bulk_upload.php'; ?>
<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
  <h4 class="mb-0">Bulk Upload Opportunities</h4>
  <div class="d-flex gap-2">
    <a class="btn btn-outline-secondary" href="<?= e(base_url($config, '/index.php?r=opps/bulk_template')) ?>">Download Template</a>
    <a class="btn btn-outline-primary" href="<?= e(base_url($config, '/index.php?r=opps/index')) ?>">Back to Opportunities</a>
  </div>
</div>

<div class="card shadow-sm">
  <div class="card-body">
    <form method="post" action="<?= e(base_url($config, '/index.php?r=opps/bulk_upload_store')) ?>" enctype="multipart/form-data">
      <?= csrf_field() ?>

      <div class="alert alert-info">
        Upload a CSV file exported from Excel. Required columns: <strong>mobile</strong>, <strong>full_name</strong>, and either <strong>branch_id</strong> in the file or a default branch selected below.
      </div>

      <div class="row g-3">
        <div class="col-12 col-md-6">
          <label class="form-label">CSV File</label>
          <input type="file" name="opportunities_file" class="form-control" accept=".csv" required>
          <div class="form-text">Open the template in Excel, fill rows, then Save As CSV UTF-8.</div>
        </div>

        <div class="col-12 col-md-6">
          <label class="form-label">Default Branch</label>
          <select name="default_branch_id" class="form-select">
            <option value="">Use branch_id from file</option>
            <?php foreach (($branches ?? []) as $b): ?>
              <option value="<?= (int)$b['id'] ?>"><?= e($b['name_en']) ?> - <?= e($b['city']) ?> (#<?= (int)$b['id'] ?>)</option>
            <?php endforeach; ?>
          </select>
        </div>

        <div class="col-12">
          <div class="form-check">
            <input class="form-check-input" type="checkbox" name="skip_duplicates" value="1" id="skip_duplicates" checked>
            <label class="form-check-label" for="skip_duplicates">Skip customers who already have an active opportunity</label>
          </div>
        </div>
      </div>

      <hr>
      <button class="btn btn-primary" type="submit">Upload Opportunities</button>
    </form>
  </div>
</div>

<div class="card shadow-sm mt-3">
  <div class="card-body">
    <h6>Template Columns</h6>
    <div class="table-responsive">
      <table class="table table-sm table-bordered">
        <thead><tr><th>Column</th><th>Required</th><th>Example</th></tr></thead>
        <tbody>
          <tr><td>mobile</td><td>Yes</td><td>500000000</td></tr>
          <tr><td>country_code</td><td>No</td><td>+966</td></tr>
          <tr><td>full_name</td><td>Yes</td><td>Customer Name</td></tr>
          <tr><td>branch_id</td><td>Yes if no default branch</td><td>1</td></tr>
          <tr><td>source, campaign, city, vehicle_of_interest, model_year, salary, salary_range, obligation, obligation_amount, pref_time, purchase_time, notes</td><td>No</td><td>Optional CRM details</td></tr>
        </tbody>
      </table>
    </div>
  </div>
</div>
