<link rel="stylesheet"
	  href="<?= base_url('<link rel="stylesheet" 
	href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap.min.css">') ?>">
<link rel="stylesheet" href="<?= base_url('bower_components/toastr/toastr.css') ?>">
<div class="content-wrapper">
	<!-- Content Header (Page header) -->
	<?php
	  switch ($role){
			case 'DOCTOR':
				$title1 ='manage_validating_doctors';
				break;
			case 'CONTROLLER':
				$title1 ='manage_controllers_doctors';
				break;
		  	case 'OPENING_ACCOUNT_AGENT':
			    $title1 ='manage_account_opening_agents';
			    break;
			default:
				$title1 = 'manage_validating_agents';
	  }
	?>
	<section class="content-header">
		<h1>
			<?= $this->lang->line($title1) ; ?>
		</h1>
	</section>

	<section class="content">
		<div class="row">
			<div class="col-xs-8">
				<div class="box">
					<div class="box-header">
						<?php
						switch ($role){
							case 'DOCTOR':
								$title2 ='validating_doctors';
								break;
							case 'CONTROLLER':
								$title2 ='controllers_doctors';
								break;
							case 'OPENING_ACCOUNT_AGENT':
								$title2 ='account_opening_agents';
								break;
							default:
								$title2 = 'validating_agents';
						}
						?>
						<h3 class="box-title"><?= $this->lang->line($title2); ?></h3>
					</div>
					<div class="box-body" style="overflow-x:auto;">
						<table id="agents" class="table table-bordered table-striped">
							<thead>
							<tr>
								<th align='center'>#</th>
								<th><?= $this->lang->line('Nom'); ?></th>
								<th><?= $this->lang->line('Prénom'); ?></th>
								<th align='center'><?= $this->lang->line('Email'); ?></th>
								<th align='center'><?= $this->lang->line('Contact'); ?></th>
								<th align='center'><?= $this->lang->line('Statut'); ?></th>
								<th>Action</th>
							</tr>
							</thead>
							<tbody>
							<?php
								if(isset($agents)){
									foreach ($agents->result() as $i => $row) {
										$statut = $this->lang->line('Non confirmé');
										if (isset($row->salt)) {
											$statut = $this->lang->line("Confirmé");
										}
										?>
										<tr>
											<td align='center'><?= $i+1?></td>
											<td><?= $row->lastname ?></td>
											<td><?=$row->firstname?></td>
											<td align='center'><?=$row->email?></td>
											<td align='center'><?=$row->phone ?></td>
											<td align='center'><?=$statut?></td>
											<td>
												<button class='btn btn-success edit' data-id="<?=$row->id?>" data-firstname="<?=$row->firstname?>" data-lastname="<?=$row->lastname?>"
														data-email="<?=$row->email?>" data-phone="<?=$row->phone?>" >
													<i class='fa fa-edit'></i>
												</button>
												<button data-toggle='modal' class='btn btn-danger delete' data-id="<?=$row->id?>">
													<i class='fa fa-trash'></i>
												</button>
											</td>
										 </tr>
									<?php
									}
								}
							?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
				<div class="col-xs-4">
					<div class="box">
						<div class="box-header">
							<?php
								switch ($role){
									case 'DOCTOR':
										$title3 ='add_validating_doctor';
										break;
									case 'CONTROLLER':
										$title3 ='add_controller_doctor';
										break;
									case 'OPENING_ACCOUNT_AGENT':
										$title3 ='add_account_opening_agent';
										break;
									default:
										$title3 = 'add_validating_agent';
								}
							?>
							<h3 class="box-title"><?= $this->lang->line($title3); ?></h3>
						</div>
						<div class="box-body">
							<form id="create-agent-form" class="bottom-75 center-block">
								<div class="form-group">
									<label for="lastname"><?= $this->lang->line('Nom'); ?></label>
									<input type="text" class="form-control input-lg" name="lastname" required>
								</div>
								<div class="form-group">
									<label for="firstname"><?= $this->lang->line('Prénom'); ?></label>
									<input type="text" class="form-control input-lg" name="firstname">
								</div>
								<div class="form-group">
									<label for="email"><?= $this->lang->line('Email'); ?></label>
									<input type="email" class="form-control input-lg" name="email" required>
								</div>
								<div class="form-group">
									<label for="contact"><?= $this->lang->line('Contact'); ?></label>
									<input type="text" class="form-control input-lg" name="phone">
								</div>
								<div class="form-group">
									<input type="submit"
										   value="<?= $this->lang->line('Créer le compte'); ?>"
										   class="btn btn-primary">
								</div>
							</form>
						</div>
					</div>
				</div>
		</div>
		<div class="modal fade" id="editModal">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
						<h4 class="modal-title"><?= $this->lang->line('Mettre à jour des informations'); ?></h4>
					</div>
					<div class="modal-body">
						<form id="update-form" method="post" class="bottom-75 center-block">

							<div class="form-group">
								<label><?= $this->lang->line('Nom'); ?></label>
								<input type="text" class="form-control input-lg" name="lastname" required>
							</div>
							<div class="form-group">
								<label><?= $this->lang->line('Prénom'); ?></label>
								<input type="text" class="form-control input-lg" name="firstname" >
							</div>
							<div class="form-group">
								<label><?= $this->lang->line('Email'); ?></label>
								<input type="email" class="form-control input-lg" name="email" required>
							</div>
							<div class="form-group">
								<label><?= $this->lang->line('Contact'); ?></label>
								<input type="text" class="form-control input-lg" name="phone">
							</div>
							<div class="form-group">
								<input id="btn-update" type="submit"
									   value="<?= $this->lang->line('save'); ?>" class="btn btn-primary">
								<button type="button" class="btn btn-default pull-right"
										data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>

		<?php $this->load->view('include/delete_modal') ?>
		<?php $this->load->view('include/loader') ?>
	</section>
