<?php
session_start();
error_reporting(0);
$user_name=$_SESSION['user_name'];
if(empty($user_name))
 {
 header("location:index.php");
 }

include("database_connection.php"); 
$sqlu="select id,zone from user_tblsbifi where user_name='$user_name' " ;
$resultu=mysqli_query($conn,$sqlu);
$datau=mysqli_fetch_array($resultu);
$idu=$datau['id'];
$zone=$datau['zone'];
set_time_limit(500);

if(isset($_POST['submit'])) {

	if($_FILES['upload_csv']['type'] == 'text/csv' or $_FILES['upload_csv']['type'] == 'application/vnd.ms-excel'){

		move_uploaded_file($_FILES['upload_csv']['tmp_name'],'uploads/'.$_FILES['upload_csv']['name']);

		$row = 1;
		if (($handle = fopen("./uploads/".$_FILES['upload_csv']['name'], "r")) !== FALSE) {
			while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
				$num = count($data);
	
				$row++;				
				for ($c=0; $c < $num; $c++) {

				     if($c == 0) {$case_id = $data[$c]; } 
				     if($c == 1) {$loan_type = $data[$c]; }				     
				     if($c == 2) {$loan_amount = $data[$c]; } 
				     if($c == 3) {$appco_applicant = $data[$c]; } 
				     if($c == 4) {$type = $data[$c]; }				     
				     if($c == 5) {$name = $data[$c]; } 
				     if($c == 6) {$address1 = $data[$c]; }
				     if($c == 7) {$pincode = $data[$c]; } 				    
				     if($c == 8) {$mobile_no = $data[$c]; }				    
				     if($c == 9) {$alt_no = $data[$c]; }
					 if($c == 10) {$company_name = $data[$c]; }	
				     if($c == 11) {$additional_information = $data[$c]; }	
					$status  =1;	
					 $entry_by=$_SESSION['user_name'];
					 $dump_date=date('m-d-Y');
					 $entry_date=date('d-m-Y');
					 date_default_timezone_set('Asia/Calcutta');	
					 $entry_time=date('h:i A');
					 $month=date('M-Y');
					 $promo_code=$idu;
					 $zone=$datau['zone'];
							
					}		 
				$insertQueryftr = 'insert into sbialloc_tbl (case_id,loan_type,loan_amount,
						   appco_applicant,type,name,address1,pincode,mobile_no,alt_no,company_name,status,
						    dump_date,month,entry_by,entry_date,entry_time,case_status,additional_information,promo_code,zone)               
		values("'.$case_id.'","'.$loan_type.'","'.$loan_amount.'","'.$appco_applicant.'",
				"'.$type.'","'.$name.'","'.$address1.'","'.$pincode.'","'.$mobile_no.'","'.$alt_no.'","'.$company_name.'","'.$status.'",
				"'.$dump_date.'", "'.$month.'","'.$entry_by.'","'.$entry_date.'",
				"'.$entry_time.'","'.$case_status.'","'.$additional_information.'","'.$promo_code.'","'.$zone.'")';
	 $result1 = mysqli_query($conn,$insertQueryftr);
				
				if(!$result1) {
					echo mysqli_error();
					
				}
			}
			
			fclose($handle);
		}
		

	}
	
	$sqlft="DELETE FROM `sbialloc_tbl` WHERE `case_id` ='Reference No' ";
	$resultft = mysqli_query($conn,$sqlft); 
	
	unlink('uploads/'.$_FILES['upload_csv']['name']);


			echo "<script type='text/javascript'>";
		echo "alert('CASE UPLOADED SUCCESSFULLY ');";
		echo "</script>";
		echo "<script type='text/javascript'>";
		echo "window.location='home.php?Bankers=SearchSBICase'";
		echo "</script>";	
		
}


?>
<html>
<title>Upload:records</title>
<head>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

 <link type="text/css" href="menu/menu_admin.css" rel="stylesheet" />

  <link type="text/css" href="menu/menu_admin.css" rel="stylesheet" />
  </head>
<body>
<div class="background">
<form name='uploader' action='' method='post' enctype='multipart/form-data'>

<?php
	include("header2.php");
	if(($_SESSION['unique_id']=='B'))
	{
	include("menu/menu_bank.php");
	}	
	else
	{
	header("location:index.php");
	}
	?>
<table style="width:750px;" align="center">
<tr >
<td bgcolor="#008CBA" align="center" style="width:750px;height:35px;" ><font color="white" font size="6px;"><b>Upload New Case</b>  &nbsp;&nbsp;</font></td>
</tr>
<tr>
<td height="25">&nbsp;</td>
</tr>
</table>
<table style="width:750px;" align="center"  class="table table-bordered"   >
<tr>
<td><input type='file' name='upload_csv' value=''>
</td>
<td>
<input type='submit' name='submit' value='submit'>
</td>
</tr>
</table>

<table style="width:750px;" align="center"  class="table table-bordered"   >
<tr>
<td height="25">&nbsp;</td>
</tr>
<tr>
<td><a href="export_uploadformat.php"><b>Download Upload Format</b></a></td>
</tr>
</table>

</form>
</div>
</div>
</div>
</body>
</html>
