<?php
/**
 * modules/locations/edit_locations.php
 */

// 1. STRICT SECURITY KICK-OUT
$p = $GLOBALS['my_privs'] ?? ['can_edit' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) {
    $p['can_edit'] = 1;
}

if ($p['can_edit'] == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=locations&page=list_locations') . "&status=access_denied");
    exit;
}

$location_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($location_id == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=locations&page=list_locations'));
    exit;
}

// 2. FORM POST HANDLER
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $location_code = strtoupper($db->escape($_POST['location_code']));
    $location_name = $db->escape($_POST['location_name']);
    $status        = $db->escape($_POST['status']);
    $updated_by    = (int)$_SESSION['user_id'];

    // Prevent duplicate Location Codes, excluding the one we are editing
    $check_sql = "SELECT LOCATION_ID FROM TBL_LOCATIONS WHERE LOCATION_CODE = '$location_code' AND LOCATION_ID != $location_id";
    if (mysqli_num_rows($db->query($check_sql)) > 0) {
        header("Location: index.php?s=" . encrypt_url("mod=locations&page=edit_locations&id=$location_id") . "&status=duplicate");
        exit;
    }

    $sql = "UPDATE TBL_LOCATIONS 
            SET LOCATION_CODE = '$location_code', 
                LOCATION_NAME = '$location_name', 
                STATUS = '$status', 
                UPDATED_BY = $updated_by, 
                UPDATED_DATE = CURRENT_TIMESTAMP 
            WHERE LOCATION_ID = $location_id";
    
    if ($db->query($sql)) {
        header("Location: index.php?s=" . encrypt_url('mod=locations&page=list_locations') . "&status=updated");
        exit;
    }
}

// 3. FETCH EXISTING DATA
$sql = "SELECT * FROM TBL_LOCATIONS WHERE LOCATION_ID = $location_id";
$result = $db->query($sql);
$data = mysqli_fetch_assoc($result);

if (!$data) {
    header("Location: index.php?s=" . encrypt_url('mod=locations&page=list_locations'));
    exit;
}
?>

<div class="p-3">
    <section class="content-header px-0">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="font-weight-bold text-primary"><i class="fas fa-edit mr-2"></i>Edit Location</h1>
                </div>
                <div class="col-sm-6 text-right">
                    <a href="index.php?s=<?php echo encrypt_url('mod=locations&page=list_locations'); ?>" class="btn btn-default shadow-sm border">
                        <i class="fas fa-arrow-left mr-1"></i> Cancel Update
                    </a>
                </div>
            </div>
        </div>
    </section>

    <section class="content px-0">
        <div class="container-fluid">
            <form action="" method="POST">
                <div class="card card-outline card-primary shadow-sm" style="border-top: 4px solid #007bff;">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-3 form-group">
                                <label class="text-secondary">Location Code <span class="text-danger">*</span></label>
                                <input type="text" name="location_code" class="form-control" value="<?php echo htmlspecialchars($data['LOCATION_CODE']); ?>" required style="text-transform: uppercase;">
                            </div>
                            <div class="col-md-5 form-group">
                                <label class="text-secondary">Location Name <span class="text-danger">*</span></label>
                                <input type="text" name="location_name" class="form-control" value="<?php echo htmlspecialchars($data['LOCATION_NAME']); ?>" required>
                            </div>
                            <div class="col-md-4 form-group">
                                <label class="text-secondary">Status</label>
                                <select name="status" class="form-control">
                                    <option value="ACTIVE" <?php echo ($data['STATUS'] == 'ACTIVE') ? 'selected' : ''; ?>>ACTIVE</option>
                                    <option value="INACTIVE" <?php echo ($data['STATUS'] == 'INACTIVE') ? 'selected' : ''; ?>>INACTIVE</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer bg-white text-right p-3">
                        <button type="submit" class="btn btn-primary px-5 font-weight-bold shadow-sm">
                            <i class="fas fa-sync mr-2"></i> UPDATE LOCATION
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </section>
</div>