<?php
session_start(); 
include ("dbinfo.php");   
$error=''; 

//--------------------------------------- add swatch ----------------------------------
if (isset($_POST['btn_ssubmit'])) 
{
	$sid=$_POST['txt_sid'];
	$snumber=$_POST['txt_snumber'];
	$sname=$_POST['txt_sname'];
	$scategory=$_POST['txt_scategory'];
	$scolor=$_POST['txt_scolor'];
	$scolor2=$_POST['txt_scolor2'];
	$sfabric=$_POST['txt_sfabric'];
	$spattern=$_POST['txt_spattern'];
	$sgsm=$_POST['txt_sgsm'];
	$smfg=$_POST['txt_smfg'];
	$sdesc=$_POST['txt_sdesc'];
	$swimage=$_FILES['swatchfile']['tmp_name'];

		$target_dir = "images/swatchfile/";
		$target_file = $target_dir . rand(10000, 1000000) . basename($_FILES["swatchfile"]["name"]);
		$uploadOk = 1;
		$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

		// Check file size
		if($_FILES["swatchfile"]["size"] > 500000 && $uploadOk == 1) 
		{
			echo "Sorry, your file is too large." ;
			$uploadOk = 0;
		} 
		else {
			$uploadOk = 1;
		}

		// Check if $uploadOk is set to 0 by an error
		if ($uploadOk == 0) { } 
		else
		{		
			move_uploaded_file($_FILES["swatchfile"]["tmp_name"], $target_file);
			
				if($sid=='')
				{   
					$query=mysqli_query($conn,"INSERT INTO `swatch`(`snumber`, `sname`, `scategory`, `scolor`, `scolor2`, `sfabric`, `spattern`, `sgsm`, `smfg`, `sdescription`, `simage`) VALUES('$snumber','$sname','$scategory','$scolor','$scolor2','$sfabric','$spattern','$sgsm','$smfg','$sdesc','$target_file')")or die(mysqli_error($conn));
					
					if($query==true)
					{
						header('location:swatch_add.php?status=1');
					}
					else
					{
						header('location:swatch_add.php?status=3');
					}
				}
				else
				{ 
					header("location:swatch_add.php?status=3");
				}
			
		}
	
}
//--------------------------------------- update swatch ----------------------------------
if (isset($_POST['btn_supdate'])) 
{
	$sid=$_POST['txt_sid'];
	$snumber=$_POST['txt_snumber'];
	$sname=$_POST['txt_sname'];
	$scategory=$_POST['txt_scategory'];
	$scolor=$_POST['txt_scolor'];
	$scolor2=$_POST['txt_scolor2'];
	$sfabric=$_POST['txt_sfabric'];
	$spattern=$_POST['txt_spattern'];
	$sgsm=$_POST['txt_sgsm'];
	$smfg=$_POST['txt_smfg'];
	$sdesc=$_POST['txt_sdesc'];
	//$simage=$_POST['swatchfile'];
	$imgpath=$_POST['txt_imgpath'];
	$swimage=$_FILES['swatchfile']['tmp_name'];

		$target_dir = "images/swatchfile/";
		$target_file = $target_dir . rand(10000, 1000000) . basename($_FILES["swatchfile"]["name"]);
		$uploadOk = 1;
		$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

		// Check file size
		if($_FILES["swatchfile"]["size"] > 500000 && $uploadOk == 1) 
		{
			echo "Sorry, your file is too large." ;
			$uploadOk = 0;
		} 
		else {
			$uploadOk = 1;
		}

		// Check if $uploadOk is set to 0 by an error
		if ($uploadOk == 0) { } 
		else
		{		
			move_uploaded_file($_FILES["swatchfile"]["tmp_name"], $target_file);
			
				if($sid!='')
				{   
					$type= (explode(".",$target_file));
					$chtype= $type[1];

					if($chtype != "jpg" && $chtype != "png" && $chtype != "jpeg")
					//if(isset($swimage))
					{
						$query=mysqli_query($conn,"UPDATE `swatch` SET `sname`='$sname', `scategory`='$scategory', `scolor`='$scolor', `scolor2`='$scolor2', `sfabric`='$sfabric', `spattern`='$spattern', `sgsm`='$sgsm', `smfg`='$smfg', `sdescription`='$sdesc' WHERE `sid`='$sid'")or die(mysqli_error($conn));
					}
					else{
						$query=mysqli_query($conn,"UPDATE `swatch` SET `sname`='$sname', `scategory`='$scategory', `scolor`='$scolor', `scolor2`='$scolor2', `sfabric`='$sfabric', `spattern`='$spattern', `sgsm`='$sgsm', `smfg`='$smfg', `sdescription`='$sdesc', `simage`='$target_file' WHERE `sid`='$sid'")or die(mysqli_error($conn));
					}
					
					if($query==true)
					{
					header('location:swatch_add.php?status=1');
					}
					else
					{
					header('location:swatch_add.php?status=3');
					}
				}
				else
				{ 
					header("location:swatch_add.php?status=3");
				}
			
		}
		
		
		
		
		
		
	
}
?>