Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/membership/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/portal/modules/membership/edit.php

<?php
require_once '../../config/config.php';
checkLogin();

$pageTitle = "Edit Member - " . APP_NAME;
$db = Database::getInstance()->getConnection();
$error = '';
$success = '';

// Get member ID
if (!isset($_GET['id']) || empty($_GET['id'])) {
    redirect('index.php');
}

$memberId = (int)$_GET['id'];

// Get areas, districts, assemblies based on access
$accessLevel = getUserAccessLevel();
$userAreaId = getUserAreaId();
$userDistrictId = getUserDistrictId();
$userAssemblyId = getUserAssemblyId();

// Get areas
$areas = [];
try {
    $areasQuery = "SELECT * FROM areas WHERE is_active = 1";
    if ($accessLevel === 'area') {
        $areasQuery .= " AND id = $userAreaId";
    }
    $areas = $db->query($areasQuery)->fetchAll();
} catch (PDOException $e) {
    $error = "Database not set up properly. Please run the installer first.";
}

// Get member details
try {
    // Check if membershipcard_id column exists
    $columnExists = false;
    try {
        $checkStmt = $db->query("SHOW COLUMNS FROM members LIKE 'membershipcard_id'");
        $columnExists = $checkStmt->fetch() !== false;
    } catch (PDOException $e) {
        // Column doesn't exist
    }

    $memberIdField = $columnExists ? 'm.membershipcard_id' : 'CONCAT("MC", YEAR(CURDATE()), LPAD(m.id, 6, "0")) as membershipcard_id';
    
    $stmt = $db->prepare("
        SELECT m.*, {$memberIdField}
        FROM members m
        WHERE m.id = :id
    ");
    
    $stmt->execute(['id' => $memberId]);
    $member = $stmt->fetch();
    
    if (!$member) {
        redirect('index.php');
    }
    
} catch (PDOException $e) {
    $error = "Error loading member: " . $e->getMessage();
    redirect('index.php');
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        $areaId = $_POST['area_id'];
        $districtId = $_POST['district_id'];
        $assemblyId = $_POST['assembly_id'];
        
        // Handle profile photo upload
        $profilePhoto = $member['profile_photo']; // Keep existing photo by default
        if (isset($_FILES['profile_photo']) && $_FILES['profile_photo']['error'] === UPLOAD_ERR_OK) {
            $uploadDir = '../../uploads/members/';
            if (!is_dir($uploadDir)) {
                mkdir($uploadDir, 0755, true);
            }
            
            $fileExtension = strtolower(pathinfo($_FILES['profile_photo']['name'], PATHINFO_EXTENSION));
            $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
            
            if (in_array($fileExtension, $allowedExtensions)) {
                $fileName = 'member_' . $memberId . '_' . time() . '.' . $fileExtension;
                $targetPath = $uploadDir . $fileName;
                
                if (move_uploaded_file($_FILES['profile_photo']['tmp_name'], $targetPath)) {
                    // Delete old photo if it exists
                    if (!empty($member['profile_photo']) && file_exists($uploadDir . $member['profile_photo'])) {
                        unlink($uploadDir . $member['profile_photo']);
                    }
                    $profilePhoto = $fileName;
                } else {
                    $error = "Failed to upload profile photo.";
                }
            } else {
                $error = "Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.";
            }
        }
        
        // Check if membershipcard_id column exists for update
        $updateColumns = "
            area_id = :area_id, district_id = :district_id, assembly_id = :assembly_id,
            family_id = :family_id, title = :title, first_name = :first_name, 
            middle_name = :middle_name, last_name = :last_name, gender = :gender,
            date_of_birth = :date_of_birth, place_of_birth = :place_of_birth,
            phone = :phone, email = :email, member_type = :member_type,
            marital_status = :marital_status, address_line1 = :address_line1,
            gps_address = :gps_address, hometown = :hometown, street_name = :street_name,
            city = :city, parent_name = :parent_name, parent_relationship = :parent_relationship,
            holyghost_baptism = :holyghost_baptism, date_of_holyspirit_baptism = :date_of_holyspirit_baptism,
            water_baptism = :water_baptism, date_of_baptism = :date_of_baptism,
            date_of_conversion = :date_of_conversion, date_of_joining = :date_of_joining,
            place_of_baptism = :place_of_baptism, officiating_minister_baptism = :officiating_minister_baptism,
            officiating_ministers_district = :officiating_ministers_district,
            communicant = :communicant, occupation = :occupation,
            level_of_education = :level_of_education, dedicated = :dedicated,
            dedication_date = :dedication_date, name_of_officiating_minister = :name_of_officiating_minister,
            church_where_dedication_done = :church_where_dedication_done,
            profile_photo = :profile_photo, is_active = :is_active
        ";
        
        if ($columnExists) {
            $updateColumns .= ", membershipcard_id = :membershipcard_id";
        }
        
        $stmt = $db->prepare("UPDATE members SET {$updateColumns} WHERE id = :id");
        
        $executeParams = [
            'area_id' => $areaId,
            'district_id' => $districtId,
            'assembly_id' => $assemblyId,
            'family_id' => $_POST['family_id'] ?: null,
            'title' => $_POST['title'],
            'first_name' => $_POST['first_name'],
            'middle_name' => $_POST['middle_name'] ?: null,
            'last_name' => $_POST['last_name'],
            'gender' => $_POST['gender'],
            'date_of_birth' => $_POST['date_of_birth'] ?: null,
            'place_of_birth' => $_POST['place_of_birth'] ?: null,
            'phone' => $_POST['phone'] ?: null,
            'email' => $_POST['email'] ?: null,
            'member_type' => $_POST['member_type'],
            'marital_status' => $_POST['marital_status'] ?: null,
            'address_line1' => $_POST['address_line1'] ?: null,
            'gps_address' => $_POST['gps_address'] ?: null,
            'hometown' => $_POST['hometown'] ?: null,
            'street_name' => $_POST['street_name'] ?: null,
            'city' => $_POST['city'] ?: null,
            'parent_name' => $_POST['parent_name'] ?: null,
            'parent_relationship' => $_POST['parent_relationship'] ?: null,
            'holyghost_baptism' => isset($_POST['holyghost_baptism']) ? 1 : 0,
            'date_of_holyspirit_baptism' => $_POST['date_of_holyspirit_baptism'] ?: null,
            'water_baptism' => isset($_POST['water_baptism']) ? 1 : 0,
            'date_of_baptism' => $_POST['date_of_baptism'] ?: null,
            'date_of_conversion' => $_POST['date_of_conversion'] ?: null,
            'date_of_joining' => $_POST['date_of_joining'] ?: null,
            'place_of_baptism' => $_POST['place_of_baptism'] ?: null,
            'officiating_minister_baptism' => $_POST['officiating_minister_baptism'] ?: null,
            'officiating_ministers_district' => $_POST['officiating_ministers_district'] ?: null,
            'communicant' => isset($_POST['communicant']) ? 1 : 0,
            'occupation' => $_POST['occupation'] ?: null,
            'level_of_education' => $_POST['level_of_education'] ?: null,
            'dedicated' => isset($_POST['dedicated']) ? 1 : 0,
            'dedication_date' => $_POST['dedication_date'] ?: null,
            'name_of_officiating_minister' => $_POST['name_of_officiating_minister'] ?: null,
            'church_where_dedication_done' => $_POST['church_where_dedication_done'] ?: null,
            'profile_photo' => $profilePhoto,
            'is_active' => isset($_POST['is_active']) ? 1 : 0,
            'id' => $memberId
        ];
        
        // Add membershipcard_id if column exists
        if ($columnExists && isset($_POST['membershipcard_id'])) {
            $executeParams['membershipcard_id'] = $_POST['membershipcard_id'];
        }
        
        $stmt->execute($executeParams);
        
        // Log the action
        $auditLog = new AuditLog();
        $auditLog->log($_SESSION['user_id'], 'update', 'members', $memberId);
        
        $success = "Member updated successfully!";
        
        // Refresh member data
        $stmt = $db->prepare("SELECT m.*, {$memberIdField} FROM members m WHERE m.id = :id");
        $stmt->execute(['id' => $memberId]);
        $member = $stmt->fetch();
        
    } catch (PDOException $e) {
        $error = "Error updating member: " . $e->getMessage();
    }
}

