<?php
require ("../connect.php");
require ("../user.php");
?>
<?php
$user=$_SESSION['user'];
$ipaddress= $_SERVER['REMOTE_ADDR'];  
//echo $_SESSION['expire'];
	$branch_id=$_SESSION['branch_id'];
	$cur_date=date("Y-m-d");
	//$smc=$_REQUEST['smc'];
$row=mysql_fetch_array(mysql_query("SELECT id,file_name,file_path FROM admin_upload_file_master where id='2' "));
$pathname=$row['file_path'];
	$id=$row['id'];
$pathname="/professional/".$pathname;
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="icon" href="logo.png" sizes="50x50" type="image/png">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Professional</title>

         <link href="../css/bootstrap.min.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="../css/metisMenu.min.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="../css/startmin.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="../css/font-awesome.min.css" rel="stylesheet" type="text/css">


        
        <!-- DataTables CSS -->
        <link href="../css/dataTables/dataTables.bootstrap.css" rel="stylesheet">

        <!-- DataTables Responsive CSS -->
        <link href="../css/dataTables/dataTables.responsive.css" rel="stylesheet">

        <!-- Custom CSS -->
        <link href="../css/startmin.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="../css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="../css/sweet-alert.css" />  
        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
  <style>
.loader {
    position: fixed;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    z-index: 9999;
    background: url('../images/pageLoader.gif') 50% 50% no-repeat rgb(249,249,249);
    opacity: .8;
}

