<?php

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/User.php';
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../includes/dashboard_helper.php';


$database = new Database();
$conn = $database->getConnection();


$auth = new Auth($conn);


$auth->requireLogin();


$userObj = new User($conn);

$message = '';
$messageType = '';


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $oldPassword = $_POST['old_password'] ?? '';
    $newPassword = $_POST['new_password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';


    if (empty($oldPassword) || empty($newPassword) || empty($confirmPassword)) {
        $message = 'All fields are required';
        $messageType = 'danger';
    } elseif ($newPassword !== $confirmPassword) {
        $message = 'New password and confirm password do not match';
        $messageType = 'danger';
    } else {

        $user = $userObj->getUserById($_SESSION['user_id']);
        if ($user && password_verify($oldPassword, $user['password'])) {

            if ($userObj->updateUser($user['user_id'], $user['username'], $newPassword, $user['role'], $user['email'], $user['branch_id'], $user['terminal_id'] ?? null)) {
                $message = 'Password updated successfully';
                $messageType = 'success';
            } else {
                $message = 'Failed to update password';
                $messageType = 'danger';
            }
        } else {
            $message = 'Current password is incorrect';
            $messageType = 'danger';
        }
    }
}


$user = $userObj->getUserById($_SESSION['user_id']);
$username = $user['username'] ?? '';
$role = $user['role'] ?? '';


$branchId = $user['branch_id'] ?? null;
$branchName = '';
if ($branchId) {
    $stmt = $conn->prepare("SELECT branch_name FROM branches WHERE branch_id = ?");
    $stmt->execute([$branchId]);
    $branchName = $stmt->fetchColumn() ?: 'Unknown';
}
?>
    <?php include_once __DIR__ . '/../../includes/header.php'; ?>
    <style>
        body {
            background-color: #f8f9fa;
            font-size: 14px;
            overflow-x: hidden;
        }

        .main-container {
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            padding: 10px 0;
        }

        .content-wrapper {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 15px 0;
        }

        .card {
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            border-radius: 8px;
            border: none;
            max-width: 450px;
            width: 100%;
        }

        .card-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-bottom: none;
            padding: 12px 20px;
            border-radius: 8px 8px 0 0;
        }

        .card-header h4 {
            margin: 0;
            font-size: 18px;
            font-weight: 600;
        }

        .card-body {
            padding: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-label {
            font-weight: 500;
            color: #495057;
            margin-bottom: 5px;
            font-size: 13px;
        }

        .form-control {
            border-radius: 6px;
            border: 1px solid #ced4da;
            padding: 8px 12px;
            font-size: 14px;
            transition: all 0.2s ease;
        }

        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }

        .btn {
            border-radius: 6px;
            font-weight: 500;
            padding: 8px 16px;
            font-size: 14px;
            transition: all 0.2s ease;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
        }

        .btn-primary:hover {
            background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
            transform: translateY(-1px);
        }

        .btn-secondary {
            background-color: #6c757d;
            border: none;
        }

        .btn-secondary:hover {
            background-color: #5a6268;
            transform: translateY(-1px);
        }

        .alert {
            border-radius: 6px;
            padding: 10px 15px;
            margin-bottom: 15px;
            font-size: 13px;
        }

        .d-grid {
            gap: 8px !important;
        }


        @media (max-height: 700px) {
            .main-container {
                padding: 5px 0;
            }
            .content-wrapper {
                padding: 5px 0;
            }
            .card-body {
                padding: 15px;
            }
            .form-group {
                margin-bottom: 12px;
            }
        }

        @media (max-width: 576px) {
            .card {
                margin: 0 15px;
            }
        }
    </style>

    <div class="main-container">
        <div class="content-wrapper">
            <div class="container-fluid">
                <div class="row justify-content-center">
                    <div class="col-12 col-sm-8 col-md-6 col-lg-5">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="mb-0">
                                    <i class="fas fa-key me-2"></i>Change Password
                                </h4>
                            </div>
                            <div class="card-body">
                                <?php if (!empty($message)): ?>
                                    <div class="alert alert-<?php echo $messageType; ?>" role="alert">
                                        <i class="fas fa-<?php echo $messageType === 'success' ? 'check-circle' : 'exclamation-triangle'; ?> me-2"></i>
                                        <?php echo htmlspecialchars($message); ?>
                                    </div>
                                <?php endif; ?>

                                <form method="POST" action="">
                                    <div class="form-group">
                                        <label for="old_password" class="form-label">
                                            <i class="fas fa-lock me-1"></i>Current Password
                                        </label>
                                        <input type="password" class="form-control" id="old_password" name="old_password" required>
                                    </div>

                                    <div class="form-group">
                                        <label for="new_password" class="form-label">
                                            <i class="fas fa-key me-1"></i>New Password
                                        </label>
                                        <input type="password" class="form-control" id="new_password" name="new_password" required>
                                    </div>

                                    <div class="form-group">
                                        <label for="confirm_password" class="form-label">
                                            <i class="fas fa-check-double me-1"></i>Confirm New Password
                                        </label>
                                        <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                                    </div>

                                    <div class="d-grid">
                                        <button type="submit" class="btn btn-primary">
                                            <i class="fas fa-save me-2"></i>Update Password
                                        </button>
                                        <a href="<?php echo getDashboardLink(); ?>" class="btn btn-secondary">
                                            <i class="fas fa-times me-2"></i>Cancel
                                        </a>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php include_once __DIR__ . '/../../includes/footer.php'; ?>
