<?php

if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['reid'])) header('location:index.php');

require_once("../dashboard/conn.php");
require_once("../dashboard/config.php");
require_once '../dashboard/dbconfig4.php';

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

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

	$user_resalt=mysqli_fetch_array($user_qury);

	$customer = $user_resalt['reid'];
	
	$addbycustomer = $user_resalt['userid'];
	
	$userbalance = number_format((float)$user_resalt['balance'],2);
	
	$sellertype = $user_resalt['sellertype'];
	
	if($user_resalt['image']==""){

		$image_path="../dashboard/images/empty.jpg";

	}

	else{

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

	}

}

   
$edit_qury=mysqli_query($conn,"SELECT * FROM ormsregister WHERE ormsregister.reid='$_GET[reid]'");
$edit_resalt=mysqli_fetch_array($edit_qury);
	if(isset($_GET['reid']) && !empty($_GET['reid']))
	{
		$id = $_GET['reid'];
		$stmt_edit = $DB_con->prepare('SELECT * FROM ormsregister WHERE reid =:reid');
		$stmt_edit->execute(array(':reid'=>$id));
		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
		extract($edit_row);
	}
	else
	{
		header("Location: customers.php");
	}	
	if(isset($_POST['update_customer']))
	{
		$title = $_POST['title'];
		$fname = $_POST['fname'];
		$lname = $_POST['lname'];
		$shopname = $_POST['shopname'];
		$sellertype = $_POST['sellertype'];
		$sellerpackage = $_POST['sellerpackage'];
		$username = $_POST['username'];
		$email = $_POST['email'];
		$mobilenumber=(int)mysqli_real_escape_string($conn,$_POST['mobilenumber']);
		$to="0".(int)mysqli_real_escape_string($conn,$_POST['mobilenumber']);
		$nicnumber = $_POST['nicnumber'];
		$shopaddress = $_POST['shopaddress'];
		$country = $_POST['country'];
		$province = $_POST['province'];
		$district = $_POST['district'];
		$division = $_POST['division'];
		$creditadd = $_POST['creditadd'];	
		$status = $_POST['status'];	
		$userid = $customer;	
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('UPDATE ormsregister
			
									     SET title=:title,
										 
										     fname=:fname,

											 lname=:lname,
											 
											 shopname=:shopname,
											  
											 sellertype=:sellertype,
											 
											 sellerpackage=:sellerpackage,
		
										     username=:username,

											 email=:email,
											 
											 mobilenumber=:mobilenumber,
											  
											 nicnumber=:nicnumber,
											 
											 shopaddress=:shopaddress,
		
										     country=:country,

											 province=:province,
											 
											 district=:district,
											 
											 division=:division,
											 
											 creditadd=:creditadd,

											 status=:status,
											 
											 userid=:userid

								       WHERE reid=:reid');

			$stmt->bindParam(':title',$title);						
			$stmt->bindParam(':fname',$fname);						
			$stmt->bindParam(':lname',$lname);
			$stmt->bindParam(':shopname',$shopname);
			$stmt->bindParam(':sellertype',$sellertype);
			$stmt->bindParam(':sellerpackage',$sellerpackage);
			$stmt->bindParam(':username',$username);						
			$stmt->bindParam(':email',$email);						
			$stmt->bindParam(':mobilenumber',$mobilenumber);
			$stmt->bindParam(':nicnumber',$nicnumber);
			$stmt->bindParam(':shopaddress',$shopaddress);
			$stmt->bindParam(':country',$country);
			$stmt->bindParam(':province',$province);						
			$stmt->bindParam(':district',$district);						
			$stmt->bindParam(':division',$division);
			$stmt->bindParam(':creditadd',$creditadd);
			$stmt->bindParam(':status',$status);
			$stmt->bindParam(':userid',$userid);
			$stmt->bindParam(':reid',$id);

			if($stmt->execute()){

				$successMSG = "Sellers/Customers Successfully Updated ...";

				header("refresh:2;customers.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 Resellers | 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>Edit Resellers</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="#">Resellers</a></li>
                            <li class="breadcrumb-item active"><a href="#.php">Edit Resellers</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 Resellers</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 method="post" id="add_client_frm" action="#" enctype="multipart/form-data">
					<div class="form-row">
						<div class="form-group col-md-2">
							<label for="seller_title">Title <span class="text-danger">*</span></label>
							<select class="form-control" name="title" id="seller_title">
																	<option value="Rev."<?php if($title=="Rev."){echo "selected";} ?>>Rev.</option>
																	<option value="Mr."<?php if($title=="Mr."){echo "selected";} ?>>Mr.</option>
																	<option value="Mrs."<?php if($title=="Mrs."){echo "selected";} ?>>Mrs.</option>
																	<option value="Miss."<?php if($title=="Miss."){echo "selected";} ?>>Miss.</option>
																	<option value="Dr."<?php if($title=="Dr."){echo "selected";} ?>>Dr.</option>
							</select>
						</div>
						<div class="form-group col-md-5">
							<label for="fname">First Name <span class="text-danger">*</span></label>
							<input type="text" class="form-control" id="fname" name="fname" placeholder="Enter First Name" value="<?php echo $fname; ?>">
						</div>
						<div class="form-group col-md-5">
							<label for="lname">Last Name <span class="text-danger">*</span></label>
							<input type="text" class="form-control" id="lname" name="lname" placeholder="Enter Last Name" value="<?php echo $lname; ?>">
						</div>
						<div class="form-group col-md-6">
							<label for="shopname">Shop Name <span class="text-danger">*</span></label>
							<input type="text" class="form-control" id="shopname" name="shopname" placeholder="Enter Shop Name or Business Name" value="<?php echo $shopname; ?>">
						</div>
						<div class="form-group col-md-3">
							<label for="com">Seller Type <span class="text-danger">*</span></label>
							<select class="form-control" name="sellertype" id="sellertype">
							
							<?php
							
                            if($sellertype == "1"){
							
                            ?>
							
							<option value="2"<?php if($edit_row['sellertype']=="2"){echo "selected";} ?>>Master</option>
							<option value="3"<?php if($edit_row['sellertype']=="3"){echo "selected";} ?>>Manager</option>
							<option value="4"<?php if($edit_row['sellertype']=="4"){echo "selected";} ?>>Distributor</option>
							<option value="5"<?php if($edit_row['sellertype']=="5"){echo "selected";} ?>>Retailer</option>
							<?php
							
                            }else if($sellertype == "2"){
							
                            ?>
							<option value="3"<?php if($edit_row['sellertype']=="3"){echo "selected";} ?>>Manager</option>
							<option value="4"<?php if($edit_row['sellertype']=="4"){echo "selected";} ?>>Distributor</option>
							<option value="5"<?php if($edit_row['sellertype']=="5"){echo "selected";} ?>>Retailer</option>
							<?php
							
                            }else if($sellertype == "3"){
							
                            ?>
							<option value="4"<?php if($edit_row['sellertype']=="4"){echo "selected";} ?>>Distributor</option>
							<option value="5"<?php if($edit_row['sellertype']=="5"){echo "selected";} ?>>Retailer</option>
							<?php
							
                            }else if($sellertype == "4"){
							
                            ?>
							<option value="5"<?php if($edit_row['sellertype']=="5"){echo "selected";} ?>>Retailer</option>
							<?php
							
                            }
							
                            ?>
							</select>
						</div>
						<div class="form-group col-md-3">
							<label for="web">Seller Package <span class="text-danger">*</span></label>
							<select class="form-control" name="sellerpackage" id="sellerpackage">
								<option value="<?php

								$id = $edit_row['sellerpackage']; 

								$query = $DB_con->prepare('SELECT pid FROM pkgs WHERE pid='.$id);
	
								$query->execute();

								$result = $query->fetch();

								echo $result['pid'];

								?>" hidden="yes"><?php

								$id = $edit_row['sellerpackage']; 

								$query = $DB_con->prepare('SELECT name FROM pkgs WHERE pid='.$id);

								$query->execute();

								$result = $query->fetch();

								echo $result['name'];

								?>
								</option>
								<?php
$count=0;
$list_qury=mysqli_query($conn,"SELECT * FROM pkgs where status='1' AND userid ='".$customer."' GROUP BY pkgcount ORDER BY pid DESC");

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

?>
								<option value="<?php echo $list_resalt['pid']; ?>" disabled><?php echo $list_resalt['name']; ?></option>
								<?php 
								}
								?>
							</select>
						</div>
						<div class="form-group col-md-3">
							<label for="u_name">User Name <span class="text-danger">*</span></label>
							<input type="text" class="form-control" id="username" name="username" placeholder="Enter User Name" onchange="check_unique_values();" value="<?php echo $username; ?>">
						</div>
						<div class="form-group col-md-3">
							<label for="email">Email Address</label>
							<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email Address" value="<?php echo $email; ?>">
						</div>
						<div class="form-group col-md-3">
							<label for="phone">Mobile Number <span class="text-danger">*</span></label>
							<input type="text" class="form-control" id="mobilenumber" name="mobilenumber" placeholder="Please provide a Mobile Number" value="<?php echo $mobilenumber; ?>">
						</div>
						<div class="form-group col-md-3">
							<label for="idnumber">NIC Number <span class="text-danger">*</span></label>
							<input type="text" class="form-control" id="nicnumber" name="nicnumber" placeholder="Enter National Identity Card Number" value="<?php echo $nicnumber; ?>">
						</div>
						<div class="form-group col-md-12">
							<label for="address">Shop Address <span class="text-danger">*</span></label>
							<textarea class="form-control" id="shopaddress" name="shopaddress"><?php echo $shopaddress; ?></textarea>
						</div>
						<div class="form-group col-md-4">
							<label for="country">Country <span class="text-danger">*</span></label>
							<select class="form-control" id="country" name="country" required="">
							<option value="1"<?php if($country=="1"){echo "selected";} ?>>ශ්&zwj;රී ලංකාව / இலங்கை / Sri Lanka</option>
							</select>
						</div>
						<div class="form-group col-md-4">
							<label for="province">Province <span class="text-danger">*</span></label>
							<select class="form-control" id="province" name="province" required="">
								<option value="<?php

								$id = $edit_row['province']; 

								$query = $DB_con->prepare('SELECT poid FROM ormsprovince WHERE poid='.$id);
	
								$query->execute();

								$result = $query->fetch();

								echo $result['poid'];

								?>" hidden="yes"><?php

								$id = $edit_row['province']; 

								$query = $DB_con->prepare('SELECT pname FROM ormsprovince WHERE poid='.$id);

								$query->execute();

								$result = $query->fetch();

								echo $result['pname'];

								?>
								</option>
<?php
$count=0;
$list_qury=mysqli_query($conn,"SELECT * FROM ormsprovince ORDER BY poid DESC");

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

?>
								<option value="<?php echo $list_resalt['poid']; ?>"><?php echo $list_resalt['pname']; ?></option>
								<?php 
								}
								?>
								</select>
							<div class="invalid-tooltip">Please Select Province</div>
						</div>
						<div class="form-group col-md-4">
							<label for="pwd">District <span class="text-danger">*</span></label>
							<select class="form-control" id="district" name="district" required="">
								<option value="<?php

								$id = $edit_row['district']; 

								$query = $DB_con->prepare('SELECT did FROM ormsdistrict WHERE did='.$id);

								$query->execute();

								$result = $query->fetch();

								echo $result['did'];

								?>" hidden="yes"><?php

								$id = $edit_row['district']; 

								$query = $DB_con->prepare('SELECT dname FROM ormsdistrict WHERE did='.$id);

								$query->execute();

								$result = $query->fetch();

								echo $result['dname'];

								?>
								</option>
<?php
$count=0;
$list_qury=mysqli_query($conn,"SELECT * FROM ormsdistrict ORDER BY did DESC");

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

?>
								<option value="<?php echo $list_resalt['did']; ?>"><?php echo $list_resalt['dname']; ?></option>
								<?php 
								}
								?>
							</select>
							<div class="invalid-tooltip">Please Select District</div>
						</div>
						<div class="col-md-4 mb-2">
							<label for="pwd">Division <span class="text-danger">*</span></label>
							<select class="form-control" id="division" name="division" required="">
								<option value="<?php

								$id = $edit_row['division']; 

								$query = $DB_con->prepare('SELECT dvid FROM ormsdivision WHERE dvid='.$id);

								$query->execute();

								$result = $query->fetch();

								echo $result['dvid'];

								?>" hidden="yes"><?php

								$id = $edit_row['division']; 

								$query = $DB_con->prepare('SELECT dvname FROM ormsdivision WHERE dvid='.$id);

								$query->execute();

								$result = $query->fetch();

								echo $result['dvname'];

								?>
								</option>
								<?php
$count=0;
$list_qury=mysqli_query($conn,"SELECT * FROM ormsdivision ORDER BY dvid DESC");

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

?>
								<option value="<?php echo $list_resalt['dvid']; ?>"><?php echo $list_resalt['dvname']; ?></option>
								<?php 
								}
								?>
							</select>
							<div class="invalid-tooltip">Please Select Division</div>
						</div>
						<div class="form-group col-md-4" style="">
							<label for="self_credit">Seller Self Credit Add <span class="text-danger">*</span></label>
							<select class="form-control" name="creditadd" id="creditadd">
								<option value="0"<?php if($creditadd=="0"){echo "selected";} ?>>Disable</option>
								<option value="1"<?php if($creditadd=="1"){echo "selected";} ?>>Enable</option>
							</select>
						</div>
						<div class="form-group col-md-4">
							<label for="self_credit">Status <span class="text-danger">*</span></label>
							<select class="form-control" name="status" id="status">
								<option value="1"<?php if($creditadd=="1"){echo "selected";} ?>>Active</option>
								<option value="0"<?php if($creditadd=="0"){echo "selected";} ?>>Deactive</option>
							</select>
						</div>
					</div>
					<div class="text-center">
						<input type="submit" name="update_customer" class="btn btn-primary" id="btnupdate" value="UPDATE SELLERS">
						<a class="btn btn-danger" href="customers.php">CANCEL</a>
					</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>