<?php

require_once("includes/initialize.php");
  date_default_timezone_get();
  
 $alldept = EDITING:: showdept();

  $connect = mysqli_connect("localhost", "oscotech_oscopt", "oscopt", "oscotech_portal");

if(isset($_POST["submit"]))
{
    $dept = $_POST['department'];
 if($_FILES['file']['name'])
 {
  $filename = explode(".", $_FILES['file']['name']);
  if($filename[1] == 'csv')
  {
   $handle = fopen($_FILES['file']['tmp_name'], "r");
   $count = 0;
   while($data = fgetcsv($handle))
   {
       if ($count==0){
       
           
       }else {
       
          $item1 = mysqli_real_escape_string($connect, $data[0]);  
          $item2 = mysqli_real_escape_string($connect, $data[1]);
    //   $item3 = mysqli_real_escape_string($connect, $data[2]);  
          $item4 = mysqli_real_escape_string($connect, $data[2]);
          $item5 = mysqli_real_escape_string($connect, $data[3]); 
          $item6 = date('Y-m-d');

        $sql=mysqli_query($connect, "SELECT * FROM admission_list WHERE formno = '$item1'");
        if (mysqli_num_rows($sql) > 0)
        {          
        //   echo  "<script>alert('Already Exist');</script>"; 
        }
        else 
        {
            $query = "INSERT into admission_list(formno,names,department,status,session,admitted_date) values('$item1','$item2','$dept','$item4','$item5','$item6')";
          mysqli_query($connect, $query);
          
          
          $qury = EDITING::students_act($item1);
          
          foreach ($qury as $rows)
          {
              
            //   $form = $rows->formno;
              $email = $rows->email;
              $query = "INSERT into student_activity_level(formno,matricno,batch,email,std_process_id,adm_date,status,start_session) values('$item1',0,0,'$email',1,'$item6','open','$item5')";
          mysqli_query($connect, $query);
          }
          
          
        }
        
   }
    $count++;  
   }
   fclose($handle);
    echo "<script>alert('Upload Successful!');</script>";
  }
 }
}
  
?>

