<?php include ("kfunctions.php"); ?>
<html>
<head>
<?php kheader(); ?>


<script src="js/bootstrap.min.js"></script>
<script src="js/jquery-1.11.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
</head>
<body>

    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <?php ktopbar(); ?>
            <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
            <?php kleftbar(); ?>
            <!-- /.navbar-collapse -->
        </nav>

        <div id="page-wrapper">
<div id="wholebody">
            <div class="container-fluid">
<div class="row">
        <div class="col-sm-3 col-md-2">
            <div class="btn-group">
                <a  href='message.php' type="button" class="btn btn-primary dropdown-toggle">
                    Back 
                </a>
                

            </div>

        </div>

        </div>
    </div>
    <br>
    <?php
                    if(isset($_POST["submit_mess"])){
                    $recepient=$_POST['recepient'];
$message=$_POST['message'];

include('connect.php');

$rolenumber=$_SESSION["rolenumber"];

$result2=$dbh->query("select * from users where rolenumber='$rolenumber'");
$row2=$result2->fetchObject();

$autoid=$row2->autoid;
$mth = date("m");
$dy = date("d");
$min = date("i");
$sec = date("s");
$yr = date("Y");
$finalyr = substr($yr,2,2);


$messageid = $autoid.$finalyr.$mth.$dy.$min.$sec;

if($recepient!="" and $message!=""){
$count=$dbh->exec("insert into message (recepient,message,sender,messageid) values('$recepient','$message','$rolenumber','$messageid')");

echo "<div class='alert alert-success'>".date("h:i:sa")."&nbsp;"."Message sent</div>";
}else{
echo "<div class='alert alert-danger'>".date("h:i:sa")."&nbsp;"."Message not sent. Please select User or Type message. </div>";
}

                }

                    ?>
    <hr />

    <div class="row">
        <?php include("message_menu.php"); ?>
        <div class="col-sm-9 col-md-10">
            <!-- Nav tabs -->
            
            <!-- Tab panes -->

            <div class="tab-content">
                <div class="tab-pane fade in active" id="home">
                    
                                
<div class="col-lg-6">
<div class="panel panel-yellow"><!--Initiative-->
<div class="panel-heading"><h3 class="panel-title">Message Body</h3></div>
<div class="panel-body">
<form method='post'>
                            <textarea class='form-control' name='message' placeholder='Type your message here' rows='12' required></textarea>

                            <input type='submit' class='btn btn-sm btn-primary' name='submit_mess' value='Send'>
                        </div>
                        <input type='hidden' id='selectedusers' name='recepient'>
                        </form>
</div></div>
                        <div class='col-lg-6'>

<div class="panel panel-yellow"><!--Initiative-->
<div class="panel-heading"><h3 class="panel-title">Select Recepients</h3></div>
<div class="panel-body">
                        <div class='panel panel-default'>
                            <div class='panel-body'>
                            <div id='chkbox'>


                                <?php
                            $rolenumber=$_SESSION["rolenumber"];
$result_users=$dbh->query("select * from users where rolenumber!='$rolenumber' and status='1'");
$row_users=$result_users->fetchObject();
$count_users=$result_users->rowCount();

if($count_users>0){
    do{
?>
<div class='col-lg-6'><input type='checkbox' id='chkout<?php echo $row_users->autoid; ?>' onclick="sender()"  value='<?php echo $row_users->rolenumber; ?>'> <?php echo $row_users->firstname." ".$row_users->lastname; ?></div>


<?php
}while($row_users=$result_users->fetchObject());
}else{
    echo "Please contact your IT department. ";
}

?>                      
</div>
                            </div>
                            </div>
 
                    </div>
                </div>
            </div>
            <!-- Ad -->
           </div>

        </div>
        <!-- /#page-wrapper -->

    </div>
    </div>
    <!-- /#wrapper -->
<script>
function sender(){
var selectedusers = document.getElementById("selectedusers");
var values = [];

<?php
$rolenumber=$_SESSION["rolenumber"];
$result_users=$dbh->query("select * from users where rolenumber!='$rolenumber' and status='1'");
$row_users=$result_users->fetchObject();
$count_users=$result_users->rowCount();
if($count_users>0){
    do{
?>
if(document.getElementById("chkout<?php echo $row_users->autoid; ?>").checked) {
 var user = document.getElementById("chkout<?php echo $row_users->autoid; ?>").value;
  values.push(user);
}
<?php
}while($row_users=$result_users->fetchObject());
}
?>
selectedusers.value = values.join(", ");
}
</script>
    <!-- jQuery -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

</body>

</html>
