<!-- DataTables -->
<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" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.6.2/css/buttons.dataTables.min.css">
<link rel="stylesheet" href="<?= base_url('bower_components/toastr/toastr.css') ?>">
<div class="content-wrapper">
<?php
?>
	<!-- Content Header (Page header) -->
	<section class="content-header">
		<h1>
			<?= $this->lang->line('manage_health_care_sheets') ?>
<!--			<input type="button" class="btn btn-primary pull-right" id="Bactiver"-->
<!--				   value="Activer/Désactiver le(s) réseau(x)" />-->
		</h1>
	</section>
	<section class="content">
		<div class="row">
			<div class="col-md-4 col-sm-6 col-xs-12">
				<div class="info-box">
					<span class="info-box-icon bg-aqua"><i class="ion ion-android-time"></i></span>
					<div class="info-box-content">
						<span class="info-box-text"><?= $this->lang->line('Période') ?> </span>
						<span class="info-box-number">
                                    <input id="picker"
										   style="background: #fff; cursor: pointer; padding: 1px 1px; border: 1px solid #ccc; width: 100%"
										   data-category="<?= isset($category) ? $category : null ?>"
										   type="text" name="daterange"
										   data-lang="<?= $this->session->userdata('site_lang') ?>"
										   value="<?= ($startDate != null & $endDate != null) ? $startDate . ' - ' . $endDate : '' ?>"/>
                                    
						</span>
						<span> Format : <?= $this->session->userdata('site_lang') === 'french' ? 'Jour - Mois - Année ' : 'Year - Month - Day' ?> </span>
					</div>
				</div>
			</div>


		</div>
		<div class="row">
			<div class="col-xs-12">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line('export_health_care_sheets_list') ?></h3>
						<div class="box-tools">
							<?php if(!empty($show_health_care_sheet_buttons)) { ?>
								<button class="btn btn-success" id="new-consultation">
									<?= $this->lang->line('new_consultation'); ?>
								</button>
								<button class="btn btn-primary" id="new-execution">
									<?= $this->lang->line('new_execution'); ?>
								</button>
								<button class="btn btn-success" id="update-consultation">
									<?= $this->lang->line('update_consultation'); ?>
								</button>
								<button class="btn btn-primary" id="update-execution">
									<?= $this->lang->line('update_execution'); ?>
								</button>
								<button class="btn btn-danger" id="generate-invoice">
									Generer la facture
								</button>
							<?php } ?>
						</div>
					</div>
					<div class="box-body" style="overflow-x:auto;">

						<table id="subscriptions" class="table table-bordered table-striped">
							<thead>
							<tr>
								<th align='center'><?= $this->lang->line('health_care_sheet_id') ?></th>
								<th>Type</th>
								<th><?= $this->lang->line('insured_id') ?></th>
								<th><?= $this->lang->line('patient_lastname') ?></th>
								<th><?= $this->lang->line('patient_situation') ?></th>
								<th><?= $this->lang->line('institution_name') ?></th>
								<th><?= $this->lang->line('practitioner_lastname') ?></th>
								<th><?= $this->lang->line('care_condition') ?></th>
								<th><?= $this->lang->line('state') ?></th>
								<th align='center'>Date</th>
								<th align='center'>Action</th>
							</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
		</div>

		<div class="modal fade" id="modal">
			<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>
						<h3 class="modal-title" id="modal-title"></h3>
					</div>
					<div class="modal-body">
						<form id="search-form">
							<div class="form-group">
								<label class="col-form-label"><?= $this->lang->line('finding_insured_by'); ?></label>
								<select class="form-control" name="option">
									<option value="name"><?= $this->lang->line('Nom'); ?></option>
									<option value="phone"><?= $this->lang->line('Telephone'); ?></option>
								</select>
							</div>
							<div class="form-group">
								<input type="text" required class="form-control" name="query">
							</div>
						</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default pull-left"
								data-dismiss="modal"><?= $this->lang->line('Fermer'); ?></button>
						<button type="button" id="check-insured"
								class="btn btn-primary"><?= $this->lang->line('Continuer'); ?></button>
					</div>
				</div>
			</div>
		</div>

	</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>
<!-- AdminLTE for demo purposes -->
<script src="<?= base_url('dist/js/demo.js') ?>"></script>

<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment-with-locales.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.19/sorting/datetime-moment.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/plug-ins/1.10.20/dataRender/datetime.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.print.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.2/js/buttons.colVis.min.js"></script>
<script src="<?= base_url('dist/js/sweetalert2.js') ?>"></script>
<script src="<?= base_url('bower_components/toastr/toastr.js') ?>"></script>


