<?php
session_start();

if(!$_SESSION['isadmin']) {
	header("Location: login.php?ref=edit.php?pageid=1");
	exit;
}

if($_SESSION['country_login'] == 1 || $_SESSION['country_login'] == 2) {
	header("Location: index.php");
	exit;
}

include_once("../functions.php");

$conn = dbconnect();

include "headers/common_header.php";

if($_POST && $_POST['update_country_login']) {
	$new_countries = implode(",",$_POST['country_access']);
	if(mysqli_query($conn,"UPDATE adminusers SET fname='".mysqli_real_escape_string($conn,$_POST['country_login_first_name'])."', lname='".mysqli_real_escape_string($conn,$_POST['country_login_last_name'])."', countries='".$new_countries."', email='".mysqli_real_escape_string($conn,$_POST['country_login_email'])."' WHERE id=".mysqli_real_escape_string($conn,$_POST['edit_countries'])))
	$_SESSION['countries_updated']=1;
}

if($_GET['edititem']) {
	$login_details = mysqli_fetch_array(mysqli_query($conn,"SELECT uname, fname, lname, email, countries FROM adminusers WHERE id=".mysqli_real_escape_string($conn,$_GET['edititem'])));
	$login_countries = explode(",",$login_details['countries']);
} else {
	$login_countries = array();
}

if($_POST && $_POST['create_country_login'] && $_POST['country_login_password']!=="" && $_POST['country_login_name']!=="") {

	$uname = mysqli_real_escape_string($conn,$_POST['country_login_name']);
	
	//Check if username exists
	$exists = mysqli_num_rows(mysqli_query($conn,"SELECT uname FROM adminusers WHERE uname='".$uname."'"));

	if($exists==0) {
		$pass = md5($_POST['country_login_password']);
		$countries = implode(",",$_POST['country_access']);
		$email = mysqli_real_escape_string($conn,$_POST['country_login_email']);
		$fname = mysqli_real_escape_string($conn,$_POST['country_login_first_name']);
		$lname = mysqli_real_escape_string($conn,$_POST['country_login_last_name']);
		
		if(mysqli_query($conn,"INSERT INTO adminusers VALUES(0, '".$uname."', '".$pass."', 1, 2, '".$countries."', '".$email."','".$fname."','".$lname."')"))
		$_SESSION['account_created']=1;
	} else {
		$_SESSION['user_exists']=1;	
	}	
}

if($_GET['disableitem']) {
	mysqli_query($conn,"UPDATE adminusers SET ustatus=0 WHERE id<>21 AND id=".mysqli_real_escape_string($conn,$_GET['disableitem']));
}

if($_GET['enableitem']) {
	mysqli_query($conn,"UPDATE adminusers SET ustatus=1 WHERE id<>21 AND id=".mysqli_real_escape_string($conn,$_GET['enableitem']));
}

if($_POST['change_country_password'] && $_POST['change_password']) {

	if(mysqli_query($conn,"UPDATE adminusers SET upasswd='".md5($_POST['change_password'])."' WHERE id=".mysqli_real_escape_string($conn,$_POST['country_login_id'])))
	$_SESSION['password_changed']=1;
}

if($_POST['change_country_password'] && $_POST['change_password']=="")
$_SESSION['password_changed']=2;
?>

<!--BEGIN PAGE WRAPPER-->

        <div id="page-wrapper"><!--BEGIN TITLE & BREADCRUMB PAGE-->

            <div id="title-breadcrumb-option-demo" class="page-title-breadcrumb">

                <div class="page-header pull-left">

                    <div class="page-title">Distribution partner login accounts</div>

                </div>

                <ol class="breadcrumb page-breadcrumb pull-right">

                    <li><i class="fa fa-home"></i>&nbsp;<a href="index.php">Home</a>&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>

                    <li class="active">Distribution partner Login</li>

                </ol>

                <div class="clearfix"></div>

            </div>

		    <!--BEGIN CONTENT TABLE-->

            <div class="page-content">

                <div class="row">

                    <div class="col-lg-12">

                        <div class="panel panel-green">

                            <div class="panel-heading">Distribution partner login accounts:</div>
	
	
