<?php 
// error_reporting(0);
session_start();
require 'function.php';
require 'config.php';
$weburl = linHome();

    
$smtp = mysqli_query($koneksi, "SELECT * FROM smtp" );

$ceksmtp = mysqli_fetch_assoc($smtp);

$host = $ceksmtp['host'];
$port = $ceksmtp['port'];
$username = $ceksmtp['username'];
$password = $ceksmtp['password'];
$encryp = $ceksmtp['encryption'];
$emailweb = $ceksmtp['emailweb'];
$nameweb = $ceksmtp['nameweb'];


$result = mysqli_query($koneksi, "SELECT * FROM users_id" );

$whatsapp = mysqli_fetch_assoc($result);

$pesan = $whatsapp['whatsapp'];

$user_role = $whatsapp['role'];

$apikeyonesender = $whatsapp['apikeyone'];
$apiurlonesender = $whatsapp['apiurlonesender'];
$apikeystarsender = $whatsapp['apikey'];

$notifwa = $whatsapp['starsender'];
$emailhost = $whatsapp['email'];

   

extract($_POST);

if(isset($reset_password)){

    $email = mysqli_real_escape_string($koneksi, $_POST['email']);
    
    $data = mysqli_query($koneksi," SELECT * FROM `users_id` WHERE email = '$email' ");
    $row=mysqli_fetch_array($data, MYSQLI_ASSOC);
    $cek = mysqli_num_rows($data);
    if($cek ==1){
    
        $email = $row['email'];
        $name = $row['username'];
        $phone = $row['phone'];
        $newpass = randomString(7);

  	    $hashpass = md5($newpass);
  	    mysqli_query($koneksi,"UPDATE `users_id` SET password = '$hashpass' WHERE email = '$email'");
        
        //*** ISI PESAN YANG AKAN TERKIRIM KE EMAIL USER ***//
    $isi ='Hai <b>[nama]</b>, <br> <br> Baru saja anda melakukan Reset password, Berikut detailnya: <br><br> Akses login [link] <br> Username: [nama] <br> Password baru: <b>[password]</b> <br><br> apabila anda tidak melakukan perubahan/reset password, segera hubungi admin. <br><br> Terima kasih. <br>';
        //*** END ISI PESAN WHATSAPP ***//
        
        $link = '<a href="'.$weburl.'">'.$weburl.'</a>';
        
        $body = file_get_contents($weburl.'/templates/email.html');
        if (file_exists($body)) {
            $body = $body;
        }else{
            $body = $isi;
        } 

        $body = str_replace('[isi]',$isi,$body);                    
        $body = str_replace('[nama]',$name,$body);
        $body = str_replace('[email]',$email,$body);
        $body = str_replace('[password]',$newpass,$body);
        $body = str_replace('[link]',$link,$body);
   
  //* ISI PESAN YANG AKAN TERKIRIM KE WAHTSAPP USER KETIKA RESET PASSWORD *//
        $isi2 ='Hai *[nama]*,
Baru saja anda melakukan Reset password, Berikut detailnya:

Akses login [link]/login
Username: [nama]
Password baru: *[password]*

apabila anda tidak melakukan perubahan/reset password, segera hubungi admin,

```Pesan ini dikirim secara otomatis anda tidak perlu membalasnya..```

Terima kasih. 
';
   //*** END ISI PESAN WHATSAPP ***//
    
    $link = linHome();
    $pesan = strip_tags($isi2); //semembers
    $pesan = str_replace('[nama]',$name,$pesan);
    $pesan = str_replace('[email]',$email,$pesan);
    $pesan = str_replace('[password]',$newpass,$pesan);
    $pesan = str_replace('[link]',$link,$pesan);
    
    if($notifwa == true){
      Starsender($apikeystarsender, $pesan, $phone);
    } else{
      OneSender($apikeyonesender, $apiurlonesender, $pesan, $phone);
    }
    
    $title = 'Reset Password';
    
    sendemail($host,$port,$username,$password,$encryp,$emailweb,$nameweb,$title,$email,$name,$body); 
    
      echo '
          <script language="JavaScript">document.location="?info=success"</script>
          ';
	    
    }else{
            
        echo '
      <script language="JavaScript">document.location="?info=error"</script>
      ';
        
    }
}

// Menampilkan semua data dari table users_id berdasarkan nama secara Descending
$users_id = query("SELECT * FROM users_id ORDER BY id DESC");

// $whatsapp = query("SELECT * FROM users_id");

$result = mysqli_query($koneksi, "SELECT * FROM users_id" );

$whatsapp = mysqli_fetch_assoc($result); ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password</title>
    <!-- CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="css/modern.css">
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <link rel="icon" type="image/png" href="<?=$whatsapp['logo'];?>">
</head>

<body class="d-flex align-items-center min-vh-100">

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-5">
                <div class="glass-card text-center p-5">
                    
                    <div class="mb-4">
                        <img src="<?php echo $whatsapp['logo'];?>" alt="Logo" class="img-fluid" style="max-height: 80px;">
                    </div>
                    
                    <h3 class="fw-bold mb-1 text-gradient">Reset Password</h3>
                    <p class="text-muted mb-4">Masukkan email Anda untuk reset password</p>

                    <!-- Ini Error jika tidak bisa login -->
                    <?php if (isset($error)) : ?>
                        <div class="alert alert-danger py-2">Username dan Password Salah!</div>
                    <?php endif; ?>

                    <form action="" method="post">
                        <div class="form-floating mb-4 text-start">
                            <input type="email" class="form-control" id="email" name="email" placeholder="example@company.com" required autofocus>
                            <label for="email">Email Address</label>
                        </div>
                        
                        <button type="submit" name="reset_password" class="btn btn-primary w-100 py-3 mb-3">
                            <i class="bi bi-envelope me-2"></i> Kirim saya password baru
                        </button>
                    </form>

                    <div class="text-center mt-3">
                        <span class="text-muted small">Kembali ke halaman</span>
                        <a href="login" class="text-decoration-none text-primary fw-bold small">
                            Login
                        </a>
                    </div>

                </div>
            </div>
        </div>
    </div>

    <?php if(isset($_GET['info'])){
        if($_GET['info'] == 'success'){
            echo '<script>swal("Sukses","Password baru terkirim ke email dan WhatsApp Anda","success");</script>';
        }
        if($_GET['info'] == 'error'){
            echo '<script>swal("Maaf","Email Anda tidak terdaftar","error");</script>';
        }
    }?>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>