<script>
	$(function () {
		const lang = $('#picker').data('lang');
		const format = lang === 'french' ? 'fr' : 'en';
		moment.updateLocale(moment.locale(format), {invalidDate: ""}); // Blank text when is invalid date

		var table = $('#subscriptions').DataTable({
			// Processing indicator
			"processing": true,
			"language": {
				"processing": "<?= $this->lang->line('loading') ?>",
				"emptyTable" : "<?= $this->lang->line('no_insured') ?>"
			},
			// DataTables server-side processing mode
			"serverSide": true,
			// Initial no order.
			"order": [],
			// Load data from an Ajax source
			"ajax": {
				"url": "<?= base_url('pagination/HealthCareSheets/getLists'); ?>",
				"data":{
					"startDate" : "<?= $startDate?>",
					"endDate" : "<?= $endDate?>",
					"id_network" : "<?= $id_network ?? null ?>",
					"network_agent_id" : "<?= $network_agent_id ?? null ?>",
					"currentURL" : "<?= current_url()?>"
				},
				"type": "POST"
			},
			"aaSorting": [[9, "desc"]],
			"columnDefs": [{
				"targets": [9],
				// "orderable": false,
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}],
			dom: 'Bfrtip',
			"buttons": [
				'pageLength',
				{
					"extend": 'excelHtml5',
					title: "<?= $this->lang->line('health_care_sheets_history') ?>",
				},
				{
					extend: 'csvHtml5',
					title: "<?= $this->lang->line('health_care_sheets_history') ?>",
				},
				{
					extend: 'pdfHtml5',
					orientation: 'landscape',
					pageSize: 'LEGAL',
					title: "<?= $this->lang->line('health_care_sheets_history') ?>",
					trim: false,
					"action": newexportaction
				},
				// 'colvis'
			]
		});

		table.buttons().container()
				.appendTo('#example_wrapper .col-sm-6:eq(0)');
		
	});
</script>

<script type="text/javascript">
	var startDate;
	var endDate;

	$(function () {
		const lang =  $('#picker').data('lang');
		const category =  $('#picker').data('category');
        const id_network = "<?= $id_network ?? null ?>";
		const ne = "<?=$network_agent_id ?? null ?>"
        $('input[name="daterange"]').daterangepicker({
			opens: 'left',
			autoUpdateInput: false,
			locale: {
				format: lang === 'french' ? 'DD-MM-YYYY' : 'YYYY-MM-DD',
				cancelLabel: 'Clear'
			}
		}, function (start, end, label) {
			const debut = start.format('YYYY-MM-DD');
			const fin = end.format('YYYY-MM-DD');
			if(category)
				window.location = "<?= current_url()?>" + "?history=insurance-health_care_sheets" + "&d=" + debut + "&f=" + fin;
			else
				window.location = "<?= current_url()?>" + "?id="+id_network+"&history=insurance-health_care_sheets" + "&d=" + debut + "&f=" + fin;

		});

		$('input[name="daterange"]').on('cancel.daterangepicker', function(ev, picker) {
			//do something, like clearing an input
			$('#daterange').val('');
			if(category)
				window.location = "<?= current_url()?>" + "?history=insurance-health_care_sheets";
			else
				window.location = "<?= current_url()?>" + "?id="+id_network+"&history=insurance-health_care_sheets";
		});
	});

</script>

<script>
	var action = "";

	$('#new-consultation').click(function () {
		action = "new_consultation";
		$('#modal-title').html("<?= $this->lang->line('new_consultation'); ?>")
		$('#modal').modal('show');
	})

	$('#new-execution').click(function () {
		action = "new_execution";
		$('#modal-title').html("<?= $this->lang->line('new_execution'); ?>")
		$('#modal').modal('show');
	})

	$('#update-consultation').click(function () {
		action = "update_consultation";
		$('#modal-title').html("<?= $this->lang->line('update_consultation'); ?>")
		$('#modal').modal('show');
	})

	$('#update-execution').click(function () {
		action = "update_execution";
		$('#modal-title').html("<?= $this->lang->line('update_execution'); ?>")
		$('#modal').modal('show');
	})

	$('#generate-invoice').click(function () {
		$.ajax({
			url: '<?= base_url('NanoHealthController/generateInvoice')?>',
			type: 'POST',
			dataType: 'json',
			data: { network_agent_id : "<?= $network_agent_id ?? null ?>"},
			success: function (data) {
				if(data.status === 200){
					Swal.fire({
						icon: 'success',
						title: "La facture a été générée",
						text:"<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
				}else{
					toastr.error(data.error , "<?= $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')?>");
			}
		});
	})

	$('#check-insured').click(function () {
		if ($('#search-form')[0].checkValidity()) {
			let option = $('#search-form').find('select[name="option"]').val();
			let query = $('#search-form').find('input[name="query"]').val();
			$.ajax({
				url: '<?= base_url('NanoHealthController/getInsured')?>',
				type: 'POST',
				dataType: 'json',
				data: { network_id : "<?= $id_network ?? null ?>" , query : query , option : option},
				success: function (data) {
					if(data.status === 200){
						if (data.response.length === 0 ) {
							toastr.error("<?= $this->lang->line('insured_not_found')?>", "<?= $this->lang->line('request_error')?>");
						}else{
							// Set data
							sessionStorage.setItem('insured' , JSON.stringify(data.response));
							window.location = '<?= current_url().('?action=')?>'+action;
						}
					}else{
						toastr.error(data.error , "<?= $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')?>");
				}
			});

		} else {
			$('#search-form')[0].reportValidity();
		}
	});
</script>
