<?php
    session_start();
    include("library/conn.php");
    
    $role_rs = array();
	$get_role_mode = mysqli_query($conn, "SELECT * FROM user_role_privileges WHERE userID = '". $_SESSION['userIdd'] ."' AND roleName = 'payment_gateways'") or die (mysqli_error($conn));
	if (mysqli_num_rows($get_role_mode) > 0) {
		$role_rs = mysqli_fetch_array($get_role_mode);
	} else {
		$role_rs = ['-1', '-1', '-1', '-1', '-1', '-1'];
	}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <?php include("library/head.php");?>
        <title>Payment Gateways | <?php echo $_SESSION['systemName']; ?></title>
    </head>
    <body class="app sidebar-mini">

        <!-- Navbar-->
        <?php include("library/header.php");?>
        <!-- Sidebar menu-->
        <?php include("library/sidebar.php");?>
        <main class="app-content">
            <div class="app-title">
                <div>
                    <h4 class="text-primary"><i class="fa fa-bank fa-lg"></i> PAYMENT GATEWAYS</h4>
                </div>
                <ul class="app-breadcrumb breadcrumb">
                    <li class="breadcrumb-item"><i class="fa fa-home"></i></li>
                    <li class="breadcrumb-item"><a href="payment-gateways">Payment Gateways</a></li>
                </ul>
            </div>

            <?php include("models/admin_config.php");?>
            <?php include("models/system_config.php");?>

            <!-- Users page -->
            <div class="row">
            
                <div class="col-md-4">
					<div class="tile">
                        <form action="#" method="POST" id="registerPaymentGatewayForm" enctype="multipart/form-data">
                            <div class="row">
                                <div class="form-group col-md-12">
									<h5 class="text-primary"><i class="fa fa-user-plus"></i> Register New Payment Gateway</h5>
								</div>
                                <div class="form-group col-sm-12">
                                    <label class="control-label">Gateway Type</label>
                                    <select class="form-control" style="width: 100%;" name="cmbGatewayType" id="cmbGatewayType" required>
                                        <option selected value="">Select Type</option>
                                        <option value="1">Bank Payment</option>
                                        <option value="2">Mobile Payment</option>
                                        <option value="3">Cash Payment</option>
                                    </select>
                                </div>
                                <div class="form-group col-md-12">
                                    <label class="control-label" id="gatewayNameLabel">Name</label>
                                    <input type="text" class="form-control" name="txtGatewaysGatewayName" id="txtGatewaysGatewayName" placeholder="Enter name" required>
                                    <input class="form-control" type="hidden" name="txtUserrIddd" id="txtUserrIddd" value="<?php echo $_SESSION['userIdd']; ?>" required>
                                    <input class="form-control" type="hidden" name="txtGatewayRegisterDate" id="txtGatewayRegisterDate" value="<?php echo date("Y-m-d H:i:s"); ?>" required>
                                </div>
                                <div class="form-group col-md-5 text-center">
                                    <br>
                                    <img class='img-responsive' src="img/upload_gateway_image.png" id="gatewayTarget" width="80%" height="100px" /><br><br>
                                    <label class="btn btn-success col-md-12">
                                        <i class="fa fa-upload"> </i> Upload <input type="file" name="gatewayPhoto" id="gatewayPhoto" hidden>
                                    </label>
                        
                                    <script>
                                        var gateway_src = document.getElementById("gatewayPhoto");
                                        var gateway_target = document.getElementById("gatewayTarget");
        
                                        function showGatewayImage(gateway_src, gateway_target) {
                                            var upl = new FileReader();
                                            upl.onload = function (e) {
                                                gateway_target.src = this.result;
                                            };
                                            gateway_src.addEventListener("change", function () {
                                                upl.readAsDataURL(gateway_src.files[0]);
                                            })
                                        }
                                        showGatewayImage(gateway_src, gateway_target);
                                    </script>
                                </div>
                                <div class="form-group col-md-12 mt-3" style="text-align: right;">
                                    <button type="submit" class="btn btn-primary" name="btnRegisterGateway" id="btnRegisterGateway"><i class="fa fa-fw fa-lg fa-save"></i>Register Gateway</button>&nbsp;&nbsp;&nbsp;
                                    <button type="reset" class="btn btn-danger" data-dismiss="modal"><i class="fa fa-fw fa-lg fa-times"></i>Close</button>
                                </div>
                            </div>

                        </form>
					</div>
				</div>


                <div class="col-md-8">
                    <div class="tile">
                        <div class="tile-body">
                            <div class="row">
								<div class="form-group col-md-12">
									<h5><span><i class="fa fa-list-ul"></i> Payment Gateways List</span></h5>
									<hr />
								</div>
							</div>
                            <div class="table-responsive">
								<table class="table table-bordered table-hover table-sm" id="paymentGatewaysTable">
                                    <thead>
										<tr class="text-center bg-primary text-light" height="35px">
                                            <th class="align-middle">Serial</th>
                                            <th class="align-middle">Gateway Logo</th>
                                            <th class="align-middle">Type</th>
                                            <th class="align-middle">Name</th>
                                            <th class="align-middle">Created By</th>
                                            <th class="align-middle">Create Date</th>
                                            <?php if (($_SESSION['userTypeID'] != 'PS0001') || ($role_rs[4] == '0' && $role_rs[5] == '0')) { } else {?><th class="align-middle"><center>Action</center></th><?php } ?>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            $getBankCompanies = mysqli_query($conn, "SELECT payment_gateways.gatewayID, payment_gateways.gatewayType, payment_gateways.gatewayName, system_users.Username, payment_gateways.registerDate, payment_gateways.gatewayLogo 
                                            FROM payment_gateways 
                                            JOIN system_users ON payment_gateways.registeredBy = system_users.userID 
                                            ORDER BY payment_gateways.gatewayName ASC") or die(mysqli_error($conn));
                                            $x = 1;
                                            $bank_comp_types = array("Bank Payment", "Mobile Payment", "Cash Payment");
                                            while ($rs = mysqli_fetch_array($getBankCompanies)) {?>
                                                <tr>
                                                    <td class="text-center align-middle"><?php echo $x; ?></td>
                                                    <td class="py-1 align-middle text-center"><img class="img-fluid rounded-square" src="<?php echo $rs[5]; ?>" width="35px" height="35px"/></td>
                                                    <td class="align-middle text-left"><?php echo $bank_comp_types[$rs[1] - 1]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $rs[2]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $rs[3]; ?></td>
                                                    <td class="align-middle text-center"><?php echo substr($rs[4], 0, 10); ?></td>
                                                    <?php
                                                        if ($_SESSION['userTypeID'] == 'PS0001') {
                                                            ?><td class="align-middle text-center">
                                                                <div class="btn-group col-md-12">
                                                                    <button type="button" class="btn btn-primary">Action</button>
                                                                    <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
                                                                        <span class="caret"></span>
                                                                    </button>
                                                                    <div class="dropdown-menu" style="min-width: 8rem;">
                                                                        <a href="#" class="dropdown-item text-primary btnUpdateGateway" id="<?php echo "Update". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>" data-toggle="modal" data-target="#updatePaymentGatewaysModal"><i class="fa fa-fw fa-lg fa-edit"></i> Edit</a>
                                                                        <a href="#" class="dropdown-item text-danger btnDeleteGateway" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</a>
                                                                    </div>
                                                                </div>
                                                            </td><?php
                                                        } else {
                                                            if ($role_rs[4] == '1' && $role_rs[5] == '1') {
                                                                ?><td class="align-middle text-center">
                                                                    <div class="btn-group col-md-12">
                                                                        <button type="button" class="btn btn-primary">Action</button>
                                                                        <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
                                                                            <span class="caret"></span>
                                                                        </button>
                                                                        <div class="dropdown-menu" style="min-width: 8rem;">
                                                                            <a href="#" class="dropdown-item text-primary btnUpdateGateway" id="<?php echo "Update". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>" data-toggle="modal" data-target="#updatePaymentGatewaysModal"><i class="fa fa-fw fa-lg fa-edit"></i> Edit</a>
                                                                            <a href="#" class="dropdown-item text-danger btnDeleteGateway" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</a>
                                                                        </div>
                                                                    </div>
                                                                </td><?php
                                                            } else if ($role_rs[4] == '1') {
                                                                ?><td class="align-middle text-center">
                                                                    <div class="btn-group col-md-12">
                                                                        <button type="button" class="btn btn-primary">Action</button>
                                                                        <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
                                                                            <span class="caret"></span>
                                                                        </button>
                                                                        <div class="dropdown-menu" style="min-width: 8rem;">
                                                                            <a href="#" class="dropdown-item text-primary btnUpdateGateway" id="<?php echo "Update". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>" data-toggle="modal" data-target="#updatePaymentGatewaysModal"><i class="fa fa-fw fa-lg fa-edit"></i> Edit</a>
                                                                        </div>
                                                                    </div>                                                                    
                                                                </td><?php
                                                            } else if ($role_rs[5] == '1') {
                                                                ?><td class="align-middle text-center">
                                                                    <div class="btn-group col-md-12">
                                                                        <button type="button" class="btn btn-primary">Action</button>
                                                                        <button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown">
                                                                            <span class="caret"></span>
                                                                        </button>
                                                                        <div class="dropdown-menu" style="min-width: 8rem;">
                                                                            <a href="#" class="dropdown-item text-danger btnDeleteGateway" id="<?php echo "Delete". $rs[0]; ?>" data-id="<?php echo $rs[0]; ?>"><i class="fa fa-fw fa-lg fa-trash"></i> Delete</a>
                                                                        </div>
                                                                    </div>   
                                                                </td><?php
                                                            }
                                                        }
                                                    ?>
                                                </tr><?php
                                                $x++;
                                            }
                                        ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        
    	<!-- Essential javascripts for application to work-->
    	<?php include("library/footer.php");?>	
        <?php include("library/scripts.php");?>
    	<script type="text/javascript">$('#paymentGatewaysTable').DataTable({"pageLength": 100});</script>
    	<script type="text/javascript"> $("#cmbGatewayType").select2({ tags: false });</script>

    </body>
</html>