<?php include '../header.php'; ?>
<?php include '../sidemenu.php'; ?>
<?php
$id = "";
if(isset($_GET['id'])){
	if(!empty($_GET['id'])){
		$id = $_GET['id'];
	}
}
?>	
<style>
	input[type=checkbox]{
		margin-right: 6px;
	}
</style>
<div class="content-wrapper">
	<section class="content-header">
		<div class="container-fluid">
			<div class="row mb-2">
					<h1>
						<?php if($id==""){ ?>
						Create User Role
						<?php }else{ ?>
						Edit User Role
						<?php } ?>
					</h1>
			</div>
		</div>
	</section>
	<section class="content">
		<div class="container-fluid">
			<div class="card card-outline card-primary">
				<div class="card-header">
					<h3 class="card-title">User Role Detail</h3>
					<div class="card-tools">
						<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>	
					</div>
				</div>
					<form role="form" id="frmEntry">
					<div class="card-body">
						<div class="form-group row">
							<input type="hidden" name="newrole" value="<?=((isset($_GET["id"]))?"update":"create")?>"/>
							<label class="col-lg-2 col-form-label" style="text-align: right;">Role Name:</label>
							<div class="col-lg-4">
								<input class="form-control" id="rolename" type="text" maxlength="255" <?=((isset($_GET["id"]))?"readonly='readonly'":"")?>/>
							</div>
							<div class="col-lg-6"></div>
						</div>

						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Admin</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >	
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="create_role" class="admin">
				                        <label for="create_role">User Role</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="create_account" class="admin">
				                        <label for="create_account">User Account</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="change_password" class="admin">
				                        <label for="change_password">Change Password</label>
				                    </div>
								</div>
							</div>
						</div>

						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Entry</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="ont" class="entry">
				                        <label for="ont">ONU</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="package" class="entry">
				                        <label for="package">Internet Plan</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="price_change" class="entry">
				                        <label for="price_change">Monthly Price</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="promotion" class="entry">
				                        <label for="promotion">Promotion</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="member" class="entry">
				                        <label for="member">Member Type</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="customer" class="entry">
				                        <label for="customer">Customer</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="extra_days" class="entry">
				                        <label for="extra_days">Extra Days</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="suspend" class="entry">
				                        <label for="suspend">Suspend</label>
				                    </div>
								</div>
							</div>
						</div>		

						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Process</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="bank_in_out_list" class="process">
				                        <label for="bank_in_out_list">Bank In Out List</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="order_list" class="process">
				                        <label for="order_list">Order</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="installation" class="process">
				                        <label for="installation">Installation</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="sales" class="process">
				                        <label for="sales">Sales</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="activate_list" class="process">
				                        <label for="activate_list">Activate List</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="item_sales" class="process">
				                        <label for="item_sales">Item Sales</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="complaint" class="process">
				                        <label for="complaint">Complaint</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="sms" class="process">
				                        <label for="sms">SMS</label>
				                    </div>
								</div>
							</div>
						</div>	

						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Inventory</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="item" class="inventory">
				                        <label for="item">Item</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="stock_in" class="inventory">
				                        <label for="stock_in">Stock In</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="stock_out" class="inventory">
				                        <label for="stock_out">Stock Out</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="stock_transfer" class="inventory">
				                        <label for="stock_transfer">Stock Transfer</label>
				                    </div>
								</div>
							</div>
						</div>	

						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Accounting</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="expense" class="accounting">
				                        <label for="expense">Expense</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="income" class="accounting">
				                        <label for="income">Income</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="transfer" class="accounting">
				                        <label for="transfer">Transfer</label>
				                    </div>
								</div>
							</div>
						</div>

						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Inventory Report</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="item_sales_rpt" class="inventoryreport">
				                        <label for="item_sales_rpt">Item Sales</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="stock_in_rpt" class="inventoryreport">
				                        <label for="stock_in_rpt">Stock In</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="stock_out_rpt" class="inventoryreport">
				                        <label for="stock_out_rpt">Stock Out</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="stock_transfer_rpt" class="inventoryreport">
				                        <label for="stock_transfer_rpt">Stock Transfer</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="stock_balance_rpt" class="inventoryreport">
				                        <label for="stock_balance_rpt">Stock Balance</label>
				                    </div>
								</div>
							</div>
						</div>

						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Accounting Report</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="expense_rpt" class="accountingreport">
				                        <label for="expense_rpt">Expense</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="income_rpt" class="accountingreport">
				                        <label for="income_rpt">Income</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="transfer_rpt" class="accountingreport">
				                        <label for="transfer_rpt">Transfer</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="cash_book_rpt" class="accountingreport">
				                        <label for="cash_book_rpt">Cash Book</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="monthly_rpt" class="accountingreport">
				                        <label for="monthly_rpt">Monthly</label>
				                    </div>
								</div>
							</div>
						</div>
						
						<legend> </legend>
						<h5><span style="font-weight: bold;margin-left:10px;">Reports</span></h5>
						<div class="form-group row">
							<label class="col-lg-1 col-form-label"></label>
							<div class="col-lg-9">
								<div class="panel-body" >
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="control_rpt" class="report">
				                        <label for="control_rpt">Control</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="sales_rpt" class="report">
				                        <label for="sales_rpt">Sales</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="payment_rpt" class="report">
				                        <label for="payment_rpt">Credit Sale</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="sales_by_mbps_rpt" class="report">
				                        <label for="sales_by_mbps_rpt">Sales (Mbps-Months)</label>
				                    </div>
				                    <div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="installation_rpt" class="report">
				                        <label for="installation_rpt">Installation</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="assign_rpt" class="report">
				                        <label for="assign_rpt">Assign</label>
				                    </div>
									<div class="col-lg-4 icheck-success d-inline">
				                        <input type="checkbox" id="expire_rpt" class="report">
				                        <label for="expire_rpt">Expire</label>
				                    </div>
								</div>
							</div>
						</div>

						<div class="form-group row">
								<div class="col-md-6"></div>
								<?php if($id==""){ ?>
								<div class="col-md-2" style="text-align: right;">
									<button class="btn btn-info" type="button" onclick="clearForm()">Clear</button>
								</div>
								<div class="col-md-2">
									<button id="btnSubmit" class="btn btn-success" type="button" value="Create" onclick="validateAndSave()">Create</button>
								</div>
								<?php }else{ ?>
								<div class="col-md-2"></div>
								<div class="col-md-2">
									<button id="btnSubmit" class="btn btn-info" type="button" value="Update" onclick="validateAndUpdate()">Update</button>
								</div>
								<?php } ?>
								<div class="col-md-2"></div>
						</div>
					</div>
				</form>
			</div>
		</div>
	</section>
