<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

/* ================================
   SESSION & CONFIG
================================ */
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/session.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/config.php';

/* ================================
   HEADER
================================ */
require_once $_SERVER['DOCUMENT_ROOT'] . '/template/tngweb/header.php';

echo '<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>';

/* ================================
   PRICE
================================ */
$charge = 299;                       // FIX PRICE
$balance = (float)$udata['balance']; // USER WALLET

/* ================================
   FORM SUBMIT
================================ */
if ($_SERVER['REQUEST_METHOD'] === "POST" && isset($_POST['submit'])) {

    $rcno   = trim($_POST['rcno']);
    $mobile = trim($_POST['mobile']);

    if ($rcno === '' || $mobile === '') {
        echo "<script>Swal.fire('Error','All fields are required','error');</script>";
        exit;
    }

    if (!preg_match('/^[0-9]{10}$/', $mobile)) {
        echo "<script>Swal.fire('Invalid Mobile','Enter valid 10 digit mobile number','error');</script>";
        exit;
    }

    // WALLET CHECK
    if ($balance < $charge) {
        echo "<script>Swal.fire('Low Balance','Please recharge wallet','error');</script>";
        exit;
    }

    // SAFE DATA
    $rcno   = mysqli_real_escape_string($tng_conn, $rcno);
    $mobile = mysqli_real_escape_string($tng_conn, $mobile);
    $phone  = $udata['phone'];

    /* ================================
       INSERT REQUEST
    ================================ */
    mysqli_query($tng_conn,"
        INSERT INTO rc_mobile_link (rc_number, new_mobile, status, date, phone, amount)
        VALUES ('$rcno','$mobile','Pending',NOW(),'$phone','$charge')
    ");

    /* ================================
       WALLET DEBIT
    ================================ */
    mysqli_query($tng_conn,"
        UPDATE users SET balance = balance - $charge WHERE phone='$phone'
    ");

    $new_balance = $balance - $charge;

    mysqli_query($tng_conn,"
        INSERT INTO wallethistory (userid, amount, balance, purpose, status, type)
        VALUES ('$phone','$charge','$new_balance','RC to Mobile Link','1','Debit')
    ");

    echo "<script>
        Swal.fire('Success','Request Submitted & ₹299 Debited','success')
        .then(()=>{ window.location='rcmobilelist.php'; });
    </script>";
    exit;
}
?>

<!-- ================================
     FORM DESIGN
================================ -->
<style>
.form-card {
    max-width: 450px;
    margin: 80px auto;
    border-radius: 12px;
}
.card-header-custom {
    background: #198754;
    color: white;
    padding: 15px;
    font-size: 18px;
    border-radius: 12px 12px 0 0;
}
</style>

<div class="container">
    <div class="card shadow-lg form-card">

        <div class="card-header-custom text-center">
            🔗 RC TO MOBILE NUMBER LINK
        </div>

        <div class="card-body p-4">

            <form method="POST" autocomplete="off">

                <div class="mb-3">
                    <label class="form-label fw-bold">RC Number</label>
                    <input type="text" name="rcno"
                           class="form-control form-control-lg"
                           placeholder="Enter RC Number" required>
                </div>

                <div class="mb-3">
                    <label class="form-label fw-bold">New Mobile Number</label>
                    <input type="text" name="mobile" maxlength="10"
                           class="form-control form-control-lg"
                           placeholder="Enter New Mobile Number" required>
                </div>

                <!-- PRICE SHOW -->
                <div class="alert alert-primary fs-5">
                    💰 <strong>Charge:</strong> ₹ <?= $charge ?>
                </div>

                <div class="text-end">
                    <button name="submit" class="btn btn-success btn-lg px-4">
                        Submit
                    </button>
                </div>

            </form>

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

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/template/tngweb/footer.php';
?>
