<?php include '../header.php'; ?> 
<?php include '../sidemenu.php'; ?> 
<div class="page-header d-print-none">
    <div class="container-xl">
      <div class="row g-2 align-items-center">
        <div class="col d-flex justify-content-between">
          <h2 class="page-title">
          User Role
          </h2>
		  <button class="btn btn-outline-success" data-bs-toggle="modal" data-bs-target="#createRoleModal" id="btnCreateNew">Create User Role</button>
        </div>
      </div>
    </div>
</div>
<div class="container" id="alertContainer1"></div>
<div class="page-body">
    <div class="container-xl">
        <div class="row row-cards">
			<div class="col-12">
				<div class="card">
				<!-- <div class="card-header d-flex justify-content-between">
                    <h3 class="card-title">Role List</h3>
                    <button class="btn btn-outline-success" data-bs-toggle="modal" data-bs-target="#createAccountModel">Create New Account</button>
                </div> -->
				<div class="card-body">
					<div class="table-responsive">
						<table class="table table-vcenter card-table" id="myTable">
							<thead>
							<tr>
								<th>Role Name</th>
								<th class="w-1"></th>
							</tr>
							</thead>
							<tbody>
							</tbody>
						</table>
						</div>
					</div>
				</div>
			</div>
        </div>
    </div>
</div>


<div class="modal fade" id="createRoleModal" tabindex="-1" aria-labelledby="createRoleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="createRoleModalLabel">Create User Role</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="createRoleForm">
				<input type="hidden" class="form-control" id="txtRoleId" >
                    <div class="mb-3">
                        <label for="txtRoleName" class="form-label">Role Name</label>
                        <input type="text" class="form-control" id="txtRoleName" placeholder="Enter role name" required>
                    </div>
                    <div class="row">
						<div class="col-md-3">
							<h6 class="mt-4">Admin</h6> 
							
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkCreateRole" value="true">
								<label class="form-check-label" for="chkCreateRole">User Role</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkEmployee" value="true">
								<label class="form-check-label" for="chkEmployee">Employee</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkCreateAccount" value="true">
								<label class="form-check-label" for="chkCreateAccount">User Account</label>
							</div>
							<div class="form-check mb-3" hidden>
								<input type="checkbox" class="form-check-input" id="chkChangePassword" value="true">
								<label class="form-check-label" for="chkChangePassword">Change Password</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkInvestor" value="true">
								<label class="form-check-label" for="chkInvestor">Investor</label>
							</div>
						</div>
						<div class="col-md-3">
							<h6 class="mt-4">Entry</h6> 
							
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkRoom" value="true">
								<label class="form-check-label" for="chkRoom">Vehicle</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkAgent" value="true">
								<label class="form-check-label" for="chkAgent">Driver</label>
							</div>
							<div class="form-check mb-3" hidden>
								<input type="checkbox" class="form-check-input" id="chkItem" value="true">
								<label class="form-check-label" for="chkItem">Item</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkDutySchedule" value="true">
								<label class="form-check-label" for="chkDutySchedule">Duty Schedule</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkAttendance" value="true">
								<label class="form-check-label" for="chkAttendance">Attendance</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkAttendanceUpdate" value="true">
								<label class="form-check-label" for="chkAttendanceUpdate">Attendance Update</label>
							</div>
						</div>
						<div class="col-md-3">
							<h6 class="mt-4">Operations</h6> 
							
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkReservation" value="true">
								<label class="form-check-label" for="chkReservation">Booking</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkUpcomingArrivals" value="true">
								<label class="form-check-label" for="chkUpcomingArrivals">Upcoming Pickups</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkArrival" value="true">
								<label class="form-check-label" for="chkArrival">Pickup List</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkOccupiedList" value="true">
								<label class="form-check-label" for="chkOccupiedList">Active Rentals</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkDeletedReservationList" value="true">
								<label class="form-check-label" for="chkDeletedReservationList">Deleted Bookings</label>
							</div>
						</div>
						<div class="col-md-3">
							<h6 class="mt-4">Billing</h6> 
							
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkSalesService" value="true">
								<label class="form-check-label" for="chkSalesService">Sales & Service</label>
							</div>
						</div>
						<div class="col-md-3" hidden>
							<h6 class="mt-4">Inventory</h6> 
							
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkStockIn" value="true">
								<label class="form-check-label" for="chkStockIn">Stock In</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkStockOut" value="true">
								<label class="form-check-label" for="chkStockOut">Stock Out</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkStockInReport" value="true">
								<label class="form-check-label" for="chkStockInReport">Stock In Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkStockOutReport" value="true">
								<label class="form-check-label" for="chkStockOutReport">Stock Out Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkStockBalanceReport" value="true">
								<label class="form-check-label" for="chkStockBalanceReport">Stock Balance Report</label>
							</div>
						</div>
						<div class="col-md-3">
							<h6 class="mt-4">Transactions</h6> 
							
							<div class="form-check mb-3" hidden>
								<input type="checkbox" class="form-check-input" id="chkPurchase" value="true">
								<label class="form-check-label" for="chkPurchase">Purchase</label>
							</div>
							<div class="form-check mb-3" hidden>
								<input type="checkbox" class="form-check-input" id="chkPurchaseReport" value="true">
								<label class="form-check-label" for="chkPurchaseReport">Purchase Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkMonthlyReport" value="true">
								<label class="form-check-label" for="chkMonthlyReport">Monthly Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkCashBookReport" value="true">
								<label class="form-check-label" for="chkCashBookReport">Cash Book Report</label>
							</div>
						</div>
						<div class="col-md-3">
							<h6 class="mt-4">Accounting</h6> 
							
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkCOA" value="true">
								<label class="form-check-label" for="chkCOA">COA</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkExpense" value="true">
								<label class="form-check-label" for="chkExpense">Expense</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkIncome" value="true">
								<label class="form-check-label" for="chkIncome">Income</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkExpenseReport" value="true">
								<label class="form-check-label" for="chkExpenseReport">Expense Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkIncomeReport" value="true">
								<label class="form-check-label" for="chkIncomeReport">Income Report</label>
							</div>
						</div>
						<div class="col-md-3">
							<h6 class="mt-4">Reports</h6> 
							
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkAttendanceReport" value="true">
								<label class="form-check-label" for="chkAttendanceReport">Attendance Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkGuestReport" value="true">
								<label class="form-check-label" for="chkGuestReport">Customer Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkArrivalReport" value="true">
								<label class="form-check-label" for="chkArrivalReport">Pickup Report</label>
							</div>
							<div class="form-check mb-3">
								<input type="checkbox" class="form-check-input" id="chkDepartureReport" value="true">
								<label class="form-check-label" for="chkDepartureReport">Return Report</label>
							</div>
						</div>
						</div>
					</div>
                </form>
				<div class="modal-footer">
					<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Close</button>
					<button type="button" id="btnSave" class="btn btn-outline-success" onclick="validateAndSave()">Create</button>
				</div>
            </div>
            
        </div>
    </div>
