<?php
include '../includes/session.php';
include '../includes/constants.php';
include '../includes/functions.php';
include 'header.php';

$partners = new partners();
$partners->connection($host, $user, $pass, $db);

$merid = intval($_POST['merid'] ?? $_GET['merid'] ?? 0);
$loginflag = $_POST['loginflag'] ?? $_GET['loginflag'] ?? '';
$ok = $mailok = $pwd = $conpass = '';
$tick = 'checked';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['B1'])) {
    $mailid = trim($_POST['mailid'] ?? '');
    $pwd = trim($_POST['t1'] ?? '');
    $conpass = trim($_POST['t2'] ?? '');
    $sendMail = isset($_POST['C1']) && $_POST['C1'] === 'ON';

    if ($pwd === '' || $conpass === '') {
        $ok = "Please don't leave any field blank.";
    } elseif ($pwd !== $conpass) {
        $ok = "Password and Confirm Password should be the same.";
    } else {
        $hashedPassword = password_hash($pwd, PASSWORD_DEFAULT);

        // Update password in database
        $stmt = $partners->conn->prepare("UPDATE partners_login SET login_password = ? WHERE login_id = ? AND login_flag = ?");
        $stmt->bind_param("sis", $hashedPassword, $merid, $loginflag);
        if ($stmt->execute()) {
            $ok = "Your password has been changed!";
        } else {
            $ok = "Failed to update password.";
        }
        $stmt->close();

        if ($sendMail) {
            $result = $partners->conn->query("SELECT admin_email FROM partners_admin LIMIT 1");
            $from = ($row = $result->fetch_assoc()) ? $row['admin_email'] : 'admin@example.com';

            $message = "Your password has been changed by Super Admin.<br><br>";
            $message .= "If this was not authorized, please contact support immediately.";

            $subject = "Password Change Notification";
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
            $headers .= "From: <$from>\r\n";

            if (mail($mailid, $subject, $message, $headers)) {
                $mailok = "Notification mail sent to: " . htmlspecialchars($mailid);
            } else {
                $mailok = "Failed to send notification mail.";
            }
        } else {
            $mailok = "No notification mail was sent.";
        }
    }
}

// Fetch email ID
$lid = '';
$stmt = $partners->conn->prepare("SELECT login_email FROM partners_login WHERE login_id = ? AND login_flag = ?");
$stmt->bind_param("is", $merid, $loginflag);
$stmt->execute();
$stmt->bind_result($lid);
$stmt->fetch();
$stmt->close();
?>

<script>
function button1_onclick() {
    window.close();
}
</script>

<html>
<body>
<form method="post" action="change_mer_pass.php" id="f1">
    <table border="0" cellpadding="0" cellspacing="0" style="width: 385px; border-collapse: collapse;" class="tablebdr">
        <tr>
            <td colspan="3" class="tdhead" align="center">Change Password</td>
        </tr>
        <?php if ($ok): ?>
        <tr>
            <td colspan="3" align="center" class="textred"><?= htmlspecialchars($ok) ?></td>
        </tr>
        <?php endif; ?>
        <tr>
            <td>&nbsp;</td>
            <td>Email ID</td>
            <td><?= htmlspecialchars($lid) ?></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>New Password</td>
            <td><input type="password" name="t1" id="t1" required></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>Confirm Password</td>
            <td><input type="password" name="t2" id="t2" required></td>
        </tr>
        <tr>
            <td colspan="3" align="center">
                <input type="checkbox" name="C1" value="ON" id="chk1" <?= $tick ?>> Send a notification mail to merchant
            </td>
        </tr>
        <tr>
            <td colspan="2" class="tdhead" align="center">
                <input type="submit" name="B1" value="Change">
            </td>
            <td class="tdhead" align="center">
                <input type="button" value="Close" onclick="return button1_onclick()">
            </td>
        </tr>
        <?php if ($mailok): ?>
        <tr>
            <td colspan="3" align="center" class="textred"><?= htmlspecialchars($mailok) ?></td>
        </tr>
        <?php endif; ?>
    </table>

    <input type="hidden" name="merid" value="<?= htmlspecialchars($merid) ?>">
    <input type="hidden" name="loginflag" value="<?= htmlspecialchars($loginflag) ?>">
    <input type="hidden" name="mailid" value="<?= htmlspecialchars($lid) ?>">
</form>
</body>
</html>
