<?php 
if(service('request')->uri->getSegment(2) == "novo-cadastro") { 
	echo form_open_multipart($MyMod . "/cadastrando", array ('class' => 'form validate', 'id' => 'form_principal')); 
} 
else { 
	echo form_open_multipart($MyMod . "/atualizando-cadastro/" . $id, array ('class' => 'form validate', 'id' => 'form_principal')); 
} 
?>

<div class="wrapper wrapper-content animated fadeInUp">
	<?php echo view('Views\view_header'); ?>

	<?php if(isset($cms_usuario_categorias->data_cadastro) AND !empty($cms_usuario_categorias->data_cadastro)) { ?>
		<div class="row">
			<div class="col-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<i class="fa-light fa-calendar mr-1"></i> Este conteúdo foi criado em <b><?php echo date("d-m-Y H:i:s", strtotime($cms_usuario_categorias->data_cadastro)); ?> </b>
						
						<?php if(isset($cms_usuario_categorias->data_alteracao) AND !empty($cms_usuario_categorias->data_alteracao)) { ?>
							e alterado em <b><?php echo date("d-m-Y H:i:s", strtotime($cms_usuario_categorias->data_alteracao)); ?></b>
						<?php } ?>
					</div>
				</div>
			</div>
		</div>
	<?php } ?>

	<div class="row">
		<div class="col-12">
			<div class="ibox float-e-margins">
				<div class="ibox-title">
					<h5>
						Formulário de cadastro
					</h5>
				</div>

				<div class="ibox-content">
					<div class="row">
						<div class="col-12">
							<label for="ativo" class="form-label">
								Status:
							</label>
							
							<div class="input-group mb-3">
								<label class="input-group-text" for="ativo">
									<i class="fa-light fa-power-off"></i>
								</label>
								
								<select class="form-select" data-rule-required="true" data-msg-required="Campo obrigatório." id="ativo" name="ativo">
									<option value="" disabled selected hidden></option>
									<option <?php if(isset($cms_usuario_categorias->ativo) and $cms_usuario_categorias->ativo == 1) { echo 'selected'; } ?> value="1">Cadastro ativo</option>
									<option <?php if(isset($cms_usuario_categorias->ativo) and $cms_usuario_categorias->ativo == 0) { echo 'selected'; } ?> value="0">Cadastro inativo</option>
								</select>
							</div>
						</div>

						<div class="col-12">
							<label for="titulo" class="form-label">
								Título:
							</label>
							
							<div class="input-group mb-0">
								<span class="input-group-text" id="titulo">
									<i class="fa-light fa-align-left"></i>
								</span>
								
								<input type="text" class="form-control" aria-label="titulo" aria-describedby="titulo" data-rule-required="true" data-msg-required="Campo obrigatório." id="titulo" name="titulo" value="<?php if(isset($cms_usuario_categorias->titulo)) { echo $cms_usuario_categorias->titulo; } ?>">
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>

	<?php if(service('session')->get('usuario')->id_categoria == 1 OR service('session')->get('usuario')->id_categoria == 2) { ?>

		<?php 
		$un 								= "";
		$idModuloCategoria 	= "";
		$idModulo 					= "";

		if(isset($cms_usuario_categorias->permissoes) AND !empty($cms_usuario_categorias->permissoes)) { 
			$un = base64_decode($cms_usuario_categorias->permissoes);
			$un = unserialize($un);

			if(isset($un['idModuloCategoria']) AND !empty($un['idModuloCategoria'])) {
				$idModuloCategoria = $un['idModuloCategoria'];
			}

			if(isset($un['idModulo']) AND !empty($un['idModulo'])) {
				$idModulo = $un['idModulo'];
			}

			if(isset($un['moduloCadastrar']) AND !empty($un['moduloCadastrar'])) {
				$moduloCadastrar = $un['moduloCadastrar'];
			}

			if(isset($un['moduloEditar']) AND !empty($un['moduloEditar'])) {
				$moduloEditar = $un['moduloEditar'];
			}

			if(isset($un['moduloSalvar']) AND !empty($un['moduloSalvar'])) {
				$moduloSalvar = $un['moduloSalvar'];
			}

			if(isset($un['moduloExcluir']) AND !empty($un['moduloExcluir'])) {
				$moduloExcluir = $un['moduloExcluir'];
			}

			if(isset($un['moduloLote']) AND !empty($un['moduloLote'])) {
				$moduloLote = $un['moduloLote'];
			}
		}
		?>

		<?php foreach ($cms_modulos_categorias as $valor) { ?>

			<?php  
			$hideAPI = "";
			if($valor->api == 1 AND $MyUser->id_categoria != 1) {
				$hideAPI = API1_HIDE;
			}

			if($valor->api == 2 AND $MyUser->id_categoria != 1) {
				$hideAPI = API2_HIDE;
			}
			?>
			
			<div class="row <?php echo $hideAPI; ?>">
				<div class="col-12">
					<div class="ibox float-e-margins <?php if($valor->root == 1) { echo 'border-danger'; } ?>">
						<div class="ibox-title">
							<h5>
								<label class="checkbox-inline i-checks"> 
									<input class="input-module-category" name="id_modulo_categoria[]" type="checkbox" value="<?php echo $valor->id; ?>" <?php if(isset($idModuloCategoria) AND !empty($idModuloCategoria) AND in_array($valor->id, $idModuloCategoria)) { echo 'checked'; } ?>> 
									<span class="<?php if($valor->root == 1) { echo 'text-danger'; } ?>">
										<i class="ml-1 <?php echo $valor->icone; ?>"></i> <?php echo $valor->titulo; ?>
									</span>
								</label>
							</h5>
						</div>

						<div class="ibox-content">
							<div class="row">
								<?php if(!empty($valor)) { ?>
									<div class="col-12 col-modules-list">
										<div class="col-modules-select">
											<?php $c0 = 0; foreach ($cms_modulos as $valor2) { ?>
												
												<?php if($valor2->id_categoria == $valor->id) { $c0++; ?>
													<div class="col-md-12 d-flex <?php if($c0 % 2 == 0) { echo "even"; } else { echo "odd"; } ?>">
														<label class="checkbox-inline i-checks"> 
															<input class="input-module" name="id_modulo[]" type="checkbox" value="<?php echo $valor2->id; ?>" <?php if(isset($idModulo) AND !empty($idModulo) AND in_array($valor2->id, $idModulo)) { echo 'checked'; } ?>> 
															<span class="<?php if($valor2->root == 1) { echo 'text-danger'; } ?>">
																<i class="ml-1 mr-1 <?php echo $valor2->icone; ?>"></i> <?php if($valor2->thirdmenu == 1) { echo $valor2->titulo; } else { echo $valor2->titulo; } ?>
															</span>
														</label>

														<div class="d-flex justify-content-center align-items-center">
															<?php if($valor2->cadastrar == 1) { ?>
																<label class="checkbox-inline i-checks ms-0"> 
																	<input class="input-module" type="checkbox" name="modulo_cadastrar[]" value="<?php echo $valor2->id . '-1'; ?>" <?php if(isset($moduloCadastrar) AND !empty($moduloCadastrar) AND in_array($valor2->id . '-1', $moduloCadastrar)) { echo 'checked'; } ?>> 
																	<span class="ms-1 <?php if($valor2->root == 1) { echo 'text-danger'; } ?>">
																		Cadastrar
																	</span>
																</label>
															<?php } else { ?> 
																<label class="d-flex justify-content-center align-items-center ms-0"> 
																	<input class="input-module" type="hidden" name="modulo_cadastrar[]" value="<?php echo $valor2->id . '-0'; ?>"> 
																	<i class="fa-light fa-square-exclamation fa-2xl text-danger"></i>
																	<span class="ms-1 text-danger text-decoration-line-through">
																		Cadastrar
																	</span>
																</label>
															<?php } ?>

															<?php if($valor2->editar == 1) { ?>
																<label class="checkbox-inline i-checks ms-3"> 
																	<input class="input-module" type="checkbox" name="modulo_editar[]" value="<?php echo $valor2->id . '-1'; ?>" <?php if(isset($moduloEditar) AND !empty($moduloEditar) AND in_array($valor2->id . '-1', $moduloEditar)) { echo 'checked'; } ?>> 
																	<span class="ms-1 <?php if($valor2->root == 1) { echo 'text-danger'; } ?>">
																		Editar
																	</span>
																</label>
															<?php } else { ?> 
																<label class="d-flex justify-content-center align-items-center ms-3"> 
																	<input class="input-module" type="hidden" name="modulo_editar[]" value="<?php echo $valor2->id . '-0'; ?>"> 
																	<i class="fa-light fa-square-exclamation fa-2xl text-danger"></i>
																	<span class="ms-1 text-danger text-decoration-line-through">
																		Editar
																	</span>
																</label>
															<?php } ?>

															<?php if($valor2->salvar == 1) { ?>
																<label class="checkbox-inline i-checks ms-3"> 
																	<input class="input-module" type="checkbox" name="modulo_salvar[]" value="<?php echo $valor2->id . '-1'; ?>" <?php if(isset($moduloSalvar) AND !empty($moduloSalvar) AND in_array($valor2->id . '-1', $moduloSalvar)) { echo 'checked'; } ?>> 
																	<span class="ms-1 <?php if($valor2->root == 1) { echo 'text-danger'; } ?>">
																		Salvar
																	</span>
																</label>
															<?php } else { ?> 
																<label class="d-flex justify-content-center align-items-center ms-3"> 
																	<input class="input-module" type="hidden" name="modulo_salvar[]" value="<?php echo $valor2->id . '-0'; ?>"> 
																	<i class="fa-light fa-square-exclamation fa-2xl text-danger"></i>
																	<span class="ms-1 text-danger text-decoration-line-through">
																		Salvar
																	</span>
																</label>
															<?php } ?>

															<?php if($valor2->excluir == 1) { ?>
																<label class="checkbox-inline i-checks ms-3"> 
																	<input class="input-module" type="checkbox" name="modulo_excluir[]" value="<?php echo $valor2->id . '-1'; ?>" <?php if(isset($moduloExcluir) AND !empty($moduloExcluir) AND in_array($valor2->id . '-1', $moduloExcluir)) { echo 'checked'; } ?>> 
																	<span class="ms-1 <?php if($valor2->root == 1) { echo 'text-danger'; } ?>">
																		Remover
																	</span>
																</label>
															<?php } else { ?> 
																<label class="d-flex justify-content-center align-items-center ms-3"> 
																	<input class="input-module" type="hidden" name="modulo_excluir[]" value="<?php echo $valor2->id . '-0'; ?>"> 
																	<i class="fa-light fa-square-exclamation fa-2xl text-danger"></i>
																	<span class="ms-1 text-danger text-decoration-line-through">
																		Remover
																	</span>
																</label>
															<?php } ?>

															<?php if($valor2->lote == 1) { ?>
																<label class="checkbox-inline i-checks ms-3 me-2"> 
																	<input class="input-module" type="checkbox" name="modulo_lote[]" value="<?php echo $valor2->id . '-1'; ?>" <?php if(isset($moduloLote) AND !empty($moduloLote) AND in_array($valor2->id . '-1', $moduloLote)) { echo 'checked'; } ?>> 
																	<span class="ms-1 <?php if($valor2->root == 1) { echo 'text-danger'; } ?>">
																		Lote
																	</span>
																</label>
															<?php } else { ?> 
																<label class="d-flex justify-content-center align-items-center ms-3 me-2"> 
																	<input class="input-module" type="hidden" name="modulo_lote[]" value="<?php echo $valor2->id . '-0'; ?>"> 
																	<i class="fa-light fa-square-exclamation fa-2xl text-danger"></i>
																	<span class="ms-1 text-danger text-decoration-line-through">
																		Lote
																	</span>
																</label>
															<?php } ?>
														</div>
													</div>
												<?php } ?>

											<?php } ?>
										</div>
									</div>
								<?php } ?>
							</div>
						</div>

						<div class="ibox-footer">
							<strong class="text-danger text-uppercase">Em vermelho:</strong> Módulos listados apenas para o usuário ROOT.<br/>
						</div>
					</div>
				</div>
			</div>
		<?php } ?>

	<?php } ?>

	<?php echo view('Views\view_logs'); ?>
</div>

<?php echo view('Views/view_footer'); ?>
<?php echo form_close(); ?>