</div>


<?php include '../footer.php'; ?>
<script>
$(function() {
	getAll();
});

function getAll(){
	$("#myTable").find("tbody").find("tr").remove();
	$.ajax({
		url: APP_URL + "api/createrole/get_all_rows.php"
	}).done(function(data) {
		console.log(data);
		$.each(data.records, function(i, v) {
			console.log(v);
			$("#myTable").find("tbody")
			.append($('<tr>')
				.append("<td>" + v.rolename + "</td>")
				.append("<td><div class='btn-list flex-nowrap'><button class='btn btn-outline-primary btn-sm' onclick='editUserRole(\"" + v.id + "\")'>Edit</button><button class='btn btn-outline-danger btn-sm' onclick='del(\"" + v.id + "\",this)'>Delete</button></div></td>")
				)
		});
	});
}



function validate(){

if($("#txtRoleName").val().trim()==""){showAlert("User Role shouldn't be blank.","warning");return false;}
	return true;
}

function validateAndSave(){
	if(validate()){

		var role = {
			"id":$("#txtRoleId").val(),
			"rolename":$("#txtRoleName").val(),
			"create_role":($("#chkCreateRole").prop('checked'))?1:0,
			"employee":($("#chkEmployee").prop('checked'))?1:0,
			"create_account":($("#chkCreateAccount").prop('checked'))?1:0,
			"change_password":($("#chkChangePassword").prop('checked'))?1:0,
			"investor":($("#chkInvestor").prop('checked'))?1:0,
			"room":($("#chkRoom").prop('checked'))?1:0,
			"agent":($("#chkAgent").prop('checked'))?1:0,
			"item":($("#chkItem").prop('checked'))?1:0,
			"duty_schedule":($("#chkDutySchedule").prop('checked'))?1:0,
			"attendance":($("#chkAttendance").prop('checked'))?1:0,
			"attendance_update":($("#chkAttendanceUpdate").prop('checked'))?1:0,
			"reservation":($("#chkReservation").prop('checked'))?1:0,
			"upcoming_arrivals":($("#chkUpcomingArrivals").prop('checked'))?1:0,
			"arrival":($("#chkArrival").prop('checked'))?1:0,
			"occupied_list":($("#chkOccupiedList").prop('checked'))?1:0,
			"deleted_reservation_list":($("#chkDeletedReservationList").prop('checked'))?1:0,
			"sales_service":($("#chkSalesService").prop('checked'))?1:0,
			"stock_in":($("#chkStockIn").prop('checked'))?1:0,
			"stock_out":($("#chkStockOut").prop('checked'))?1:0,
			"stock_in_report":($("#chkStockInReport").prop('checked'))?1:0,
			"stock_out_report":($("#chkStockOutReport").prop('checked'))?1:0,
			"stock_balance_report":($("#chkStockBalanceReport").prop('checked'))?1:0,
			"purchase":($("#chkPurchase").prop('checked'))?1:0,
			"purchase_report":($("#chkPurchaseReport").prop('checked'))?1:0,
			"monthly_report":($("#chkMonthlyReport").prop('checked'))?1:0,
			"cash_book_report":($("#chkCashBookReport").prop('checked'))?1:0,
			"coa":($("#chkCOA").prop('checked'))?1:0,
			"expense":($("#chkExpense").prop('checked'))?1:0,
			"income":($("#chkIncome").prop('checked'))?1:0,
			"expense_report":($("#chkExpenseReport").prop('checked'))?1:0,
			"income_report":($("#chkIncomeReport").prop('checked'))?1:0,
			"attendance_report":($("#chkAttendanceReport").prop('checked'))?1:0,
			"guest_report":($("#chkGuestReport").prop('checked'))?1:0,
			"arrival_report":($("#chkArrivalReport").prop('checked'))?1:0,
			"departure_report":($("#chkDepartureReport").prop('checked'))?1:0
		};

		$.ajax({
			type: "POST",
			url: APP_URL + "api/createrole/create_update.php",
			data: JSON.stringify(role),
			success: function(data){	
				$('#createRoleModal').modal('hide');
				if(data.message=="created"){
					showAlert("Role Created Successfully!", "success",1);
					getAll();
				}else if(data.message=="updated"){
					showAlert("Role Updated Successfully!", "success",1);
					getAll();
				}else if(data.message=="duplicate"){
					showAlert("Duplicate Entry!", "warning",1);
				}else{
					showAlert("Error on server side!", "danger",1);
				}
			}
		});
	}
}

