<?php
//Developer: core tech web designTechnology
//Whatsapp: +2349162671360
//Telegram: @jeditech
include('include/header.php');
$page_title = WEB_NAME . " | Add SM Service";

// Fetch active categories
try {
    $cat_stmt = $pdo->query("SELECT id, category_name FROM sm_categories WHERE status='1' ORDER BY category_name ASC");
    $categories = $cat_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $categories = [];
}
?>
<title>Add SM Service - <?php echo $site_data['web_name']; ?></title>

<div class="container-fluid" id="container-wrapper">
  <div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-gray-800">Add SM Service</h1>
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
      <li class="breadcrumb-item"><a href="show_sm_services.php">SM Services</a></li>
      <li class="breadcrumb-item active" aria-current="page">Add Service</li>
    </ol>
  </div>

  <div class="row">
    <div class="col-lg-9 mx-auto">
      <div class="card mb-4">
        <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
          <h6 class="m-0 font-weight-bold text-primary">
            <i class="fas fa-plus-square mr-2"></i>Add New Service
          </h6>
          <a href="show_sm_services.php" class="btn btn-sm btn-secondary">
            <i class="fas fa-arrow-left mr-1"></i> Back
          </a>
        </div>
        <div class="card-body">

          <div id="alertBox"></div>

          <div class="form-group">
            <label class="font-weight-bold">Category <span class="text-danger">*</span></label>
            <select class="form-control" id="category_id">
              <option value="">-- Select Category --</option>
              <?php foreach ($categories as $cat): ?>
              <option value="<?php echo $cat['id']; ?>"><?php echo htmlspecialchars($cat['category_name']); ?></option>
              <?php endforeach; ?>
            </select>
          </div>

          <div class="form-group">
            <label class="font-weight-bold">Service Name <span class="text-danger">*</span></label>
            <input type="text" class="form-control" id="service_name" placeholder="e.g., Premium Facebook Account">
          </div>

          <div class="form-group">
            <label class="font-weight-bold">Service Description <span class="text-danger">*</span></label>
            <textarea class="form-control" id="service_description" rows="4"
                      placeholder="Enter detailed description of the service..."></textarea>
          </div>

          <div class="row">
            <div class="col-md-6">
              <div class="form-group">
                <label class="font-weight-bold">Amount (USD) <span class="text-danger">*</span></label>
                <input type="number" step="0.01" class="form-control" id="amount" placeholder="0.00">
              </div>
            </div>
            <div class="col-md-6">
              <div class="form-group">
                <label class="font-weight-bold">Preview Link (Optional)</label>
                <input type="url" class="form-control" id="preview_link" placeholder="https://example.com/preview">
              </div>
            </div>
          </div>

          <div class="form-group">
            <label class="font-weight-bold">Login Credentials <span class="text-danger">*</span></label>
            <textarea class="form-control" id="login_credentials" rows="5"
                      placeholder="Email: example@email.com&#10;Password: your_password&#10;Additional Info: ..."></textarea>
            <small class="text-muted"><i class="fas fa-lock mr-1"></i>This information will be shown to buyer after purchase</small>
          </div>

          <button type="button" id="add_service_btn" class="btn btn-primary btn-block">
            <i class="fas fa-plus-circle mr-2"></i>Add Service
          </button>
        </div>
      </div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
document.getElementById('add_service_btn').addEventListener('click', function() {
  var cat   = document.getElementById('category_id').value;
  var name  = document.getElementById('service_name').value.trim();
  var desc  = document.getElementById('service_description').value.trim();
  var amt   = document.getElementById('amount').value;
  var cred  = document.getElementById('login_credentials').value.trim();

  if (!cat || !name || !desc || !amt || !cred) {
    Swal.fire('Validation', 'Please fill in all required fields.', 'warning'); return;
  }

  var btn = this;
  btn.disabled = true;
  btn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>Adding...';

  $.ajax({
    url: 'ajax/sm_handler.php',
    type: 'POST',
    data: {
      action: 'add_service',
      category_id:          cat,
      service_name:         name,
      service_description:  desc,
      amount:               amt,
      preview_link:         document.getElementById('preview_link').value.trim(),
      login_credentials:    cred
    },
    dataType: 'json',
    success: function(res) {
      btn.disabled = false;
      btn.innerHTML = '<i class="fas fa-plus-circle mr-2"></i>Add Service';
      if (res.status == '1') {
        document.getElementById('alertBox').innerHTML =
          '<div class="alert alert-success alert-dismissible fade show"><i class="fas fa-check-circle mr-2"></i>' +
          res.msg + '<button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>';
        ['category_id','service_name','service_description','amount','preview_link','login_credentials']
          .forEach(id => document.getElementById(id).value = '');
      } else {
        document.getElementById('alertBox').innerHTML =
          '<div class="alert alert-danger alert-dismissible fade show"><i class="fas fa-exclamation-circle mr-2"></i>' +
          res.msg + '<button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>';
      }
    },
    error: function() {
      btn.disabled = false;
      btn.innerHTML = '<i class="fas fa-plus-circle mr-2"></i>Add Service';
      Swal.fire('Error!', 'An error occurred. Please try again.', 'error');
    }
  });
});
</script>

<?php include('include/footer.php'); ?>