<?php
  function createRandomPassword()
 {
	$chars = "003232303232023232023456789";
	srand((double)microtime()*1000000);
	$i = 0;
	$pass = '' ;
	while ($i <= 7) {

		$num = rand() % 33;
		$tmp = substr($chars, $num, 1);
		$pass = $pass . $tmp;
		$i++;
	}
	return $pass;
}

$sess=createRandomPassword();
?>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h4 class="m-0">Emails Management</h4>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item">Dashboard</li>
              <li class="breadcrumb-item">Extra</li>
              <li class="breadcrumb-item active">Emails Management</li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-4 col-sm-6 col-12">
            <div class="info-box">
              <span class="info-box-icon bg-success"><i class="fa fa-paper-plane"></i></span>
              <?php
                include('../include/config.php');
                $result = mysqli_query($con,"SELECT * FROM emails where ESent='Yes'");
                $num_rows1 = mysqli_num_rows($result);
                {
                 $sent=htmlentities($num_rows1); 
                }
                ?>
              <div class="info-box-content">
                <span class="info-box-text">Number of Sent mails</span>
                <span class="info-box-number"><?php echo $sent; ?></span>
              </div>
              <!-- /.info-box-content -->
            </div>
            <!-- /.info-box -->
          </div>
          </div>
         </div>
    </section>
          
    
    
    <section class="content">
    <div class="row">
 
        <div class="col-md-7">
          <div class="card ">
            <div class="card-header">
                <div class="card-body">
                <div class="table-responsive" id=''>
                 <h5 class="alert-danger"><center>Select Recipients of your mail</center></h5>
                 <table class="table table-hover" id="tableemployees" style="font-size:12px">
                     <thead>
                    <tr class="">
                        <th colspan=""></th>
                        <th colspan=""></th>
                        <th colspan="2">
                        </th>
                        <th colspan="2"><label class="text-orange"> Select/Unselect All </label>  <input type="checkbox" class="pull-right" id="selectAll" /></th>
                    </tr>
                         <tr>
                             <th>#</th>
                             <th>Staff</th>
                             <th>Nature of Work</th>
                             <th>Email</th>
                             <th>Contact</th>
                             <th>Action</th>
                         </tr>
                     </thead>
                     <tbody>
                        <?php
                        $query=mysqli_query($con,"select * from Staffs where Status='Active' AND Email !='' AND Name !='Teachers-Default' ORDER BY PRNo ASC");
                        //$query=mysqli_query($con,"select * from Staffs where Status='Active' AND Email !='' AND id !='1' AND Name !='Teachers-Default' ORDER BY PRNo ASC");
                        $cnt=1;
                        while($row=mysqli_fetch_array($query))
                        {
                          ?>  
                          <tr class="row-select">
                			<td><?php echo $cnt; ?></td>
                			<td><?php echo $row["Name"]; ?></td>
                			<td><?php echo $row["Nature"]; ?></td>
                			<td><?php echo $row["Email"]; ?></td>
                			<td><?php echo $row["Phone"]; ?></td>
                			<td>
                			  <input type="hidden" class="staffsid" value="<?php echo $row["id"]; ?>" />
                			  <input type="checkbox" name='checkbox[$rowid]' value="<?php echo $row["id"]; ?>">
                			</td>
                		</tr>
                         <?php  
                         $cnt=$cnt+1;
                        }
                    	?>
                     </tbody>
                 </table> 
                  
              </div>
              </div>
            </div>
            </div>
          </div> 
          
          <div class="col-md-5">
         <form method="POST" action="" id="" enctype="multipart/form-data" novalidate onsubmit="return false;">
      <div class="row">
        <div class="col-md-12">
          <div class="card card-primary">
            <div class="card-header">
              <h3 class="card-title">Compose Email</h3>

              <div class="card-tools">
                <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                  <i class="fas fa-minus"></i>
                </button>
              </div>
            </div>
            <div class="card-body">
              <div class="form-group">
                <label for="inputName">Subject <span class="text-danger">*</span></label>
                <input type="text" id="subj" class="form-control" required>
              </div>
              <div class="form-group">
                <label for="inputDescription">Message <span class="text-danger">*</span></label>
                <textarea class="tinymce-editor form-control" id="mainmess" required></textarea>
              </div>
              
              <!--div class="form-group">
                 <label for="inputName"> Send email to <span class="text-danger">*</span></label>
                 <select id="receivers" class="form-control custom-select" required>
                    <option selected disabled value="">---</option>
                    <option value="All">All Staffs</option></option>
                    <option value="Tchs">Teachers Only</option>
                    <option value="Sele">Selected</option>
                  </select> 
              </div-->
             
              <div class="form-group">
                  <input type="hidden" id="txtsession" name="" value="<?php echo $sess; ?>" />
                  <center><label id="feedbackemails"></label></center>
              </div>
              
                <div class="form-group">
                    <center><label class="text-danger"><em>Email will be sent to selected staffs only</em></label></center>
                    <button class="btn btn-success pull-right" id="subemail">Send <i class="fa fa-paper-plane"></i></button>
               </div>
              
            </div>
            <!-- /.card-body -->
            <hr />
            <h6 class="alert-success"><center>Latest Sent Emails</center></h6>
             <table class="table table-hover" id="tablemails" style="font-size:11px;font-family:Times">
                     <thead>
                         <tr class="alert-warning">
                             <th>#</th>
                             <th>Recipient </th>
                             <th>Message</th>
                             <th>Date</th>
                         </tr>
                     </thead>
                     <tbody>
                        <?php
                        $query=mysqli_query($con,"select emails.*,Staffs.Name as snam from emails left join Staffs ON Staffs.id=emails.sid where emails.ESent='Yes'  ORDER BY eid DESC");
                        $cnt=1;
                        while($row=mysqli_fetch_array($query))
                        {
                          ?>  
                          <tr class="row-select">
                			<td><?php echo $cnt; ?></td>
                			<td><?php echo $row["snam"]; ?></td>
                			<td><?php echo $row["Msg"]; ?></td>
                			<td class="text-danger"><?php echo $row["Dat"]; ?></td>
                		</tr>
                         <?php  
                         $cnt=$cnt+1;
                        }
                    	?>
                     </tbody>
                 </table> 
                  
          </div>
          <!-- /.card -->
        </div>
      </div>
        </form>
    </div>
            
        
        
    </div>
    </section>
</div>