</style>
<div class="loader"></div>
<?php
require("header.php");
?>


        <div id="wrapper">

        

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                       <h4 class="page-header">Upload Student Details</h4>
						
						
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
				
                <!-- /.row -->
                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            
                                    	<div class="span6" id="form-login">
							<?php
								# error messages
								if (isset($message)) {
								foreach ($message as $msg) {
								printf("<p class='status'>%s</p></ br>\n", $msg);
								}
								}
								# success message
								/* if($count !=0){
								printf("<p class='status'>%d files added successfully!</p>\n", $count);
								} */
							?>
							
							<form class="form-horizontal well" action="" method="post" name="upload_excel" onsubmit="popup()" enctype="multipart/form-data" >
								
									<div class="control-group">
									
										
														<div class="col-md-12">
				<div class="form-group" style="text-align: center;width: 100%;font-size: 25px;text-transform: uppercase;color: #3c8dbc;">
					<a href="<?php echo "$pathname"?>">download template</a></p>
				</div>
			</div>
			<div class="col-md-12">
				<div class="form-group" style="text-align: center;">
					<div class="controls"  style="">
						<input type="file" name="file" id="file"  multiple="multiple" style="border: 2px solid gray; color: gray; background-color: #fff; padding: 8px 20px; border-radius: 8px; font-size: 20px; font-weight: bold;" accept=".csv" required >
						
					</div>
					<div class="controls"  style="float:left;">
					<b>Note:</b><span style="color:red;">Choose CSV Format Only</span>
					</div>
				</div>
			</div>
										<br>
										<div id="change"></div>
										<div id="events"></div>
										<div class="control-label"><label></label></div>

									<div class="control-group" style="margin-top:20px; margin-left:120px">
										<div class="form-group"><button type="submit" id="submit" name="Import"  class="btn btn-success"   data-loading-text="Loading..."> Upload</button>
										 
									</div>
									
								</div>
							</form>
						</div>
						
					<?php
			if(isset($_POST["Import"]))
			{
				
				 $filename=$_FILES["file"]["tmp_name"];	
						$file = fopen($filename, "r");
						$heading=true;
						$sno_query=mysql_fetch_array(mysql_query("SELECT distinct sno  FROM `success_data` order by sno desc limit 0,1"));
						 $sno=$sno_query['sno']+1;
						while (($emapData = fgetcsv($file)) !== FALSE)
						{
							
							if($heading) 
							{
								
								$heading = false;
								
								continue;
							}
							//It wiil insert a row to our subject table from our csv file`
						
							if($id==2)
							{ 
						
					
					$var = $emapData[4];
					$date = str_replace('/', '-', $var);
					 $dob_date= date('Y-m-d', strtotime($date));
					
					$prj_id=mysql_fetch_array(mysql_query("SELECT id FROM `project_type` where project_type='$emapData[0]'"));
					$p_id=$prj_id['id'];
					$f_id=mysql_fetch_array(mysql_query("SELECT id FROM `financial_year` where financial_year='$emapData[1]'"));
					$fy_id=$f_id['id'];
					//$batch_id=mysql_fetch_array(mysql_query("SELECT batch_id FROM `batch_master` where batch_no='$emapData[2]'"));
					//echo "SELECT batch_id FROM `batch_master` where batch_no='$emapData[2]'";
					// $bat_id=$batch_id['batch_id'];
					$caste_id=mysql_fetch_array(mysql_query("SELECT id FROM `caste_master` where caste='$emapData[14]'"));
					$c_id=$caste_id['id'];
					$edu_id=mysql_fetch_array(mysql_query("SELECT id FROM `educational_master` where educational='$emapData[15]'"));
					$e_id=$edu_id['id'];
					$blood_id=mysql_fetch_array(mysql_query("SELECT id FROM `blood_grp_master` where blood_group='$emapData[16]'"));
					$b_id=$blood_id['id'];
					
					$cntr=mysql_fetch_array(mysql_query("SELECT id FROM `branch_master` where branch_name='$emapData[21]'"));
					$cntr_id=$cntr['id'];
					
					$tabup=mysql_query("SELECT * FROM `batch_stu_reg` where c_name='$emapData[3]' and dob='$dob_date' and project_type='$p_id' and center='$cntr_id' and financial_year='$fy_id'");
					//echo "SELECT * FROM `batch_stu_reg` where c_name='$emapData[0]' and dob='$dob_date'";
					 $rs=mysql_num_rows($tabup);
					
					if($rs==0)
					{
					
						$batch_no_val=mysql_query("select * from batch_master where financial_year='$fy_id' and batch_no='$emapData[2]' and project_type='$p_id' and center='$cntr_id'");
						//echo "select * from batch_master where financial_year='$fy_id' and batch_no='$emapData[2]' and project_type='$p_id'";
						$batch_val=mysql_num_rows($batch_no_val);
						
						if($batch_val>0)
						{
							if($emapData[0]!=='' && $emapData[1]!=='' && $emapData[2]!=='' && $emapData[3]!=='' && $emapData[4]!=='' && $emapData[5]!=='' && $emapData[7]!=='' && $emapData[8]!=='' && $emapData[10]!=='' && $emapData[11]!=='' && $emapData[12]!=='' && $emapData[13]!=='' && $emapData[14]!=='' && $emapData[15]!=='' && $emapData[16]!=='' && $emapData[17]!==''  && $emapData[21]!=='')
							{
								$sql = mysql_query("INSERT INTO `batch_stu_reg`(`c_name`, `dob`, `father_name`, `mother_name`, `gender`, `marital_sts`, `spouse_name`, `contact_no`, `permanent_addr`, `present_addr`, `religion`, `caste`, `edu_qualify`, `blood`, `aadhar_no`, `voter_id`, `emg_no`, `driving_license_no`, `financial_year`, `batch_no`, `created_by`, `created_on`, `modified_by`, `modified_on`, `project_type`, `center`) VALUES  ('$emapData[3]','$dob_date','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]','$emapData[9]','$emapData[10]','$emapData[11]','$emapData[12]','$emapData[13]','$c_id','$e_id','$b_id','$emapData[17]','$emapData[18]','$emapData[19]','$emapData[20]','$fy_id','$emapData[2]','','','$user','$cur_date','$p_id','$cntr_id')");
								//echo "INSERT INTO `batch_stu_reg`(`c_name`, `dob`, `father_name`, `mother_name`, `gender`, `marital_sts`, `spouse_name`, `contact_no`, `permanent_addr`, `present_addr`, `religion`, `caste`, `edu_qualify`, `blood`, `aadhar_no`, `voter_id`, `emg_no`, `driving_license_no`, `financial_year`, `batch_no`, `created_by`, `created_on`, `modified_by`, `modified_on`, `project_type`, `center`) VALUES  ('$emapData[3]','$dob_date','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]','$emapData[9]','$emapData[10]','$emapData[11]','$emapData[12]','$emapData[13]','$c_id','$e_id','$b_id','$emapData[17]','$emapData[18]','$emapData[19]','$emapData[20]','$fy_id','$emapData[2]','','','$user','$cur_date','$p_id','$cntr_id')";
								
							//echo "done query"." ".$emapData[3].'<br>';	
				$success_data=mysql_query("INSERT INTO `success_data`(`sno`,`candidate_name`, `dob`, `project_type`, `batch_no`, `financial_year`, `center`, `created_by`, `created_on`,`status`,`reason`)values('$sno','$emapData[3]','$dob_date','$p_id','$emapData[2]','$fy_id','$cntr_id','$user','$cur_date','0','DONE')");
				//echo "INSERT INTO `success_data`(`sno`,`candidate_name`, `dob`, `project_type`, `batch_no`, `financial_year`, `center`, `created_by`, `created_on`,`status`,`reason`)values('$sno','$emapData[3]','$dob_date','$p_id','$emapData[2]','$fy_id','$branch_id','$user','$cur_date','0','DONE')";
								
						
							}
							else
							{
						$reason2="REQUIRED FIELD MISSING".'<br>';
						$success_data=mysql_query("INSERT INTO `success_data`(`sno`,`candidate_name`, `dob`, `project_type`, `batch_no`, `financial_year`, `center`, `created_by`, `created_on`,`status`,`reason`)values('$sno','$emapData[3]','$dob_date','$p_id','$emapData[2]','$fy_id','$cntr_id','$user','$cur_date','1','$reason2')");
							}
					}
					else
					{
						 $reason="Batch number and financial year not Mach for your center on Private/Govt".'<br>';
						$success_data=mysql_query("INSERT INTO `success_data`(`sno`,`candidate_name`, `dob`, `project_type`, `batch_no`, `financial_year`, `center`, `created_by`, `created_on`,`status`,`reason`)values('$sno','$emapData[3]','$dob_date','$p_id','$emapData[2]','$fy_id','$cntr_id','$user','$cur_date','1','$reason')");
						//echo "INSERT INTO `success_data`(`sno`,`candidate_name`, `dob`, `project_type`, `batch_no`, `financial_year`, `center`, `created_by`, `created_on`,`status`,`reason`)values('$sno','$emapData[3]','$dob_date','$p_id','$emapData[2]','$fy_id','$branch_id','$user','$cur_date','0','$reason')".'<br>';
					}			
					}
					else
					{
					$fetch=mysql_fetch_array($tabup);
					 $reason1="candidate is already exist for that same details".'<br>';
					
					$success_data=mysql_query("INSERT INTO `success_data`(`sno`,`candidate_name`, `dob`, `project_type`, `batch_no`, `financial_year`, `center`, `created_by`, `created_on`,`status`,`reason`)values('$sno','$emapData[3]','$dob_date','$p_id','$emapData[2]','$fy_id','$cntr_id','$user','$cur_date','1','$reason1')");
					//echo "INSERT INTO `success_data`(`sno`,`candidate_name`, `dob`, `project_type`, `batch_no`, `financial_year`, `center`, `created_by`, `created_on`,`status`,`reason`)values('$sno','$emapData[3]','$dob_date','$p_id','$emapData[2]','$fy_id','$branch_id','$user','$cur_date','1','$reason1')".'<br>';
					} 	
							}	
							
							
						}
						fclose($file);
						$report=mysql_fetch_array(mysql_query("select count(sno) as cnt from success_data where sno='$sno' and status='0'"));
						$erro_report=mysql_fetch_array(mysql_query("select count(sno) as cnt from success_data where sno='$sno' and status='1'"));
						
	?>
	<script>
		
    setTimeout(function() {
		var suc_data="<?php echo $report['cnt']; ?>";
		var err_data="<?php echo $erro_report['cnt']; ?>";
		var upload_sno="<?php echo $sno ?>";
		//alert(upload_sno);
        swal({
            title: "Bulk Upload Log",
            text: "Uploaded :"+suc_data+"\nNot Uploaded :"+err_data,
            type: "success"
        }, function() {
       window.location = "report_upload_private_batch.php?sno="+upload_sno;
        });
    }, 100);
	
	
</script> 


	<?php
				
			}
							
		?>
                                    </div>
                                    <!-- /.col-lg-6 (nested) -->

                                    <!-- /.col-lg-6 (nested) -->
                                </div>
                                <!-- /.row (nested) -->
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                    <!-- /.col-lg-12 -->
              

          <script src="../js/jquery.min.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="../js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="../js/metisMenu.min.js"></script>


        <!-- DataTables JavaScript -->
        <script src="../js/dataTables/jquery.dataTables.min.js"></script>
        <script src="../js/dataTables/dataTables.bootstrap.min.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="../js/startmin.js"></script>
<script src="../js/startmin.js"></script>
		
  <script src="../js/sweet-alert.js"></script>
        <!-- Page-Level Demo Scripts - Tables - Use for reference -->
        <script>
            $(document).ready(function() {
				$(".loader").fadeOut("slow");

                $('#dataTables-example').DataTable({
                        responsive: true
                });
            });
        </script>
		<script>
		function popup(){
			$(".loader").show();
		}
		</script>
<?php
require ("footer.php");
?>
    </body>
</html>



	  