$("#btnCreateNew").click(function(){
		$("#btnSave").text("Create");
		$("#btnSave").removeClass("btn-outline-primary").addClass("btn-outline-success");
		clear();
	});

function editUserRole(id){
	clear();
	$("#btnSave").text("Update");
	$("#btnSave").removeClass("btn-outline-success").addClass("btn-outline-primary");

	$('#createRoleModal').modal('show');
	$("#txtRoleId").val(id);
	$.ajax({
		url: APP_URL + "api/createrole/get_one_row.php",
		type: "POST",
		data: JSON.stringify({ id: id })
	}).done(function(data){	
		if(data.rolename){
			$("#txtRoleName").val(data.rolename);
			var rn = data.rolename;

			if(data.create_role == "1"){
				$("#chkCreateRole").prop("checked", true);
			}

			if(data.employee == "1"){
				$("#chkEmployee").prop("checked", true);
			}

			if(data.create_account == "1"){
				$("#chkCreateAccount").prop("checked", true);
			}

			if(data.change_password == "1"){
				$("#chkChangePassword").prop("checked", true);
			}

			if(data.investor == "1"){
				$("#chkInvestor").prop("checked", true);
			}

			if(data.room == "1"){
				$("#chkRoom").prop("checked", true);
			}

			if(data.agent == "1"){
				$("#chkAgent").prop("checked", true);
			}

			if(data.item == "1"){
				$("#chkItem").prop("checked", true);
			}

			if(data.duty_schedule == "1"){
				$("#chkDutySchedule").prop("checked", true);
			}

			if(data.attendance == "1"){
				$("#chkAttendance").prop("checked", true);
			}

			if(data.attendance_update == "1"){
				$("#chkAttendanceUpdate").prop("checked", true);
			}

			if(data.reservation == "1"){
				$("#chkReservation").prop("checked", true);
			}

			if(data.upcoming_arrivals == "1"){
				$("#chkUpcomingArrivals").prop("checked", true);
			}

			if(data.arrival == "1"){
				$("#chkArrival").prop("checked", true);
			}

			if(data.occupied_list == "1"){
				$("#chkOccupiedList").prop("checked", true);
			}
			
			if(data.deleted_reservation_list == "1"){
				$("#chkDeletedReservationList").prop("checked", true);
			}

			if(data.sales_service == "1"){
				$("#chkSalesService").prop("checked", true);
			}

			if(data.stock_in == "1"){
				$("#chkStockIn").prop("checked", true);
			}

			if(data.stock_out == "1"){
				$("#chkStockOut").prop("checked", true);
			}

			if(data.stock_in_report == "1"){
				$("#chkStockInReport").prop("checked", true);
			}

			if(data.stock_out_report == "1"){
				$("#chkStockOutReport").prop("checked", true);
			}

			if(data.stock_balance_report == "1"){
				$("#chkStockBalanceReport").prop("checked", true);
			}

			if(data.purchase == "1"){
				$("#chkPurchase").prop("checked", true);
			}

			if(data.purchase_report == "1"){
				$("#chkPurchaseReport").prop("checked", true);
			}

			if(data.monthly_report == "1"){
				$("#chkMonthlyReport").prop("checked", true);
			}

			if(data.cash_book_report == "1"){
				$("#chkCashBookReport").prop("checked", true);
			}

			if(data.coa == "1"){
				$("#chkCOA").prop("checked", true);
			}

			if(data.expense == "1"){
				$("#chkExpense").prop("checked", true);
			}

			if(data.income == "1"){
				$("#chkIncome").prop("checked", true);
			}

			if(data.expense_report == "1"){
				$("#chkExpenseReport").prop("checked", true);
			}

			if(data.income_report == "1"){
				$("#chkIncomeReport").prop("checked", true);
			}

			if(data.attendance_report == "1"){
				$("#chkAttendanceReport").prop("checked", true);
			}

			if(data.guest_report == "1"){
				$("#chkGuestReport").prop("checked", true);
			}

			if(data.arrival_report == "1"){
				$("#chkArrivalReport").prop("checked", true);
			}

			if(data.departure_report == "1"){
				$("#chkDepartureReport").prop("checked", true);
			}


		}else{
			console.log(data);
		}
	});
}