<!DOCTYPE html>
<html lang="en">
  <head>

    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Bootstrap, a sleek, intuitive, and powerful mobile first front-end framework for faster and easier web development.">
    <meta name="keywords" content="HTML, CSS, JS, JavaScript, framework, bootstrap, front-end, frontend, web development">
    <meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
    

    <title>OSCOTECH::Admission Page</title>

    <script>
      <!--
        // var ScrollMsg= "OSCOTECH::Admission List"
        // var CharacterPosition=0;
        // function StartScrolling() {
        // document.title=ScrollMsg.substring(CharacterPosition,ScrollMsg.length)+
        // ScrollMsg.substring(0, CharacterPosition);
        // CharacterPosition++;
        // if(CharacterPosition > ScrollMsg.length) CharacterPosition=0;
        // window.setTimeout("StartScrolling()",150); }
        // StartScrolling();
      // -->
    </script>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
    <link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.2/css/buttons.dataTables.min.css"/>
  </head>
  <body>

    <div class="container">

      <h3 align="center">Upload CSV File Only</h3><br />
  <form method="post" enctype="multipart/form-data">
   <div align="center">  
    <label>Select CSV File:</label>
    <input type="file" name="file" />
    <br />
    <div class="table-responsive">
        <div class="col-md-12">

        <br>
          <div class="well bs-component">
              <fieldset>
                  <table class="table table-bordered table-hover" align="center"><tr>
                  
                            <td>                 
                              <select name="department" id="department" class="form-control action" style="width:150px" required="required">
                                  <option value="">Select Department</option>
                                  <?php foreach($alldept as $dep){?>
                                  <option value="<?php echo $dep->department;?>"><?php 
                                  echo ucwords($dep->department);?></option> <?php }?>
                              </select>
                          </td></tr></table>

                <div>
                  <input type="submit" name="submit" value="Upload" class="btn btn-info" />
                </div>

                <p align="center"><big><b>List of Admitted Students</b></big></p>
            
            	<!--<tr>
                    <td>
                        <div class="col-sm-4"> 
                              <select name="department" id="department" class="form-control action" style="width:150px">
                                  <option value="">Programme Type</option>
                                  <option value="DP"></option>
                                  <option value="FT"></option>
                              </select>
                          </td>
                          <td>
        					<select name="level" id="level" class="form-control action" style="width:120px">
        						<option value="">Select Level</option>
        						<option value="ND">ND</option>
        	                    <option value="HND">HND</option>
        	                    <option value="PGD">PGD</option>		
        					</select></td> -->
                          
                           <!-- <td>
                                <div class="col-md-4">
                                    <input type="submit" name="search" id ="search" value="Search" class="btn btn-info" />
                                </div> 
        					</td>
                      </tr>
                  </table> -->
                
                <div class="table-responsive">
                    <table class="table table-bordered table-hover table-condensed" id="myTable">
                      <thead>
                        <tr class="info">
                          <th>S/N</th>  
                          <th>FORM NO</th>
                          <th>FULL NAME</th>
                          <th>DEPARTMENT</th>
                          <th>STATUS</th>
                          <!--<th>E-MAIL</th>-->
                          <th>SESSION</th>
                          <th>ADMITTED DATE</th>
                          <th>ACTION</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php

                          $list = EDITING:: get_admitted_students();
                          $count = 0;
                          if ($list)
                          {
                            foreach ($list as $rows) {
                              $count +=1;
                              $formno = $rows->formno;
                              $surname = $rows->names;
                              $dept = $rows->department;
                              $sta = $rows->status;
                            //   $email = $rows->email;
                              $ses = $rows->session;
                              $date = $rows->admitted_date;
                              
                              if (substr($formno,0,2) == 'PU')
                                { }else{
                        ?>

                        <tr>

                        	<td align="center"><?php echo $count ?></a></td>
                          <td align="center"><?php echo $formno ?></td>
                          <td align="center"><?php echo $surname ?></td>
                          <td align="center"><?php echo $dept ?></td>
                           <td align="center"><?php echo $sta ?></td>
                          <!--<td align="center"><?php //echo $email ?></td>-->
                          <td align="center"><?php echo $ses ?></td>
                          <td align="center"><?php echo $date ?></td>
                            <td><input type="button" name="view" value="view" id="<?php echo $formno; ?>" class="btn btn-info btn-xs view_data" /></td>  
                        </tr>                        
                        <?php } } } ?>
                      </tbody>
                      
                        <tr class="info">
                        	<th>S/N</th>  
                          <th>FORM NO</th>
                          <th>FULL NAME</th>
                          <th>DEPARTMENT</th>
                          <th>STATUS</th>
                          <!--<th>E-MAIL</th>-->
                          <th>SESSION</th>
                          <th>ADMITTED DATE</th>
                          <th>ACTION</th>
                        </tr>
                    </table>
                </div>
              </fieldset>
          </div>


</div>
    </div>
  </div>
</form>
</div>

 <!-- this modal is for UPDATES -->
 <div class="modal fade" id="edit" role="dialog">
        <div class="modal-dialog">
          <!-- Modal content -->
          <div class="modal-content">
            <div class="modal-body" id ="details"  style="padding:10px 50px;">

            </div>
          </div>
      </div>
  </div>


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    
        <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
        <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.flash.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script>
        <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>

<!-- FOR DataTable -->
<script>
  {
    $('#myTable').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        'copy', 'csv', 'excel', 'pdf', 'print'
        // extend:'print',
            // autoPrint: true,
            //     title: '2019/2020 ADIMISSION LIST',
    ]
});
  }
</script>
    
<!-- FOR Modal -->
<script>  
 $(document).ready(function(){  
      $('.view_data').click(function(){  
           var formno = $(this).attr("id");  
           $.ajax({  
                url:"select.php",  
                method:"post",  
                data:{formno:formno},  
                success:function(data){  
                     $('#details').html(data);  
                     $('#edit').modal("show");  
                }  
           });  
      });  
 });  
</script>
    
  </body>
</html>