<?php
include("../connection.php");
include("../user.php");
?>
<link rel="stylesheet" href="../css/sweet-alert.css" /> 
<?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='4'"));
	$id=$row['id'];
	$locations = mysqli_fetch_all(mysqli_query($con,"SELECT id,location FROM location_master WHERE status = 0"));
	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==4)
			{ 
			    if($emapData[0]!=='' && $emapData[1]!=='')
				{
				    $isloc = array_search($emapData[2], array_column($locations, '1'));
				    if($isloc == ''){
				        $isloc = array_search(strtoupper($emapData[2]), array_column($locations, '1'));
				    }
				    if($isloc != ''){
				        $loct = $measurement[$isloc];
				        $locat = $loct[0];
    				$sql = mysqli_query($con,"INSERT INTO asset_master(description,outgoingto,location,make,model,serialno,capacity,equipment_id,maintenance,status)VALUES('$emapData[0]','$emapData[1]','$locat','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]',0)");
				}
			}
		}	
		fclose($file);
    }
?>
<script>
	setTimeout(function() {
		    swal({
            title: "Bulk Upload Log",
            text: "Uploaded",
            type: "success"
        }, function() {
			window.location = "asset_master.php";
        });
    }, 100);	
</script> 
<script src="../js/sweet-alert.js"></script>