<?php
include('../includes/session.php');
include('../includes/config.php');
include('trick_buy_common.php');
include('../template/ahkweb/header.php');

header('Content-Type: text/html; charset=UTF-8');

$services = trick_get_services();
$serviceKey = $_POST['service_key'] ?? '';
$whatsappNo = trim($_POST['whatsapp_number'] ?? '');
$anydeskId  = trim($_POST['anydesk_id'] ?? '');
$customerNote = trim($_POST['customer_note'] ?? '');
$pageAlert = null;

// --------- Ensure trick service is enabled (admin on/off switch) ---------
$trickEnabled = 1;
if (isset($webdata['trick_service_enabled'])) $trickEnabled = (int)$webdata['trick_service_enabled'];
if ($trickEnabled !== 1) {
    $pageAlert = ['Service Disabled', 'Trick Buy service is temporarily turned off by admin. Please try later.', 'warning'];
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $trickEnabled === 1 && !$pageAlert) {
    try {
        if (!isset($services[$serviceKey])) throw new Exception('Invalid service selected.');
        $amount = trick_service_fee($serviceKey, $webdata);
        if ($amount <= 0) throw new Exception('Invalid service fee.');
        if (!preg_match('#^[0-9+\-\s]{7,20}$#', $whatsappNo)) throw new Exception('WhatsApp number invalid.');
        if (!preg_match('#^[A-Za-z0-9\-\s]{4,30}$#', $anydeskId))  throw new Exception('AnyDesk ID invalid.');
        if (!isset($udata)) throw new Exception('Login required.');

        // Wallet balance check
        $balance = 0.0;
        if (isset($udata['id'])) {
            $uid = (int)$udata['id'];
            $stmt = mysqli_prepare($ahk_conn, "SELECT balance FROM users WHERE id=? LIMIT 1");
            mysqli_stmt_bind_param($stmt, 'i', $uid);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $balance);
            mysqli_stmt_fetch($stmt);
            mysqli_stmt_close($stmt);
            $balance = (float)$balance;
        }
        if ($balance < $amount) {
            throw new Exception('Insufficient wallet balance. Required: ₹' . number_format($amount, 2) . ' | Your balance: ₹' . number_format($balance, 2) . '. Please top up wallet first.');
        }

        // Deduct wallet
        $deduct = $amount;
        $stmt = mysqli_prepare($ahk_conn, "UPDATE users SET balance = balance - ? WHERE id=? AND balance >= ? LIMIT 1");
        mysqli_stmt_bind_param($stmt, 'did', $deduct, $uid, $deduct);
        if (!mysqli_stmt_execute($stmt) || mysqli_stmt_affected_rows($stmt) < 1) {
            throw new Exception('Wallet deduct failed. Insufficient balance.');
        }
        mysqli_stmt_close($stmt);

        // Insert order
        $appNo = trickAppNumber();
        $userId  = isset($udata['id']) ? (int)$udata['id'] : null;
        $userPhone = $udata['phone'] ?? '';
        $userName  = $udata['name']  ?? '';
        $sLabel = $services[$serviceKey]['label'];

        $stmt = mysqli_prepare($ahk_conn,
            "INSERT INTO trick_orders
             (application_no, user_id, user_phone, user_name, service_key, service_label, whatsapp_number, anydesk_id, customer_note, amount, status)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending')");
        $status = 'pending';
        mysqli_stmt_bind_param($stmt, 'sisssssssd',
            $appNo, $userId, $userPhone, $userName, $serviceKey, $sLabel, $whatsappNo, $anydeskId, $customerNote, $amount);
        if (!mysqli_stmt_execute($stmt)) throw new Exception('Order failed. Try again.');
        $orderId = mysqli_insert_id($ahk_conn);
        mysqli_stmt_close($stmt);

        // Log wallet transaction (correct table columns: uid, amount, description, txn_ref, date, status)
        $desc = "TRICK BUY | {$sLabel} | App:{$appNo}";
        $stmt = mysqli_prepare($ahk_conn,
            "INSERT INTO transactions (uid, amount, description, txn_ref, date, status)
             VALUES (?, ?, ?, ?, NOW(), 'success')");
        $ref = $appNo;
        $deductSigned = -1 * abs((float)$deduct);
        if ($stmt) {
            mysqli_stmt_bind_param($stmt, 'idss', $uid, $deductSigned, $desc, $ref);
            @mysqli_stmt_execute($stmt);
            @mysqli_stmt_close($stmt);
        }

        header('Location: trick_buy_list.php?submitted=1');
        exit;
    } catch (Throwable $e) {
        $pageAlert = ['Order Failed', $e->getMessage(), 'error'];
    }
}

?>

  <div class="page-wrapper">
    <div class="page-content">
      <div class="row justify-content-center">
        <div class="col-12 col-md-8 col-lg-6">
          <div class="card border-0 shadow">
            <div class="card-body p-4 p-md-5">
              <h3 class="mb-3">TRICK Buy</h3>
              <p class="text-muted mb-4">Select a trick service → Enter details → Pay & Submit. Admin will deliver via AnyDesk / WhatsApp within 1-4 hours.</p>

              <?php if ($trickEnabled !== 1): ?>
                <div class="alert alert-warning"><i class="bi bi-slash-circle me-2"></i> Trick Buy service is currently <b>OFF</b> by admin. Please check later.</div>
              <?php endif; ?>

              <?php if ($pageAlert): ?>
                <div class="alert alert-danger"><i class="bi bi-exclamation-triangle me-2"></i> <b><?= htmlspecialchars($pageAlert[0]) ?>:</b> <?= htmlspecialchars($pageAlert[1]) ?></div>
              <?php endif; ?>

              <form method="POST" class="trick-form mb-4" novalidate>
                <div class="mb-3">
                  <label class="form-label fw-semibold">Select Service</label>
                  <select name="service_key" id="service_key" class="form-select form-select-lg" required onchange="updatePrice()">
                    <option value="">-- Choose service --</option>
                    <?php foreach ($services as $k => $s):
                        $fee = trick_service_fee($k, $webdata);
                        $selected = $serviceKey === $k ? 'selected' : '';
                    ?>
                      <option value="<?= $k ?>" data-fee="<?= $fee ?>" <?= $selected ?>>
                        <?= htmlspecialchars($s['label']) ?> (₹<?= number_format($fee) ?>)
                      </option>
                    <?php endforeach; ?>
                  </select>
                </div>
                <div class="mb-3">
                  <label class="form-label fw-semibold">Whatsapp Number</label>
                  <input type="tel" name="whatsapp_number" pattern="[0-9+\-\s]{7,20}" class="form-control form-control-lg"
                         placeholder="e.g. 9999999999" value="<?= htmlspecialchars($whatsappNo) ?>" required>
                  <div class="form-text">Admin contact you on this WhatsApp for updates.</div>
                </div>
                <div class="mb-3">
                  <label class="form-label fw-semibold">Anydesk ID</label>
                  <input type="text" name="anydesk_id" pattern="[A-Za-z0-9\-\s]{4,30}" class="form-control form-control-lg"
                         placeholder="e.g. 123 456 789" value="<?= htmlspecialchars($anydeskId) ?>" required>
                  <div class="form-text">AnyDesk ID of your PC so admin can work remotely.</div>
                </div>
                <div class="mb-3">
                  <label class="form-label fw-semibold">Additional info (optional)</label>
                  <textarea name="customer_note" rows="2" maxlength="500" class="form-control"
                            placeholder="e.g. Applicant name, Reference no, etc."><?= htmlspecialchars($customerNote) ?></textarea>
                </div>

                <button type="submit" class="btn btn-primary btn-lg w-100" <?= $trickEnabled !== 1 ? 'disabled' : '' ?>>
                  Pay &amp; Submit (<span id="price_display">₹0</span>)
                </button>
              </form>

              <a class="btn btn-outline-primary w-100" href="trick_buy_list.php">
                <i class="bi bi-list-ul me-1"></i> My Trick Orders
              </a>

            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

<script>
function updatePrice() {
    var sel = document.getElementById('service_key');
    var opt = sel.options[sel.selectedIndex];
    var fee = opt ? opt.getAttribute('data-fee') : 0;
    var pd = document.getElementById('price_display');
    if (pd) pd.textContent = '₹' + Number(fee).toLocaleString('en-IN');
}
document.addEventListener('DOMContentLoaded', updatePrice);
</script>

<?php include('footers.php');
if ($pageAlert) trickAlert($pageAlert[0], $pageAlert[1], $pageAlert[2]);
?>
