<?php
ob_start();
if (session_status() === PHP_SESSION_NONE) { session_start(); }
if (!isset($_SESSION['user_id'])) { header("Location: login.php"); exit(); }

require_once('../titancore/titanconfig.php');

// Fix Database Encoding
mysqli_set_charset($conn, "utf8mb4");

require_once('../titancore/titanheader.php');

$uid = $_SESSION['user_id'];
$u_name = $_SESSION['user_name'] ?? 'User';

$swal_msg = "";

// Pricing Fetch
$price_check = $conn->query("SELECT price FROM pricing WHERE service_name = 'ration_to_aadhar'");
$service_price = ($price_check && $price_check->num_rows > 0) ? $price_check->fetch_assoc()['price'] : 140.00;

// API Config Fetch
$api_settings = $conn->query("SELECT titanurl, titan_api_key FROM TitanPayment WHERE id = 1")->fetch_assoc();
$TitanApi_Url = $api_settings['titanurl'] ?? '';
$api_key = $api_settings['titan_api_key'] ?? '';

if (empty($TitanApi_Url) || empty($api_key)) {
    echo "<script>alert('System Error: API Configuration missing.'); window.location.href='titanhome.php';</script>";
    exit();
}

// Handle Form Logic
if (isset($_POST['find_ration_btn'])) {
    $input_ration = mysqli_real_escape_string($conn, trim($_POST['ration_number']));
    
    // Wallet Check
    $user_wallet = $conn->query("SELECT wallet FROM users WHERE id = '$uid'")->fetch_assoc()['wallet'];
    
    if (empty($input_ration)) {
        $swal_msg = "Swal.fire('Invalid Input!', 'Please enter the Ration Number.', 'error');";
    } elseif ($user_wallet < $service_price) {
        $swal_msg = "Swal.fire('Low Balance!', 'You need ₹$service_price in your wallet.', 'warning');";
    } else {
        // Build Endpoint for Instant Submit (Updated Endpoint)
        $endpoint = rtrim($TitanApi_Url, '/') . "/api/v1/Ration_To_Uid_Instant.php?api_key=" . urlencode($api_key) . "&ration_no=" . urlencode($input_ration);
        
        // cURL Request
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $endpoint);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $api_response = curl_exec($ch);
        curl_close($ch);
        
        if ($api_response) {
            $data = json_decode($api_response, true);
            
            // Check status and code based on new API format
            if (isset($data['status']) && strtolower($data['status']) === 'success' && isset($data['code']) && $data['code'] == 200) {
                
                // Deduct Wallet
                $conn->query("UPDATE users SET wallet = wallet - $service_price WHERE id = '$uid'");
                
                // Extract Data from response
                $txn_id = mysqli_real_escape_string($conn, $data['transaction']['transaction_id'] ?? 'N/A');
                $status = mysqli_real_escape_string($conn, $data['status']);
                $date_time = date('Y-m-d H:i:s');
                
                // Extract and safe-string the entire members JSON array
                $members_json = isset($data['members']) ? json_encode($data['members'], JSON_UNESCAPED_UNICODE) : '[]';
                $secure_member_data = mysqli_real_escape_string($conn, $members_json);
                
                // Save to History 
                $insert = "INSERT INTO ration_to_aadhar_find (user_id, ration_number, transaction_id, status, member_data, created_at) 
                           VALUES ('$uid', '$input_ration', '$txn_id', '$status', '$secure_member_data', '$date_time')";
                
                if ($conn->query($insert)) {
                    $swal_msg = "Swal.fire('Success!', 'Data fetched successfully!', 'success').then(() => { window.location.href='Ration_To_Uid_List.php'; });";
                } else {
                    $swal_msg = "Swal.fire('Database Error!', 'Fetched successfully but failed to update history log.', 'error');";
                }
                
            } else {
                $err = $data['message'] ?? 'Record not found or API issue.';
                $swal_msg = "Swal.fire('Failed!', '" . addslashes($err) . "', 'error');";
            }
        } else {
            $swal_msg = "Swal.fire('Server Error!', 'Could not connect to the Master API Server.', 'error');";
        }
    }
}
?>

<div class="row mb-3">
    <div class="col">
        <div class="card bg-100 shadow-none border">
            <div class="row gx-0 flex-between-center p-3">
                <div class="col-sm-auto d-flex align-items-center">
                    <div class="icon-item bg-success-subtle shadow-none me-3">
                        <span class="fas fa-search text-success fs-4"></span>
                    </div>
                    <div>
                        <h6 class="text-success fs-10 mb-0">Identity Search</h6>
                        <h4 class="text-primary fw-bold mb-0">Ration To <span class="text-success fw-medium">Aadhar Find</span></h4>
                    </div>
                </div>
                <div class="col-md-auto">
                    <a href="Ration_To_Uid_List.php" class="btn btn-sm btn-outline-primary shadow-none fw-bold"><i class="fas fa-list me-1"></i> View History</a>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row justify-content-center">
    <div class="col-lg-5">
        <div class="card h-100">
            <div class="card-header bg-light border-bottom">
                <h5 class="mb-0 text-primary fw-bold text-center"><i class="fas fa-bolt me-2 text-warning"></i>Aadhar Find Process</h5>
            </div>
            <div class="card-body bg-body-tertiary p-4">
                <div class="alert alert-success border-0 d-flex align-items-center mb-4">
                    <i class="fas fa-rupee-sign fs-3 me-3 text-success"></i>
                    <p class="mb-0 fs-11">Service charge: <strong>₹<?php echo number_format($service_price, 2); ?></strong> will be deducted instantly.</p>
                </div>
                
                <form method="POST">
                    <div class="mb-4">
                        <label class="form-label fw-bold text-900">Ration Number <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text bg-white"><i class="fas fa-hashtag"></i></span>
                            <input type="text" class="form-control form-control-lg shadow-none" name="ration_number" placeholder="Enter Ration Number" required>
                        </div>
                    </div>
                    
                    <button class="btn btn-success w-100 fw-bold shadow-none py-2" type="submit" name="find_ration_btn">
                        <i class="fas fa-search me-2"></i> Fetch Details Now
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    <?php echo $swal_msg; ?>
</script>

<?php 
require_once('../titancore/TitanFooter.php'); 
ob_end_flush();
?>