</div>
<?php include '../footer.php'; ?>
<script>	
var id = '<?=$id;?>';

$(document).ready(function () {
	$('body').addClass('sidebar-collapse');
	if (id) getRoleCheck(id);
});

function validate() {
    if (!$('#rolename').val().trim()) {
        bootbox.alert("User Role shouldn't be blank.");
        return false;
    }
    return true;
}

function clearForm() {
    $('#frmEntry')[0].reset();
}

function collectRoleData() {
    const role = { "rolename": $('#rolename').val() };

    $("input[type='checkbox']").each(function () {
        const key = $(this).attr('id').replace('chk', '').toLowerCase();  // directly use the checkbox ID
        role[key] = $(this).prop('checked') ? 1 : 0;
    });

    return role;
}


function saveRole(apiEndpoint) {
    if (!validate()) return;

    $.post(APP_URL + apiEndpoint, JSON.stringify(collectRoleData()), function (data) {
        if (data === "created" || data === "updated") {
            // bootbox.alert("User Role " + (data === "created" ? "Created." : "Updated."));
            if (data === "updated") document.location = APP_URL + "admin/userrole.php";
            else clearForm();
        } else {
            bootbox.alert(data === "duplicate" ? "Duplicate entry not allowed." : "Server error.");
        }
    });
}

function saveRole1(apiEndpoint) {
    if (!validate()) return;

    $.post(APP_URL + apiEndpoint, JSON.stringify(collectRoleData()), function (data) {
        if (data === "created" || data === "updated") {
            // bootbox.alert("User Role " + (data === "created" ? "Created." : "Updated."));
            if (data === "updated") document.location = APP_URL + "admin/userrole.php";
            else clearForm();
        } else {
            bootbox.alert(data === "duplicate" ? "Duplicate entry not allowed." : "Server error.");
        }
    });
}

function validateAndSave() {
    saveRole("api/createrole/create.php");
}

function validateAndUpdate() {
    saveRole1("api/createrole/update.php");
}

function getRoleCheck(id) {
    $.post(APP_URL + "api/createrole/get_one_row.php", JSON.stringify({ id }), function (data) {
        if (data.rolename) {
            $('#rolename').val(data.rolename);
			$('#txtID').val(data.id);
            $("input[type='checkbox']").each(function () {
                // Get the checkbox's id and map it to the correct field in the data object
                var fieldName = $(this).attr('id').replace('chk', '').toLowerCase();
                
                // Ensure the field exists in the data object and check the value properly
                if (data.hasOwnProperty(fieldName)) {
                    // Check if the value is '1' (or any other value that should mean checked)
                    $(this).prop('checked', data[fieldName] === "1" || data[fieldName] === 1);
                }
            });
        }
    });
}

$("#chkAdmin").change(function () {
	if($("#chkAdmin").prop('checked')){
		$(".admin").prop( "checked", true );
	}else{
		$(".admin").prop( "checked", false );
	}	
});

$("#chkEntry").change(function () {
	if($("#chkEntry").prop('checked')){
		$(".entry").prop( "checked", true );
	}else{
		$(".entry").prop( "checked", false );
	}	
});

$("#chkProcess").change(function () {
	if($("#chkProcess").prop('checked')){
		$(".process").prop( "checked", true );
	}else{
		$(".process").prop( "checked", false );
	}	
});

$("#chkInventory").change(function () {
	if($("#chkInventory").prop('checked')){
		$(".inventory").prop( "checked", true );
	}else{
		$(".inventory").prop( "checked", false );
	}	
});

$("#chkAccounting").change(function () {
	if($("#chkAccounting").prop('checked')){
		$(".accounting").prop( "checked", true );
	}else{
		$(".accounting").prop( "checked", false );
	}	
});

$("#chkInventoryReport").change(function () {
	if($("#chkInventoryReport").prop('checked')){
		$(".inventoryreport").prop( "checked", true );
	}else{
		$(".inventoryreport").prop( "checked", false );
	}	
});

$("#chkAccountingReport").change(function () {
	if($("#chkAccountingReport").prop('checked')){
		$(".accountingreport").prop( "checked", true );
	}else{
		$(".accountingreport").prop( "checked", false );
	}	
});

$("#chkReport").change(function () {
	if($("#chkReport").prop('checked')){
		$(".report").prop( "checked", true );
	}else{
		$(".report").prop( "checked", false );
	}	
});
</script>	