<?php
namespace App\Controllers;
SESSION_START();
use \Core\View;
use \Core\Inputs;
use \Core\Userlog;
use \Core\Menu;
use \Core\Form;
use App\Models\Classm;
use App\Email\Emails;
class Classes extends \Core\Controller
{
    private static function css()
    {
        $css = 
        '<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
            <title>Hindu College of Engineering & Technology || Guntur</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
            <title>Hindu College of Engineering & Technology || Guntur</title>
            <link rel="stylesheet" href="/hindu/assets/css/bootstrap.min.css">
            <link rel="stylesheet" href="/hindu/assets/css/style.css">
            <link rel="stylesheet" href="/hindu/assets/sweetalert2/dist/sweetalert2.min.css" >
            <style>
                @media print
                {
                    body * {
                        visibility: hidden;
                    }
                    .print-this, .print-this * {
                        visibility: visible;
                    }
                }
            </style>
            <script>
                function exportTableToExcel(tableID, filename = "")
                {
                    var downloadLink;
                    var dataType = "application/vnd.ms-excel";
                    var tableSelect = document.getElementById(tableID);
                    var tableHTML = tableSelect.outerHTML.replace(/ /g, "%20");
                    
                    // Specify file name
                    filename = filename?filename+".xls":"excel_data.xls";
                    
                    // Create download link element
                    downloadLink = document.createElement("a");
                    
                    document.body.appendChild(downloadLink);
                    
                    if(navigator.msSaveOrOpenBlob){
                        var blob = new Blob(["\ufeff", tableHTML], {
                            type: dataType
                        });
                        navigator.msSaveOrOpenBlob( blob, filename);
                    }else{
                        // Create a link to the file
                        downloadLink.href = "data:" + dataType + ", " + tableHTML;
                    
                        // Setting the file name
                        downloadLink.download = filename;
                        
                        //triggering the function
                        downloadLink.click();
                    }
                }
            </script>
        </head>';
        return $css;  
    }
    private static function script()
    {
        $script =
                '
                <div class="footer navbar-fixed-bottom text-center">        
                    <span class="text-muted text-center">Copyright &#169 2020. Hindu College of Engineering & Technology, Guntur</span>
                </div>
                <script src="/hindu/assets/word/jquery.min.js" ></script>
                <script src="/hindu/assets/js/bootstrap.min.js" ></script>
                <script src="/hindu/assets/vendor/sweetalert2/dist/sweetalert2.min.js" ></script>';
        return $script;  
    }
    private static function normalmenu()
    {
        // bg-light
        $menu = '
            <nav class="navbar navbar-expand-lg navbar-light ">
                <a class="navbar-brand" href="#" >HINDU COLLEGE</a>
                <button style="background-color:white;" class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav ml-auto navbar-right">
                    
                    <li class="nav-item active" >
                        <a class="nav-link" href="/Homes/index" >Home</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="/homes/latest" >Latest News</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="/homes/suggestions" >Suggestions</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="/homes/contactus" >Contact Us</a>
                    </li>
                    <!--
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Dropdown
                        </a>
                        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Another action</a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                    </li>
                    -->
                    </ul>
                    <form class="form-inline my-2 my-lg-0">
                    <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-light my-2 my-sm-0" type="submit">Search</button>
                    </form>
                </div>
            </nav>
        ';
        return $menu;
    }
    public static function index()
    {
        echo static::css();
        echo '<body>';
        echo static::normalmenu();
        echo '
            <div class="container">
                    <div class="container-fluid">
                        <div class="row" style="margin-top:20px;">
                            <div class="col-xl-4"></div>
                            <div class="col-xl-4">
                            <div class="card">
                                <div class="card-body">
                                    <form action="/Classes/auth" method="POST" enctype="multipart/form-data">
                                        <div class="form-group">
                                            <div class="float-center">Login Here</div>
                                        </div>
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="username" id="username" placeholder="User Name" required>
                                        </div>
                                        <div class="form-group">
                                            <input type="password" class="form-control" name="password" id="password" placeholder="Password" required>
                                        </div>
                                        <div class="form-group">
                                            <div class="float-right">
                                                <input type="submit" class="btn btn-primary" style="background-color: #002366 !important;" value="Submit">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <a href="/Classes/signup">New User Registration</a>
                                        </div>
                                    </form>
                                </div>
                            </div>
                            </div>
                            <div class="col-xl-4"></div>
                        </div>
                    </div>
                </div>
            ';
        echo '</body>';
        echo static::script();
    }
    public static function auth()
    {
        if(isset($_POST['username']) && isset($_POST['password']))
        {
            $data = Classm::getdata("SELECT * FROM `student_details` WHERE (`ht_no`='$_POST[username]' OR `email`='$_POST[username]' OR `mobile`='$_POST[username]') AND `password`='$_POST[password]'");
            echo json_encode($data);
        }
    }
    public static function joinyear()
    {
        $year = date('Y');
        $a = '';
        for($i=0; $i<4; $i++)
        {
            $j = $year-$i;
            $a .= '<option value="'.$j.'">'.$j.'</option>';
        }
        return $a;
    }
    public static function signup()
    {
        $salutation = Inputs::optionsdb(Classm::getdata("SELECT * FROM `salutation`"), 'salu_id', 'salu_name');
        $course = Inputs::optionsdb(Classm::getdata("SELECT * FROM `courses`"), 'course_id', 'course_name');
        $dept = Classm::getdata("SELECT * FROM `departments`");
        echo static::css();
        echo '<body>';
        echo static::normalmenu();
        echo '
            <div class="container">
                    <div class="container-fluid">
                        <div class="row" style="margin-top:20px;">
                            <div class="col-xl-3"></div>
                            <div class="col-xl-6">
                            <div class="card">
                                <div class="card-body">
                                    <form action="/Classes/register" method="POST" enctype="multipart/form-data">
                                        <div class="form-group">
                                            <div class="float-center">Signup Here</div>
                                        </div>
                                        
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <input type="text" class="form-control" name="number" id="number" placeholder="Hallticket Number" required>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <select class="form-control" name="joinyear" id="joinyear" required>
                                                    <option value="">Join Year</option>
                                                    '.static::joinyear().'
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                <select class="form-control" name="course" id="course" onchange="getdept(this.value)" required>
                                                <option value="">Select Course</option>
                                                '.$course.'
                                                </select>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <select class="form-control" name="department" id="department" required>
                                                    <option value="">Select Department</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-4">
                                                <div class="form-group">
                                                    <select class="form-control" name="salutation" id="salutation" required>
                                                    <option value="">Salutation</option>
                                                    '.$salutation.'
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="col-md-8">
                                                <div class="form-group">
                                                <input type="text" class="form-control" name="name" id="name" placeholder="Name" required>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <select class="form-control" name="gender" id="gender" required>
                                                        <option value="">Gender</option>
                                                        <option value="M">Male</option>
                                                        <option value="F">Female</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                <input type="text" name="dob" id="dob" class="form-control" onfocus="(this.type=`date`)" onblur="(this.type=`text`)" placeholder="Date of Birth" required="">
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="email" id="email" placeholder="Email" required>
                                        </div>
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="mobile" id="mobile" placeholder="Mobile" required>
                                        </div>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                <input type="password" class="form-control" name="password" id="password" placeholder="Password" required>
                                                </div>
                                            </div>
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <select class="form-control" name="quota" id="quota" required>
                                                        <option value="">Select Admission Type</option>
                                                        <option value="C">Covener</option>
                                                        <option value="M">Management</option>
                                                        <option value="S">Spot</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group" id="submitbutton">
                                            <div class="float-right">
                                                <input  type="submit" class="btn btn-primary" style="background-color: #002366 !important;" value="Submit">
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                            </div>
                            <div class="col-xl-3"></div>
                        </div>
                    </div>
                </div>
            ';
        echo '</body>';
        echo static::script();
        echo '
            <script>
                function getdept(course)
                {
                    var selectobject = document.getElementById("department");
                    for (var i=0; i<selectobject.length; i++) 
                    {
                        selectobject.remove(i);
                    }
                    $.ajax({
                        type: "POST",
                        data : {course: course},
                        url: "/Classes/getdept",
                        success: function(d){
                            $("#department").append(d);
                        }
                    });
                }
            </script>
        ';
    }
    public static function getdept()
    {
        $data = Inputs::optionsdb(Classm::getdata("SELECT dept.* FROM departments dept JOIN dept_relations dr ON dr.fk_dept_id=dept.dept_id WHERE dr.fk_course_id='$_POST[course]'"), 'dept_id', 'dept_name');
        echo $data;
    }
    public static function register()
    {
        if(isset($_POST['number']))
        {
            $date = date('Y-m-d');
            $data = Classm::getdata("SELECT * FROM `student_details` WHERE `ht_no`='$_POST[number]' OR `email`='$_POST[email]' OR `mobile`='$_POST[mobile]'");
            if(sizeof($data) == 0)
            {
                Classm::insert("INSERT INTO `student_details` (`ad_no`, `ht_no`, `fk_course_id`, `fk_dept_id`, `salutation`, `name`, `dob`, `gender`, `email`, `mobile`, `joinyear`, `quota`, `joindate`, `password`, `e_verf`, `m_verf`, `f_login`, `status`) VALUES ('', '$_POST[number]', '$_POST[course]', '$_POST[department]', '$_POST[salutation]', '$_POST[name]', '$_POST[dob]', '$_POST[gender]', '$_POST[email]', '$_POST[mobile]', '$_POST[joinyear]', '$_POST[quota]', '$date', '$_POST[password]', '0', '0', '1', '0')");
            }
            else
            {
                echo '<script>window.location="/Classes/index?status=0";</script>';
            }
        }
        else
        {
            echo '<script>window.location="/Classes/index?status=-1";</script>';
        }
    }
    public static function home()
    {
        if($_SESSION['user_role'] != '')
        {
            echo static::css();
            echo '<body>';
                echo $_SESSION['menu'];
                echo '
                <div class="container">
                    <div class="container-fluid">
                        <div class="row" style="margin-top:20px;">
                            <div class="col-xl-12">
                            <!--Welcome to HINDU COLLEGE-->
                            </div>
                        </div>
                    </div>
                </div>
                ';
            echo '</body>';
            echo static::script();
        }
    }
    public static function emailverification()
    {
        $data = Classm::getdata("SELECT * FROM `org_details`");

    }
    public static function test()
    {
        // header('content-type: text/xml;charset=utf-8');
        $name = "Fianl Test";
        $email = "s.krishnarjuna@gmail.com";
        $token = "1234567890qwertyui";
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => "http://apie.mycvmaker.com/email/index.php",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS =>"{\"apikey\":\"s.krishnarjuna@gmail.com\", \"secrectkey\":\"SSRSKJR\", \"toemail\":\"$email\", \"toname\":\"$name\", \"token\":\"$token\"}",
        CURLOPT_HTTPHEADER => array(
            ": ",
            "Content-Type: application/json"
        ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        //$data = $response;
        echo $response;
    }
}