<?php
ob_start();
session_start();

require_once 'includes.php';

require_once("conn.php");

require_once("config.php");

require_once 'dbconfig4.php';

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

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

	$user_resalt=mysqli_fetch_array($user_qury);

	

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

		$image_path="images/empty.jpg";

	}

	else{

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

	}

}


	if(isset($_GET['adid']) && !empty($_GET['adid']))

	{

		$id = $_GET['adid'];

		$stmt_edit = $DB_con->prepare('SELECT * FROM ormsusers WHERE user_id =:adid');

		$stmt_edit->execute(array(':adid'=>$id));

		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);

		extract($edit_row);

	}

	else

	{

	echo "<script type='text/javascript'>window.location.href = 'admin.php';</script>";

	}	

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

	{

		$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=$edit_resalt['image'];
		$error_png=1;
		}
		}
		else{
		$db_send_name=$edit_resalt['image'];
		}

		if(!isset($errMSG))

		{

			$stmt = $DB_con->prepare('UPDATE ormsusers

									     SET image=:db_send_name,
										 
											 fullname=:fullname,
										 
											 user_name=:user_name,
											 
											 user_email=:user_email,
											 
											 user_pass=:user_pass,
											 
											 admintype=:admintype,
											 
											 admin=:admin,
											 
											 paydesk=:paydesk,
											 
											 category=:category,
											 
											 providers=:providers,

											 services=:services,
											 
											 reload=:reload,

											 datacard=:datacard,
											 
											 customers=:customers,
											 
											 reports=:reports,

											 status=:status

								       WHERE user_id=:adid');
									   
			$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);

			$stmt->bindParam(':adid',$id);

			if($stmt->execute()){

				$successMSG = "New Admin Users Account Successfully Updated ...";

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

			}

			else{

				$errMSG = "Sorry Data Could Not Updated !";

			}

		

		}

		

						

	}

	

?>

<!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>Edit Admin | Mobile Recharge and Billing System | ORMS | Dashbaord</title>
	<?php
	require_once 'headercss.php';
	?>
	
</head>

<body>
<!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">

        <!--**********************************
            Nav header start
        ***********************************-->
        <div class="nav-header">
            <a href="index.php" class="brand-logo">
                <img class="logo-abbr" src="images/logo-white.png" alt="">
            </a>

            <div class="nav-control">
                <div class="hamburger">
                    <span class="line"></span><span class="line"></span><span class="line"></span>
                </div>
            </div>
        </div>
        <!--**********************************
            Nav header end
        ***********************************-->

        <!--**********************************
            Header start
        ***********************************-->
        <div class="header">
            <div class="header-content">
                <nav class="navbar navbar-expand">
                    <div class="collapse navbar-collapse justify-content-between">
                        <div class="header-left">
                            
                        </div>

                        <ul class="navbar-nav header-right">
							<li class="nav-item notification_dropdown show">
                                <a class="btn btn-danger" href="#"><i class='fa fa-money' style='font-size:18px'></i> BALANCE Rs.0.00</a>
                            </li>
							<li class="nav-item notification_dropdown show">
                                <a class="btn btn-success" href="topup.php"><i class="fa fa-plus-circle" style="font-size:18px;color:white"></i> TOP-UP</a>
                            </li>
                            <li class="nav-item dropdown header-profile">
                                <a class="nav-link" href="#" role="button" data-toggle="dropdown">
                                    <img src="<?php echo $image_path; ?>" width="20" alt=""/>
                                </a>
                                <div class="dropdown-menu dropdown-menu-right">
                                    <a href="admin.php" class="dropdown-item ai-icon">
                                        <svg id="icon-user1" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-user"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path><circle cx="12" cy="7" r="4"></circle></svg>
                                        <span class="ml-2"><?php echo $user_resalt['fullname'];?></span>
                                    </a>
                                    <a href="logout.php" class="dropdown-item ai-icon">
                                        <svg id="icon-logout" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-log-out"><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path><polyline points="16 17 21 12 16 7"></polyline><line x1="21" y1="12" x2="9" y2="12"></line></svg>
                                        <span class="ml-2">Logout </span>
                                    </a>
                                </div>
                            </li>
                        </ul>
                    </div>
                </nav>
            </div>
        </div>
        <!--**********************************
            Header end ti-comment-alt
        ***********************************-->

        <!--**********************************
            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>Edit 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 active"><a href="admin.php">Admin</a></li>
                            <li class="breadcrumb-item active"><a href="edit_admin.php">Edit Admin</a></li>
                        </ol>
                    </div>
                </div>
				
				<div class="row">
					<div class="col-xl-12 col-xxl-12 col-sm-12">
                        <div class="card">
                            <div class="card-header">
								<h5 class="card-title">Edit Admin User Account</h5>
							</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 action="" 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/admin/<?php echo $image; ?>" 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" value="<?php echo $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" value="<?php echo $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" value="<?php echo $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" value="<?php echo $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><?php echo $admintype; ?></option>
												<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</label>
												<select class="form-control" name="admin" required>
												<option><?php echo $admin; ?></option>
												<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><?php echo $paydesk; ?></option>
												<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><?php echo $category; ?></option>
												<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><?php echo $providers; ?></option>
												<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><?php echo $services; ?></option>
												<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><?php echo $reload; ?></option>
												<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><?php echo $datacard; ?></option>
												<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><?php echo $customers; ?></option>
												<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><?php echo $reports; ?></option>
												<option>True</option>
												<option>False</option>
												</select>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group fallback w-100">
												<label class="form-label">Status</label>
												<select class="form-control" id="input-6" name="status" required>
												<option <?php if($edit_row['status']=="1"){echo "selected";} ?> value="1">Publish</option>
												<option <?php if($edit_row['status']=="0"){echo "selected";} ?> value="0">Unpublish</option>
												</select>
											</div>
										</div>
										<div class="col-lg-12 col-md-12 col-sm-12">
											<input type="submit" name="update" class="btn btn-primary" value="Update">
											<a class="btn btn-light" href="admin.php"><i class="fa fa-times"></i> Close</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>