</div>

<!-- jQuery 2.2.4 -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

<!-- Bootstrap 3.3.7 -->
<script src="<?= base_url('bower_components/bootstrap/dist/js/bootstrap.min.js') ?>"></script>
<!-- DataTables -->
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap.min.js"></script>
<!-- Slimscroll -->
<script src="<?= base_url('bower_components/jquery-slimscroll/jquery.slimscroll.min.js') ?>"></script>
<!-- FastClick -->
<script src="<?= base_url('bower_components/fastclick/lib/fastclick.js') ?>"></script>
<!-- AdminLTE App -->
<script src="<?= base_url('dist/js/adminlte.min.js') ?>"></script>
<script src="<?= base_url('bower_components/toastr/toastr.js') ?>"></script>
<script src="<?= base_url('dist/js/sweetalert2.js') ?>"></script>

<script>

	toastr.options.closeButton = true;
	toastr.options.closeMethod = 'fadeOut';
	toastr.options.closeDuration = 5000;
	toastr.options.closeEasing = 'swing';

	$(function () {
		$('#agents').DataTable();
	})

	var networkId = "<?=$network_id ?>"
	var selectedId = null

	$(document).on("click", ".edit", function () {
		selectedId = $(this).data('id');
		$("#update-form input[name='lastname']").val($(this).data('lastname'));
		$("#update-form input[name='firstname']").val($(this).data('firstname'));
		$("#update-form input[name='email']").val($(this).data('email'));
		$("#update-form input[name='phone']").val($(this).data('phone'));
		$('#editModal').modal('show');

	});

	$(document).on("click", ".delete", function () {
		selectedId = $(this).data('id');
		$('#delete-modal').modal('show');
	});

	<?php
		switch ($role){
			case 'DOCTOR':
				$title4 ='validating_doctor_updated';
				break;
			case 'CONTROLLER':
				$title4 ='controller_doctor_updated';
				break;
			case 'OPENING_ACCOUNT_AGENT':
				$title4 ='account_opening_agent_updated';
				break;
			default:
				$title4 = 'validating_agent_updated';
		}
	?>

	$("#update-form").submit(function () {
		$.ajax({
			url: '<?= base_url('NanoHealthController/updateValidatingAgent')?>',
			type: 'post',
			data: {
				id: selectedId,
				lastname: $("input[name=lastname]",this).val(),
				firstname:  $("input[name=firstname]",this).val(),
				email: $("input[name=email]",this).val(),
				phone: $("input[name=phone]",this).val()
			},
			dataType: 'json',
			success: function (data) {
				if(data.code === 200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line($title4)?>",
						text:"<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					toastr.error(data.message , "<?= $this->lang->line('request_error')?>");
				}
			},
			error: function (resultat, statut, error) {
				console.log(resultat + " " + error);
				toastr.error("<?= $this->lang->line('error_message')?>" , "<?= $this->lang->line('request_error')?>");
			}
		});

		event.preventDefault();
	});

	<?php
		switch ($role){
			case 'DOCTOR':
				$title5 ='validating_doctor_created';
				break;
			case 'CONTROLLER':
				$title5 ='controller_doctor_created';
				break;
			case 'OPENING_ACCOUNT_AGENT':
				$title5 ='account_opening_agent_created';
				break;
			default:
				$title5 = 'validating_agent_created';
		}
	?>
	$("#create-agent-form").submit(function (event) {

		$.ajax({
			url: '<?= base_url('NanoHealthController/createValidatingAgent')?>',
			type: 'post',
			data: {
				network_id : networkId,
				lastname: $("input[name=lastname]",this).val(),
				firstname:  $("input[name=firstname]",this).val(),
				email: $("input[name=email]",this).val(),
				phone: $("input[name=phone]",this).val(),
				role : "<?=$role?>"
			},
			dataType: 'json',
			success: function (data) {
				if(data.code === 200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line($title5)?>",
						text:"<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					toastr.error(data.message , "<?= $this->lang->line('request_error')?>");
				}
			},
			error: function (resultat, statut, error) {
				console.log(resultat + " " + error);
				toastr.error("<?= $this->lang->line('error_message')?>" , "<?= $this->lang->line('request_error')?>");
			}
		});

		event.preventDefault();
	});

	<?php
	switch ($role){
		case 'DOCTOR':
			$title6 ='validating_doctor_deleted';
			break;
		case 'CONTROLLER':
			$title6 ='controller_doctor_deleted';
			break;
		case 'OPENING_ACCOUNT_AGENT':
			$title6 ='account_opening_agent_deleted';
			break;
		default:
			$title6 = 'validating_agent_deleted';
	}
	?>
	$("#delete-btn").click(function () {
		$.ajax({
			url: '<?= base_url('NanoHealthController/deleteValidatingAgent')?>',
			type: 'post',
			data: {id: selectedId},
			dataType: 'json',
			success: function (data) {
				if(data.code === 200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line($title6)?>",
						text:"<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					toastr.error(data.message , "<?= $this->lang->line('request_error')?>");
				}
			},
			error: function (resultat, statut, error) {
				console.log(resultat + " " + error);
				toastr.error("<?= $this->lang->line('error_message')?>" , "<?= $this->lang->line('request_error')?>");
			}
		});

		event.preventDefault();
	});

</script>
