<?php
ob_start();
session_start();

require_once 'includes.php';

require_once("conn.php");

require_once("config.php");

require_once 'dbconfig4.php';

if(isset($_SESSION['admin_user_id'])){

	$user_qury=mysqli_query($conn,"SELECT * FROM ormsusers WHERE user_id='$_SESSION[admin_user_id]'");

	$user_resalt=mysqli_fetch_array($user_qury);

	$customer = $user_resalt['user_id'];
	
	$adminbalance = number_format((float)$user_resalt['balance'],2);

	if($user_resalt['image']==""){

		$image_path="images/empty.jpg";

	}

	else{

		$image_path="../dashboard/images/admin/".$user_resalt['image'];

	}

}

$msg = '';

$msg5 = '';	

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

{

		$fullname = $_POST['fullname'];
		
		$user_name = $_POST['user_name'];
		
		$user_email = $_POST['user_email'];
		
		$user_pass = $_POST['user_pass'];

        $user_pass = password_hash($user_pass, PASSWORD_DEFAULT);
		
		$admintype = $_POST['admintype'];
		
		$admin = $_POST['admin'];
		
		$paydesk = $_POST['paydesk'];
		
		$category = $_POST['category'];
				
		$providers = $_POST['providers'];
		
		$services = $_POST['services'];
		
		$reload = $_POST['reload'];

		$datacard = $_POST['datacard'];
		
		$customers = $_POST['customers'];
		
		$reports = $_POST['reports'];

		$status = $_POST['status'];
		
		if(!$_FILES['image']['name']==""){
	
		if ($_FILES['image']['type']=="image/jpeg"){
              $imagename = time().$_FILES['image']['name'];
              $source = $_FILES['image']['tmp_name'];
              $target = "images/admin/".str_replace(" ","_",$imagename);
			  $db_send_name = str_replace(" ","_",$imagename);
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save = "images/admin/" . $imagepath; //This is the new file you saving
              $file = "images/admin/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ;

              $modwidth = 300;

              $diff = $width / $modwidth;

              $modheight = $height / $diff;
              $tn = imagecreatetruecolor($modwidth, $modheight) ;
              $image = imagecreatefromjpeg($file) ;
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

              imagejpeg($tn, $save, 100) ;	
		}
		else{
		$db_send_name="";
		}
		}
		else{
		$db_send_name="";
		}

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

		if(!isset($errMSG))

		{

			$stmt = $DB_con->prepare('INSERT INTO ormsusers(image,fullname,user_name,user_email,user_pass,admintype,admin,paydesk,category,providers,services,reload,datacard,customers,reports,status) VALUES(:db_send_name,:fullname,:user_name,:user_email,:user_pass,:admintype,:admin,:paydesk,:category,:providers,:services,:reload,:datacard,:customers,:reports,:status)');

			$stmt->bindParam(':db_send_name',$db_send_name);
			
			$stmt->bindParam(':fullname',$fullname);
			
			$stmt->bindParam(':user_name',$user_name);
			
			$stmt->bindParam(':user_email',$user_email);

			$stmt->bindParam(':user_pass',$user_pass);
			
			$stmt->bindParam(':admintype',$admintype);
			
			$stmt->bindParam(':admin',$admin);
			
			$stmt->bindParam(':paydesk',$paydesk);
			
			$stmt->bindParam(':category',$category);

			$stmt->bindParam(':providers',$providers);
			
			$stmt->bindParam(':services',$services);
			
			$stmt->bindParam(':reload',$reload);

			$stmt->bindParam(':datacard',$datacard);
			
			$stmt->bindParam(':customers',$customers);
			
			$stmt->bindParam(':reports',$reports);

			$stmt->bindParam(':status',$status);
			
			if($stmt->execute())

			{

				$successMSG = "Successfully! Created New Admin User Account....";

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

			}

			else

			{

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

			}

		}

	}

?>

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Add Admin | Mobile Recharge and Billing System | ORMS | Dashbaord</title>
	<?php
	require_once 'headercss.php';
	?>

</head>

<body>

        <!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">
	
<?php
require_once 'header.php';
?>

        <!--**********************************
            Sidebar start
        ***********************************-->
        <div class="deznav">
            <div class="deznav-scroll">
<?php

require_once 'sidebarmenu.php';

