<?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'];
    $measurement = mysqli_fetch_all(mysqli_query($con,"SELECT id,name FROM measurement_master"));
    $row=mysqli_fetch_array(mysqli_query($con,"SELECT id,file_name,file_path FROM admin_upload_file_master where id='2'"));
	$id=$row['id'];
	if(isset($_POST["submit"]))
	{
		$filename=$_FILES["file"]["tmp_name"];
		$file = fopen($filename, "r");
		$heading=true;
		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==2)
			{ 
			    if($emapData[1]!=='')
				{
				    $key = array_search(strtoupper($emapData[4]), array_column($measurement, '1'));
				    if($key == ''){
				        $key = array_search(strtoupper($emapData[4]), array_column($measurement, '1'));
				    }
				    if($key != ''){
				        $measure = $measurement[$key];
				        $measure = $measure[0];
				        $sql = mysqli_query($con,"INSERT INTO category_master(category_id,category_type,category_brand,category_description,measurement,availability,amount,total_amount,status,created_on,created_by)VALUES('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$measure','$emapData[5]','$emapData[6]','$emapData[7]',0,now(),'$user')");
				    }
				}
			}
	    }	
		 fclose($file);
	}
?>
<script>
	setTimeout(function() {
		    swal({
            title: "Bulk Upload Log",
            text: "Uploaded",
            type: "success"
        }, function() {
			window.location = "category_master.php";
        });
    }, 100);	
</script> 
<?php
?>
<script src="../js/sweet-alert.js"></script>