<?php 
include 'db.php';
include 'config.php';


use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

include '../mailer/vendor/autoload.php';



function sendMail($email, $subject, $message) {
  global $sitemail, $sitename, $siteurl, $host, $mail_username, $mail_password;
  
  require_once "../mailer/PHPMailer.php";
  require_once "../mailer/SMTP.php";
  require_once "../mailer/Exception.php";

  $mail = new PHPMailer();
  $mail->isSMTP();
  $mail->Host = $host;
  $mail->SMTPAuth = true;
  $mail->Username = $mail_username;
  $mail->Password = $mail_password;
  $mail->Port = 465;
  $mail->SMTPSecure = "ssl";
  $mail->isHTML(true);
  $mail->setFrom($mail_username, $sitename);
  $mail->addAddress($email);
  $mail->AddReplyTo($mail_username, $sitename);
  $mail->Subject = $subject;
  
  // Get the styled email template
  $styledMessage = getEmailTemplate($subject, $message, $siteurl, $sitename);
  
  $mail->MsgHTML($styledMessage);
  return $mail->Send();
}


function getEmailTemplate($subject, $message, $siteurl, $sitename) {
    return '
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>' . $subject . '</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 0;
                background-color: #f4f4f4;
            }
            .container {
                max-width: 600px;
                margin: 20px auto;
                background: #ffffff;
                border-radius: 8px;
                overflow: hidden;
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            }
            .header {
                background: #004aad; /* Change color as needed */
                padding: 20px;
                text-align: center;
                color: white;
                font-size: 24px;
                font-weight: bold;
            }
            .content {
                padding: 20px;
                font-size: 16px;
                color: #333;
                line-height: 1.6;
            }
            .footer {
                background: #004aad;
                color: white;
                text-align: center;
                padding: 10px;
                font-size: 14px;
            }
            .footer a {
                color: #ffcc00;
                text-decoration: none;
            }
            .button {
                display: inline-block;
                background: #004aad;
                color: white;
                padding: 10px 20px;
                border-radius: 5px;
                text-decoration: none;
                font-weight: bold;
                margin-top: 20px;
            }
            .button:hover {
                background: #003080;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                ' . $sitename . '
            </div>
            <div class="content">
                <p>' . $message . '</p>
                <p style="text-align: center;color:white">
                    <a href="' . $siteurl . '" class="button">Visit Website</a>
                </p>
            </div>
            <div class="footer">
                &copy; ' . date("Y") . ' ' . $sitename . ' | <a href="' . $siteurl . '">Website</a>
            </div>
        </div>
    </body>
    </html>';
}

	
function text_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}


function customAlert($case, $content){
    switch ($case) {
      case 'success':
        $mesg =  '<script type="text/javascript">
          $(document).ready(function() {
              swal({
                  title: "Success",
                  text: " '.$content.' ",
                  icon: "success",
                  button: "Ok",
                  timer: 5000
              });    
          });
        </script>';
        break;

        case 'error':
          $mesg = '<script type="text/javascript">
              $(document).ready(function() {
                  swal({
                      title: "Error",
                      text: " '.$content.' ",
                      icon: "error",
                      button: "Ok",
                      timer: 5000
                  });    
              });
          </script>';
        break;
      default:
        break;
    }
  return $mesg;
}

function pageRedirect($sec, $route){
  $c = "<meta http-equiv='refresh' Content='".$sec."; url=".$route." ' />";
  return $c;
}


function getRandomStrings() {
    $rnumbs = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $tnumbs = str_shuffle($rnumbs);
    $tnumbs = substr($tnumbs, 0, 30);
    return $tnumbs;
  }

  function ct($email){
    global $link;
    $query = mysqli_query($link, "SELECT currency FROM users WHERE email = '$email' ");
    if (mysqli_num_rows($query) > 0) {
      $row = mysqli_fetch_assoc($query);
      $c = $row['currency'];
      if ($c == "") {
        $c = "$";
      }
    }else{
      $c = "$";
    }
    return $c;
  }


?>