<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$created = can_action('70', 'created');
$edited = can_action('70', 'edited');
if (!empty($created) || !empty($edited)) {
	$is_inline_modal = !empty($inline_modal);
	$form_action = $is_inline_modal ? base_url('admin/departments/save_inline_departments') : (base_url() . 'admin/departments/save_departments/' . (!empty($departments_info->departments_id) ? $departments_info->departments_id : ''));
?>
	<form method="post" data-parsley-validate="" novalidate=""
		action="<?= $form_action ?>" class="form-horizontal">
		<?php if (!empty($departments_info)) {
			$details = $departments_info->deptname . ' ⇒ ' . $designations_info->designations . ' ' . lang('details');
		} else {
			$details = lang('new_department');
		} ?>
		<div class="space-y-6">
			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="border-b border-border px-4 py-4">
					<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
						<div>
							<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('departments') ?></p>
							<h2 class="text-xl font-semibold text-foreground"><?= $details ?></h2>
							<p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('departments') ?></p>
						</div>
						<div class="flex items-center gap-2">
							<button type="submit" name="save" value="1"
								class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
								<i data-lucide="save" class="h-4 w-4"></i>
								<?php echo !empty($departments_info->deptname) ? lang('update') : lang('save') ?>
							</button>

							<?php if (!$is_inline_modal) { ?>
								<button type="submit" name="save" value="2"
									class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted hidden-xs">
									<i data-lucide="plus-square" class="h-4 w-4"></i>
									<?php echo !empty($departments_info->deptname) ? lang('update') . ' & ' . lang('add_more') : lang('save') . ' & ' . lang('add_more') ?>
								</button>
							<?php } ?>
						</div>
					</div>
				</div>

				<div class="p-4">
					<div class="grid grid-cols-1 xl:grid-cols-12 gap-6">
						<div class="xl:col-span-4">
							<div class="rounded-xl border border-border bg-card p-5 space-y-5">
								<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
									<i data-lucide="building-2" class="h-5 w-5"></i>
									<h3 class="text-base font-semibold text-foreground"><?= lang('department') . ' ' . lang('details') ?></h3>
								</div>
								<div class="space-y-4">
									<?php
									$department_options = array('' => lang('new_department'));
									$all_department = $this->db->get('tbl_departments')->result();
									if (!empty($all_department)) {
										foreach ($all_department as $v_departments) {
											$deptname = !empty($v_departments->deptname) ? $v_departments->deptname : lang('undefined_department');
											$department_options[$v_departments->departments_id] = $deptname;
										}
									}
									$dept_selected = !empty($departments_info->departments_id) ? $departments_info->departments_id : '';
									echo form_select_group(
										'departments_id',
										lang('select') . ' ' . lang('department'),
										$department_options,
										$dept_selected,
										false,
										array('class' => 'select_box', 'id' => 'new_departments', 'data-live-search' => 'true')
									);
									?>
									<div class="new_departments" style="display: <?= (!empty($departments_info->departments_id) ? 'none' : 'block') ?>">
										<?=
										form_input_group(
											'deptname',
											lang('new_department'),
											'',
											'text',
											false,
											array('class' => 'new_departments', 'disabled' => !empty($departments_info->departments_id) ? 'disabled' : null)
										);
										?>
									</div>
									<?= form_input_group('designations', lang('designation'), !empty($designations_info->designations) ? $designations_info->designations : '', 'text', true); ?>
									<input type="hidden" name="designations_id" class="form-control"
										value="<?php if (!empty($designations_info->designations_id)) echo $designations_info->designations_id; ?>" />
								</div>
							</div>
						</div>
						<div class="xl:col-span-8">
							<div class="rounded-xl border border-border bg-card p-4">
								<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3 mb-3">
									<i data-lucide="shield-check" class="h-5 w-5"></i>
									<h3 class="text-base font-semibold text-foreground"><?= lang('permission') ?></h3>
								</div>
								<style>
									.permission-table {
										border: 1px solid var(--border);
										border-radius: 14px;
										overflow: hidden;
										background: var(--card);
									}

									.permission-grid {
										margin: 0;
										width: 100%;
										border-collapse: collapse;
									}

									.permission-grid th,
									.permission-grid td {
										padding: 8px 14px;
										font-size: 13px;
										vertical-align: middle;
										border-top: 1px solid var(--border);
									}

									.permission-grid thead th {
										background: var(--muted);
										color: var(--muted-foreground);
										font-size: 12px;
										text-transform: uppercase;
										letter-spacing: 0.06em;
										border-top: none;
									}

									.permission-grid tbody tr:hover {
										background: var(--muted);
									}

									.permission-grid th:nth-child(n+2),
									.permission-grid td:nth-child(n+2) {
										text-align: center;
										width: 110px;
									}

									.permission-grid th:first-child,
									.permission-grid td:first-child {
										text-align: left;
										min-width: 260px;
									}

									.permission-grid .dept-parent {
										background: var(--card);
										font-weight: 500;
									}

									.permission-grid .child {
										display: none;
									}

									.permission-grid tr.child.is-open {
										display: table-row;
									}

									.permission-grid .child td:first-child {
										border-left: 2px solid var(--border);
									}

									.permission-grid .c-checkbox {
										display: inline-flex;
										align-items: center;
										gap: 6px;
									}

									.permission-grid .c-checkbox label {
										display: inline-flex;
										align-items: center;
										gap: 6px;
										margin: 0;
									}

									.parent {
										width: 22px;
										height: 22px;
										margin-right: 8px;
										cursor: pointer;
										border: 1px solid var(--border);
										border-radius: 6px;
										display: inline-flex;
										align-items: center;
										justify-content: center;
										background: var(--card);
										transition: all 0.15s ease;
									}

									.parent:hover {
										background: var(--muted);
									}

									.parent.plus::before {
										content: "+";
										font-weight: 600;
									}

									.parent.minus::before {
										content: "–";
										font-weight: 600;
									}

									.parent span {
										display: none;
									}
								</style>

								<div class="permission-table overflow-x-auto">
									<table class="permission-grid" cellspacing="0" width="100%">

										<thead>
											<tr>
												<th>
													<div class="c-checkbox">
														<label class="needsclick" data-toggle="tooltip" data-placement="top"
															title="<?= lang('select_all') . ' ' . lang('permission') ?>">
															<input id="select_all" type="checkbox">
															
															<strong><?= lang('permission') ?></strong>
														</label>
													</div>
												</th>
												<th>
													<div class="c-checkbox">
														<label class="needsclick" data-toggle="tooltip" data-placement="top"
															title="<?= lang('view_help') ?>">
															<input id="select_all_view" type="checkbox">
															
															<strong><?= lang('view') ?></strong>
														</label>
													</div>
												</th>
												<th>
													<div class="c-checkbox">
														<label class="needsclick" data-toggle="tooltip" data-placement="top"
															title="<?= lang('select_all') . ' ' . lang('create') ?>">
															<input id="select_all_create" type="checkbox">
															
															<strong><?= lang('create') ?></strong>
														</label>
													</div>
												</th>
												<th>
													<div class="c-checkbox">
														<label class="needsclick" data-toggle="tooltip" data-placement="top"
															title="<?= lang('select_all') . ' ' . lang('edit') ?>">
															<input id="select_all_edit" type="checkbox">
															
															<strong><?= lang('edit') ?></strong>
														</label>
													</div>
												</th>
												<th>
													<div class="c-checkbox">
														<label class="needsclick" data-toggle="tooltip" data-placement="top"
															title="<?= lang('select_all') . ' ' . lang('delete') ?>">
															<input id="select_all_delete" type="checkbox">
															
															<strong><?= lang('delete') ?></strong>
														</label>
													</div>
												</th>
											</tr>
										</thead>
										<tbody>
											<?php
											$menu_info = $this->db->where('status !=', 0)->order_by('sort')->get('tbl_menu')->result();
											foreach ($menu_info as $items) {
												$menu['parents'][$items->parent][] = $items;
											?>
												<script type="text/javascript">
													$(document).ready(function() {
														<?php if ($items->label == 'dashboard' || $items->label == 'calendar') { ?>
															$('#<?= $items->menu_id ?>').prop('checked', true);
															$('.<?= $items->menu_id ?>').prop('checked', true);
															$('.<?= $items->menu_id ?>').prop('disabled', true);
															$('.view .<?= $items->menu_id ?>').prop('disabled', false);
															$(".<?= $items->menu_id ?>").next().css('display', 'none');
															$(".view .<?= $items->menu_id ?>").next().css('display', 'block');
														<?php } ?>
														<?php if (
															$items->label == 'transfer_report' || $items->label == 'transactions_report' || $items->label == 'balance_sheet' || $items->label == 'time_history' || $items->label == 'timechange_request'
															|| $items->label == 'attendance_report' || $items->label == 'jobs_applications' || $items->label == 'manage_salary_details' || $items->label == 'employee_salary_list' || $items->label == 'make_payment'
															|| $items->label == 'payroll_summary' || $items->label == 'generate_payslip' || $items->label == 'provident_fund' || $items->label == 'filemanager' || $items->label == 'mailbox' || $items->label == 'leave_management' || $items->label == 'advance_salary' || $items->label == 'quotations_list'
															|| $items->label == 'tasks_assignment' || $items->label == 'bugs_assignment' || $items->label == 'project_report' || $items->label == 'tasks_report' || $items->label == 'bugs_report' || $items->label == 'tickets_report' || $items->label == 'client_report'
															|| $items->label == 'account_statement' || $items->label == 'income_report' || $items->label == 'expense_report' || $items->label == 'income_expense' || $items->label == 'date_wise_report' || $items->label == 'all_income' || $items->label == 'all_expense' || $items->label == 'all_transaction'
															|| $items->label == 'report_by_month' || $items->label == 'stock_history' || $items->label == 'assign_stock_report' || $items->label == 'stock_report' || $items->label == 'overtime' || $items->label == 'performance_report' || $items->label == 'database_backup' || $items->link == 'admin/knowledgebase'
															|| $items->label == 'private_chat'
														) { ?>
															$('.<?= $items->menu_id ?>').prop('disabled', true);
															$(".<?= $items->menu_id ?>").next().css('display', 'none');
															$('.view .<?= $items->menu_id ?>').prop('disabled', false);
															$(".view .<?= $items->menu_id ?>").next().css('display', 'block');

														<?php } ?>
														<?php
														if (
															$items->label == 'performance_indicator' || $items->label == 'give_appraisal' || $items->label == 'company_details' || $items->label == 'system_settings' || $items->label == 'email_settings' || $items->label == 'email_templates' || $items->label == 'email_integration' || $items->label == 'payment_settings' || $items->label == 'invoice_settings' || $items->label == 'estimate_settings'
															|| $items->label == 'estimate_settings' || $items->label == 'tickets_leads_settings' || $items->label == 'theme_settings' || $items->label == 'working_days'  || $items->label == 'cronjob' || $items->label == 'menu_allocation' || $items->label == 'notification'
															|| $items->label == 'email_notification' || $items->label == 'translations' || $items->label == 'dashboard_settings' || $items->label == 'system_update'
														) { ?>
															$('.delete .<?= $items->menu_id ?>').prop('disabled', true);
															$(".delete .<?= $items->menu_id ?>").next().css('display', 'none');
														<?php } ?>
														$('#select_all').change(function() {
															var c = this.checked;
															$(':checkbox').prop('checked', c);
														});

														//select select_all_view
														$("#select_all_view").change(function() { //"select all" change
															$(".view > input").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $(".view > input").map(function() {
																if ($(".view > input").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});
														//select select_all_create
														$("#select_all_create").change(function() { //"select all" change
															$(".create > input").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $(".create > input").map(function() {
																if ($(".create > input").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});

														//select select_all_create
														$("#select_all_edit").change(function() { //"select all" change
															$(".edit > input").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $(".edit > input").map(function() {
																if ($(".edit > input").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});
														//select select_all_create
														$("#select_all_delete").change(function() { //"select all" change
															$(".delete > input").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $(".delete > input").map(function() {
																if ($(".delete > input").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});
														//select all view
														$("#all_view_<?= $items->menu_id; ?>").change(function() { //"select all" change
															$(".view .<?= $items->menu_id; ?>").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $('.view .<?= $items->menu_id; ?>').map(function() {
																if ($(".view .<?= $items->menu_id; ?>").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});
														//select all all_create
														$("#all_create_<?= $items->menu_id; ?>").change(function() { //"select all" change
															$(".create .<?= $items->menu_id; ?>").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $('.create .<?= $items->menu_id; ?>').map(function() {
																if ($(".create .<?= $items->menu_id; ?>").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});
														//select all all_edit
														$("#all_edit_<?= $items->menu_id; ?>").change(function() { //"select all" change
															$(".edit .<?= $items->menu_id; ?>").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $('.edit .<?= $items->menu_id; ?>').map(function() {
																if ($(".edit .<?= $items->menu_id; ?>").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});
														//select all all_edit
														$("#all_delete_<?= $items->menu_id; ?>").change(function() { //"select all" change
															$(".delete .<?= $items->menu_id; ?>").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
															var values = $('.delete .<?= $items->menu_id; ?>').map(function() {
																if ($(".delete .<?= $items->menu_id; ?>").is(":checked")) {
																	$("#" + this.value).prop('checked', true);
																} else {
																	if ($('.' + this.value + ':checked').length == 0) {
																		$("#" + this.value).prop('checked', false);
																	}
																}
															}).get();
														});


														//select all checkboxes
														$("#<?= $items->menu_id; ?>").change(function() { //"select all" change
															$(".<?= $items->menu_id; ?>").prop('checked', $(this).prop(
																"checked")); //change all ".checkbox" checked status
														});
														if ($("input#<?= $items->menu_id; ?>").is(':checked')) {
															$('.c_<?= $items->menu_id; ?>').addClass('is-open');
															$("#parent_<?= $items->menu_id; ?>").addClass('minus');
															$("#parent_<?= $items->menu_id; ?>").removeClass('plus');
														}
														$("#parent_<?= $items->menu_id; ?>").on("click", function() {
															$("#parent_<?= $items->menu_id; ?>").toggleClass('minus');
															$("#parent_<?= $items->menu_id; ?>").toggleClass('plus');
															$('.c_<?= $items->menu_id; ?>').toggleClass('is-open');
														});
														//".checkbox" change
														$('.<?= $items->menu_id; ?>').change(function() {
															//check "select all" if all checkbox items are checked
															if ($('.<?= $items->menu_id; ?>:checked').length) {
																$("#<?= $items->menu_id; ?>").prop('checked', true);
															}
															if ($('.<?= $items->menu_id; ?>:checked').length == 0) {
																$("#<?= $items->menu_id; ?>").prop('checked',
																	false); //change "select all" checked status to false
															}
														});
													});
												</script>

												<?php }
											$CI = &get_instance();
											$all_menus = $CI->buildChild(0, $menu);
											if (!empty($all_menus)) {
												foreach ($all_menus as $parent => $v_parent) {
													if (is_array($v_parent)) { // if this have child
														if (!empty($v_parent)) {
															foreach ($v_parent as $parent_id => $v_child) { ?>
																<style type="text/css">
																	.parent {
																		width: 22px;
																		height: 22px;
																		margin-top: 4px;
																		margin-right: 8px;
																		cursor: pointer;
																		border: 1px solid #cbd5e1;
																		border-radius: 6px;
																		display: inline-flex;
																		align-items: center;
																		justify-content: center;
																		background: #ffffff;
																		transition: all 0.15s ease;
																	}

																	.parent:hover {
																		background: #f1f5f9;
																		border-color: #94a3b8;
																	}

																	.parent.plus::before {
																		content: "+";
																		color: #334155;
																		font-size: 12px;
																		font-weight: 600;
																		line-height: 1;
																	}

																	.parent.minus::before {
																		content: "–";
																		color: #334155;
																		font-size: 14px;
																		font-weight: 600;
																		line-height: 1;
																	}

																	.parent span {
																		display: none;
																	}

																	.child {
																		display: none;
																	}

																	.permission-grid tr.child.is-open {
																		display: table-row;
																	}
																</style>
																<tr class="dept-parent">
																	<th>
																		<div id="parent_<?= $parent_id; ?>" class="parent plus ">
																			<span>X</span>
																		</div>
																		<div class="checkbox c-checkbox ">
																			<label class="needsclick " data-toggle="tooltip" data-placement="top"
																				title="<?= lang('select_all') ?>">
																				<input <?php
																						if (!empty($roll[$parent_id])) {
																							echo $roll[$parent_id] ? 'checked' : '';
																						}
																						?> id="<?php if (!empty($parent_id)) {
																									echo $parent_id;
																								} ?>" type="checkbox" name="menu[]"
																					value="<?= $parent_id; ?>">
																				
																				<strong><?= lang($parent); ?></strong>
																			</label>
																		</div>
																	</th>
																	<th>
																		<div class="c-checkbox">
																			<label class="needsclick view" data-toggle="tooltip" data-placement="top"
																				title="<?= lang('select_all') ?>">
																				<input id="all_view_<?php if (!empty($parent_id)) {
																										echo $parent_id;
																									} ?>" class="<?php if (!empty($parent_id)) {
																														echo $parent_id;
																													} ?>" type="checkbox"
																					name="view_<?= $parent_id ?>" value="<?= $parent_id ?>">
																				
																			</label>
																		</div>
																	</th>
																	<th>
																		<div class="c-checkbox">
																			<label class="needsclick create" data-toggle="tooltip" data-placement="top"
																				title="<?= lang('select_all') ?>">
																				<input id="all_create_<?php if (!empty($parent_id)) {
																											echo $parent_id;
																										} ?>" class="<?php if (!empty($parent_id)) {
																															echo $parent_id;
																														} ?>"
																					type="checkbox" name="created_<?= $parent_id ?>" value="<?= $parent_id ?>">
																				
																			</label>
																		</div>
																	</th>
																	<th>
																		<div class="checkbox c-checkbox">
																			<label class="needsclick edit" data-toggle="tooltip" data-placement="top"
																				title="<?= lang('select_all') ?>">
																				<input id="all_edit_<?php if (!empty($parent_id)) {
																										echo $parent_id;
																									} ?>" class="<?php if (!empty($parent_id)) {
																														echo $parent_id;
																													} ?>" type="checkbox"
																					name="edited_<?= $parent_id ?>" value="<?= $parent_id ?>">
																				
																			</label>
																		</div>
																	</th>
																	<th>
																		<div class="checkbox c-checkbox">
																			<label class="needsclick delete" data-toggle="tooltip" data-placement="top"
																				title="<?= lang('select_all') ?>">
																				<input id="all_delete_<?php if (!empty($parent_id)) {
																											echo $parent_id;
																										} ?>" class="<?php if (!empty($parent_id)) {
																															echo $parent_id;
																														} ?>"
																					type="checkbox" name="deleted_<?= $parent_id ?>" value="<?= $parent_id ?>">
																				
																			</label>
																		</div>
																	</th>
																</tr>
																<?php
																if (!empty($v_child)) {
																	foreach ($v_child as $child => $v_sub_child) {
																		if (is_array($v_sub_child)) {
																			foreach ($v_sub_child as $sub_chld => $v_sub_chld) { ?>
																				<tr class="dept-parent">
																					<td class="pl-9">
																						<div id="parent_<?= $sub_chld; ?>" class="parent plus ">
																							<span>X</span>
																						</div>
																						<div class="checkbox c-checkbox ">
																							<label class="needsclick " data-toggle="tooltip" data-placement="top"
																								title="<?= lang('select_all') ?>">
																								<input <?php
																										if (!empty($roll[$sub_chld])) {
																											echo $roll[$sub_chld] ? 'checked' : '';
																										}
																										?> class="<?= $parent_id; ?>"
																									id="<?= $sub_chld; ?>" type="checkbox" name="menu[]" value="<?= $sub_chld; ?>">
																								
																								<strong><?= lang($child); ?></strong>
																							</label>
																						</div>
																					</td>
																					<td></td>
																					<td></td>
																					<td></td>
																				</tr>
																				<?php
																				foreach ($v_sub_chld as $sub_chld_name => $sub_chld_id) {
																					if (is_array($sub_chld_id)) {
																						foreach ($sub_chld_id as $sub_chld_1 => $v_sub_chld_1) { ?>
																							<tr class="dept-parent">
																								<td class="pl-14">
																									<div id="parent_<?= $sub_chld_1; ?>" class="parent plus ">
																										<span>X</span>
																									</div>
																									<div class="checkbox c-checkbox ">
																										<label class="needsclick " data-toggle="tooltip" data-placement="top"
																											title="<?= lang('select_all') ?>">
																											<input <?php
																													if (!empty($roll[$sub_chld_1])) {
																														echo $roll[$sub_chld_1] ? 'checked' : '';
																													}
																													?>
																												class="<?= $parent_id . ' ' . $sub_chld; ?>" id="<?= $sub_chld_1; ?>"
																												type="checkbox" name="menu[]" value="<?= $sub_chld_1; ?>">
																											
																											<strong><?= lang($sub_chld_name); ?></strong>
																										</label>
																									</div>
																								</td>
																								<td></td>
																								<td></td>
																								<td></td>
																							</tr>
																							<?php
																							foreach ($v_sub_chld_1 as $sub_chld_name_1 => $v_sub_chld_2) {
																								if (is_array($v_sub_chld_2)) {
																									foreach ($v_sub_chld_2 as $sub_chld_name_2 => $v_sub_chld_3) {
																							?>
																										<tr class="dept-parent">
																											<td class="pl-20">
																												<div id="parent_<?= $sub_chld_name_2; ?>" class="parent plus ">
																													<span>X</span>
																												</div>
																												<div class="checkbox c-checkbox ">
																													<label class="needsclick " data-toggle="tooltip" data-placement="top"
																														title="<?= lang('select_all') ?>">
																														<input <?php if (!empty($roll[$sub_chld_name_2])) {
																																	echo $roll[$sub_chld_name_2] ? 'checked' : '';
																																}
																																?>
																															class="<?= $parent_id . ' ' . $sub_chld . ' ' . $sub_chld_1; ?>"
																															id="<?= $sub_chld_name_2; ?>" type="checkbox" name="menu[]"
																															value="<?= $sub_chld_name_2; ?>">
																														
																														<strong><?= lang($sub_chld_name_1); ?></strong>
																													</label>
																												</div>
																											</td>
																											<td></td>
																											<td></td>
																											<td></td>
																										</tr>
																										<?php
																										foreach ($v_sub_chld_3 as $sub_chld_name_3 => $v_sub_chld_4) {
																											if (is_array($v_sub_chld_4)) {
																											} else {
																										?>
																												<tr class="child c_<?= $sub_chld_name_2; ?>">
																													<td class="pl-28">
																														<div class="checkbox c-checkbox">
																															<label class="needsclick " data-toggle="tooltip" data-placement="top"
																																title="<?= lang('select') ?>">
																																<input <?php if (!empty($roll[$v_sub_chld_4])) {
																																			echo $roll[$v_sub_chld_4] ? 'checked' : '';
																																		}
																																		?>
																																	id="<?= $v_sub_chld_4; ?>"
																																	class="<?= $parent_id . ' ' . $sub_chld . ' ' . $sub_chld_name_2 . ' ' . $sub_chld_1; ?>"
																																	type="checkbox" name="menu[]" value="<?= $v_sub_chld_4; ?>">
																																
																																<strong><?= lang($sub_chld_name_3); ?></strong>
																															</label>
																														</div>
																													</td>
																													<td>
																														<div class="c-checkbox">
																															<label class="needsclick view" data-toggle="tooltip" data-placement="top"
																																title="<?= lang('view_help') ?>">
																																<input <?php if (!empty($roll[$v_sub_chld_4])) {
																																			echo $roll[$v_sub_chld_4] ? 'checked' : '';
																																		}
																																		?>
																																	class="<?= $sub_chld . ' ' . $v_sub_chld_4 . ' ' . $sub_chld_name_2 . ' ' . $parent_id . ' ' . $sub_chld_1; ?>"
																																	type="checkbox" name="view_<?= $v_sub_chld_4; ?>" value="<?= $v_sub_chld_4; ?>">
																																
																															</label>
																														</div>
																													</td>
																													<td>
																														<div class="c-checkbox">
																															<label class="needsclick create" data-toggle="tooltip" data-placement="top"
																																title="<?= lang('can') . ' ' . lang('create') ?>">
																																<input
																																	<?php if (!empty($roll[$v_sub_chld_4])) {
																																		echo $roll[$v_sub_chld_4]->created == $v_sub_chld_4 ? 'checked' : '';
																																	}
																																	?>class="<?= $sub_chld . ' ' . $v_sub_chld_4 . ' ' . $sub_chld_name_2 . ' ' . $parent_id . ' ' . $sub_chld_1; ?>"
																																	type="checkbox" name="created_<?= $v_sub_chld_4; ?>"
																																	value="<?= $v_sub_chld_4; ?>">
																																
																															</label>
																														</div>
																													</td>
																													<td>
																														<div class="checkbox c-checkbox">
																															<label class="needsclick edit" data-toggle="tooltip" data-placement="top"
																																title="<?= lang('can') . ' ' . lang('edit') ?>">
																																<input <?php
																																		if (!empty($roll[$v_sub_chld_4])) {
																																			echo $roll[$v_sub_chld_4]->edited == $v_sub_chld_4 ? 'checked' : '';
																																		}
																																		?>
																																	class="<?= $sub_chld . ' ' . $v_sub_chld_4 . ' ' . $sub_chld_name_2 . ' ' . $parent_id . ' ' . $sub_chld_1; ?>"
																																	type="checkbox" name="edited_<?= $v_sub_chld_4; ?>"
																																	value="<?= $v_sub_chld_4; ?>">
																																
																															</label>
																														</div>
																													</td>
																													<td>
																														<div class="checkbox c-checkbox">
																															<label class="needsclick delete" data-toggle="tooltip" data-placement="top"
																																title="<?= lang('can') . ' ' . lang('delete') ?>">
																																<input <?php
																																		if (!empty($roll[$v_sub_chld_4])) {
																																			echo $roll[$v_sub_chld_4]->deleted == $v_sub_chld_4 ? 'checked' : '';
																																		}
																																		?>
																																	class="<?= $sub_chld . ' ' . $v_sub_chld_4 . ' ' . $sub_chld_name_2 . ' ' . $parent_id . ' ' . $sub_chld_1; ?>"
																																	type="checkbox" name="deleted_<?= $v_sub_chld_4; ?>"
																																	value="<?= $v_sub_chld_4; ?>">
																																
																															</label>
																														</div>
																													</td>
																												</tr>
																									<?php
																											}
																										}
																									}
																								} else { ?>
																									<tr class="child c_<?= $sub_chld_1; ?>">
																										<td style="display: block;padding-left: 85px">
																											<div class="c-checkbox">
																												<label class="needsclick " data-toggle="tooltip" data-placement="top"
																													title="<?= lang('select') ?>">
																													<input <?php if (!empty($roll[$v_sub_chld_2])) {
																																echo $roll[$v_sub_chld_2] ? 'checked' : '';
																															}
																															?>
																														id="<?= $v_sub_chld_2; ?>"
																														class="<?= $parent_id . ' ' . $sub_chld . ' ' . $sub_chld_1; ?>" type="checkbox"
																														name="menu[]" value="<?= $v_sub_chld_2; ?>">
																													
																													<strong><?= lang($sub_chld_name_1); ?></strong>
																												</label>
																											</div>
																										</td>
																										<td>
																											<div class="c-checkbox">
																												<label class="needsclick view" data-toggle="tooltip" data-placement="top"
																													title="<?= lang('view_help') ?>">
																													<input <?php if (!empty($roll[$v_sub_chld_2])) {
																																echo $roll[$v_sub_chld_2] ? 'checked' : '';
																															}
																															?>
																														class="<?= $sub_chld . ' ' . $parent_id . ' ' . $v_sub_chld_2 . ' ' . $sub_chld_1; ?>"
																														type="checkbox" name="view_<?= $v_sub_chld_2; ?>" value="<?= $v_sub_chld_2; ?>">
																													
																												</label>
																											</div>
																										</td>
																										<td>
																											<div class="c-checkbox">
																												<label class="needsclick create" data-toggle="tooltip" data-placement="top"
																													title="<?= lang('can') . ' ' . lang('create') ?>">
																													<input <?php if (!empty($roll[$v_sub_chld_2])) {
																																echo $roll[$v_sub_chld_2]->created == $v_sub_chld_2 ? 'checked' : '';
																															} ?>
																														class="<?= $sub_chld . ' ' . $parent_id . ' ' . $v_sub_chld_2 . ' ' . $sub_chld_1; ?>"
																														type="checkbox" name="created_<?= $v_sub_chld_2; ?>"
																														value="<?= $v_sub_chld_2; ?>">
																													
																												</label>
																											</div>
																										</td>
																										<td>
																											<div class="checkbox c-checkbox">
																												<label class="needsclick edit" data-toggle="tooltip" data-placement="top"
																													title="<?= lang('can') . ' ' . lang('edit') ?>">
																													<input <?php
																															if (!empty($roll[$v_sub_chld_2])) {
																																echo $roll[$v_sub_chld_2]->edited == $v_sub_chld_2 ? 'checked' : '';
																															}
																															?>
																														class="<?= $sub_chld_1 . ' ' . $sub_chld . ' ' . $v_sub_chld_2 . ' ' . $parent_id; ?>"
																														type="checkbox" name="edited_<?= $v_sub_chld_2; ?>"
																														value="<?= $v_sub_chld_2; ?>">
																													
																												</label>
																											</div>
																										</td>
																										<td>
																											<div class="checkbox c-checkbox">
																												<label class="needsclick delete" data-toggle="tooltip" data-placement="top"
																													title="<?= lang('can') . ' ' . lang('delete') ?>">
																													<input <?php
																															if (!empty($roll[$v_sub_chld_2])) {
																																echo $roll[$v_sub_chld_2]->deleted == $v_sub_chld_2 ? 'checked' : '';
																															}
																															?>
																														class="<?= $sub_chld_1 . ' ' . $sub_chld . ' ' . $v_sub_chld_2 . ' ' . $parent_id; ?>"
																														type="checkbox" name="deleted_<?= $v_sub_chld_2; ?>"
																														value="<?= $v_sub_chld_2; ?>">
																													
																												</label>
																											</div>
																										</td>
																									</tr>
																						<?php
																								}
																							}
																						}
																					} else { ?>
																						<tr class="child c_<?= $sub_chld; ?>">
																							<td style="display: block;padding-left: 60px">
																								<div class="c-checkbox">
																									<label class="needsclick " data-toggle="tooltip" data-placement="top"
																										title="<?= lang('select') ?>">
																										<input <?php
																												if (!empty($roll[$sub_chld_id])) {
																													echo $roll[$sub_chld_id] ? 'checked' : '';
																												}
																												?>
																											id="<?= $sub_chld_id; ?>" class="<?= $parent_id . ' ' . $sub_chld; ?>"
																											type="checkbox" name="menu[]" value="<?= $sub_chld_id; ?>">
																										
																										<strong><?= lang($sub_chld_name); ?></strong>
																									</label>
																								</div>
																							</td>
																							<td>
																								<div class="c-checkbox">
																									<label class="needsclick view" data-toggle="tooltip" data-placement="top"
																										title="<?= lang('view_help') ?>">
																										<input <?php
																												if (!empty($roll[$sub_chld_id])) {
																													echo $roll[$sub_chld_id] ? 'checked' : '';
																												}
																												?>
																											class="<?= $sub_chld . ' ' . $sub_chld_id . ' ' . $parent_id; ?>"
																											type="checkbox" name="view_<?= $sub_chld_id; ?>" value="<?= $sub_chld_id; ?>">
																										
																									</label>
																								</div>
																							</td>
																							<td>
																								<div class="c-checkbox">
																									<label class="needsclick create" data-toggle="tooltip" data-placement="top"
																										title="<?= lang('can') . ' ' . lang('create') ?>">
																										<input <?php
																												if (!empty($roll[$sub_chld_id])) {
																													echo $roll[$sub_chld_id]->created == $sub_chld_id ? 'checked' : '';
																												}
																												?>
																											class="<?= $sub_chld . ' ' . $sub_chld_id . ' ' . $parent_id; ?>"
																											type="checkbox" name="created_<?= $sub_chld_id; ?>"
																											value="<?= $sub_chld_id; ?>">
																										
																									</label>
																								</div>
																							</td>
																							<td>
																								<div class="checkbox c-checkbox">
																									<label class="needsclick edit" data-toggle="tooltip" data-placement="top"
																										title="<?= lang('can') . ' ' . lang('edit') ?>">
																										<input <?php
																												if (!empty($roll[$sub_chld_id])) {
																													echo $roll[$sub_chld_id]->edited == $sub_chld_id ? 'checked' : '';
																												}
																												?>
																											class="<?= $sub_chld . ' ' . $sub_chld_id . ' ' . $parent_id; ?>"
																											type="checkbox" name="edited_<?= $sub_chld_id; ?>" value="<?= $sub_chld_id; ?>">
																										
																									</label>
																								</div>
																							</td>
																							<td>
																								<div class="checkbox c-checkbox">
																									<label class="needsclick delete" data-toggle="tooltip" data-placement="top"
																										title="<?= lang('can') . ' ' . lang('delete') ?>">
																										<input <?php
																												if (!empty($roll[$sub_chld_id])) {
																													echo $roll[$sub_chld_id]->deleted == $sub_chld_id ? 'checked' : '';
																												}
																												?>
																											class="<?= $sub_chld . ' ' . $sub_chld_id . ' ' . $parent_id; ?>"
																											type="checkbox" name="deleted_<?= $sub_chld_id; ?>"
																											value="<?= $sub_chld_id; ?>">
																										
																									</label>
																								</div>
																							</td>
																						</tr>
																			<?php
																					}
																				}
																			}
																		} else { ?>
																			<tr class="child c_<?= $parent_id; ?>">
																				<td class="pl-9">
																					<div class="c-checkbox">
																						<label class="needsclick " data-toggle="tooltip" data-placement="top"
																							title="<?= lang('select') ?>">
																							<input <?php
																									if (!empty($roll[$v_sub_child])) {
																										echo $roll[$v_sub_child] ? 'checked' : '';
																									}
																									?> id="<?= $v_sub_child; ?>"
																								class="<?= $parent_id; ?>" type="checkbox" name="menu[]"
																								value="<?= $v_sub_child; ?>">
																							
																							<strong><?= lang($child); ?></strong>
																						</label>
																					</div>
																				</td>
																				<td>
																					<div class="c-checkbox">
																						<label class="needsclick view" data-toggle="tooltip" data-placement="top"
																							title="<?= lang('view_help') ?>">
																							<input <?php
																									if (!empty($roll[$v_sub_child])) {
																										echo $roll[$v_sub_child] ? 'checked' : '';
																									}
																									?>
																								class="<?= $parent_id . ' ' . $v_sub_child; ?>" type="checkbox"
																								name="view_<?= $v_sub_child; ?>" value="<?= $v_sub_child; ?>">
																							
																						</label>
																					</div>
																				</td>
																				<td>
																					<div class="c-checkbox">
																						<label class="needsclick create" data-toggle="tooltip" data-placement="top"
																							title="<?= lang('can') . ' ' . lang('create') ?>">
																							<input <?php
																									if (!empty($roll[$v_sub_child])) {
																										echo $roll[$v_sub_child]->created == $v_sub_child ? 'checked' : '';
																									}
																									?>
																								class="<?= $parent_id . ' ' . $v_sub_child; ?>" type="checkbox"
																								name="created_<?= $v_sub_child; ?>" value="<?= $v_sub_child; ?>">
																							
																						</label>
																					</div>
																				</td>
																				<td>
																					<div class="checkbox c-checkbox">
																						<label class="needsclick edit" data-toggle="tooltip" data-placement="top"
																							title="<?= lang('can') . ' ' . lang('edit') ?>">
																							<input class="<?= $parent_id . ' ' . $v_sub_child; ?>" type="checkbox"
																								name="edited_<?= $v_sub_child; ?>" value="<?= $v_sub_child; ?>"
																								<?php
																								if (!empty($roll[$v_sub_child])) {
																									echo $roll[$v_sub_child]->edited == $v_sub_child ? 'checked' : '';
																								}
																								?>>
																							
																						</label>
																					</div>
																				</td>
																				<td>
																					<div class="checkbox c-checkbox">
																						<label class="needsclick delete" data-toggle="tooltip" data-placement="top"
																							title="<?= lang('can') . ' ' . lang('delete') ?>">
																							<input <?php
																									if (!empty($roll[$v_sub_child])) {
																										echo $roll[$v_sub_child]->deleted == $v_sub_child ? 'checked' : '';
																									}
																									?>
																								class="<?= $parent_id . ' ' . $v_sub_child; ?>" type="checkbox"
																								name="deleted_<?= $v_sub_child; ?>" value="<?= $v_sub_child; ?>">
																							
																						</label>
																					</div>
																				</td>
																			</tr>
																<?php }
																	}
																} ?>

														<?php }
														}
													} else { ?>
														<tr>
															<td>
																<div class="c-checkbox">
																	<label class="needsclick " data-toggle="tooltip" data-placement="top"
																		title="<?= lang('select') ?>">
																		<input id="<?= $v_parent; ?>" type="checkbox" name="menu[]"
																			value="<?= $v_parent; ?>"
																			<?php if (!empty($roll[$v_parent])) {
																				echo $roll[$v_parent] ? 'checked' : '';
																			}
																			?>>
																		
																		<strong><?= lang($parent); ?></strong>
																	</label>
																</div>
															</td>
															<td>
																<div class="c-checkbox">
																	<label class="needsclick view" data-toggle="tooltip" data-placement="top"
																		title="<?= lang('view_help') ?>">
																		<input id="<?= $v_parent; ?>" <?php
																										if (!empty($roll[$v_parent])) {
																											echo $roll[$v_parent] ? 'checked' : '';
																										}
																										?> class="<?= $v_parent; ?>"
																			type="checkbox" name="view_<?= $v_parent; ?>" value="<?= $v_parent; ?>">
																		
																	</label>
																</div>
															</td>
															<td>
																<div class="c-checkbox">
																	<label class="needsclick create" data-toggle="tooltip" data-placement="top"
																		title="<?= lang('can') . ' ' . lang('create') ?>">
																		<input <?php
																				if (!empty($roll[$v_parent])) {
																					echo $roll[$v_parent]->created == $v_parent ? 'checked' : '';
																				}
																				?> class="<?= $v_parent; ?>" type="checkbox"
																			name="created_<?= $v_parent; ?>" value="<?= $v_parent; ?>">
																		
																	</label>
																</div>
															</td>
															<td>
																<div class="checkbox c-checkbox">
																	<label class="needsclick edit" data-toggle="tooltip" data-placement="top"
																		title="<?= lang('can') . ' ' . lang('edit') ?>">
																		<input <?php
																				if (!empty($roll[$v_parent])) {
																					echo $roll[$v_parent]->edited == $v_parent ? 'checked' : '';
																				}
																				?> class="<?= $v_parent; ?>" type="checkbox"
																			name="edited_<?= $v_parent; ?>" value="<?= $v_parent; ?>">
																		
																	</label>
																</div>
															</td>
															<td>
																<div class="checkbox c-checkbox">
																	<label class="needsclick delete" data-toggle="tooltip" data-placement="top"
																		title="<?= lang('can') . ' ' . lang('delete') ?>">
																		<input <?php
																				if (!empty($roll[$v_parent])) {
																					echo $roll[$v_parent]->deleted == $v_parent ? 'checked' : '';
																				}
																				?> class="<?= $v_parent; ?>" type="checkbox"
																			name="deleted_<?= $v_parent; ?>" value="<?= $v_parent; ?>">
																		
																	</label>
																</div>
															</td>
														</tr>
											<?php }
												}
											}
											?>

										</tbody>
									</table>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</form>
<?php } ?>

<script type="text/javascript">
	$(document).ready(function() {
		function toggleNewDepartment() {
			if ($('#new_departments').val() !== '') {
				$('.new_departments').hide();
				$('.new_departments input').prop('disabled', true);
			} else {
				$('.new_departments').show();
				$('.new_departments input').prop('disabled', false);
			}
		}

		$('#new_departments').on('change', toggleNewDepartment);
		toggleNewDepartment();
	});
</script>
