<?php
session_start();

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

include_once("../functions.php");
$conn = dbconnect();
include "headers/headerlicensee.php";

if($_POST['add_licensee']){
$name = $_POST['licensee_name'];
$country = $_POST['licensee_country'];
$amount = $_POST['licensee_cents'];
$time = date("Y-m-d H:i:s");

if($_POST['edit_licensee']){
if(mysqli_query($conn,"UPDATE licensee SET licensee_name='".$name."', licensee_country = '".$country."', licensee_cents=$amount WHERE id=".$_POST['edit_licensee']))
	$licensee_updated = 1;
}else{
	if(mysqli_query($conn,"INSERT INTO licensee VALUES(0,'".$name."','".$country."',$amount,'".$time."')"))
	$licensee_added = 1;
	}
}

if($_GET['delete_id']){
	if(mysqli_query($conn,"DELETE FROM licensee WHERE id=".$_GET['delete_id']))
	$deleted = 1;
}

if($_GET['edit_id']){
	$licensee_details = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM licensee WHERE id=".$_GET['edit_id']));
}


$all_licensee = mysqli_query($conn,"SELECT * FROM licensee ORDER BY addedon DESC");
?>
<!--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">Licensee</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">Licensee</li>
                </ol>
                <div class="clearfix"></div>
            </div>
            <!--END TITLE & BREADCRUMB PAGE-->
			<!--BEGIN CONTENT-->
            <div class="page-content">
                <div id="form-layouts" class="row">
                                    <div class="col-lg-12">
                                        <div class="panel panel-green">
                                            <div class="panel-heading">Add a Licensee Below:</div>
											<?php if($licensee_added==1) {?>	
	                                        <div class="alert alert-info">
                                            <button type="button" class="close" data-dismiss="alert">x</button>
                                            <strong>Success:</strong> Licensee added successfully.
                                            </div>
                                            <?php }?>
                                            <div class="panel-body pan">
                                                <form action="#" method="post" id="add_licensee" class="form-horizontal">
                                                    <div class="form-body pal">
													   <div class="form-group"><label for="contactname" class="col-md-3 control-label">Licensee Name:</label>
                                                            <div class="col-md-9">
                                                                <div class="input-icon"><input type="text" name="licensee_name" value="<?php echo $licensee_details['licensee_name']?>" class="form-control"></div>
                                                            </div>
                                                        </div>
														 <div class="form-group"><label for="contactname" class="col-md-3 control-label">Licensee Country:</label>
                                                            <div class="col-md-9">
                                                                <div class="input-icon"><input type="text" name="licensee_country" value="<?php echo $licensee_details['licensee_country']?>" class="form-control"></div>
                                                            </div>
                                                        </div>
														<div class="form-group"><label for="contactname" class="col-md-3 control-label">Cents per ticket:</label>
                                                            <div class="col-md-9">
                                                                <div class="input-icon"><input type="text" name="licensee_cents" value="<?php echo $licensee_details['licensee_cents']?>" class="form-control" maxlength="2"></div>
                                                            </div>
                                                        </div>
                                                        <div class="form-actions none-bg">
                                                        <div class="col-md-offset-3 col-md-9">
														<?php if($_GET['edit_id']) {?>
														<input type="submit" name="add_licensee" value="Update Licensee" class="btn btn-primary">
														<?php } else {?>
														<input type="submit" name="add_licensee" value="Add Licensee" class="btn btn-primary">
														<?php }?>
                                                        </div>
														<?php if($_GET['edit_id']) {?>
                                                        <input type="hidden" name="edit_record" value="1">
                                                        <input type="hidden" name="edit_licensee" value="<?php echo $_GET['edit_id']?>">
                                                        <?php }?>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
            <!--END CONTENT-->
		    <!--BEGIN CONTENT TABLE-->
            <div class="page-content">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-green">
                            <div class="panel-heading">Barcode Search Details:</div>
											<?php if($deleted==1) {?>	
	                                        <div class="alert alert-info">
                                            <button type="button" class="close" data-dismiss="alert">x</button>
                                            <strong>Success:</strong> Licensee deleted successfully.
                                            </div>
                                            <?php }?>

                                            <?php if($licensee_updated==1) {?>	
	                                        <div class="alert alert-info">
                                            <button type="button" class="close" data-dismiss="alert">x</button>
                                            <strong>Success:</strong> Licensee updated successfully.
                                            </div>
                                            <?php }?>
                            <div class="panel-body">
                                <div id="no-more-tables">
                                    <table class="table table-bordered table-striped table-condensed cf">
                                        <thead class="cf">
                                        <tr>
                                            <th>Licensee Name</th>
                                            <th>Country</th>
                                            <th class="numeric">Cents/Ticket</th>
                                            <th class="numeric">Added On</th>
                                            <th>Asign to Club</th>
                                            <th>Edit/Delete</th>
                                        </tr>
                                        </thead>
                                        <?php while($get_licensee = mysqli_fetch_array($all_licensee)) {?>
                                        <tbody>
                                        <tr>
                                            <td data-title="Licensee Name"><?php echo $get_licensee['licensee_name'];?></td>
                                            <td data-title="Country"><?php echo $get_licensee['licensee_country'];?></td>
                                            <td data-title="Cents/Ticket"><?php echo $get_licensee['licensee_cents'];?></td>
                                            <td data-title="Added On" class="numeric"><?php echo date('jS M Y h:i A',strtotime($get_licensee['addedon']));?></td>
                                            <td data-title="Asign to Club"><a href="assign-licensee.php?lid=<?php echo $get_licensee['id']?>" class="btn btn-info btn-xs"><i class="fa fa-plus"></i>&nbsp;Assign</a></td>
                                            <td data-title="Edit/Delete"><a href="licensee.php?edit_id=<?php echo $get_licensee['id']?>" title="Edit" class="btn-orange btn-xs"><i class="fa fa-pencil"></i>&nbsp;Edit</a>&nbsp;&nbsp;&nbsp;<a href="licensee.php?delete_id=<?php echo $get_licensee['id']?>" title="Delete" onclick="javascript: return confirm('Delete?')" class="btn-red btn-xs"><i class="fa fa-trash-o"></i>&nbsp;Delete</a></td>
                                        </tr>
                                        </tbody>
										<?php }?>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!--END CONTENT-->
			<?php if($_POST['register_club']) {?>
            <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>Club registered successfully.</p>
            </div>
            <br />
            <?php }?>

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

<script>

  $(function() {
    $( ".datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });
  });

  
/*$('.an').alphanumeric();

$('.name').alpha();

$('.suburb').alpha({allow:" "});

$('.anandspace').alphanumeric({allow:"#., "});

$('.numeric').numeric({allow:"+ "});*/

$('.alpha').alpha({allow:" "});
$('.cents').numeric();

	
	$().ready(function() {
		// validate signup form on keyup and submit
		$("#add_licensee").validate({
			rules: {
				
				licensee_name: {
					required: true
				},
				
				licensee_country: {
					required: true
				},
				
				licensee_cents: {
					required: true
				},
				
			},
			messages: {
				licensee_name:"Please enter licensee name",
				licensee_country:"Please enter licensee country",
				licensee_cents:"Please enter cents per ticket"
			}
		});

	});
</script>


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