<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Send Mail';
$maindb = 'accmast';
$para = $_GET['para'];


$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

if (isset($_POST["submit"])) {


    $count = $_POST['count'];
    $txtsub = $_POST['subject'];
    $txtmsg = $_POST['message'];
    $target_dir = "updImages/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $file = $_FILES["fileToUpload"]["tmp_name"];
    $fileType = $_FILES["fileToUpload"]["type"];
//$target_file = $target_dir . ($fileName);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));


// Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        browse("Sorry, your file is too large.(Only 500 Allowed)");
        $uploadOk = 0;
    }

// Allow certain file formats
    if ($imageFileType != "jpg" && $imageFileType != "pdf" && $imageFileType != "jpeg" && $imageFileType != "docx" && $imageFileType != "zip" && $imageFileType != "txt") {
        browse("Sorry, only jpg, jpeg, pdf, zip, txt & docx files are allowed.");
        $uploadOk = 0;
    }
// Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        browse("Sorry, your file was not uploaded.");
// if everything is ok, try to upload file
    } else {

        include("/home/techfar/public_html/epiweb/libmail.php");

        for ($i = 0; $i <= $count; $i++) {
            $chk = $_POST["chk_$i"];
            if($chk == 1) {
                
                $email = $_POST["email_$i"];
                $m = new Mail; // create the mail
                $m->From("MBSM");
                $m->To($email);
                $m->Subject($txtsub);
                $m->Body($txtmsg); // set the body
                $m->Priority(1); // set the priority to Low
                $m->Attach($file, "$fileType"); // attach a file of type image/gif to be displayed in the message if possible
                $m->Send(); // send the mail
            }
        }
        browse("Mail Send Sucessfully");
        unlink($file);
    }
}
include("includes/linkshead.php");
?>
<script src="MyScript/multimail.js" type="text/javascript"></script>
</head>
<body>
    <form action='' method="post" enctype="multipart/form-data" onsubmit="return confirm('Do you want to send mail ?');">
        <!-- Default box -->
        <div class="col-lg-6 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php Echo $formname . ' ' . '(jpg, jpeg, pdf, zip, txt & docx)'; ?></h3>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">Center</label>
                        </div>
                        <div class="col-lg-7 col-md-6 col-sm-8 col-xs-8 pad">
                            <select class="form-control select2 center" name="center" >
                                <option></option>
                                <?php
                                $gcent = mysqli_query($conn, "SELECT centcode,name FROM `center` order by name") or die(mysqli_error($conn));
                                while ($scent = mysqli_fetch_array($gcent)) {
                                    echo"<option value='$scent[centcode]'>$scent[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control ">Medium</label>
                        </div>
                        <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                            <select class="form-control select2 medium" name="medium" >
                                <option></option>
                                <?php
                                $gmedium = mysqli_query($conn, "SELECT code,name FROM `medium` order by name") or die(mysqli_error($conn));
                                while ($smedium = mysqli_fetch_array($gmedium)) {
                                    echo"<option value='$smedium[code]'>$smedium[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control">Exam</label>
                        </div>
                        <div class="col-lg-4 col-md-5 col-sm-5 col-xs-3 pad">
                            <select class='form-control select2 exam' name='exam' >
                                <option value=""></option>
                                <?php
                                $gexam = mysqli_query($conn, "SELECT code,name FROM `exam` ORDER BY `name`") or die(mysqli_error($conn));
                                while ($schexam = mysqli_fetch_array($gexam)) {
                                    echo "<option value='$schexam[code]'>$schexam[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Section</label>
                        </div>
                        <div class="col-lg-4 col-md-5 col-sm-5 col-xs-5 pad">
                            <select name="section" class="form-control select2 section">
                                <option value=""></option>
                                <option value="P">Primary</option>
                                <option value="S">Secondary</option>
                                <option value="M">Prelims</option>
                            </select>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Session</label>
                        </div>
                        <div class="col-lg-4 col-md-5 col-sm-5 col-xs-5 pad">
                            <select name="session" class="form-control select2 session">
                                <option value=""></option>
                                <option value="M">Morning</option>
                                <option value="A">Afternoon</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg" >Subject</label>
                        </div>
                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type='text' class='form-control subject' name='subject'>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Message</label>
                        </div>
                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                            <textarea name="message" class='form-control'></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">File</label>
                        </div>
                        <div class="col-lg-7 col-md-7 col-sm-8 col-xs-8 pad">
                            <input type="file" name="fileToUpload" id="fileToUpload" class='form-control'>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-8 text-right pad">
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 text-right pad">
                            <input type="button" class='form-control' id="mailproceed" value="Proceed" name="proceed" style="border: 2px solid #6082BF;">
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                </div>
            </div>
        </div>
        <div class="modal fade" id="mailmodal"  role="dialog" aria-labelledby="myLargeModalLabel" >
            <div class="modal-dialog modal-lg" style="margin-top: 5%;">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal"><span>&times;</span></button>
                        <div class="row">
                            <div class="col-lg-2 text-right padi">
                                <label class="label-control">Search By Code</label>
                            </div>
                            <div class="col-lg-2 padi" >
                                <input type='text' id="tsearch" style="" name='search' class="form-control tsearch">
                            </div>
                        </div>
                    </div>
                    <div class="modal-body">
                        <div class="col-lg-12 iloader text-center">
                            <img src="images/g2a_spinner.gif" alt="loading">       
                        </div>
                        <div class="table-responsive" style="height: 300px;">
                            <div class="mydata myTable"></div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <div><input type="submit" class="btn btn-info proceed" name='submit' value="Proceed"></div>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
