<link rel="stylesheet"
	  href="<?= base_url('bower_components/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css') ?>">
<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="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css">
<link rel="stylesheet" href="https://cdn.datatables.net/select/1.3.1/css/select.dataTables.min.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/jquery-ui/themes/base/jquery-ui.css') ?>">

<link rel="stylesheet" href="<?= base_url('bower_components/toastr/toastr.css') ?>">
<style>
	#details tr > td:nth-child(even) {
		text-align: center;
	}
</style>
<div class="content-wrapper">
	<section class="content-header">

		<h1 style="margin-bottom: 20px">
			<?= $this->lang->line('health_care_sheets'). ' - ' . $health_care_sheet_id; ?>

			<div class="pull-right">
				<?php if($health_care_sheet->state == 'REJECTED') { ?>
					<button id="resubmit" class="btn btn-success" >
						<?= $this->lang->line('resubmit') ?>
					</button>
				<?php } ?>
			</div>
		</h1>

		<?php

		use Brick\Money\Context\AutoContext;
		use Brick\Money\CurrencyConverter;
		use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
		use Brick\Money\ExchangeRateProvider\PDOProvider;
		use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;
		use Brick\Money\Money;

		// set to whatever your rates are relative to
		$baseCurrency = 'USD';

		// use your own credentials, or re-use your existing PDO connection
		$pdo = new PDO('mysql:host=' . $this->db->hostname . ';dbname=' . $this->db->database, $this->db->username, $this->db->password);

		$configuration = new PDOProviderConfiguration();

		$configuration->tableName = 'exchange_rate';
		$configuration->exchangeRateColumnName = 'exchange_rate';
		$configuration->targetCurrencyColumnName = 'target_currency';
		$configuration->sourceCurrencyCode = $baseCurrency;

		// this provider loads exchange rates from your database
		$provider = new PDOProvider($pdo, $configuration);

		// this provider calculates exchange rates relative to the base currency
		$provider = new BaseCurrencyProvider($provider, $baseCurrency);

		// this currency converter can now handle any currency pair
		$this->converter = new CurrencyConverter($provider);
		$this->context = new AutoContext();
		$fmt = new NumberFormatter('fr_FR', NumberFormatter::DECIMAL);
		?>
	</section>

	<section class="content">
		<div class="box box-success">
			<div class="box-header with-border">
				<h3 class="box-title"><?= $this->lang->line('health_care_sheet_infos')?></h3>
			</div>
			<div class="box-body row" style="overflow-x:auto;" id="details">
				<div class="col-lg-6" style="padding-left: 25px;">
					<table style="width: 100% ; border-collapse: separate; border-spacing: 0.4em;">
						<colgroup>
							<col span="1" style="width: 30%;">
							<col span="1" style="width: 70%;">
						</colgroup>
						<thead>
						<tr>
							<th style="text-align: center"><strong>Patient</strong></th>
							<th></th>
						</tr>
						</thead>
						<tbody>
						<tr>
							<td><?= $this->lang->line('insured_id'); ?></td>
							<td><strong><?= $health_care_sheet->insured_id ?></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('insured_phone'); ?></td>
							<td><strong><?= $health_care_sheet->insured_phone ?></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('patient_lastname'); ?></td>
							<td><strong><?= $health_care_sheet->patient_lastname ; ?></strong></td>
						</tr>

						<tr>
							<td><?= $this->lang->line('patient_firstname')?></td>
							<td><strong><?= $health_care_sheet->patient_firstname  ?></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('patient_situation')?></td>
							<td><strong><?= $this->lang->line($health_care_sheet->patient_situation) ?></strong></td>
						</tr>
						<?php if($health_care_sheet->type == 'EXECUTION') { ?>
							<tr>
								<th style="text-align: center"><strong>Consultation</strong></th>
								<th></th>
							</tr>
							<tr>
								<td><?= $this->lang->line('consultation_id')?></td>
								<td><strong><?= $health_care_sheet->consultation_health_care_sheet_id ?></strong></td>
							</tr>
							<tr>
								<td><?= $this->lang->line('practitioner_lastname')?></td>
								<td><strong><?= $health_care_sheet->consultation_practitioner_lastname ?></strong></td>
							</tr>
							<tr>
								<td><?= $this->lang->line('practitioner_firstname')?></td>
								<td><strong><?= $health_care_sheet->consultation_practitioner_firstname ?></strong></td>
							</tr>
							<tr>
								<td>Classification</td>
								<td><strong><?= $health_care_sheet->consultation_practitioner_provider_class ?></strong></td>
							</tr>
							<tr>
								<td><?= $this->lang->line('institution_name'); ?></td>
								<td><strong><?= $health_care_sheet->consultation_institution_name ?></strong></td>
							</tr>
						<?php } ?>
						</tbody>
					</table>
				</div>
				<div class="col-lg-6" style="padding-left: 25px;">
					<table style="width: 100% ;border-collapse: separate; border-spacing: 0.4em;">
						<colgroup>
							<col span="1" style="width: 30%;">
							<col span="1" style="width: 70%;">
						</colgroup>
						<thead>
						<tr>
							<th><?=$this->lang->line('practitioner') ?></th>
							<th></th>
						</tr>
						</thead>
						<tbody>
						<tr>
							<td><?= $this->lang->line('institution_name'); ?></td>
							<td><strong><?= $health_care_sheet->institution_name ?></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('institution_code'); ?></td>
							<td><strong><?= $health_care_sheet->institution_code ?></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('practitioner_lastname'); ?></td>
							<td><strong><?= $health_care_sheet->practitioner_lastname ; ?></strong></td>
						</tr>

						<tr>
							<td><?= $this->lang->line('practitioner_firstname')?></td>
							<td><strong><?= $health_care_sheet->practitioner_firstname  ?></strong></td>
						</tr>
						<tr>
							<td>Classification</td>
							<td><strong><?= $health_care_sheet->practitioner_provider_class ?></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('care_condition')?></td>
							<td><strong><?= $this->lang->line($health_care_sheet->care_condition) ?></strong></td>
						</tr>
						<?php if(isset($health_care_sheet->accident_date)){ ?>
							<tr>
								<td><?= $this->lang->line('accident_date'); ?></td>
								<td><strong id="accident_date"></strong></td>
							</tr>
						<?php } ?>
						<?php if(isset($health_care_sheet->pregnancy_start_at)){ ?>
							<tr>
								<td><?= $this->lang->line('pregnancy_start_at'); ?></td>
								<td><strong id="pregnancy_start_at"></strong></td>
							</tr>
							<tr>
								<td><?= $this->lang->line('pregnancy_end_at'); ?></td>
								<td><strong id="pregnancy_end_at"></strong></td>
							</tr>
						<?php } ?>

						</tbody>
					</table>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-blue-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?=$health_care_sheet->type?> </h3>
						<p>Type </p>
					</div>
					<div class="icon">
						<i class="fa fa-info"></i>
					</div>
				</div>
			</div>

			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-blue-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?= $this->lang->line($health_care_sheet->state)?> </h3>
						<p><?= $this->lang->line('Statut') ?></p>
					</div>
					<div class="icon">
						<i class="fa fa-info"></i>
					</div>
				</div>
			</div>

		</div>
		<div class="row">
			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-blue-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?=Money::of(round($health_care_sheet->insured_amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR')?> </h3>
						<p><?= $this->lang->line('percentage_insured') ?></p>
					</div>
					<div class="icon">
						<i class="fa fa-money"></i>
					</div>
				</div>
			</div>

			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-blue-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?= Money::of(round($health_care_sheet->insurance_amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR')?> </h3>
						<p><?= $this->lang->line('percentage_insurer') ?></p>
					</div>
					<div class="icon">
						<i class="fa fa-money"></i>
					</div>
				</div>
			</div>

		</div>

		<?php if($health_care_sheet->type != 'EXECUTION') { ?>
			<div class="box box-primary">
				<div class="box-header with-border">
					<h3 class="box-title"><?= $this->lang->line('performances') ?></h3>
				</div>
				<div class="box-body" style="overflow-x:auto;">
					<table id="performances" class="table table-bordered table-hover">
						<thead>
						<tr>
							<th>#</th>
							<th><?= $this->lang->line('act_code'); ?></th>
							<th><?= $this->lang->line('Montant'); ?></th>
							<th><?= $this->lang->line('home_visit_fees'); ?></th>
							<th><?= $this->lang->line('percentage_insured'); ?></th>
							<th><?= $this->lang->line('percentage_insurer'); ?></th>
							<th>Date</th>
						</tr>
						</thead>
						<tbody>
						<?php
						if (isset($performances)) {
							foreach ($performances->result() as $i => $row) {
								$amount = Money::of(round($row->amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
								$moderator_ticket = Money::of(round($row->moderator_ticket, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
								$insurance_amount = Money::of(round($row->insurance_amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
								$home_visit_fees = isset($row->home_visit_fees) ? Money::of(round($row->home_visit_fees, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR') : '';

								echo "<tr>
												<td>" . ($i+1) . "</td>
												<td>" . $row->act_code . "</td>
												<td>" . $amount . "</td>
                                               <td>".$home_visit_fees."</td>
											   <td>" . $moderator_ticket . "</td>
											    <td>" . $insurance_amount . "</td>
                                               <td>" . $row->created_at. "</td>";
								?>
								</tr>
								<?php
							}
						}
						?>
						</tbody>
					</table>
				</div>
			</div>
		<?php } ?>


		<?php if (isset($exams) && $exams->num_rows() > 0 ) { ?>
			<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title"><?= $this->lang->line('exams') ?></h3>
			</div>
			<div class="box-body" style="overflow-x:auto;">
				<table id="exams" class="table table-bordered table-hover">
					<thead>
					<tr>
						<th>#</th>
						<th><?= $this->lang->line('exam_class'); ?></th>
						<th><?= $this->lang->line('exam_description'); ?></th>
						<?php if($health_care_sheet->type == 'EXECUTION') { ?>
							<th><?= $this->lang->line('quantity'); ?></th>
							<th><?= $this->lang->line('UNIT_PRICE'); ?></th>
							<th><?= $this->lang->line('percentage_insured'); ?></th>
							<th><?= $this->lang->line('percentage_insurer'); ?></th>
						<?php } ?>
					</tr>
					</thead>
					<tbody>
					<?php
						foreach ($exams->result() as $i => $row) {
							if($health_care_sheet->type == 'EXECUTION') {
								$unit_price = Money::of(round($row->unit_price, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
								$insurance_part = Money::of(round($row->insurer_paid_amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
								$insured_part = Money::of(round($row->insured_paid_amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
							}
						echo "<tr>
												<td>" . ($i+1) . "</td>
												<td>" . $row->act_name . "</td>
												<td>" . $row->description . "</td>";
							      if($health_care_sheet->type == 'EXECUTION') {
									  	$unit_quantity = !empty($row->unit_quantity) ? "$row->unit_quantity $row->act_unit_value x " : '';
									  ?>
									  			<td><?= $row->quantity ?></td>
												<td><?= $unit_quantity.$unit_price ?></td>
											    <td><?= $insured_part ?></td>
											    <td><?= $insurance_part ?></td>
								  <?php }
							?>
						</tr>
						<?php
					}
					?>
					</tbody>
				</table>
			</div>
		</div>
		<?php } ?>


		<?php if (isset($prescriptions) && $prescriptions->num_rows() > 0 ) { ?>
			<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title"><?= $this->lang->line('medical_prescriptions') ?></h3>
			</div>
			<div class="box-body" style="overflow-x:auto;">
				<table id="prescriptions" class="table table-bordered table-hover">
					<thead>
					<tr>
						<th>#</th>
						<th><?= $this->lang->line('drugs_or_device'); ?></th>
						<th><?= $this->lang->line('dosage'); ?></th>
						<th><?= $this->lang->line('quantity'); ?></th>
						<?php if($health_care_sheet->type == 'CONSULTATION') { ?>
							<th><?= $this->lang->line('billed_quantity'); ?></th>
						<?php } ?>
						<?php if($health_care_sheet->type == 'EXECUTION') { ?>
							<th><?= $this->lang->line('UNIT_PRICE'); ?></th>
							<th><?= $this->lang->line('percentage_insured'); ?></th>
							<th><?= $this->lang->line('percentage_insurer'); ?></th>
						<?php } ?>
					</tr>
					</thead>
					<tbody>
					<?php
						foreach ($prescriptions->result() as $i => $row) {
							if($health_care_sheet->type == 'EXECUTION') {
		//							$amount = Money::of(round($row->amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR');
								$unit_price = Money::of(round($row->unit_price, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
								$insurance_part = Money::of(round($row->insurer_paid_amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
								$insured_part = Money::of(round($row->insured_paid_amount, 2), $health_care_sheet->currency_code, $this->context)->formatTo('fr_FR');
							}

							echo "<tr>
												<td>" . ($i+1) . "</td>
												<td>" . $row->drug_or_device_name . "</td>
												<td>" . $row->dosage . "</td>
												<td>" . $row->quantity. "</td>";
								 	if($health_care_sheet->type == 'CONSULTATION') { ?>
											<td><?= $row->billed_quantity ?? 0 ?></td>
									<?php	}
									if($health_care_sheet->type == 'EXECUTION') {  ?>
										<td><?= $unit_price ?></td>
										<td><?= $insured_part ?></td>
										<td><?= $insurance_part ?></td>
									<?php } ?>
						</tr>
						<?php
					}
					?>
					</tbody>
				</table>
			</div>
		</div>
		<?php } ?>

	</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 src="<?= base_url('bower_components/jquery-ui/jquery-ui.js') ?>"></script>

<script src="<?= base_url('bower_components/moment/min/moment.min.js') ?>"></script>
<script src="https://cdn.datatables.net/select/1.3.1/js/dataTables.select.min.js"></script>
<script src="<?= base_url('bower_components/toastr/toastr.js') ?>"></script>
<script src="<?= base_url('dist/js/sweetalert2.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 type="text/javascript">
	toastr.options.closeButton = true;
	toastr.options.closeMethod = 'fadeOut';
	toastr.options.closeDuration = 5000;
	toastr.options.closeEasing = 'swing';

	$(document).ready(function () {
		$('#accident_date').text(moment("<?=$health_care_sheet->accident_date?>", 'YYYY-MM-DD').format('D MMMM YYYY'));
		$('#pregnancy_start_at').text(moment("<?=$health_care_sheet->pregnancy_start_at?>", 'YYYY-MM-DD').format('D MMMM YYYY'));
		$('#pregnancy_end_at').text(moment("<?=$health_care_sheet->pregnancy_end_at?>", 'YYYY-MM-DD').format('D MMMM YYYY'));
	});


	const format = "<?=$this->session->userdata('site_lang')?>" === 'french' ? 'fr' : 'en';
	moment.updateLocale(moment.locale(format), {invalidDate: ""}); // Blank text when is invalid date
	$('#performances').DataTable(
			{
				order: [[0, 'asc']],
				"columnDefs": [{
					"targets": [6],
					// "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('performances') . ' :: ' . $this->lang->line('health_care_sheet_infos') . ' - ' . $health_care_sheet_id?>",
						// exportOptions: {
						// 	columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
						// },
						customizeData: function (data) {
							for (var i = 0; i < data.body.length; i++) {
								for (var j = 0; j < data.body[i].length; j++) {
									// data.body[i][j] = '\u200C' + data.body[i][j];
									if ([5].includes(j)) {
										// Get the value and strip the non numeric characters
										// var value = $(this).text();
										value = data.body[i][j].replace(',', ".")
										data.body[i][j] = Number(value.replace(/[^0-9\.-]+/g, ""));
									}
								}
							}
						},
					},
					{
						extend: 'csvHtml5',
						title: "<?= $this->lang->line('performances') . ' :: ' . $this->lang->line('health_care_sheet_infos') . ' - ' . $health_care_sheet_id?>",
					},
					{
						extend: 'pdfHtml5',
						orientation: 'landscape',
						pageSize: 'LEGAL',
						title: "<?= $this->lang->line('performances') . ' :: ' . $this->lang->line('health_care_sheet_infos') . ' - ' . $health_care_sheet_id?>",
					},
					// 'colvis'
				]
			}
	);

	$('#exams').DataTable(
		{
			order: [[0, 'asc']],
		}
	);

	$('#prescriptions').DataTable(
		{
			order: [[0, 'asc']],
		}
	);

	$('#resubmit').click(function() {
		$.ajax({
			url: '<?= base_url('NanoHealthController/resubmitHealthCareSheet')?>',
			type: 'POST',
			dataType: 'json',
			data: {
				"health_care_sheet_id": "<?=$health_care_sheet->id ?>",
				"user_id": "<?= $health_care_sheet->user_id ?>",
			},
			success: function (data) {
				// console.log('data',data);
				if(data.status === 200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('subscription_resubmitted')?>",
						text:"<?= $this->lang->line('informations_updated')?>",
						timer: 3000
					}).then(()=>{
						location.reload();
					});
					// alert("Les informations ont été mises à jour.") ? "" :
				}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')?>");
			}
		});
	});

</script>
