<?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'];

	}

}


$msg5 = '';	

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

	{

        $province = $_POST['province'];
		$dname = $_POST['dname'];
		$status = $_POST['status'];
		$userid = $_SESSION['admin_user_id'];	
	
		date_default_timezone_set("Asia/Colombo");

		if(empty($province)){			
		$errMSG = "Please Select Province Name.";
		}else if(empty($dname)){			
		$errMSG = "Please Enter District Name.";
		}else if(empty($status)){
			$errMSG = "Please Select Status.";
		}
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO ormsdistrict(province,dname,status,userid,usertype) VALUES(:province,:dname,:status,:userid,"Admin")');
			$stmt->bindParam(':province',$province);
			$stmt->bindParam(':dname',$dname);	
			$stmt->bindParam(':status',$status);
			$stmt->bindParam(':userid',$userid);

			if($stmt->execute())

			{

				$successMSG = "District Successfully Added.";

				header("refresh:2;district.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 District | 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 District</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);">District</a></li>
                            <li class="breadcrumb-item active"><a href="javascript:void(0);">Add District</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 District</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" autocomplete="off" enctype="multipart/form-data">
									<div class="row">
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Province</label>
												<select name="province" class="form-control" required>
												<option selected>Select Province</option>
												<?php

												$stmt = $DB_con->prepare('SELECT * FROM ormsprovince ORDER BY poid');

												$stmt->execute();

												if($stmt->rowCount() > 0)

												{

												while($row=$stmt->fetch(PDO::FETCH_ASSOC))

												{

												extract($row);

												?>
												<option value="<?php echo $row['poid']; ?>"><?php echo $row['pname']; ?></option>
												<?php } 

												}
												?>
												</select>
											</div>
										</div>
										<div class="col-lg-4 col-md-4 col-sm-12">
											<div class="form-group">
												<label class="form-label">District Name</label>
												<input type="text" name="dname" class="form-control" required>
											</div>
										</div>
										<div class="col-lg-2 col-md-2 col-sm-12">
											<div class="form-group">
												<label class="form-label">Status</label>
												<select name="status" class="form-control" required>
												<option value="Active">Active</option>
												<option value="Deactive">Deactive</option>
												</select>
											</div>
										</div>
										<div class="col-lg-12 col-md-12 col-sm-12">
											<input type="submit" name="add_district" class="btn btn-primary" value="Save Changes">
											<a class="btn btn-light" href="district.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>