<?php 
include 'header.php';

if(isset($_GET['email'])){
    $email = $_GET['email'];
}else{
    $email = '';
}

$msg = "";
$err = "";
                  
if(isset($_POST['edit'])){
    // Only allow editing of non-KYC fields
    $first_name = $link->real_escape_string($_POST['first_name']);
    $last_name = $link->real_escape_string($_POST['last_name']);
    $email = $link->real_escape_string($_POST['email']);
    $phone_no = $link->real_escape_string($_POST['phone_no']);
    $street_address = $link->real_escape_string($_POST['street_address']);
    $state = $link->real_escape_string($_POST['state']);
    $country = $link->real_escape_string($_POST['country']);
    $gender = $link->real_escape_string($_POST['gender']);
    $dob = $link->real_escape_string($_POST['dob']);

      // Handle profile picture upload only
    $upload_dir = "../uploads/profile/";
    $profile_picture = $row['profile_picture'] ?? '';
    
    if(isset($_FILES['profile_picture']) && $_FILES['profile_picture']['error'] == 0) {
        $profile_picture_name = basename($_FILES['profile_picture']['name']);
        $profile_picture_path = $upload_dir . uniqid() . '_' . $profile_picture_name;
        if(move_uploaded_file($_FILES['profile_picture']['tmp_name'], $profile_picture_path)) {
            $profile_picture = $profile_picture_path;
        }
    }
  
    
    $sql = "UPDATE users SET 
            first_name = '$first_name',
            last_name = '$last_name',
            email = '$email',
            phone_no = '$phone_no',
            street_address = '$street_address',
            state = '$state',
            country = '$country',
            gender = '$gender',
            dob = '$dob',
          
            profile_picture = '$profile_picture',
           
            updated_at = NOW()
            WHERE email = '$email'";
      
    if (mysqli_query($link, $sql)) {
        $msg = "Account Details Updated Successfully!";
    } else {
        $err = mysqli_error($link);
    }
}

