<?php

include 'load.php';

if(isset($_POST['save']))

{		

		$catename = $_POST['catename'];
		$status = $_POST['status'];
		
		$imgFile = $_FILES['user_image']['name'];
		$tmp_dir = $_FILES['user_image']['tmp_name'];
		$imgSize = $_FILES['user_image']['size'];
		
		if(empty($catename)){
			$errMSG = "Please Enter Category Name.";
		}
		else if(empty($status)){
			$errMSG = "Please Select Active Or Deactive.";
		}
		{
			$upload_dir = 'images/category/'; // upload directory
	
			$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
		
			// valid image extensions
			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif' ,'docx' , 'pdf' , 'video' , 'mp3' ); // valid extensions
		
			// rename uploading image
			$userpic = rand(1,1000000).".".$imgExt;
				
			// allow valid image file formats
			if(in_array($imgExt, $valid_extensions)){			
				// investck file size '100MB'
				if($imgSize < 100000000)				{
					move_uploaded_file($tmp_dir,$upload_dir.$userpic);
				}
				else{
					$errMSG = "Sorry, your file is too large.";
				}
			}
			else{
				$errMSG = "Sorry, only JPG, JPEG, PNG & GIF , DOCX & PDF files are allowed.";		
			}
		}
		// if no error occured, continue ....

		// if no error occured, continue ....

		if(!isset($errMSG))

		{

			$stmt = $DB_con->prepare('INSERT INTO mobilecategory(catename,image,status) VALUES(:catename,:upic,:status)');
			$stmt->bindParam(':catename',$catename);
			$stmt->bindParam(':upic',$userpic);
			$stmt->bindParam(':status',$status);
			if($stmt->execute())

			{

				$successMSG = "Successfully! Add Your Category....";

				header("refresh:2;category.php"); // redirects image view page after 5 seconds.

			}

			else

			{

				$errMSG = "error while inserting....";

			}

		}

	}

?>

<!doctype html>
<html lang="en">
	<head>
		<!-- Required meta tags -->
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

		<!-- Title -->
		<title>Add Category | Eat More Resturant   | Pos & Invetory System | Dashboard

</title>

		<?php
		require_once 'headercss.php';
	    ?>

	</head>

	<body>

		<!-- Loading starts -->
		<div id="loading-wrapper">
			<div class="spinner-border" role="status">
				<span class="sr-only">Loading...</span>
			</div>
		</div>
		<!-- Loading ends -->

		<!-- Page wrapper start -->
		<div class="page-wrapper">
			
			<?php
			require_once 'navbar.php';
			?>

			<!-- Page content start  -->
			<div class="page-content">

				<?php
				require_once 'header.php';
				?>

				<!-- Page header start -->
				<div class="page-header">
					<ol class="breadcrumb">
						<li class="breadcrumb-item">Home</li>
						<li class="breadcrumb-item active">Add Category</li>
					</ol>

					<ul class="app-actions">
						<li>
							<a href="#" id="reportrange">
								<span class="range-text"></span>
								<i class="icon-chevron-down"></i>	
							</a>
						</li>
						<li>
							<a href="#">
								<i class="icon-export"></i>
							</a>
						</li>
					</ul>
				</div>
				<!-- Page header end -->

				<!-- Main container start -->
				<div class="main-container">

					<!-- Row start -->
					<div class="row gutters">
						<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
							<div class="card h-100">
								<div class="card-header">
									<div class="card-title">Add New Category</div>
								</div>
								<div class="card-body">
									<form method="POST" enctype="multipart/form-data">
									<?php

									if(isset($errMSG)){

									?>
			
									<div class="alert alert-danger alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Error!</strong> <?php echo $errMSG; ?>
									</div>

									<?php

									}
	
									else if(isset($successMSG)){

									?>
			
									<div class="alert alert-success alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Success!</strong> <?php echo $successMSG; ?>.
									</div>

									<?php

									}

									?>
									<div class="row gutters">
										<div class="col-xl-4 col-lg-6 col-md-6 col-sm-6 col-12">
											<div class="form-group">
												<label for="fullName">Category Name</label>
												<input type="text" name="catename" class="form-control" placeholder="Enter Category Name">
											</div>
										</div>
										<div class="col-xl-4 col-lg-6 col-md-6 col-sm-6 col-12">
										<div class="form-group">
										<label for="user_image">Image</label><br>
										<label for="user_image"><img src="assets/images/empty.png" id="yourImgTag" class="pro_pick"></label>
												<input type="file" name="user_image" id="user_image" hidden="yes" onChange="dis_name();">
												<script>
												function dis_name(){
												var input = document.getElementById("user_image");
												var fReader = new FileReader();
												fReader.readAsDataURL(input.files[0]);
												fReader.onloadend = function(event){
												var img = document.getElementById("yourImgTag");
												img.src = event.target.result;
												}
												}
												</script>
										</div>
										</div>
										<div class="col-xl-4 col-lg-6 col-md-6 col-sm-6 col-12">
											<div class="form-group">
												<label for="status">Status</label>
												<select class="form-control" name="status" required>
												<option value="1">Active</option>
												<option value="0">Deactive</option>
												</select>
											</div>
										</div>
										<div class="col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
											<div class="text-right">
												<a href="category.php" class="btn btn-white">Cancel</a>
												<button type="submit" name="save" class="btn btn-primary">Save</button>
											</div>
										</div>
									
									</div>
									</form>
								</div>
							</div>
						</div>
					</div>
					<!-- Row end -->

				</div>
				<!-- Main container end -->

			</div>
			<!-- Page content end -->

		</div>
		<!-- Page wrapper end -->

		<?php
		require_once 'footerjs.php';
	    ?>

	</body>
</html>