<link rel="stylesheet" href="../css/sweet-alert.css" /> 


<?php
include("../connection.php");
include("../user.php");
$cur_date=date('Y-m-d H:i:s');
$user=$_SESSION['user'];
$branch_id=$_SESSION['branch_id'];
$ho_access=$_SESSION['ha'];
$row=mysqli_fetch_array(mysqli_query($con,"SELECT id,file_name,file_path FROM admin_upload_file_master where id='1'"));
	$id=$row['id'];
?>


	<?php
	if(isset($_POST["submit"]))
	{
			
				  $filename=$_FILES["file"]["tmp_name"];
				  $branch=$_REQUEST['branch'];
				 
						$file = fopen($filename, "r");
					 //$file;
					$heading=true;
						$sno_query=mysqli_fetch_array(mysqli_query($con,"SELECT distinct sno  FROM `emp_bulk_sucess_data` order by sno desc limit 0,1"));
						 $sno=$sno_query['sno']+1;
						while (($emapData = fgetcsv($file, 1024)))
						{
							
							if($heading) 
							{
								
								 $heading = false;
								
								continue;
							}
							//It wiil insert a row to our subject table from our csv file`
						
							if($id==1)
							{ 
					
				
					$var = $emapData[3];
					$date = str_replace('/', '-', $var);
					 $dob_date= date('Y-m-d', strtotime($date));
					
					
					$tabup=mysqli_query($con,"SELECT * FROM `employee_master` where emp_no='$emapData[0]' and dob='$dob_date' and employee_name='$emapData[1]'");
						
					$rs=mysqli_num_rows($tabup);
					
					if($rs==0)
					{ 
						if($emapData[0]!=='' && $emapData[1]!=='' && $emapData[2]!=='' && $emapData[3]!=='' && $emapData[4]!=='' && $emapData[5]!=='' && $emapData[6]!=='' && $emapData[7]!=='' && $emapData[8]!=='' && $emapData[9]!=='' && $emapData[10]!=='')
							{
									$sql = mysqli_query($con,"INSERT INTO `employee_master`(`emp_no`, `employee_name`, `fathers_name`, `dob`, `department`, `gender`, `mobile`, `adhar_no`, `blood_group`, `mail_id`, `address`,`status`,`branch_id`,`created_on`, `created_by`) VALUES  ('$emapData[0]','$emapData[1]','$emapData[2]','$dob_date','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]','$emapData[9]','$emapData[10]','0','$branch','$cur_date','$user')");
								
								//echo "INSERT INTO `employee_master`(`emp_no`, `employee_name`, `fathers_name`, `dob`, `department`, `gender`, `mobile`, `adhar_no`, `blood_group`, `mail_id`, `address`,`status`,`created_on`, `created_by`) VALUES  ('$emapData[0]','$emapData[1]','$emapData[2]','$dob_date','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]','$emapData[9]','$emapData[10]','0','$cur_date','$user')";
									
								
								$success_data=mysqli_query($con,"INSERT INTO `emp_bulk_sucess_data`(`sno`,`branch_id`,`emp_no`, `employee_name`, `dob`, `department`, `reason`, `status`, `created_on`,`created_by`)values('$sno','$branch','$emapData[0]','$emapData[1]','$dob_date','$emapData[4]','EMPLOYEE CREATED','0','$cur_date','$user')");
								
								//echo "INSERT INTO `emp_bulk_sucess_data`(`sno`,`emp_no`, `employee_name`, `dob`, `department`, `reason`, `status`, `created_on`,`created_by`)values('$sno','$emapData[0]','$emapData[1]','$dob_date','$emapData[4]','DONE','0','$cur_date','$user')";
							}
							else
							{
						$reason2="REQUIRED FIELD MISSING".'<br>';
									$success_data=mysqli_query($con,"INSERT INTO `emp_bulk_sucess_data`(`sno`,`branch_id`,`emp_no`, `employee_name`, `dob`,  `department`, `reason`, `status`, `created_on`,`created_by`)values('$sno','$branch','$emapData[0]','$emapData[1]','$dob_date','$emapData[4]','$reason2','0','$cur_date','$user')");
						
								//echo "INSERT INTO `emp_bulk_sucess_data`(`sno`,`emp_no`, `employee_name`, `dob`,  `department`, `reason`, `status`, `created_on`,`created_by`)values('$sno','$emapData[0]','$emapData[1]','$dob_date','$emapData[4]','$reason2','0','$cur_date','$user')";
							
								}
					
					}
					else
					{
					 $fetch=mysqli_fetch_array($tabup);
					
					$reason1="Employee is already exist for that same details".'<br>';
					
					$success_data=mysqli_query($con,"INSERT INTO `emp_bulk_sucess_data`(`sno`,`branch_id`,`emp_no`, `employee_name`, `dob`,  `department`, `reason`, `status`, `created_on`,`created_by`)values('$sno','$branch','$emapData[0]','$emapData[1]','$dob_date','$emapData[4]','$reason1','0','$cur_date','$user')");
					
				//	echo "INSERT INTO `emp_bulk_sucess_data`(`sno`,`emp_no`, `employee_name`, `dob`,  `department`, `reason`, `status`, `created_on`,`created_by`)values('$sno','$emapData[0]','$emapData[1]','$dob_date','$emapData[4]','$reason1','0','$cur_date','$user')";
					} 	
							}	
							
							
						}
			fclose($file);
		$report=mysqli_fetch_array(mysqli_query($con,"select count(sno) as cnt from emp_bulk_sucess_data where sno='$sno' and status='0'"));
		$error_report=mysqli_fetch_array(mysqli_query($con,"select count(sno) as cnt from emp_bulk_sucess_data where sno='$sno' and status='1'"));
	}
	?>
<script>
		
    setTimeout(function() {
		var suc_data="<?php echo $report['cnt']; ?>";
		var err_data="<?php echo $error_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 = "employee_bulk_report.php?sno="+upload_sno;
        });
    }, 100);
	
	
</script> 
<?php
?>

	
  <script src="../js/sweet-alert.js"></script>