<?php 
include 'load.php';


if(isset($_FILES)){
	
		$filename=$_FILES["file"]["tmp_name"];
	
		//echo json_encode(array('code' => 200, 'text' => 'file available:' . $_FILES["file"]["name"],));
	
		 if($_FILES["file"]["size"] > 0){

			 $file = fopen($filename, "r"); 
			 
			 $wrong = ""; $error = ""; $update = "Updated Products:"; 
			 
			 while (($csvData = fgetcsv($file, 10000, ",")) !== FALSE){
				 
				//echo $csvData[0]
				 
				if(ctype_digit($csvData[0]) && ctype_digit($csvData[4])){
					
					$stmt = $DB_con->prepare('SELECT IFNULL(MAX(prid)+1,1) AS prid FROM `mobileproducts2`');								
					$stmt->execute();								
					while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
						extract($row);			
						$proiductid = "0000$row[prid]";
					}
					
					$e0 = str_replace(",","",$csvData[0]); $e1 = str_replace(",","",$csvData[1]); 
					$e2 = str_replace(",","",$csvData[2]); $e3 = str_replace(",","",$csvData[3]); 
					$e4 = str_replace(",","",$csvData[4]); $e5 = str_replace(",","",$csvData[5]); 
					$e6 = str_replace(",","",$csvData[6]); $e7 = str_replace(",","",$csvData[7]); 
					$e8 = str_replace(",","",$csvData[8]); $e9 = str_replace(",","",$csvData[9]); 
					$e10 = str_replace(",","",$csvData[10]); $e11 = str_replace(",","",$csvData[11]); 
					$e12 = str_replace(",","",$csvData[12]); $e13 = str_replace(",","",$csvData[13]); 
					$e14 = str_replace(",","",$csvData[14]); 
						
					if(empty($e0)){ $e0 = $proiductid;}if(empty($e1)){ $e1 = 0;} if(empty($e2)){ $e2 = 0;} if(empty($e3)){ $e3 = 0;}
					if(empty($e4)){ $e4 = 0;} if(empty($e5)){ $e5 = 0;} if(empty($e6)){ $e6 = 0;} if(empty($e7)){ $e7 = 0;}
					if(empty($e8)){ $e8 = 0;} if(empty($e9)){ $e9 = 0;} if(empty($e10)){ $e10 = 0;} if(empty($e11)){ $e11 = 0;}
					if(empty($e12)){ $e12 = 0;} if(empty($e13)){ $e13 = 0;} if(empty($e14)){ $e14 = 0;} 
					
					$query = mysqli_query($conn, "SELECT prcode FROM mobileproducts2 WHERE prcode='$e0'");
					$result = mysqli_fetch_array($query);

				if(!empty($result)){

					if( $e0 == $result['prcode'] ){
						
						mysqli_query($conn,"UPDATE clients SET prcode='$e0', proname='$e1', brandname='$e2', category='$e3', unit='$e4', mqty='$e5', qty='$e6', discount='$e7', price='$e8', opdprice='$e9', mobileprice='$e10', prousage='$e11', cover='', status='$e14' WHERE prcode='$e0'");
						
						$update .= $update.",";
						
						if(mysqli_error($conn)){
							$error .= $error.$e0.mysqli_error($conn).", ";
						}
					}
						

				}else{

					$stmt = $DB_con->prepare('INSERT INTO mobileproducts2(prcode,proname,brandname,category,unit,mqty,qty,discount,price,opdprice,mobileprice,cover,status)VALUES(:prcode,:proname,:brandname,:category,:unit,:mqty,:qty,:discount,:price,:opdprice,:mobileprice,:prousage,:upic,:status)');
					$stmt->bindParam(':prcode',$e0);
					$stmt->bindParam(':proname',$e1);
					$stmt->bindParam(':brandname',$e2);
					$stmt->bindParam(':category',$e3);
					$stmt->bindParam(':unit',$e4);
					$stmt->bindParam(':mqty',$e5);
					$stmt->bindParam(':qty',$e6);
					$stmt->bindParam(':discount',$e7);
					$stmt->bindParam(':price',$e8);
					$stmt->bindParam(':opdprice',$e9);
					$stmt->bindParam(':mobileprice',$e10);
					$stmt->bindParam(':prousage',$e11);
					$stmt->bindParam(':upic',$e12);
					$stmt->bindParam(':status',$e14);
						
					if($stmt->execute()){

						$successMSG = "Successfully! Add Your Products....";
						
					}else{
						
						$error .= $error.mysqli_error($conn);

					}

				}
					
			 	}else{
					$wrong .= $wrong.$csvData[1].", ";
				}
	         }
			 
	         fclose($file);
			 
			 echo json_encode(array('code' => 200, 'title' => "Import result", 'text' => $successMSG.$update.$wrong.$error,));
			 
		 }else{
			 
			 echo json_encode(array('code' => 201, 'title' => 'Empty File', 'text' => 'No file found. Please upload a file',));
		 }
	
	}else{
	
	echo json_encode(array('code' => 202, 'title' => 'Empty File', 'text' => 'No file format available. Please upload a file',));
}

?>