<?php

session_start();
    include_once '../config/database.php';
    include_once '../security/session_config.php';
    include_once '../security/csrf.php';
    include_once '../config/properties.php';
    include_once "../resources/EncryptDecrypt.php";
    
    require("../resources/manageusers.php");
    //added for sendinblue
    require_once('../vendor/autoload.php');

    $csrf_token = $_POST['csrf_token'];
    validate_csrf_request($csrf_token);

    $object = new ManageUsers();

    switch ($_POST['mode']) {
        case "change": {
                $mcrypt = new MCrypt();
                $old_pass = $_POST['old'];
                $new_pass = $_POST['new'];
                $name = "";
                $email_id_user = "";

                $query = $object->getPassword($_SESSION['uid']);
                if ($row = $query->fetch(PDO::FETCH_ASSOC)) {
                    $name = $row['first_name'];
                    $email_id_user = $row['email_id'];

                    $decrypted = $mcrypt->decrypt($row['password']);
                    if (strcmp($old_pass, $decrypted) != 0) {
//                        echo $decrypted;
                        echo json_encode(array("result1" => "not_match", "result2" => _("US0513")));
                        break;
                    }
                    if (strcmp($new_pass, $decrypted) == 0) {
                        echo json_encode(array("result1" => "same", "result2" => _("US0514")));
                        break;
                    }
                }

                $val2 = $object->UpdatePassword($new_pass, $_SESSION['uid'], $date_time);
                if ($val2) {
                    if ($email_id_user == "ON") {
                        if ($email_id_user != "") {
                            $website_link_comp = "";
                            $stmt_company = $object->UserCompanyDetails($user_id);
                            if ($row_user = $stmt_company->fetch(PDO::FETCH_ASSOC)) {
                                extract($row_user);
                                if ($row_user['website_link'] != null) {
                                    $website_link_comp = $row_user['website_link'];
                                }
                                $logo_path = ADMIN_HTTP_PATH . $properties['jdbc.comp_image_folder'] . "/" . $row_user['logo_path'];
                            }
                            //sendinblue
                            $credentials = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', $api_key);
                            $apiInstance = new SendinBlue\Client\Api\TransactionalEmailsApi(new GuzzleHttp\Client(), $credentials);
                            $sendSmtpEmail = new \SendinBlue\Client\Model\SendSmtpEmail([
                                'subject' => 'K-nest LMS',
                                'sender' => ['name' => 'K-nest LMS', 'email' => 'donotreply@knestlms.com'],
                                'to' => [['email' => $email_id_user]],
                                'htmlContent' => '<html><div style="background-color:#f3f5f9;width:100%;padding:10px 50px 10px 10px;"><div style="background:#ffffff;background-color:#ffffff;Margin:0px auto;max-width:620px;padding:30px;">Dear ' . $first_name . ',<br/><br/>Your password has been changed.<br/><br/>Regards,<br/>K-nest LMS Admin</div><div style="padding:30px;"><center><img src=' . $logo_path . ' /><br/>' . $website_link_comp . '</center></div></div></html>',
                                    // 'htmlContent' => '<html>Dear {{params.name}},<br/><br/>Your password has been changed.<br/><br/>Regards,<br/>K-nest LMS Admin</html>',
                                    //'params' => ['name' => $name]
                            ]);

                            try {
                                $result = $apiInstance->sendTransacEmail($sendSmtpEmail);
                                // print_r($result);
                            } catch (Exception $e) {
                                echo $e->getMessage(), PHP_EOL;
                            }
                            /* $to = $email_id;
                              $subject = "K-nest LMS";
                              $message = "Dear " . $name . ",<br/>Your password has been changed.<br/><br/>Regards,<br/>K-nest LMS Admin";
                              $headers = "MIME-Version: 1.0" . "\r\n";
                              $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                              $headers .= 'From: K-nest LMS <donotreply@knestlms.com>' . "\r\n";

                              mail($to, $subject, $message, $headers); */
                        }
                    }
                    echo json_encode(array("result1" => "success", "result2" => _("US0201")));
                }

                break;
            }
        default:
            break;
    }
?>

