<?php
/**
 * Direct Land Create Page - Bypasses Router
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

// Include config
require_once 'config/config.php';

// Check login
if (!isLoggedIn()) {
    header('Location: ' . BASE_URL . '/index.php?error=' . urlencode('Please login'));
    exit;
}

// Check permission
if (!hasPermission('land.create')) {
    $_SESSION['error'] = 'You do not have permission';
    header('Location: ' . BASE_URL . '/dashboard.php');
    exit;
}

// Get data for dropdowns
$pdo = getDBConnection();
$states = $pdo->query("SELECT * FROM states ORDER BY state_name")->fetchAll();
$zones = $pdo->query("SELECT * FROM zones ORDER BY zone_name")->fetchAll();
$commands = $pdo->query("SELECT * FROM commands ORDER BY command_name")->fetchAll();

$title = 'Add Land Asset';
$active = 'land';

// Include layouts
require_once 'views/layouts/header.php';
require_once 'views/layouts/sidebar.php';
require_once 'views/layouts/alerts.php';
?>

<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header">
        <div class="header-content">
            <h1>
                <i class="fas fa-plus-circle"></i>
                Add New Land Asset (Direct)
            </h1>
            <p>Enter land asset details</p>
        </div>
        <div class="header-actions">
            <a href="<?php echo BASE_URL; ?>/land_index_direct.php" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> Back to List
            </a>
        </div>
    </div>

    <!-- Form -->
    <div class="form-section">
        <form method="POST" action="<?php echo BASE_URL; ?>/land_store_direct.php" enctype="multipart/form-data" id="landForm">
            <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>">
            
            <!-- Basic Information -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-info-circle"></i> Basic Information</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Ownership Type</label>
                        <select name="ownership_type" id="ownership_type" required class="form-control">
                            <option value="">Select Type</option>
                            <option value="FGN">FGN (NIS)</option>
                            <option value="State">State</option>
                            <option value="Private">Private</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Title Holder</label>
                        <input type="text" name="title_holder" required maxlength="255" 
                               class="form-control" placeholder="e.g., Nigeria Immigration Service">
                    </div>
                </div>
                
                <div class="form-group">
                    <label class="required">Address</label>
                    <textarea name="address" required rows="3" class="form-control"></textarea>
                </div>
            </div>
            
            <!-- Location Information -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-map-marker-alt"></i> Location Information</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">State</label>
                        <select name="state_id" id="state_id" required class="form-control">
                            <option value="">Select State</option>
                            <?php foreach ($states as $state): ?>
                            <option value="<?php echo $state['id']; ?>">
                                <?php echo htmlspecialchars($state['state_name']); ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">LGA</label>
                        <select name="lga_id" id="lga_id" required class="form-control">
                            <option value="">Select State First</option>
                        </select>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Zone</label>
                        <select name="zone_id" id="zone_id" required class="form-control">
                            <option value="">Select Zone</option>
                            <?php foreach ($zones as $zone): ?>
                            <option value="<?php echo $zone['id']; ?>">
                                <?php echo htmlspecialchars($zone['zone_name']); ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Command</label>
                        <select name="command_id" id="command_id" required class="form-control">
                            <option value="">Select Zone First</option>
                        </select>
                    </div>
                </div>
            </div>
            
            <!-- Land Details -->
            <div class="form-section-inner">
                <div class="section-title">
                    <h3><i class="fas fa-ruler-combined"></i> Land Details</h3>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label class="required">Size</label>
                        <input type="number" step="0.01" name="size" required class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label class="required">Size Unit</label>
                        <select name="size_unit" required class="form-control">
                            <option value="">Select Unit</option>
                            <option value="m²">Square Meter (m²)</option>
                            <option value="hectares">Hectares</option>
                            <option value="acres">Acres</option>
                        </select>
                    </div>
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label>Survey Plan No.</label>
                        <input type="text" name="survey_plan_no" class="form-control">
                    </div>
                    
                    <div class="form-group">
                        <label>C of O No.</label>
                        <input type="text" name="certificate_of_occupancy_no" class="form-control">
                    </div>
                </div>
            </div>
            
            <!-- Form Actions -->
            <div class="form-actions">
                <button type="submit" class="btn btn-success">
                    <i class="fas fa-save"></i> Save Land Asset
                </button>
                <a href="<?php echo BASE_URL; ?>/land_index_direct.php" class="btn btn-secondary">
                    <i class="fas fa-times"></i> Cancel
                </a>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // State to LGA dropdown
    const stateSelect = document.getElementById('state_id');
    const lgaSelect = document.getElementById('lga_id');
    
    stateSelect.addEventListener('change', function() {
        const stateId = this.value;
        
        if (!stateId) {
            lgaSelect.innerHTML = '<option value="">Select State First</option>';
            return;
        }
        
        fetch('<?php echo BASE_URL; ?>/api/get_lgas.php?state_id=' + stateId)
            .then(response => response.json())
            .then(lgas => {
                lgaSelect.innerHTML = '<option value="">Select LGA</option>';
                lgas.forEach(lga => {
                    const option = document.createElement('option');
                    option.value = lga.id;
                    option.textContent = lga.lga_name;
                    lgaSelect.appendChild(option);
                });
            });
    });
    
    // Zone to Command dropdown
    const zoneSelect = document.getElementById('zone_id');
    const commandSelect = document.getElementById('command_id');
    
    zoneSelect.addEventListener('change', function() {
        const zoneId = this.value;
        
        if (!zoneId) {
            commandSelect.innerHTML = '<option value="">Select Zone First</option>';
            return;
        }
        
        fetch('<?php echo BASE_URL; ?>/api/get_commands.php?zone_id=' + zoneId)
            .then(response => response.json())
            .then(commands => {
                commandSelect.innerHTML = '<option value="">Select Command</option>';
                commands.forEach(cmd => {
                    const option = document.createElement('option');
                    option.value = cmd.id;
                    option.textContent = cmd.command_name;
                    commandSelect.appendChild(option);
                });
            });
    });
});
</script>

<?php
require_once 'views/layouts/footer.php';
?>