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

$mysqli = new mysqli($host, $user, $pass, $db);
if ($mysqli->connect_errno) {
    die("Failed to connect to MySQL: " . $mysqli->connect_error);
}

$merid      = isset($_POST['merid']) ? intval($_POST['merid']) : intval($_GET['id']);
$loginflag  = isset($_POST['loginflag']) ? $_POST['loginflag'] : $_GET['loginflag'];

$tick = 'checked';
$ok = '';
$mailok = '';
$pwd = '';
$conpass = '';
$lid = '';

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

    $tick = ($cb === 'ON') ? 'checked' : '';

    if (empty($pwd) || empty($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);

        $stmt = $mysqli->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!";
            $mailok = "Didn't send any mail now.";
        } else {
            $ok = "Error updating password.";
        }
        $stmt->close();

        // Send notification email if checkbox selected
        if ($cb === 'ON') {
            $result = $mysqli->query("SELECT admin_email FROM partners_admin LIMIT 1");
            $from = "admin@example.com";
            if ($row = $result->fetch_assoc()) {
                $from = $row['admin_email'];
            }

            $to = $mailid;
            $subject = "Password Change Notification Mail";
            $message = "Your password has been changed by the super admin.<br>";
            $message .= "Your new password is: <strong>" . htmlspecialchars($pwd) . "</strong>";

            $headers  = "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
            $headers .= "From: <$from>\r\n";

            if (mail($to, $subject, $message, $headers)) {
                $mailok = "Password change notification mail has been sent.<br>To: $to<br>From: $from";
            } else {
                $mailok = "Failed to send notification email.";
            }
        }
    }
}

// Get current email ID
$stmt = $mysqli->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;" bordercolor="#111111" class="tablebdr">
    <tr>
      <td colspan="3" class="tdhead"><p align="center">Change Password</p></td>
    </tr>
    <tr>
      <td colspan="3"><p align="center" class="textred"><?= htmlspecialchars($ok) ?></p></td>
    </tr>
    <tr>
      <td width="2%">&nbsp;</td>
      <td width="48%">Email ID</td>
      <td width="50%"><?= htmlspecialchars($lid) ?></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Password</td>
      <td><input name="t1" id="t1" type="password" value="<?= htmlspecialchars($pwd) ?>"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>Confirm Password</td>
      <td><input name="t2" id="t2" type="password" value="<?= htmlspecialchars($conpass) ?>"></td>
    </tr>
    <tr>
      <td colspan="3">
        <p align="center">
          <input type="checkbox" name="C1" value="ON" id="chk1" <?= $tick ?>> Send a Notification Mail To Merchant with new Password
        </p>
      </td>
    </tr>
    <tr>
      <td colspan="2" class="tdhead" align="center">
        <input type="submit" value="Change" name="B1">
      </td>
      <td class="tdhead" align="center">
        <input id="button1" type="button" value="Close" name="button1" onclick="return button1_onclick()">
      </td>
    </tr>
    <tr>
      <td colspan="3"><p align="center" class="textred"><?= nl2br(htmlspecialchars($mailok)) ?></p></td>
    </tr>
  </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>
