<?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);
	
	$sellertype2 = $user_resalt['sellertype'];
	
	$sellerpackage = $user_resalt['sellerpackage'];
	
	if($user_resalt['image']==""){

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

	}

	else{

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

	}

}

$msg5 = '';	

if(isset($_POST['add_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'];	
$password=md5(mysqli_real_escape_string($con,$_POST['password']));
$confirm_password=md5(mysqli_real_escape_string($con,$_POST['confirm_password']));
$status = $_POST['status'];
$balance = $_POST['balance'];
$pkgcount = $_POST['sellerpackage'];
$userid = $addbycustomer;
$usertype = "Seller";
$selltype = $sellertype2;

if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO ormsregister(title,fname,lname,shopname,sellertype,sellerpackage,username,email,mobilenumber,nicnumber,shopaddress,country,province,district,division,creditadd,password,image,status,balance,pkgcount,userid,usertype,selltype) VALUES(:title,:fname,:lname,:shopname,:sellertype,:sellerpackage,:username,:email,:mobilenumber,:nicnumber,:shopaddress,:country,:province,:district,:division,:creditadd,:password,"",:status,:balance,:sellerpackage,:userid,:usertype,:selltype)');
$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(':password',$password);
$stmt->bindParam(':status',$status);
$stmt->bindParam(':balance',$balance);
$stmt->bindParam(':pkgcount',$pkgcount);
$stmt->bindParam(':userid',$userid);
$stmt->bindParam(':usertype',$usertype);
$stmt->bindParam(':selltype',$selltype);
if($stmt->execute())

{

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

header("refresh:2;customers.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 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="background-color:#0847cb;font-weight:bold;font-size:18px;color:#ffffff;padding:4px 4px;">සම්පුර්න කරන්න</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 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"><a href="javascript:void(0);">Resellers</a></li>
<li class="breadcrumb-item active"><a href="javascript:void(0);">Add Resellers</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 Resellers</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" 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="">Select Title</option>
	<option value="Rev.">Rev.</option>
	<option value="Mr.">Mr.</option>
	<option value="Mrs.">Mrs.</option>
	<option value="Miss.">Miss.</option>
	<option value="Dr.">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">
</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">
</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">
</div>
<div class="form-group col-md-6">
<label for="com">Seller Type <span class="text-danger">*</span></label>
<select class="form-control" name="sellertype" id="sellertype">
	<option value="">Select Seller Type</option>
							<?php 

                            if($user_resalt['sellertype'] == "1"){
							
                            ?>

                            <option value="2">Master</option>
							<option value="3">Manager</option>
							<option value="4">Distributor</option>
							<option value="5">Retailer</option>

							<?php
							
                            }else if($user_resalt['sellertype'] == "2"){
							
                            ?>

							<option value="3">Manager</option>
							<option value="4">Distributor</option>
							<option value="5">Retailer</option>

							<?php 
							
                            }else if($user_resalt['sellertype'] == "3"){
							
                            ?>

							<option value="4">Distributor</option>
							<option value="5">Retailer</option>

							<?php 
							
                            }else if($user_resalt['sellertype'] == "4"){
							
                            ?>

							<option value="5">Retailer</option>

							<?php 

                            }

                            ?>
										
								</select>
</div>
<div class="form-group col-md-6">
<label for="web">Seller Package <span class="text-danger">*</span></label>
<select class="form-control" name="sellerpackage" id="sellerpackage">
	<option value="">Select a Package</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['pkgcount']; ?>"><?php echo $list_resalt['name']; ?></option>
	<?php 
	}
	?>
</select>
</div>
<div class="form-group col-md-6">
<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();">
</div>
<div class="form-group col-md-6">
<label for="email">Email Address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter Email Address">
</div>
<div class="form-group col-md-6">
<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">
</div>
<div class="form-group col-md-6">
<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">
</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"></textarea>
</div>
<div class="form-group col-md-6">
<label for="country">Country <span class="text-danger">*</span></label>
<select class="form-control" id="country" name="country" required="">
<option value="1">ශ්&zwj;රී ලංකාව / இலங்கை / Sri Lanka</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="province">Province <span class="text-danger">*</span></label>
<select class="form-control" id="province" name="province" required="">
	<option value="">Select Your Province</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-6">
<label for="pwd">District <span class="text-danger">*</span></label>
<select class="form-control" id="district" name="district" required="">
	<option value="">Select Your District</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-6 mb-2">
<label for="pwd">Division <span class="text-danger">*</span></label>
<select class="form-control" id="division" name="division" required="">
	<option value="">Select Your Division</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-12" 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">Disable</option>
	<option value="1">Enable</option>
</select>
</div>
<div class="form-group col-md-6">
<label for="idnumber">Balance <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="balance" name="balance" placeholder="Enter Balance" value="0" readonly>
</div>
<div class="form-group col-md-6">
<label for="pwd">Password <span class="text-danger">*</span></label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password">
</div>
<div class="form-group col-md-6">
<label for="cnfpwd">Confirm Password <span class="text-danger">*</span></label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
</div>
<div class="form-group col-md-2">
<label for="self_credit">Status <span class="text-danger">*</span></label>
<select class="form-control" name="status" id="status">
	<option value="1">Active</option>
	<option value="0">Deactive</option>
</select>
</div>
</div>
<div class="text-center">
<input type="submit" name="add_customer" class="btn btn-primary" id="btnsave" value="ADD 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>