<?php if($_SESSION['password_changed']==1) {?>
<br />
<div style="padding: 0.1em; width:96%; margin-left:20px;" class="ui-state-highlight ui-corner-all">

   <p style="padding-top:10px;"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>Password changed successfully.</p>

</div>
<?php 
$_SESSION['password_changed']=0;
}?> 

<?php if($_SESSION['countries_updated']==1) {?>
<br />
<div style="padding: 0.1em; width:96%; margin-left:20px;" class="ui-state-highlight ui-corner-all">

   <p style="padding-top:10px;"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>Countries for this login updated successfily.</p>

</div>
<?php 
$_SESSION['countries_updated']=0;
}?> 


<?php if($_SESSION['password_changed']==2) {?>
<br />
<div style="padding: 0.1em; width:96%; margin-left:20px;" class="ui-state-error ui-corner-all">

   <p style="padding-top:10px;"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>Not changed. Password can not be blank.</p>

</div>

<?php 
$_SESSION['password_changed']=0;
}?>

<?php if($_SESSION['user_exists']==1) {?>
<br />
<div style="padding: 0.1em; width:96%; margin-left:20px;" class="ui-state-error ui-corner-all">

   <p style="padding-top:10px;"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>User name already exist, Please choose different username.</p>

</div>

<?php 
$_SESSION['user_exists']=0;
}?>


