<?php
/**
 * Debug Login Tool
 * URL: https://swabhimanmfi.com/debug_login.php
 */

include "./core/dbInfo.php";
include_once "./core/dbFunctions.php";

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Debug Login - Swabhiman MFI</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 30px 0;
        }
        .card {
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
        }
        .card-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 20px 20px 0 0 !important;
            padding: 20px;
        }
        .status-ok { color: #28a745; font-weight: bold; }
        .status-fail { color: #dc3545; font-weight: bold; }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header text-center">
                        <h4 class="mb-0">Debug User Login</h4>
                        <small>Check why user cannot login</small>
                    </div>
                    <div class="card-body p-4">
                        <form method="POST">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label"><strong>Email or Mobile</strong></label>
                                    <input type="text" name="email" class="form-control"
                                           placeholder="Enter email or mobile"
                                           value="<?= isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label"><strong>Password</strong></label>
                                    <input type="text" name="password" class="form-control"
                                           placeholder="Enter password"
                                           value="<?= isset($_POST['password']) ? htmlspecialchars($_POST['password']) : ''; ?>" required>
                                </div>
                            </div>
                            <button type="submit" name="debug" class="btn btn-primary w-100">Debug Login</button>
                        </form>

                        <?php if(isset($_POST['debug'])): ?>
                            <?php
                            $email = $_POST['email'];
                            $password = $_POST['password'];
                            $encrypted_password = abs(crc32(md5($password)));
                            ?>

                            <hr class="my-4">

                            <h5>Step 1: Password Encryption</h5>
                            <table class="table table-bordered">
                                <tr>
                                    <td>Plain Password:</td>
                                    <td><code><?= htmlspecialchars($password); ?></code></td>
                                </tr>
                                <tr>
                                    <td>Encrypted Password:</td>
                                    <td><code><?= $encrypted_password; ?></code></td>
                                </tr>
                            </table>

                            <h5>Step 2: Find User in Database</h5>
                            <?php
                            $sql_find = "SELECT user_id, user_name, name, email, mobile, password, user_type, is_active, kyc_id, kyc_date FROM users WHERE email = '$email' OR mobile = '$email'";
                            $res_find = getXbyY($sql_find);
                            ?>

                            <?php if(count($res_find) > 0): ?>
                                <?php $user = $res_find[0]; ?>
                                <table class="table table-bordered">
                                    <tr>
                                        <td>User ID:</td>
                                        <td><?= $user['user_id']; ?></td>
                                        <td class="status-ok">FOUND</td>
                                    </tr>
                                    <tr>
                                        <td>Name:</td>
                                        <td><?= $user['name']; ?></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>Email:</td>
                                        <td><?= $user['email']; ?></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>Mobile:</td>
                                        <td><?= $user['mobile']; ?></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>User Type:</td>
                                        <td><?= $user['user_type']; ?></td>
                                        <td><?= $user['user_type'] != 'Admin' ? '<span class="status-ok">OK (Not Admin)</span>' : '<span class="status-fail">ADMIN - Cannot login to user panel</span>'; ?></td>
                                    </tr>
                                    <tr>
                                        <td>Password in DB:</td>
                                        <td><code><?= $user['password']; ?></code></td>
                                        <td><?= $user['password'] == $encrypted_password ? '<span class="status-ok">MATCH</span>' : '<span class="status-fail">MISMATCH</span>'; ?></td>
                                    </tr>
                                    <tr>
                                        <td>Expected Password:</td>
                                        <td><code><?= $encrypted_password; ?></code></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>is_active:</td>
                                        <td><?= $user['is_active']; ?></td>
                                        <td><?= $user['is_active'] == 1 ? '<span class="status-ok">ACTIVE</span>' : '<span class="status-fail">INACTIVE - User blocked!</span>'; ?></td>
                                    </tr>
                                    <tr>
                                        <td>kyc_id:</td>
                                        <td><?= $user['kyc_id']; ?></td>
                                        <td><?= $user['kyc_id'] > 0 ? '<span class="status-ok">KYC Done</span>' : '<span class="status-fail">KYC Pending - Check kyc_date</span>'; ?></td>
                                    </tr>
                                    <tr>
                                        <td>kyc_date:</td>
                                        <td><?= $user['kyc_date']; ?></td>
                                        <td>
                                            <?php
                                            if($user['kyc_id'] == 0) {
                                                if(strtotime($user['kyc_date']) > time()) {
                                                    echo '<span class="status-ok">Not Expired Yet</span>';
                                                } else {
                                                    echo '<span class="status-fail">EXPIRED - KYC not done, account will block!</span>';
                                                }
                                            } else {
                                                echo '<span class="status-ok">KYC Done - Date not relevant</span>';
                                            }
                                            ?>
                                        </td>
                                    </tr>
                                </table>

                                <h5>Step 3: Admin Login Query Test</h5>
                                <?php
                                $sql_admin = "SELECT user_id FROM users WHERE (email = '$email' OR mobile = '$email') AND password = '$encrypted_password' AND user_type = 'Admin' AND is_active = 1";
                                $res_admin = getXbyY($sql_admin);
                                ?>
                                <div class="alert <?= count($res_admin) > 0 ? 'alert-success' : 'alert-danger'; ?>">
                                    <strong>ADMIN Query:</strong><br>
                                    <code style="font-size: 11px;"><?= $sql_admin; ?></code><br><br>
                                    <strong>Result:</strong> <?= count($res_admin); ?> row(s)<br>
                                    <?php if(count($res_admin) > 0): ?>
                                        <span class="status-ok">ADMIN LOGIN SHOULD WORK!</span>
                                    <?php else: ?>
                                        <span class="status-fail">ADMIN LOGIN WILL FAIL!</span>
                                    <?php endif; ?>
                                </div>

                                <h5>Step 4: User Login Query Test</h5>
                                <?php
                                $sql_login = "SELECT user_id, kyc_id, kyc_date FROM users WHERE user_type != 'Admin' AND (email = '$email' OR mobile = '$email') AND password = '$encrypted_password' AND is_active = 1";
                                $res_login = getXbyY($sql_login);
                                ?>
                                <div class="alert <?= count($res_login) > 0 ? 'alert-success' : 'alert-danger'; ?>">
                                    <strong>USER Query:</strong><br>
                                    <code style="font-size: 11px;"><?= $sql_login; ?></code><br><br>
                                    <strong>Result:</strong> <?= count($res_login); ?> row(s)<br>
                                    <?php if(count($res_login) > 0): ?>
                                        <span class="status-ok">USER LOGIN SHOULD WORK!</span>
                                    <?php else: ?>
                                        <span class="status-fail">USER LOGIN WILL FAIL!</span>
                                    <?php endif; ?>
                                </div>

                                <?php if($user['password'] != $encrypted_password): ?>
                                <h5>Fix: Update Password</h5>
                                <div class="alert alert-warning">
                                    <strong>Run this SQL to fix password:</strong><br>
                                    <code>UPDATE users SET password = '<?= $encrypted_password; ?>' WHERE user_id = <?= $user['user_id']; ?>;</code>
                                </div>
                                <?php endif; ?>

                                <?php if($user['is_active'] != 1): ?>
                                <h5>Fix: Activate User</h5>
                                <div class="alert alert-warning">
                                    <strong>Run this SQL to activate user:</strong><br>
                                    <code>UPDATE users SET is_active = 1 WHERE user_id = <?= $user['user_id']; ?>;</code>
                                </div>
                                <?php endif; ?>

                                <?php if($user['kyc_id'] == 0 && strtotime($user['kyc_date']) < time()): ?>
                                <h5>Fix: Extend KYC Date</h5>
                                <div class="alert alert-warning">
                                    <strong>Run this SQL to extend KYC date:</strong><br>
                                    <code>UPDATE users SET kyc_date = '2026-12-31' WHERE user_id = <?= $user['user_id']; ?>;</code>
                                    <br><br>
                                    <strong>OR set KYC as done:</strong><br>
                                    <code>UPDATE users SET kyc_id = 1 WHERE user_id = <?= $user['user_id']; ?>;</code>
                                </div>
                                <?php endif; ?>

                            <?php else: ?>
                                <div class="alert alert-danger">
                                    <span class="status-fail">USER NOT FOUND!</span><br>
                                    No user found with email/mobile: <?= htmlspecialchars($email); ?>
                                </div>
                            <?php endif; ?>

                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
