<?php
/**
 * Transfer Land - Transfer land ownership to another user
 * Includes transfer history and validation
 */
define('APP_RUNNING', true);
session_start();
require_once 'db_connect.php';
require_once 'includes/auth.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}

$user_id = $_SESSION['user_id'];
$land_id = intval($_GET['id'] ?? 0);

// Verify land ownership
$land = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM lands WHERE id = $land_id AND user_id = $user_id"));
if (!$land) {
    header('Location: lands.php?error=Ardhi haipatikani au huna ruhusa');
    exit;
}

// Ensure land_transfers table exists
$tableCheck = mysqli_query($conn, "SHOW TABLES LIKE 'land_transfers'");
if (mysqli_num_rows($tableCheck) == 0) {
    mysqli_query($conn, "
        CREATE TABLE `land_transfers` (
            `id` INT(11) NOT NULL AUTO_INCREMENT,
            `land_id` INT(11) NOT NULL,
            `from_user_id` INT(11) NOT NULL,
            `to_user_id` INT(11) DEFAULT NULL,
            `to_email` VARCHAR(255) NOT NULL,
            `transfer_token` VARCHAR(100) NOT NULL,
            `reason` TEXT,
            `transfer_type` ENUM('sale', 'gift', 'inheritance', 'surveyed', 'other') DEFAULT 'sale',
            `amount` DECIMAL(15,2) DEFAULT NULL,
            `currency` VARCHAR(10) DEFAULT 'TZS',
            `status` ENUM('pending', 'accepted', 'rejected', 'cancelled', 'completed') DEFAULT 'pending',
            `initiated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            `responded_at` TIMESTAMP NULL,
            `completed_at` TIMESTAMP NULL,
            `notes` TEXT,
            PRIMARY KEY (`id`),
            KEY `land_id` (`land_id`),
            KEY `from_user_id` (`from_user_id`),
            KEY `to_user_id` (`to_user_id`),
            KEY `transfer_token` (`transfer_token`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
    ");
} else {
    // Ensure 'surveyed' type exists in existing table
    mysqli_query($conn, "ALTER TABLE land_transfers MODIFY COLUMN transfer_type ENUM('sale', 'gift', 'inheritance', 'surveyed', 'other') DEFAULT 'sale'");
}

// Get current user details
$current_user = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM users WHERE id = $user_id"));

$success = '';
$error = '';

// Handle transfer initiation
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] == 'initiate_transfer') {
        $to_email = mysqli_real_escape_string($conn, trim($_POST['to_email']));
        $reason = mysqli_real_escape_string($conn, trim($_POST['reason']));
        $transfer_type = mysqli_real_escape_string($conn, $_POST['transfer_type']);
        $amount = floatval($_POST['amount'] ?? 0);
        $notes = mysqli_real_escape_string($conn, trim($_POST['notes'] ?? ''));

        // Validate email
        if (empty($to_email) || !filter_var($to_email, FILTER_VALIDATE_EMAIL)) {
            $error = 'Tafadhali weka barua pepe halali / Please enter a valid email';
        } elseif ($to_email == $current_user['email']) {
            $error = 'Huwezi kuhamisha ardhi kwako mwenyewe / You cannot transfer land to yourself';
        } elseif (empty($reason)) {
            $error = 'Tafadhali weka sababu ya uhamishaji / Please provide a reason for transfer';
        } else {
            // Check for pending transfers
            $pending = mysqli_query($conn, "SELECT id FROM land_transfers WHERE land_id = $land_id AND status = 'pending'");
            if (mysqli_num_rows($pending) > 0) {
                $error = 'Kuna uhamishaji unasubiri kwa ardhi hii / There is a pending transfer for this land';
            } else {
                // Generate transfer token
                $transfer_token = bin2hex(random_bytes(32));

                // Check if recipient is an existing user
                $recipient = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id FROM users WHERE email = '$to_email'"));
                $to_user_id = $recipient ? $recipient['id'] : 'NULL';

                // Create transfer record
                $sql = "INSERT INTO land_transfers
                        (land_id, from_user_id, to_user_id, to_email, transfer_token, reason, transfer_type, amount, notes)
                        VALUES ($land_id, $user_id, $to_user_id, '$to_email', '$transfer_token', '$reason', '$transfer_type', $amount, '$notes')";

                if (mysqli_query($conn, $sql)) {
                    $success = 'Ombi la uhamishaji limetumwa kwa ' . htmlspecialchars($to_email) . '. Watakapoikubali, umiliki utahamishwa.';
                } else {
                    $error = 'Imeshindwa kuanzisha uhamishaji. Jaribu tena.';
                }
            }
        }
    } elseif ($_POST['action'] == 'cancel_transfer') {
        $transfer_id = intval($_POST['transfer_id']);
        mysqli_query($conn, "UPDATE land_transfers SET status = 'cancelled' WHERE id = $transfer_id AND from_user_id = $user_id AND status = 'pending'");
        $success = 'Uhamishaji umefutwa / Transfer cancelled';
    }
}

// Get pending transfers for this land
$pending_transfers = [];
$pending_result = mysqli_query($conn, "
    SELECT lt.*, u.full_name as recipient_name, u.email as recipient_email_user
    FROM land_transfers lt
    LEFT JOIN users u ON lt.to_user_id = u.id
    WHERE lt.land_id = $land_id AND lt.status = 'pending'
    ORDER BY lt.initiated_at DESC
");
while ($row = mysqli_fetch_assoc($pending_result)) {
    $pending_transfers[] = $row;
}

// Get transfer history for this land
$transfer_history = [];
$history_result = mysqli_query($conn, "
    SELECT lt.*,
           fu.full_name as from_name, fu.email as from_email,
           tu.full_name as to_name
    FROM land_transfers lt
    LEFT JOIN users fu ON lt.from_user_id = fu.id
    LEFT JOIN users tu ON lt.to_user_id = tu.id
    WHERE lt.land_id = $land_id
    ORDER BY lt.initiated_at DESC
");
while ($row = mysqli_fetch_assoc($history_result)) {
    $transfer_history[] = $row;
}
?>
<!DOCTYPE html>
<html lang="sw">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hamisha Ardhi - Land Secure</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?php echo url('assets/font-awesome/css/all.min.css'); ?>">
    <link href="<?php echo url('assets/vendor/bootstrap/css/bootstrap.min.css'); ?>" rel="stylesheet">
    <style>
        * { font-family: 'Inter', sans-serif; }
        :root { --primary: #1a3a6e; --secondary: #2d5a3d; --danger: #dc2626; --warning: #f59e0b; }
        body { background: #f4f6f9; margin: 0; }

        .topbar {
            background: linear-gradient(135deg, #dc2626 0%, #991b1b 100%);
            color: white;
            padding: 20px 25px;
            border-radius: 10px;
            margin-bottom: 25px;
        }

        .topbar h5 { margin: 0; }
        .topbar p { margin: 5px 0 0 0; opacity: 0.9; font-size: 14px; }

        .card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            margin-bottom: 20px;
        }

        .card-header {
            background: white;
            border-bottom: 1px solid #eee;
            padding: 20px;
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .warning-box {
            background: linear-gradient(135deg, rgba(220, 38, 38, 0.1) 0%, rgba(153, 27, 27, 0.1) 100%);
            border: 1px solid rgba(220, 38, 38, 0.3);
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 20px;
        }

        .warning-box h5 {
            color: #dc2626;
            margin: 0 0 10px 0;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .warning-box p {
            margin: 0;
            color: #7f1d1d;
            font-size: 14px;
        }

        .warning-box ul {
            margin: 10px 0 0 0;
            padding-left: 20px;
            color: #7f1d1d;
            font-size: 14px;
        }

        .transfer-form {
            background: linear-gradient(135deg, rgba(26, 58, 110, 0.05) 0%, rgba(45, 90, 61, 0.05) 100%);
            border-radius: 12px;
            padding: 25px;
        }

        .form-group label {
            font-weight: 600;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .form-control {
            border-radius: 10px;
            border: 1px solid #e2e8f0;
            padding: 12px 16px;
        }

        .form-control:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(26, 58, 110, 0.1);
        }

        .btn-primary {
            background: linear-gradient(135deg, #dc2626 0%, #991b1b 100%);
            border: none;
            padding: 14px 28px;
            border-radius: 12px;
            font-weight: 600;
        }

        .btn-primary:hover {
            background: linear-gradient(135deg, #b91c1c 0%, #7f1d1d 100%);
        }

        .btn-secondary {
            background: #e2e8f0;
            color: #475569;
            border: none;
            padding: 14px 28px;
            border-radius: 12px;
        }

        .transfer-type-options {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 12px;
            margin-bottom: 20px;
        }

        .transfer-type-option {
            border: 2px solid #e2e8f0;
            border-radius: 12px;
            padding: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
        }

        .transfer-type-option:hover {
            border-color: var(--primary);
        }

        .transfer-type-option.selected {
            border-color: var(--primary);
            background: rgba(26, 58, 110, 0.05);
        }

        .transfer-type-option i {
            font-size: 24px;
            margin-bottom: 8px;
            display: block;
        }

        .transfer-type-option.sale i { color: #22c55e; }
        .transfer-type-option.gift i { color: #f59e0b; }
        .transfer-type-option.inheritance i { color: #6366f1; }
        .transfer-type-option.surveyed i { color: #0ea5e9; }
        .transfer-type-option.other i { color: #64748b; }

        .pending-transfer {
            background: linear-gradient(135deg, rgba(245, 158, 11, 0.1) 0%, rgba(217, 119, 6, 0.1) 100%);
            border: 1px solid rgba(245, 158, 11, 0.3);
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
        }

        .pending-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 12px;
        }

        .pending-header h5 {
            margin: 0;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .pending-status {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            background: #fef3c7;
            color: #d97706;
        }

        .pending-details {
            font-size: 14px;
            color: #475569;
        }

        .pending-details p {
            margin: 6px 0;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .history-item {
            border-left: 4px solid #e2e8f0;
            padding: 16px 20px;
            margin-bottom: 12px;
            background: white;
            border-radius: 0 12px 12px 0;
        }

        .history-item.completed { border-left-color: #22c55e; }
        .history-item.rejected { border-left-color: #dc2626; }
        .history-item.cancelled { border-left-color: #64748b; }
        .history-item.pending { border-left-color: #f59e0b; }

        .history-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
            flex-wrap: wrap;
            gap: 8px;
        }

        .history-type {
            font-weight: 600;
            color: #1e293b;
        }

        .history-status {
            padding: 4px 10px;
            border-radius: 6px;
            font-size: 11px;
            font-weight: 600;
        }

        .history-status.completed { background: #dcfce7; color: #16a34a; }
        .history-status.rejected { background: #fef2f2; color: #dc2626; }
        .history-status.cancelled { background: #f1f5f9; color: #64748b; }
        .history-status.pending { background: #fef3c7; color: #d97706; }

        .history-parties {
            font-size: 13px;
            color: #64748b;
            display: flex;
            align-items: center;
            gap: 8px;
            flex-wrap: wrap;
        }

        .history-arrow {
            color: #94a3b8;
        }

        .history-meta {
            font-size: 12px;
            color: #94a3b8;
            margin-top: 8px;
        }

        @media (max-width: 768px) {
            .transfer-type-options {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="app-main">
        <div class="topbar">
            <h5><i class="fas fa-exchange-alt mr-2"></i>Hamisha Ardhi / Transfer Land</h5>
            <p><?php echo htmlspecialchars($land['land_name']); ?> - <?php echo number_format($land['total_area'], 2) . ' ' . $land['area_unit']; ?></p>
        </div>

        <?php if($success): ?>
        <div class="alert alert-success alert-dismissible fade show">
            <i class="fas fa-check-circle mr-2"></i><?php echo $success; ?>
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
        <?php endif; ?>

        <?php if($error): ?>
        <div class="alert alert-danger alert-dismissible fade show">
            <i class="fas fa-exclamation-circle mr-2"></i><?php echo $error; ?>
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
        <?php endif; ?>

        <div class="row">
            <div class="col-lg-7">
                <!-- Warning -->
                <div class="warning-box">
                    <h5><i class="fas fa-exclamation-triangle"></i>Tahadhari / Warning</h5>
                    <p>Uhamishaji wa ardhi ni kitendo cha kudumu ambacho hakiwezi kutenduliwa.</p>
                    <ul>
                        <li>Mmiliki mpya atapata udhibiti kamili wa ardhi hii</li>
                        <li>Huwezi kupata ardhi hii tena baada ya uhamishaji kukamilika</li>
                        <li>Hakikisha umejiridhisha kabla ya kuendelea</li>
                    </ul>
                </div>

                <?php if(count($pending_transfers) > 0): ?>
                <!-- Pending Transfers -->
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-clock text-warning"></i>
                        Uhamishaji Unasubiri / Pending Transfer
                    </div>
                    <div class="card-body">
                        <?php foreach($pending_transfers as $transfer): ?>
                        <div class="pending-transfer">
                            <div class="pending-header">
                                <h5>
                                    <i class="fas fa-user"></i>
                                    <?php echo $transfer['recipient_name'] ?: htmlspecialchars($transfer['to_email']); ?>
                                </h5>
                                <span class="pending-status">Inasubiri Kukubaliwa</span>
                            </div>
                            <div class="pending-details">
                                <p><i class="fas fa-envelope"></i><?php echo htmlspecialchars($transfer['to_email']); ?></p>
                                <p><i class="fas fa-tag"></i>
                                    <?php
                                    $types = ['sale' => 'Mauzo', 'gift' => 'Zawadi', 'inheritance' => 'Urithi', 'surveyed' => 'Kupimwa', 'other' => 'Nyingine'];
                                    echo $types[$transfer['transfer_type']] ?? $transfer['transfer_type'];
                                    ?>
                                    <?php if($transfer['amount'] > 0): ?>
                                    - <?php echo number_format($transfer['amount']); ?> <?php echo $transfer['currency']; ?>
                                    <?php endif; ?>
                                </p>
                                <p><i class="fas fa-comment"></i><?php echo htmlspecialchars($transfer['reason']); ?></p>
                                <p><i class="fas fa-calendar"></i>Ilianzishwa: <?php echo date('M d, Y H:i', strtotime($transfer['initiated_at'])); ?></p>
                            </div>
                            <form method="POST" class="mt-3">
                                <input type="hidden" name="action" value="cancel_transfer">
                                <input type="hidden" name="transfer_id" value="<?php echo $transfer['id']; ?>">
                                <button type="submit" class="btn btn-outline-danger btn-sm" onclick="return confirm('Una uhakika unataka kufuta uhamishaji huu?')">
                                    <i class="fas fa-times mr-1"></i>Futa Uhamishaji
                                </button>
                            </form>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php else: ?>
                <!-- Transfer Form -->
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-paper-plane text-primary"></i>
                        Anzisha Uhamishaji / Initiate Transfer
                    </div>
                    <div class="card-body">
                        <div class="transfer-form">
                            <form method="POST" id="transferForm">
                                <input type="hidden" name="action" value="initiate_transfer">
                                <input type="hidden" name="transfer_type" id="transferType" value="sale">

                                <div class="form-group">
                                    <label>Aina ya Uhamishaji / Transfer Type</label>
                                    <div class="transfer-type-options">
                                        <div class="transfer-type-option surveyed" onclick="selectType('surveyed', this)">
                                            <i class="fas fa-drafting-compass"></i>
                                            <strong>Kupimwa / Surveyed</strong>
                                            <small class="d-block text-muted">Nilipima tu, mmiliki ni mwingine</small>
                                        </div>
                                        <div class="transfer-type-option sale" onclick="selectType('sale', this)">
                                            <i class="fas fa-hand-holding-usd"></i>
                                            <strong>Mauzo / Sale</strong>
                                            <small class="d-block text-muted">Kuuza ardhi</small>
                                        </div>
                                        <div class="transfer-type-option gift" onclick="selectType('gift', this)">
                                            <i class="fas fa-gift"></i>
                                            <strong>Zawadi / Gift</strong>
                                            <small class="d-block text-muted">Kutoa bure</small>
                                        </div>
                                        <div class="transfer-type-option inheritance" onclick="selectType('inheritance', this)">
                                            <i class="fas fa-scroll"></i>
                                            <strong>Urithi / Inheritance</strong>
                                            <small class="d-block text-muted">Kurithi</small>
                                        </div>
                                        <div class="transfer-type-option other" onclick="selectType('other', this)">
                                            <i class="fas fa-ellipsis-h"></i>
                                            <strong>Nyingine / Other</strong>
                                            <small class="d-block text-muted">Sababu nyingine</small>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label><i class="fas fa-envelope"></i>Barua Pepe ya Mpokeaji / Recipient Email <span class="text-danger">*</span></label>
                                    <input type="email" name="to_email" class="form-control" placeholder="Weka barua pepe ya mtu anayepokea ardhi" required>
                                    <small class="text-muted">Mtu huyu atapata arifa na lazima akubali uhamishaji</small>
                                </div>

                                <div class="form-group" id="amountGroup">
                                    <label><i class="fas fa-money-bill"></i>Kiasi cha Mauzo / Sale Amount (TZS)</label>
                                    <input type="number" name="amount" class="form-control" placeholder="0" min="0" step="1000">
                                    <small class="text-muted">Weka 0 kama ni zawadi au urithi</small>
                                </div>

                                <div class="form-group">
                                    <label><i class="fas fa-comment"></i>Sababu ya Uhamishaji / Reason <span class="text-danger">*</span></label>
                                    <textarea name="reason" class="form-control" rows="3" placeholder="Eleza kwa nini unahamisha ardhi hii..." required></textarea>
                                </div>

                                <div class="form-group">
                                    <label><i class="fas fa-sticky-note"></i>Maelezo ya Ziada (Si lazima)</label>
                                    <textarea name="notes" class="form-control" rows="2" placeholder="Maelezo mengine yoyote..."></textarea>
                                </div>

                                <div class="d-flex justify-content-between mt-4">
                                    <a href="<?php echo url('land/' . $land_id); ?>" class="btn btn-secondary">
                                        <i class="fas fa-arrow-left mr-1"></i>Rudi
                                    </a>
                                    <button type="submit" class="btn btn-primary" onclick="return confirm('Una uhakika unataka kuhamisha ardhi hii? Hatua hii haiwezi kutenduliwa.')">
                                        <i class="fas fa-exchange-alt mr-1"></i>Hamisha Ardhi
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>

            <div class="col-lg-5">
                <!-- Transfer History -->
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-history text-info"></i>
                        Historia ya Uhamishaji / Transfer History
                    </div>
                    <div class="card-body">
                        <?php if(empty($transfer_history)): ?>
                        <div class="text-center text-muted py-4">
                            <i class="fas fa-exchange-alt fa-3x mb-3" style="opacity: 0.3;"></i>
                            <p>Hakuna historia ya uhamishaji kwa ardhi hii.</p>
                        </div>
                        <?php else: ?>
                        <?php foreach($transfer_history as $history): ?>
                        <div class="history-item <?php echo $history['status']; ?>">
                            <div class="history-header">
                                <span class="history-type">
                                    <?php
                                    $type_icons = ['sale' => 'hand-holding-usd', 'gift' => 'gift', 'inheritance' => 'scroll', 'surveyed' => 'drafting-compass', 'other' => 'ellipsis-h'];
                                    $icon = $type_icons[$history['transfer_type']] ?? 'exchange-alt';
                                    ?>
                                    <i class="fas fa-<?php echo $icon; ?> mr-1"></i>
                                    <?php
                                    $types = ['sale' => 'Mauzo', 'gift' => 'Zawadi', 'inheritance' => 'Urithi', 'surveyed' => 'Kupimwa', 'other' => 'Nyingine'];
                                    echo $types[$history['transfer_type']] ?? $history['transfer_type'];
                                    ?>
                                </span>
                                <span class="history-status <?php echo $history['status']; ?>">
                                    <?php
                                    $statuses = ['pending' => 'Inasubiri', 'completed' => 'Imekamilika', 'rejected' => 'Imekataliwa', 'cancelled' => 'Imefutwa'];
                                    echo $statuses[$history['status']] ?? $history['status'];
                                    ?>
                                </span>
                            </div>
                            <div class="history-parties">
                                <span><i class="fas fa-user mr-1"></i><?php echo htmlspecialchars($history['from_name'] ?: $history['from_email']); ?></span>
                                <span class="history-arrow"><i class="fas fa-arrow-right"></i></span>
                                <span><i class="fas fa-user mr-1"></i><?php echo htmlspecialchars($history['to_name'] ?: $history['to_email']); ?></span>
                            </div>
                            <?php if($history['amount'] > 0): ?>
                            <div class="history-meta">
                                <i class="fas fa-money-bill mr-1"></i>
                                <?php echo number_format($history['amount']); ?> <?php echo $history['currency']; ?>
                            </div>
                            <?php endif; ?>
                            <div class="history-meta">
                                <i class="fas fa-calendar mr-1"></i>
                                <?php echo date('M d, Y H:i', strtotime($history['initiated_at'])); ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Land Summary -->
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-map-marked-alt text-success"></i>
                        Muhtasari wa Ardhi
                    </div>
                    <div class="card-body">
                        <table class="table table-sm">
                            <tr>
                                <td class="text-muted">Jina</td>
                                <td class="text-right font-weight-bold"><?php echo htmlspecialchars($land['land_name']); ?></td>
                            </tr>
                            <tr>
                                <td class="text-muted">Ukubwa</td>
                                <td class="text-right font-weight-bold"><?php echo number_format($land['total_area'], 2) . ' ' . $land['area_unit']; ?></td>
                            </tr>
                            <?php if($land['location_address']): ?>
                            <tr>
                                <td class="text-muted">Mahali</td>
                                <td class="text-right"><?php echo htmlspecialchars($land['location_address']); ?></td>
                            </tr>
                            <?php endif; ?>
                            <tr>
                                <td class="text-muted">Imetengenezwa</td>
                                <td class="text-right"><?php echo date('M d, Y', strtotime($land['created_at'])); ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <script src="<?php echo url('assets/vendor/jquery/jquery.min.js'); ?>"></script>
    <script src="<?php echo url('assets/vendor/bootstrap/js/bootstrap.bundle.min.js'); ?>"></script>
    <script>
        function selectType(type, element) {
            document.querySelectorAll('.transfer-type-option').forEach(el => el.classList.remove('selected'));
            element.classList.add('selected');
            document.getElementById('transferType').value = type;

            // Show/hide amount field based on type
            const amountGroup = document.getElementById('amountGroup');
            if (type === 'sale') {
                amountGroup.style.display = 'block';
            } else {
                amountGroup.style.display = 'none';
            }
        }
    </script>
</body>
</html>