?>
            </div>
        </div>
        <!--**********************************
            Sidebar end
        ***********************************-->
		
        <!--**********************************
            Content body start
        ***********************************-->
        <div class="content-body">
            <!-- row -->
            <div class="container-fluid">
				<div class="row">
				<div class="col-xl-12 col-xxl-12 col-sm-12">
				<div class="alert alert-danger bg-danger text-white" role="alert">
				<strong>Alert!</strong> ඔබගේ ගිනුම තහවුරු කිරීම අසම්පුර්නයි.<a href="identity_verify.php" class="btn btn-success float-right" style="padding: 0px 3px;">සම්පුර්න කරන්න</a>
				</div>
				</div>
				</div>   
                <div class="row page-titles mx-0">
                    <div class="col-sm-6 p-md-0">
                        <div class="welcome-text">
                            <h4>Add Admin</h4>
                        </div>
                    </div>
                    <div class="col-sm-6 p-md-0 justify-content-sm-end mt-2 mt-sm-0 d-flex">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Admin</a></li>
                            <li class="breadcrumb-item active"><a href="javascript:void(0);">Add Admin</a></li>
                        </ol>
                    </div>
                </div>
				
				<div class="row">
					<div class="col-lg-12">
						<div class="card">
							<div class="card-header">
								<h4 class="card-title">Add Admin Users Account</h4>
							</div>
							<div class="card-body">
							<?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

							}

							?>
								<form method="POST" enctype="multipart/form-data">
									<div class="row">
										<div class="col-lg-3 col-md-3 col-sm-12">
											<div class="form-group">
												<label class="form-label">Profile Photo</label>
												<p style="color:red;">Only JPG</p>
<label for="fileName"><img src="images/empty.jpg" id="yourImgTag" class="pro_pick"></label>
<input type="file" name="image" id="fileName" hidden="yes" onChange="dis_name();">

<script>
function dis_name(){
var input = document.getElementById("fileName");
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-lg-3 col-md-3 col-sm-12">
											<div class="form-group">
												<label class="form-label">Full Name</label>
												<input type="text" class="form-control" name="fullname" placeholder="Enter Full Name" required>
											</div>
										</div>
										<div class="col-lg-3 col-md-3 col-sm-12">
											<div class="form-group">
												<label class="form-label">Username</label>
												<input type="text" class="form-control" name="user_name" placeholder="Enter Username" required>
											</div>
										</div>
										<div class="col-lg-3 col-md-3 col-sm-12">
											<div class="form-group">
												<label class="form-label">Email</label>
												<input type="email" class="form-control" name="user_email" placeholder="Enter Email" required>
											</div>
										</div>
										<div class="col-lg-4 col-md-4 col-sm-12">
											<div class="form-group">
												<label class="form-label">Password</label>
												<input type="password" class="form-control" name="user_pass" placeholder="Enter Password" required>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Admin Type</label>
												<select class="form-control" name="admintype" required>
													<option>Super Admin</option>
													<option>Core Admin</option>
													<option>Editor</option>
													<option>Viewer</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Admin Type</label>
												<select class="form-control" name="package" id="package">
	<option value="">Select a Package</option>
	<?php
$count=0;
$list_qury=mysqli_query($conn,"SELECT * FROM pkgs where status='1' AND userid ='".$_SESSION['user_id']."' GROUP BY pkgcount ORDER BY pid DESC");

while($list_resalt=mysqli_fetch_array($list_qury)){
$count++;

?>
	<option value="<?php echo $list_resalt['pkgcount']; ?>"><?php echo $list_resalt['name']; ?></option>
	<?php 
	}
	?>
</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Balance</label>
												<input type="text" class="form-control" id="balance" name="balance" placeholder="Balance">
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Admin</label>
												<select class="form-control" name="admin" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Pay Desk</label>
												<select class="form-control" name="paydesk" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Category</label>
												<select class="form-control" name="category" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Providers</label>
												<select class="form-control" name="providers" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Services</label>
												<select class="form-control" name="services" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>										
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Reload</label>
												<select class="form-control" name="reload" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Data Card</label>
												<select class="form-control" name="datacard" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Customers</label>
												<select class="form-control" name="customers" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Reports</label>
												<select class="form-control" name="reports" required>
													<option>True</option>
													<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Status</label>
												<select class="form-control" name="status" required>
													<option value="1">Publish</option>
													<option value="0">Unpublish</option>
												</select>
											</div>
										</div>
										<div class="col-lg-12 col-md-12 col-sm-12">
											<input type="submit" name="save" class="btn btn-primary" value="Save changes">
											<a class="btn btn-light" href="admin.php"><i class="fa fa-times"></i> Cancel</a>
										</div>
									</div>
								</form>
							</div>
						</div>
					</div>
				</div>
				
            </div>
        </div>
        <!--**********************************
            Content body end
        ***********************************-->

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

		<!--**********************************
           Support ticket button start
        ***********************************-->

        <!--**********************************
           Support ticket button end
        ***********************************-->


    </div>
    <!--**********************************
        Main wrapper end
    ***********************************-->

    <?php
	require_once 'footerjs.php';
	?>
    
</body>
</html>