$sql = "SELECT * FROM users WHERE email = '$email'";
$result = mysqli_query($link,$sql);
if(mysqli_num_rows($result) > 0){
    $row = mysqli_fetch_assoc($result);
}
?>


    <style>
        .image-preview {
            max-width: 200px;
            max-height: 200px;
            margin: 10px 0;
            border: 1px solid #ddd;
            padding: 5px;
        }
        .tab-content {
            padding: 20px;
            background: #fff;
            border: 1px solid #dee2e6;
            border-top: none;
            border-radius: 0 0 5px 5px;
        }
        .nav-tabs-custom {
            border-bottom: 1px solid #dee2e6;
        }
        .form-section {
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        .form-section h5 {
            color: #495057;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #dee2e6;
        }
        .read-only-field {
            background-color: #f8f9fa;
            cursor: not-allowed;
        }
        .kyc-badge {
            font-size: 0.8rem;
            padding: 0.35em 0.65em;
        }
    </style>
<br> <br>
<div class="page-content">
    <div class="container-fluid">
        <?php 
        if ($msg != "") {
            echo customAlert("success", $msg);
            echo pageRedirect("2", "user-edit.php?email=".$email);
        }
        if ($err != "") {
            echo customAlert("error", $err);
        }
        ?>
        
        <div class="row">
            <div class="col-xxl-12">
                <div class="card mt-xxl-n5">
                    <div class="card-header">
                        <ul class="nav nav-tabs-custom rounded card-header-tabs border-bottom-0" role="tablist">
                            <li class="nav-item">
                                <a class="nav-link active" data-bs-toggle="tab" href="#personalDetails" role="tab">
                                    <i class="fas fa-user"></i> Personal Details
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" data-bs-toggle="tab" href="#kycDetails" role="tab">
                                    <i class="fas fa-id-card"></i> KYC Details
                                </a>
                            </li>
                            
                        </ul>
                    </div>
                    
                    <div class="card-body p-4">
                        <form action="user-edit.php?email=<?php echo $email ?>" method="post" enctype="multipart/form-data">
                            <div class="tab-content">
                                <!-- Personal Details Tab -->
                                <div class="tab-pane active" id="personalDetails" role="tabpanel">
                                    <div class="form-section">
                                        <h5>Basic Information</h5>
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">First Name</label>
                                                    <input type="text" class="form-control" name="first_name" value="<?php echo htmlspecialchars($row['first_name'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Last Name</label>
                                                    <input type="text" class="form-control" name="last_name" value="<?php echo htmlspecialchars($row['last_name'] ?? ''); ?>">
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Email</label>
                                                    <input type="email" class="form-control" name="email" value="<?php echo htmlspecialchars($row['email'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Phone Number</label>
                                                    <input type="text" class="form-control" name="phone_no" value="<?php echo htmlspecialchars($row['phone_no'] ?? ''); ?>">
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Gender</label>
                                                    <select class="form-select" name="gender">
                                                        <option value="male" <?php echo ($row['gender'] ?? '') == 'male' ? 'selected' : ''; ?>>Male</option>
                                                        <option value="female" <?php echo ($row['gender'] ?? '') == 'female' ? 'selected' : ''; ?>>Female</option>
                                                        <option value="neutral" <?php echo ($row['gender'] ?? '') == 'neutral' ? 'selected' : ''; ?>>Neutral</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Date of Birth</label>
                                                    <input type="date" class="form-control" name="dob" value="<?php echo htmlspecialchars($row['dob'] ?? ''); ?>">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="form-section">
                                        <h5>Address Information</h5>
                                        <div class="row">
                                            <div class="col-lg-12">
                                                <div class="mb-3">
                                                    <label class="form-label">Street Address</label>
                                                    <textarea class="form-control" name="street_address"><?php echo htmlspecialchars($row['street_address'] ?? ''); ?></textarea>
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">State</label>
                                                    <input type="text" class="form-control" name="state" value="<?php echo htmlspecialchars($row['state'] ?? ''); ?>">
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Country</label>
                                                    <input type="text" class="form-control" name="country" value="<?php echo htmlspecialchars($row['country'] ?? ''); ?>">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="form-section">
                                        <h5>Profile Picture</h5>
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Upload New Profile Picture</label>
                                                    <input type="file" class="form-control" name="profile_picture">
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <?php if(!empty($row['profile_picture'])): ?>
                                                    <img src="<?php echo htmlspecialchars($row['profile_picture']); ?>" class="image-preview">
                                                <?php else: ?>
                                                    <div class="text-muted">No profile picture uploaded</div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                
                                <!-- KYC Details Tab (Read-only) -->
                                <div class="tab-pane" id="kycDetails" role="tabpanel">
                                    <!--<div class="alert alert-info">-->
                                    <!--    <i class="fas fa-info-circle me-2"></i> KYC information cannot be edited directly. Contact support for any corrections.-->
                                    <!--</div>-->
                                    
                                    <div class="form-section">
                                        <h5>KYC Information</h5>
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Marital Status</label>
                                                    <input type="text" class="form-control read-only-field" value="<?php echo htmlspecialchars($row['marital_status'] ?? 'Not provided'); ?>" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">ID Type</label>
                                                    <input type="text" class="form-control read-only-field" value="<?php echo htmlspecialchars($row['id_type'] ?? 'Not provided'); ?>" readonly>
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Account ID</label>
                                                    <input type="text" class="form-control read-only-field" value="<?php echo htmlspecialchars($row['account_id'] ?? ''); ?>" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="mb-3">
                                                    <label class="form-label">Referred By</label>
                                                    <input type="text" class="form-control read-only-field" value="<?php echo htmlspecialchars($row['referred_by'] ?? 'Not referred'); ?>" readonly>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="form-section">
                                        <h5>ID Verification Documents</h5>
                                        <div class="row">
                                            <div class="col-lg-12 mb-3">
                                                <?php 
                                                $kyc_status = (!empty($row['id_front']) && !empty($row['id_back']) && !empty($row['selfie'])) 
                                                    ? '<span class="badge bg-success kyc-badge">Verified</span>' 
                                                    : '<span class="badge bg-warning text-dark kyc-badge">Pending</span>';
                                                echo $kyc_status;
                                                ?>
                                            </div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-lg-4">
                                                <div class="mb-3">
                                                    <label class="form-label">ID Front</label>
                                                    <?php if(!empty($row['id_front'])): ?>
                                                        <img src="<?php echo htmlspecialchars($row['id_front']); ?>" class="image-preview d-block">
                                                    <?php else: ?>
                                                        <div class="text-muted">Not uploaded</div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                            <div class="col-lg-4">
                                                <div class="mb-3">
                                                    <label class="form-label">ID Back</label>
                                                    <?php if(!empty($row['id_back'])): ?>
                                                        <img src="<?php echo htmlspecialchars($row['id_back']); ?>" class="image-preview d-block">
                                                    <?php else: ?>
                                                        <div class="text-muted">Not uploaded</div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                            <div class="col-lg-4">
                                                <div class="mb-3">
                                                    <label class="form-label">Selfie with ID</label>
                                                    <?php if(!empty($row['selfie'])): ?>
                                                        <img src="<?php echo htmlspecialchars($row['selfie']); ?>" class="image-preview d-block">
                                                    <?php else: ?>
                                                        <div class="text-muted">Not uploaded</div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                
                                <!-- Account Details Tab -->
                                <div class="tab-pane" id="accountDetails" role="tabpanel">
                                    <div class="form-section">
                                        <h5>Security</h5>
                                        
                                    </div>
                                    
                                    <!--<div class="form-section">-->
                                    <!--    <h5>Account Status</h5>-->
                                    <!--    <div class="row">-->
                                    <!--        <div class="col-lg-6">-->
                                    <!--            <div class="mb-3">-->
                                    <!--                <label class="form-label">Balance</label>-->
                                    <!--                <input type="text" class="form-control" name="balance" value="<?php echo htmlspecialchars($row['balance'] ?? '0.00'); ?>">-->
                                    <!--            </div>-->
                                    <!--        </div>-->
                                    <!--        <div class="col-lg-6">-->
                                    <!--            <div class="mb-3">-->
                                    <!--                <label class="form-label">Status</label>-->
                                    <!--                <select class="form-select" name="status">-->
                                    <!--                    <option value="active" <?php echo ($row['status'] ?? '') == 'active' ? 'selected' : ''; ?>>Active</option>-->
                                    <!--                    <option value="inactive" <?php echo ($row['status'] ?? '') == 'inactive' ? 'selected' : ''; ?>>Inactive</option>-->
                                                       
                                    <!--                </select>-->
                                    <!--            </div>-->
                                    <!--        </div>-->
                                    <!--    </div>-->
                                    <!--</div>-->
                                    
                                    <!--<div class="form-section">-->
                                    <!--    <h5>Timestamps</h5>-->
                                    <!--    <div class="row">-->
                                    <!--        <div class="col-lg-4">-->
                                    <!--            <div class="mb-3">-->
                                    <!--                <label class="form-label">Created At</label>-->
                                    <!--                <input type="text" class="form-control read-only-field" value="<?php echo htmlspecialchars($row['created_at'] ?? ''); ?>" readonly>-->
                                    <!--            </div>-->
                                    <!--        </div>-->
                                    <!--        <div class="col-lg-4">-->
                                    <!--            <div class="mb-3">-->
                                    <!--                <label class="form-label">Updated At</label>-->
                                    <!--                <input type="text" class="form-control read-only-field" value="<?php echo htmlspecialchars($row['updated_at'] ?? ''); ?>" readonly>-->
                                    <!--            </div>-->
                                    <!--        </div>-->
                                    <!--        <div class="col-lg-4">-->
                                    <!--            <div class="mb-3">-->
                                    <!--                <label class="form-label">Last Login</label>-->
                                    <!--                <input type="text" class="form-control read-only-field" value="<?php echo htmlspecialchars($row['last_login'] ?? 'Never'); ?>" readonly>-->
                                    <!--            </div>-->
                                    <!--        </div>-->
                                    <!--    </div>-->
                                    <!--</div>-->
                                <!--</div>-->
                            </div>
                            
                            <div class="row mt-4">
                                <div class="col-lg-12">
                                    <div class="hstack gap-2 justify-content-end">
                                        <button type="submit" name="edit" class="btn btn-primary">Save Changes</button>
                                        <a href="users.php" class="btn btn-light">Cancel</a>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

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