function del(id, obj) {

$('#modal-delete').modal('show');
	$('#confirm-delete').off('click').on('click', function () {
		$.ajax({
			type: "POST",
			url: APP_URL + "api/createrole/delete.php",
			data: JSON.stringify({ id: id }),
			success: function (data) {
				if (data === "deleted") {
					$(obj).closest('tr').remove();
					showAlert("Role Deleted Successfully!", "success",1);
				} else {
					showAlert("Error on server side!", "danger",1);
				}
				$('#modal-delete').modal('hide');
			}
		});
	});
}

function clear(){
	$("#txtRoleId").val("");
	$("#txtRoleName").val("");
// Uncheck all checkboxes in the form
	$("#chkCreateRole").prop("checked", false);
	$("#chkCreateAccount").prop("checked", false);
	$("#chkChangePassword").prop("checked", false);
	$("#chkEmployee").prop("checked", false);
	$("#chkInvestor").prop("checked", false);
	$("#chkRoom").prop("checked", false);
	$("#chkAgent").prop("checked", false);
	$("#chkItem").prop("checked", false);
	$("#chkDutySchedule").prop("checked", false);
	$("#chkAttendance").prop("checked", false);
	$("#chkAttendanceUpdate").prop("checked", false);
	$("#chkReservation").prop("checked", false);
	$("#chkUpcomingArrivals").prop("checked", false);
	$("#chkArrival").prop("checked", false);
	$("#chkOccupiedList").prop("checked", false);
	$("#chkDeletedReservationList").prop("checked", false);
	$("#chkSalesService").prop("checked", false);
	$("#chkStockIn").prop("checked", false);
	$("#chkStockOut").prop("checked", false);
	$("#chkStockInReport").prop("checked", false);
	$("#chkStockOutReport").prop("checked", false);
	$("#chkStockBalanceReport").prop("checked", false);
	$("#chkPurchase").prop("checked", false);
	$("#chkPurchaseReport").prop("checked", false);
	$("#chkMonthlyReport").prop("checked", false);
	$("#chkCashBookReport").prop("checked", false);
	$("#chkCOA").prop("checked", false);
	$("#chkExpense").prop("checked", false);
	$("#chkIncome").prop("checked", false);
	$("#chkExpenseReport").prop("checked", false);
	$("#chkIncomeReport").prop("checked", false);
	$("#chkAttendanceReport").prop("checked", false);
	$("#chkGuestReport").prop("checked", false);
	$("#chkArrivalReport").prop("checked", false);
	$("#chkDepartureReport").prop("checked", false);

}

function showAlert(message, type) {
    Swal.fire({
        title: type === 'success' ? 'Success!' : 'Oops!',
        text: message,
        icon: type,
        // confirmButtonText: 'OK',
        timer: 1000, // Auto close after 3000 milliseconds (3 seconds)
        timerProgressBar: true, // Optional: Show a progress bar
        willClose: () => {
            // Optional: Execute code just before the alert closes
            console.log('Alert closed');
        }
    });
}
</script>	