<?php if($_SESSION['account_created']==1) {?>
<br />
<div style="padding: 0.1em; width:96%; margin-left:20px;" class="ui-state-highlight ui-corner-all">

   <p style="padding-top:10px;"><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span>Country login created successfully.</p>

</div>

<?php 
$_SESSION['account_created']=0;
}?>  

                            <div class="panel-body">
									
									<form action="#" method="post" id="add_country_login" class="form-horizontal">
													
                                                    <div class="form-body pal">
													
													<?php if($_GET['edititem']) {?>
													<div class="form-group"><label for="businessname" class="col-md-3 control-label"></label>
                                                            <div class="col-md-9">
                                                        <div class="input-icon">Updating countries for login name: <?php echo ucfirst($login_details['uname']);?> <br /><p style="color:#993366">Please note: This account must be logged out and logged in again to take this effect of updated countries.</p></div>
                                                            </div>
                                                        </div>
													<?php }?>	
													
														<?php if(!$_GET['edititem']) {?>
														<div class="form-group"><label for="businessname" class="col-md-3 control-label">Login name:</label>

                                                            <div class="col-md-9">

                                                                <div class="input-icon"><input type="text" name="country_login_name" id="country_login_name"  class="form-control"></div>

                                                            </div>

                                                        </div>
														
														<div class="form-group"><label for="sloperating" class="col-md-3 control-label">Password:</label>

                                                            <div class="col-md-9">

                                                                <div class="input-icon"><input type="password" id="country_login_password" name="country_login_password"  class="form-control"></div>

                                                            </div>

                                                        </div>
														<?php }?>
														
														<div class="form-group"><label for="businessname" class="col-md-3 control-label">First name:</label>

                                                            <div class="col-md-9">

                                                                <div class="input-icon"><input type="text" name="country_login_first_name" id="country_login_first_name"  class="form-control" value="<?php echo $login_details['fname']?>"></div>

                                                            </div>

                                                        </div>
														
														<div class="form-group"><label for="businessname" class="col-md-3 control-label">Last name:</label>

                                                            <div class="col-md-9">

                                                                <div class="input-icon"><input type="text" name="country_login_last_name" id="country_login_last_name"  class="form-control" value="<?php echo $login_details['lname']?>"></div>

                                                            </div>

                                                        </div>
														
													   <div class="form-group"><label for="address" class="col-md-3 control-label">Countries:</label>

                                                            <div class="col-md-9">

                                                                <div class="input-icon">
																	<?php $get_countries = mysqli_query($conn,"SELECT name FROM country"); ?>
																	<select name="country_access[]" class="form-control" multiple="multiple" size="10">
																	<?php while($country = mysqli_fetch_array($get_countries)) {?>
																
															<option value="<?php echo $country['name']?>" <?php if(in_array($country['name'],$login_countries)) {?> selected="selected" <?php }?>><?php echo $country['name']?></option>
																	
																	<?php }?>
																	</select>
																</div>

                                                            </div>

                                                        </div>

														<div class="form-group"><label for="businessname" class="col-md-3 control-label">Email:</label>

                                                            <div class="col-md-9">

                                                                <div class="input-icon"><input type="text" value="<?php echo $login_details['email']?>" name="country_login_email" id="country_login_email"  class="form-control"></div>

                                                            </div>

                                                        </div>

                                                        <div class="col-md-offset-3 col-md-9">
														<?php if($_GET['edititem']) { $label="Update countries for this login";  $but_name="update_country_login";} else { $label="Create distribution partner login"; $but_name="create_country_login"; } ?>
														<input type="submit" name="<?php echo $but_name; ?>" value="<?php echo $label?>" class="btn btn-primary">

                                                      <br /><br />

                                                    </div>
													<?php if($_GET['edititem']) {?>
													<input type="hidden" name="edit_countries" value="<?php echo $_GET['edititem']?>">
													<?php }?>
                                                </form>
																	
							<div id="no-more-tables">

                                    <table class="table table-bordered table-striped table-condensed cf">

                                        <thead class="cf">

                                        <tr>

                                            <th>Username</th>

                                            <th class="numeric">Password</th>

											<th class="numeric">Status</th>
											
                                            <th class="numeric">Has access to countries</th>
											
											<th class="numeric">Action</th>

                                        </tr>

                                        </thead>

                                        <tbody>
										
										<?php 
											
											$country_login_accounts = mysqli_query($conn,"SELECT * FROM adminusers WHERE country_login=2");
											
											while($login_accounts = mysqli_fetch_array($country_login_accounts)) {
                                        ?>

                                        <tr>

                                            <td data-title="Barcode"><?php echo ucwords($login_accounts['uname'])?> <br /> <?php echo $login_accounts['email']; ?></td>
											
											<td data-title="Barcode"><form action="distribution-login.php" method="POST"><input type="password" name="change_password"> &nbsp; <input type="submit" value="Change" name="change_country_password"><input type="hidden" name="country_login_id" value="<?php echo $login_accounts['id']?>"></form></td>
											
											<td data-title="Barcode"><?php if($login_accounts['ustatus']==1) echo "Active"; else echo "Inactive";?></td>
											
											<td data-title="Barcode"><?php echo ucwords(str_replace(",",", ",$login_accounts['countries']))?></td>
											
                                            <td data-title="Delete">
											
											<?php if($login_accounts['ustatus']==1) {?>
											<a href="distribution-login.php?disableitem=<?php echo $login_accounts['id']?>" title="Disable" onclick="return confirm('Disable this account?')" class="btn-red btn-xs">Disable</a>
											<?php } else {?>
											<a href="distribution-login.php?enableitem=<?php echo $login_accounts['id']?>" title="Enable" onclick="return confirm('Enable this account?')" class="btn-green btn-xs">Enable</a>
											<?php }?>
											
											<a href="distribution-login.php?edititem=<?php echo $login_accounts['id']?>" title="Edit" class="btn-green btn-xs">Edit</a>
											
											</td>

                                        </tr>
										<?php }?>	
                                        </tbody>

                                    </table>

                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

            <!--END CONTENT-->



<script src="../js/jquery.validate.js"></script>

<script src="../js/jquery.alphanumeric.pack.js"></script>



<script>

$(document).ready(function() { 



 $("#add_country_login").validate({ 

			rules: {
				country_login_email:"required",
				country_login_first_name:"required",
				country_login_last_name:"required",
				country_login_name:"required",
				country_login_password"required"
			},
			messages: {
				country_login_email:"Please enter email id",
				country_login_name:"Please enter distribution partner login name",
				country_login_first_name:"Please enter first name",
				country_login_last_name:"Please enter last name",
				country_login_password:"Please enter password"
			}
		});
	});

</script>



<?php include "footer.php";?>