include '../../includes/header.php';
?>

<?php include '../../includes/sidebar.php'; ?>

<!-- Main Content -->
<main class="flex-1 md:ml-64 mt-16">
<div class="container mx-auto px-4 py-8">
    <div class="max-w-5xl mx-auto">
        <!-- Header -->
        <div class="flex justify-between items-center mb-6">
            <div>
                <h1 class="text-3xl font-bold text-gray-800">
                    <i class="fas fa-user-edit mr-2 text-blue-500"></i>Edit Member
                </h1>
                <p class="text-gray-600 mt-2">Update member information and details</p>
            </div>
            <div class="flex space-x-3">
                <a href="view.php?id=<?php echo $member['id']; ?>" 
                   class="bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600 transition">
                    <i class="fas fa-eye mr-2"></i>View Member
                </a>
                <a href="index.php" 
                   class="bg-gray-500 text-white px-4 py-2 rounded-lg hover:bg-gray-600 transition">
                    <i class="fas fa-arrow-left mr-2"></i>Back to List
                </a>
            </div>
        </div>

        <!-- Success/Error Messages -->
        <?php if (!empty($success)): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-6 flex items-center">
                <i class="fas fa-check-circle mr-2"></i>
                <span><?php echo $success; ?></span>
            </div>
        <?php endif; ?>

        <?php if (!empty($error)): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-6 flex items-center">
                <i class="fas fa-exclamation-circle mr-2"></i>
                <span><?php echo $error; ?></span>
            </div>
        <?php endif; ?>

        <!-- Edit Form -->
        <form method="POST" action="" enctype="multipart/form-data" class="space-y-8">
            <!-- Basic Information -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-6 border-b pb-3">
                    <i class="fas fa-user mr-2 text-blue-500"></i>Basic Information
                </h2>
                
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <div>
                        <label for="title" class="block text-sm font-medium text-gray-700 mb-2">Title</label>
                        <select name="title" id="title" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">Select Title</option>
                            <option value="Mr." <?php echo ($member['title'] ?? '') === 'Mr.' ? 'selected' : ''; ?>>Mr.</option>
                            <option value="Mrs." <?php echo ($member['title'] ?? '') === 'Mrs.' ? 'selected' : ''; ?>>Mrs.</option>
                            <option value="Miss" <?php echo ($member['title'] ?? '') === 'Miss' ? 'selected' : ''; ?>>Miss</option>
                            <option value="Dr." <?php echo ($member['title'] ?? '') === 'Dr.' ? 'selected' : ''; ?>>Dr.</option>
                            <option value="Rev." <?php echo ($member['title'] ?? '') === 'Rev.' ? 'selected' : ''; ?>>Rev.</option>
                            <option value="Pastor" <?php echo ($member['title'] ?? '') === 'Pastor' ? 'selected' : ''; ?>>Pastor</option>
                            <option value="Elder" <?php echo ($member['title'] ?? '') === 'Elder' ? 'selected' : ''; ?>>Elder</option>
                            <option value="Deacon" <?php echo ($member['title'] ?? '') === 'Deacon' ? 'selected' : ''; ?>>Deacon</option>
                            <option value="Deaconess" <?php echo ($member['title'] ?? '') === 'Deaconess' ? 'selected' : ''; ?>>Deaconess</option>
                            <option value="Evangelist" <?php echo ($member['title'] ?? '') === 'Evangelist' ? 'selected' : ''; ?>>Evangelist</option>
                            <option value="Prophet" <?php echo ($member['title'] ?? '') === 'Prophet' ? 'selected' : ''; ?>>Prophet</option>
                            <option value="Apostle" <?php echo ($member['title'] ?? '') === 'Apostle' ? 'selected' : ''; ?>>Apostle</option>
                        </select>
                    </div>
                    
                    <div>
                        <label for="first_name" class="block text-sm font-medium text-gray-700 mb-2">First Name *</label>
                        <input type="text" name="first_name" id="first_name" required
                               value="<?php echo htmlspecialchars($member['first_name'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="middle_name" class="block text-sm font-medium text-gray-700 mb-2">Middle Name</label>
                        <input type="text" name="middle_name" id="middle_name"
                               value="<?php echo htmlspecialchars($member['middle_name'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="last_name" class="block text-sm font-medium text-gray-700 mb-2">Last Name *</label>
                        <input type="text" name="last_name" id="last_name" required
                               value="<?php echo htmlspecialchars($member['last_name'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="gender" class="block text-sm font-medium text-gray-700 mb-2">Gender *</label>
                        <select name="gender" id="gender" required class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">Select Gender</option>
                            <option value="Male" <?php echo ($member['gender'] ?? '') === 'Male' ? 'selected' : ''; ?>>Male</option>
                            <option value="Female" <?php echo ($member['gender'] ?? '') === 'Female' ? 'selected' : ''; ?>>Female</option>
                        </select>
                    </div>
                    
                    <div>
                        <label for="date_of_birth" class="block text-sm font-medium text-gray-700 mb-2">Date of Birth</label>
                        <input type="date" name="date_of_birth" id="date_of_birth"
                               value="<?php echo $member['date_of_birth'] ?? ''; ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-6">
                    <div>
                        <label for="place_of_birth" class="block text-sm font-medium text-gray-700 mb-2">Place of Birth</label>
                        <input type="text" name="place_of_birth" id="place_of_birth"
                               value="<?php echo htmlspecialchars($member['place_of_birth'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="marital_status" class="block text-sm font-medium text-gray-700 mb-2">Marital Status</label>
                        <select name="marital_status" id="marital_status" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">Select Status</option>
                            <option value="Single" <?php echo ($member['marital_status'] ?? '') === 'Single' ? 'selected' : ''; ?>>Single</option>
                            <option value="Married" <?php echo ($member['marital_status'] ?? '') === 'Married' ? 'selected' : ''; ?>>Married</option>
                            <option value="Divorced" <?php echo ($member['marital_status'] ?? '') === 'Divorced' ? 'selected' : ''; ?>>Divorced</option>
                            <option value="Widowed" <?php echo ($member['marital_status'] ?? '') === 'Widowed' ? 'selected' : ''; ?>>Widowed</option>
                        </select>
                    </div>
                    
                    <div>
                        <label for="occupation" class="block text-sm font-medium text-gray-700 mb-2">Occupation</label>
                        <input type="text" name="occupation" id="occupation"
                               value="<?php echo htmlspecialchars($member['occupation'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="phone" class="block text-sm font-medium text-gray-700 mb-2">Phone Number</label>
                        <input type="tel" name="phone" id="phone"
                               value="<?php echo htmlspecialchars($member['phone'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="email" class="block text-sm font-medium text-gray-700 mb-2">Email Address</label>
                        <input type="email" name="email" id="email"
                               value="<?php echo htmlspecialchars($member['email'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="level_of_education" class="block text-sm font-medium text-gray-700 mb-2">Level of Education</label>
                        <input type="text" name="level_of_education" id="level_of_education"
                               value="<?php echo htmlspecialchars($member['level_of_education'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="family_id" class="block text-sm font-medium text-gray-700 mb-2">Family ID</label>
                        <input type="text" name="family_id" id="family_id"
                               value="<?php echo htmlspecialchars($member['family_id'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <!-- Profile Photo -->
                <div class="mt-6">
                    <label for="profile_photo" class="block text-sm font-medium text-gray-700 mb-2">Profile Photo</label>
                    <div class="flex items-center space-x-4">
                        <?php if (!empty($member['profile_photo'])): ?>
                            <img src="<?php echo BASE_URL . 'uploads/members/' . $member['profile_photo']; ?>" 
                                 alt="Current Photo" 
                                 class="w-16 h-16 rounded-full object-cover border-2 border-gray-300">
                        <?php else: ?>
                            <div class="w-16 h-16 rounded-full bg-gray-200 flex items-center justify-center border-2 border-gray-300">
                                <i class="fas fa-user text-gray-400 text-xl"></i>
                            </div>
                        <?php endif; ?>
                        <div class="flex-1">
                            <input type="file" name="profile_photo" id="profile_photo" accept="image/*"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <p class="text-sm text-gray-500 mt-1">Upload JPG, JPEG, PNG, or GIF files only</p>
                        </div>
                    </div>
                </div>
                
                <!-- Parent Information (for children/youth) -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="parent_name" class="block text-sm font-medium text-gray-700 mb-2">Parent/Guardian Name</label>
                        <input type="text" name="parent_name" id="parent_name"
                               value="<?php echo htmlspecialchars($member['parent_name'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="parent_relationship" class="block text-sm font-medium text-gray-700 mb-2">Relationship</label>
                        <input type="text" name="parent_relationship" id="parent_relationship"
                               value="<?php echo htmlspecialchars($member['parent_relationship'] ?? ''); ?>"
                               placeholder="e.g., Father, Mother, Guardian"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
            </div>

            <!-- Church Information -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-6 border-b pb-3">
                    <i class="fas fa-church mr-2 text-blue-500"></i>Church Information
                </h2>
                
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <div>
                        <label for="area_id" class="block text-sm font-medium text-gray-700 mb-2">Area *</label>
                        <select name="area_id" id="area_id" required onchange="loadDistricts(this.value)"
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">Select Area</option>
                            <?php foreach ($areas as $area): ?>
                                <option value="<?php echo $area['id']; ?>" <?php echo $member['area_id'] == $area['id'] ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars($area['area_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div>
                        <label for="district_id" class="block text-sm font-medium text-gray-700 mb-2">District *</label>
                        <select name="district_id" id="district_id" required onchange="loadAssemblies(this.value)"
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">Select District</option>
                        </select>
                    </div>
                    
                    <div>
                        <label for="assembly_id" class="block text-sm font-medium text-gray-700 mb-2">Assembly *</label>
                        <select name="assembly_id" id="assembly_id" required
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">Select Assembly</option>
                        </select>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="member_type" class="block text-sm font-medium text-gray-700 mb-2">Member Type</label>
                        <select name="member_type" id="member_type" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="Full Member" <?php echo ($member['member_type'] ?? '') === 'Full Member' ? 'selected' : ''; ?>>Full Member</option>
                            <option value="Associate Member" <?php echo ($member['member_type'] ?? '') === 'Associate Member' ? 'selected' : ''; ?>>Associate Member</option>
                            <option value="Youth" <?php echo ($member['member_type'] ?? '') === 'Youth' ? 'selected' : ''; ?>>Youth</option>
                            <option value="Children" <?php echo ($member['member_type'] ?? '') === 'Children' ? 'selected' : ''; ?>>Children</option>
                        </select>
                    </div>
                    
                    <?php if ($columnExists): ?>
                    <div>
                        <label for="membershipcard_id" class="block text-sm font-medium text-gray-700 mb-2">Membership Card ID</label>
                        <input type="text" name="membershipcard_id" id="membershipcard_id"
                               value="<?php echo htmlspecialchars($member['membershipcard_id'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Address Information -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-6 border-b pb-3">
                    <i class="fas fa-map-marker-alt mr-2 text-blue-500"></i>Address Information
                </h2>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="address_line1" class="block text-sm font-medium text-gray-700 mb-2">Address</label>
                        <textarea name="address_line1" id="address_line1" rows="3"
                                  class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500"><?php echo htmlspecialchars($member['address_line1'] ?? ''); ?></textarea>
                    </div>
                    
                    <div>
                        <label for="gps_address" class="block text-sm font-medium text-gray-700 mb-2">GPS Address</label>
                        <input type="text" name="gps_address" id="gps_address"
                               value="<?php echo htmlspecialchars($member['gps_address'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-6">
                    <div>
                        <label for="street_name" class="block text-sm font-medium text-gray-700 mb-2">Street Name</label>
                        <input type="text" name="street_name" id="street_name"
                               value="<?php echo htmlspecialchars($member['street_name'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="city" class="block text-sm font-medium text-gray-700 mb-2">City</label>
                        <input type="text" name="city" id="city"
                               value="<?php echo htmlspecialchars($member['city'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="hometown" class="block text-sm font-medium text-gray-700 mb-2">Hometown</label>
                        <input type="text" name="hometown" id="hometown"
                               value="<?php echo htmlspecialchars($member['hometown'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
            </div>

            <!-- Spiritual Information -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-6 border-b pb-3">
                    <i class="fas fa-cross mr-2 text-blue-500"></i>Spiritual Information
                </h2>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div class="space-y-4">
                        <div class="flex items-center">
                            <input type="checkbox" name="water_baptism" id="water_baptism" value="1"
                                   <?php echo ($member['water_baptism'] ?? 0) ? 'checked' : ''; ?>
                                   class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                            <label for="water_baptism" class="ml-2 block text-sm text-gray-900">Water Baptism</label>
                        </div>
                        
                        <div>
                            <label for="date_of_baptism" class="block text-sm font-medium text-gray-700 mb-2">Date of Baptism</label>
                            <input type="date" name="date_of_baptism" id="date_of_baptism"
                                   value="<?php echo $member['date_of_baptism'] ?? ''; ?>"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                    </div>
                    
                    <div class="space-y-4">
                        <div class="flex items-center">
                            <input type="checkbox" name="holyghost_baptism" id="holyghost_baptism" value="1"
                                   <?php echo ($member['holyghost_baptism'] ?? 0) ? 'checked' : ''; ?>
                                   class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                            <label for="holyghost_baptism" class="ml-2 block text-sm text-gray-900">Holy Ghost Baptism</label>
                        </div>
                        
                        <div>
                            <label for="date_of_holyspirit_baptism" class="block text-sm font-medium text-gray-700 mb-2">Date of Holy Spirit Baptism</label>
                            <input type="date" name="date_of_holyspirit_baptism" id="date_of_holyspirit_baptism"
                                   value="<?php echo $member['date_of_holyspirit_baptism'] ?? ''; ?>"
                                   class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="date_of_conversion" class="block text-sm font-medium text-gray-700 mb-2">Date of Conversion</label>
                        <input type="date" name="date_of_conversion" id="date_of_conversion"
                               value="<?php echo $member['date_of_conversion'] ?? ''; ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="date_of_joining" class="block text-sm font-medium text-gray-700 mb-2">Date of Joining</label>
                        <input type="date" name="date_of_joining" id="date_of_joining"
                               value="<?php echo $member['date_of_joining'] ?? ''; ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="place_of_baptism" class="block text-sm font-medium text-gray-700 mb-2">Place of Baptism</label>
                        <input type="text" name="place_of_baptism" id="place_of_baptism"
                               value="<?php echo htmlspecialchars($member['place_of_baptism'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="officiating_minister_baptism" class="block text-sm font-medium text-gray-700 mb-2">Officiating Minister (Baptism)</label>
                        <input type="text" name="officiating_minister_baptism" id="officiating_minister_baptism"
                               value="<?php echo htmlspecialchars($member['officiating_minister_baptism'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="mt-6">
                    <label for="officiating_ministers_district" class="block text-sm font-medium text-gray-700 mb-2">Officiating Minister's District</label>
                    <input type="text" name="officiating_ministers_district" id="officiating_ministers_district"
                           value="<?php echo htmlspecialchars($member['officiating_ministers_district'] ?? ''); ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div class="flex items-center">
                        <input type="checkbox" name="communicant" id="communicant" value="1"
                               <?php echo ($member['communicant'] ?? 0) ? 'checked' : ''; ?>
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                        <label for="communicant" class="ml-2 block text-sm text-gray-900">Communicant</label>
                    </div>
                    
                    <div class="flex items-center">
                        <input type="checkbox" name="dedicated" id="dedicated" value="1"
                               <?php echo ($member['dedicated'] ?? 0) ? 'checked' : ''; ?>
                               class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                        <label for="dedicated" class="ml-2 block text-sm text-gray-900">Dedicated</label>
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
                    <div>
                        <label for="dedication_date" class="block text-sm font-medium text-gray-700 mb-2">Dedication Date</label>
                        <input type="date" name="dedication_date" id="dedication_date"
                               value="<?php echo $member['dedication_date'] ?? ''; ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                    
                    <div>
                        <label for="name_of_officiating_minister" class="block text-sm font-medium text-gray-700 mb-2">Officiating Minister (Dedication)</label>
                        <input type="text" name="name_of_officiating_minister" id="name_of_officiating_minister"
                               value="<?php echo htmlspecialchars($member['name_of_officiating_minister'] ?? ''); ?>"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>
                </div>
                
                <div class="mt-6">
                    <label for="church_where_dedication_done" class="block text-sm font-medium text-gray-700 mb-2">Church Where Dedication Was Done</label>
                    <input type="text" name="church_where_dedication_done" id="church_where_dedication_done"
                           value="<?php echo htmlspecialchars($member['church_where_dedication_done'] ?? ''); ?>"
                           class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
            </div>

            <!-- Status Information -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h2 class="text-xl font-semibold text-gray-800 mb-6 border-b pb-3">
                    <i class="fas fa-info-circle mr-2 text-blue-500"></i>Status Information
                </h2>
                
                <div class="flex items-center">
                    <input type="checkbox" name="is_active" id="is_active" value="1"
                           <?php echo ($member['is_active'] ?? 1) ? 'checked' : ''; ?>
                           class="h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                    <label for="is_active" class="ml-2 block text-sm text-gray-900">Active Member</label>
                </div>
            </div>

            <!-- Submit Button -->
            <div class="flex justify-end space-x-4">
                <a href="view.php?id=<?php echo $member['id']; ?>" 
                   class="px-6 py-3 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 transition">
                    Cancel
                </a>
                <button type="submit" 
                        class="px-6 py-3 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition">
                    <i class="fas fa-save mr-2"></i>Update Member
                </button>
            </div>
        </form>
    </div>
</div>
</main>

<script>
// Load districts based on selected area
function loadDistricts(areaId) {
    const districtSelect = document.getElementById('district_id');
    const assemblySelect = document.getElementById('assembly_id');
    
    // Clear existing options
    districtSelect.innerHTML = '<option value="">Select District</option>';
    assemblySelect.innerHTML = '<option value="">Select Assembly</option>';
    
    if (areaId) {
        fetch('../../api/get-districts.php?area_id=' + areaId)
            .then(response => response.json())
            .then(data => {
                data.forEach(district => {
                    const option = document.createElement('option');
                    option.value = district.id;
                    option.textContent = district.district_name;
                    <?php if (isset($member['district_id'])): ?>
                    if (district.id == <?php echo $member['district_id']; ?>) {
                        option.selected = true;
                    }
                    <?php endif; ?>
                    districtSelect.appendChild(option);
                });
                
                // Load assemblies for the selected district if it's pre-selected
                <?php if (isset($member['district_id'])): ?>
                loadAssemblies(<?php echo $member['district_id']; ?>);
                <?php endif; ?>
            });
    }
}

// Load assemblies based on selected district
function loadAssemblies(districtId) {
    const assemblySelect = document.getElementById('assembly_id');
    
    // Clear existing options
    assemblySelect.innerHTML = '<option value="">Select Assembly</option>';
    
    if (districtId) {
        fetch('../../api/get-assemblies.php?district_id=' + districtId)
            .then(response => response.json())
            .then(data => {
                data.forEach(assembly => {
                    const option = document.createElement('option');
                    option.value = assembly.id;
                    option.textContent = assembly.assembly_name;
                    <?php if (isset($member['assembly_id'])): ?>
                    if (assembly.id == <?php echo $member['assembly_id']; ?>) {
                        option.selected = true;
                    }
                    <?php endif; ?>
                    assemblySelect.appendChild(option);
                });
            });
    }
}

// Load initial data on page load
document.addEventListener('DOMContentLoaded', function() {
    <?php if (isset($member['area_id'])): ?>
    loadDistricts(<?php echo $member['area_id']; ?>);
    <?php endif; ?>
});
</script>

<?php include '../../includes/footer.php'; ?>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists