<?php
/*$servername = "localhost";
$username = "devknestlms_dbuser";
$password = "Narahari1!";
$dbname = "devknestlms_multi";*/
include_once __DIR__.'/../config/database.php';
include_once __DIR__.'/../config/properties.php';
$servername = HOST;
$username = USER;
$password = PASSWORD;
$dbname = DATABASE;
date_default_timezone_set('Asia/Kolkata');
$datetime = date("Y-m-d H:i:s");
$date = date("Y-m-d");

$ADMIN_PATH=ADMIN_HTTP_PATH;
$COMPANY = $properties['jdbc.comp_image_folder'];
$CLIENT_PATH=CLIENT_HTTP_PATH;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
try {
    echo "date:" . $date;
    echo "<br/>";
    $result = mysqli_query($conn, "SELECT days,company_id FROM configuration WHERE company_id is not null");
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_array($result)) {
            $days = $row['days'];
            $company_id = $row['company_id'];
            echo "days:" . $company_id;
            echo "<br/>";
            echo "days:" . $days;
            echo "<br/>";
           $result1 = mysqli_query($conn, "SELECT u.user_id,u.first_name,u.email_id,u.mobile_no,co.offering_id,cad.end_date,co.course_name,cm.logo_path,cm.website_link  
			FROM course_offering as co,course_assignment_date as cad,course_assignment as ca,user_account as u,company as cm 
            WHERE cm.company_id=u.company_id AND ca.offering_id=co.offering_id AND cad.user_id=ca.user_id AND cad.offering_id=ca.offering_id 
            AND ca.user_id=u.user_id AND cad.user_id=u.user_id AND ca.result is null AND co.status = 'Active' 
            AND u.status='Active' AND co.company_id='" . $company_id . "' AND cad.start_date <= '" . $date . "' AND cad.end_date= '" . $date . "' + interval " . $days . " day ORDER BY co.offering_id");
            if (mysqli_num_rows($result1) > 0) {
                $dirname = "logs";
                if (!is_dir($dirname)) {
                    mkdir($dirname);
                }
                $file_name = $dirname . "/" . $date . ".txt";
                $fp = fopen($file_name, "a");
                fwrite($fp, "Date:" . $datetime . "\n");
                while ($row1 = mysqli_fetch_array($result1)) {
                    $oid = $row1["offering_id"];
                    $end_date1 = $row1["end_date"];
                    $date1 = new DateTime($end_date1);
                    $end_date = $date1->format('d/m/Y');
                    $course_name = $row1["course_name"];
                    echo $end_date;
                    echo "<br/>";
                    $mobile_no = $row1["mobile_no"];
                    echo $mobile_no;
                    echo "<br/>";
                    $email_id = $row1["email_id"];
                    echo $email_id;
                            echo "<br/>";
                            $first_name = $row1["first_name"];
                            echo $first_name;
                            echo "<br/>";
                            $website_link = "";
                            if ($row1["website_link"] != null) {
                                $website_link = $row1["website_link"];
                            }
                            $logo_path = $ADMIN_PATH . $COMPANY."/" .$row1["logo_path"];

                            // $mobile_no = trim($mobile_no);
                            if ($mobile_no != null || $mobile_no != "") {
                                $msgbody = urlencode("Dear User, The course " . $course_name . ", is about to end on " . $end_date . ". Please complete it in time - 4EdgeIT Solutions");
//                                file_get_contents('http://trans.kapsystem.com/api/v3/index.php?method=sms&api_key=A7cca971e58ac774dd98d2830c7aea569&to=' . $mobile_no . '&sender=EDGEIT&message=' . $msgbody);
                                  file_get_contents('http://www.smsjust.com/blank/sms/user/urlsms.php?username=haniballal01&pass=user@123&senderid=EDGEIT&dest_mobileno=' . $mobile_no . '&message=' . $msgbody.'&dltentityid=1201160450888723221&dlttempid=1607100000000287020&response=Y');             
                            }
                            if ($email_id != "") {
                                require_once('../vendor/autoload.php');
                                $credentials = SendinBlue\Client\Configuration::getDefaultConfiguration()->setApiKey('api-key', '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');
                                $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]],
                                    'htmlContent' => '<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 Sir/Madam,<br/><br/>
                              The course ' . $course_name . ', assigned to you is about to end on ' . $end_date . '. Please complete it ASAP. Access the learner panel at '.$CLIENT_PATH.'<br/><br/>
                              Regards,<br/>K-nest LMS</div><div style="padding:30px;"><center><img src="' . $logo_path . '" /><br/>' . $website_link . '</center></div></div>']);

                                try {
                                    $result = $apiInstance->sendTransacEmail($sendSmtpEmail);
                                    // print_r($result);
                                } catch (Exception $e) {
                                    echo $e->getMessage(), PHP_EOL;
                                }
                            }
                           

                            fwrite($fp, "First Name:" . $first_name . "\n");
                            fwrite($fp, "Mobile No:" . $mobile_no . "\n");
                            fwrite($fp, "Email Id:" . $email_id . "\n");
                  
                }
                fclose($fp);
            }
        }
    }
} catch (Exception $e) {
    echo 'Message: ' . $e->getMessage();
}
$conn->close();
?>		
