<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') ?>">
<div class="content-wrapper">
	<section class="content-header">

		<h1 style="margin-bottom: 20px">
			<?= $this->lang->line('insured') . ' :: ' . $this->lang->line('insured_detail') . ' - ' . $insured_id; ?>
		</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('insurance_infos')?></h3>
			</div>
			<div class="box-body row" style="overflow-x:auto;">
				<div class="col-lg-6" style="padding-left: 25px;">
					<table style="width: 100%">
						<colgroup>
							<col span="1" style="width: 30%;">
							<col span="1" style="width: 70%;">
						</colgroup>
						<thead>
						<tr>
							<th></th>
							<th></th>
						</tr>
						</thead>
						<tbody>
						<tr>
							<td><?= $this->lang->line('insured_id'); ?></td>
							<td style="text-align: center"><strong><?= $insured_id ?></strong></td>
						</tr>

						<tr>
							<td><?= $this->lang->line('Nom'); ?></td>
							<td style="text-align: center"><strong><?= $user->lastname ?? '' . $user->firstname ?? ''; ?></strong></td>
						</tr>

						<tr>
							<td><?= $this->lang->line('Adresse')?></td>
							<td style="text-align: center"><strong><?= $user->adresse ?></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('Contact')?></td>
							<td style="text-align: center"><strong><?= $user->phone." | ".$user->email ?></strong></td>
						</tr>
						</tbody>
					</table>
				</div>
				<div class="col-lg-6" style="padding-left: 25px;">
					<table style="width: 100%">
						<colgroup>
							<col span="1" style="width: 30%;">
							<col span="1" style="width: 70%;">
						</colgroup>
						<thead>
						<tr>
							<th></th>
							<th></th>
						</tr>
						</thead>
						<tbody>
						<tr>
							<td><?= $this->lang->line('start_at'); ?></td>
							<td><strong id="start_at"></strong></td>
						</tr>
						<tr>
							<td><?= $this->lang->line('end_at'); ?></td>
							<td><strong id="end_at"></strong></td>
						</tr>
						</tbody>
					</table>
				</div>
			</div>
		</div>

		<div class="row">
			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-green-active">
					<div class="inner">
						<h3><?= Money::of(round($insurance->total_bonus_amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR') ?></h3>
						<p><?= $this->lang->line('bonus_amount') ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-cash"></i>
					</div>
				</div>
			</div>

			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-red">
					<div class="inner">
						<h3><?= Money::of(round($insurance->bonus_amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR') ?></h3>
						<p><?= $this->lang->line('insured_bonus_amount') ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-cash"></i>
					</div>
				</div>
			</div>

			<div class="col-lg-4 col-xs-6">
				<div class="small-box bg-light-blue">
					<div class="inner">
						<h3> <?= $insurance->number_of_months ?> </h3>
						<p><?= $this->lang->line('number_of_months') ?></p>
					</div>
					<div class="icon">
						<i class="fa fa-clock-o"></i>
					</div>
				</div>
			</div>

		</div>
		<div class="row">
			<div class="col-lg-3 col-xs-6">
				<div class="small-box bg-aqua-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?= $this->lang->line($insurance->state) ?></h3>
						<p><?= $this->lang->line('state') ?></p>
					</div>
					<div class="icon">
						<i class="fa fa-info"></i>
					</div>
				</div>
			</div>
			<div class="col-lg-3 col-xs-6">
				<div class="small-box bg-aqua-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?= Money::of(round(($insurance_invoices_total_amount - $insurance_invoices_total_paid_amount)  , 2), $insurance->currency_code, $this->context)->formatTo('fr_FR') ?></h3>
						<p><?= $this->lang->line('remaining_amount') ?></p>
					</div>
					<div class="icon">
						<i class="ion ion-cash"></i>
					</div>
				</div>
			</div>
			<div class="col-lg-3 col-xs-6">
				<div class="small-box bg-aqua-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?= $this->lang->line($insurance->payment_period) ?></h3>
						<p><?= $this->lang->line('payment_period') ?></p>
					</div>
					<div class="icon">
						<i class="fa fa-clock"></i>
					</div>
				</div>
			</div>
			<div class="col-lg-3 col-xs-6">
				<div class="small-box bg-aqua-active">
					<div class="inner">
						<h3 style="white-space: pre-wrap; font-size: 1.9em;"><?= $insurance->number_of_fractions ?></h3>
						<p><?= $this->lang->line('number_of_fractions') ?></p>
					</div>
					<div class="icon">
						<i class="fa fa-clock"></i>
					</div>
				</div>
			</div>
		</div>


		<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title"><?= $this->lang->line('beneficiaries') ?></h3>
			</div>
			<div class="box-body" style="overflow-x:auto;">
				<table id="beneficiaries" class="table table-bordered table-hover">
					<thead>
					<tr>
						<th>#</th>
						<th><?= $this->lang->line('Nom'); ?></th>
						<th><?= $this->lang->line('gender'); ?></th>
						<th><?= $this->lang->line('birth_date'); ?></th>
						<th>Affiliation</th>
						<th><?= $this->lang->line('bonus_amount'); ?></th>
						<th>Images</th>
					</tr>
					</thead>
					<tbody>
					<?php
					if (isset($beneficiaries)) {

						foreach ($beneficiaries->result() as $i => $row) {
							$bonus_amount = Money::of(round($row->bonus_amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR');

							echo "<tr>
												<td>" . ($i+1) . "</td>
												<td>" . $row->lastname.' '.$row->firstname . "</td>
												<td>" . $row->gender . "</td>
											   <td>" . $row->birthdate . "</td>
                                               <td>".$this->lang->line($row->affiliation)."</td>
                                               <td>" . $bonus_amount. "</td>";
							?>

							<td>
										<button class="btn btn-primary" data-target="<?= '#images-modal'.$i ?>" data-toggle="modal">
											<?= $this->lang->line('show'); ?>
										</button>
								</td>
							</tr>
							<?php $this->load->view('nano_health/modals/beneficiary_images_modal',['i' => $i, 'row' => $row]) ?>
							<?php
						}
					}
					?>
					</tbody>
				</table>
			</div>
		</div>

		<div class="box box-default">
			<div class="box-header with-border">
				<h3 class="box-title"><?= $this->lang->line('invoices_history') ?></h3>
			</div>
			<div class="box-body" style="overflow-x:auto;">
				<table id="invoices" class="table table-bordered table-hover">
					<thead>
					<tr>
						<th>ID</th>
						<th><?= $this->lang->line('souscription_id'); ?></th>
						<th><?= $this->lang->line('Montant'); ?></th>
						<th><?= $this->lang->line('remaining_amount'); ?></th>
						<th><?= $this->lang->line('reason'); ?></th>
						<th><?= $this->lang->line('state'); ?></th>
						<th>Date</th>
					</tr>
					</thead>
					<tbody>
					<?php
					if (isset($invoices)) {
						foreach ($invoices->result() as $i => $row) {
							$amount = Money::of(round($row->amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR');
							$remaining_amount = Money::of(round($row->amount - $row->paid_amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR');

							echo "<tr>
												<td>" . $row->invoice_id . "</td>
												<td>" . $row->insurance_subscription_id . "</td>
												<td>" . $amount . "</td>
												<td>" . $remaining_amount . "</td>
												<td>" . $this->lang->line($row->reason). "</td>
												<td>" . $this->lang->line($row->state). "</td>
                                               <td>" . $row->created_at. "</td>";
							?>
							</tr>
							<?php
						}
					}
					?>
					</tbody>
				</table>
			</div>
		</div>

		<div class="box box-default">
			<div class="box-header with-border">
				<h3 class="box-title"><?= $this->lang->line('payment_transaction_history') ?></h3>
			</div>
			<div class="box-body" style="overflow-x:auto;">
				<table id="payments" class="table table-bordered table-hover">
					<thead>
					<tr>
						<th>ID</th>
						<th><?= $this->lang->line('invoice_id'); ?></th>
						<th><?= $this->lang->line('Montant'); ?></th>
						<th>Date</th>
					</tr>
					</thead>
					<tbody>
					<?php
					if (isset($payments)) {
						foreach ($payments->result() as $i => $row) {
							$amount = Money::of(round($row->amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR');

							echo "<tr>
												<td>" . $row->payment_id . "</td>
												<td>" . $row->invoice_id . "</td>
												<td>" . $amount . "</td>
                                               <td>" . $row->created_at. "</td>";
							?>
							</tr>
							<?php
						}
					}
					?>
					</tbody>
				</table>
			</div>
		</div>

		<div class="box box-primary">
			<div class="box-header with-border">
				<h3 class="box-title"><?= $this->lang->line('beneficiaries_deletion_history') ?></h3>
			</div>
			<div class="box-body" style="overflow-x:auto;">
				<table id="deleted-beneficiaries" class="table table-bordered table-hover">
					<thead>
					<tr>
						<th>#</th>
						<th><?= $this->lang->line('Nom'); ?></th>
						<th><?= $this->lang->line('gender'); ?></th>
						<th><?= $this->lang->line('birth_date'); ?></th>
						<th>Affiliation</th>
						<th><?= $this->lang->line('bonus_amount'); ?></th>
						<th><?= $this->lang->line('deletion_date'); ?></th>
						<th>Images</th>
					</tr>
					</thead>
					<tbody>
					<?php
					if (isset($deletion_of_beneficiaries)) {

						foreach ($deletion_of_beneficiaries->result() as $i => $row) {
							$bonus_amount = Money::of(round($row->bonus_amount, 2), $insurance->currency_code, $this->context)->formatTo('fr_FR');

							echo "<tr>
												<td>" . ($i+1) . "</td>
												<td>" . $row->lastname.' '.$row->firstname . "</td>
												<td>" . $row->gender . "</td>
											   <td>" . $row->birthdate . "</td>
                                               <td>".$this->lang->line($row->affiliation)."</td>
                                                <td>".$bonus_amount."</td>
                                               <td>" . $row->deleted_at. "</td>";
							?>

							<td>
								<button class="btn btn-primary" data-target="<?= '#images-modal'.$i ?>" data-toggle="modal">
									<?= $this->lang->line('show'); ?>
								</button>
							</td>
							</tr>
							<?php $this->load->view('nano_health/modals/beneficiary_images_modal',['i' => $i, 'row' => $row]) ?>
							<?php
						}
					}
					?>
					</tbody>
				</table>
			</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 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 () {
		$('#start_at').text(moment("<?=$insurance->start_at?>", 'YYYY-MM-DD').format('D MMMM YYYY'));
		$('#end_at').text(moment("<?=$insurance->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
	$('#beneficiaries').DataTable(
			{
				order: [[1, 'asc']],
				dom: 'Bfrtip',
				"buttons": [
					'pageLength',
					{
						"extend": 'excelHtml5',
						title: "<?= $this->lang->line('beneficiaries') . ' :: ' . $this->lang->line('subscription_detail') . ' - ' . $insured_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('beneficiaries') . ' :: ' . $this->lang->line('subscription_detail') . ' - ' . $insured_id?>",
					},
					{
						extend: 'pdfHtml5',
						orientation: 'landscape',
						pageSize: 'LEGAL',
						title: "<?= $this->lang->line('beneficiaries') . ' :: ' . $this->lang->line('subscription_detail') . ' - ' . $insured_id?>",
					},
					// 'colvis'
				]
			}
	);

	$('#deleted-beneficiaries').DataTable(
		{
			order: [[1, 'asc']],
			dom: 'Bfrtip',
			"columnDefs": [{
				"targets": [6],
				// "orderable": false,
				render: $.fn.dataTable.render.moment( 'YYYY-MM-DD HH:mm:ss' , 'D MMMM YYYY HH:mm:ss', format)
			}],
			"buttons": [
				'pageLength',
				{
					"extend": 'excelHtml5',
					title: "<?= $this->lang->line('beneficiaries_deletion_history') . ' :: ' . $this->lang->line('subscription_detail') . ' - ' . $insured_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('beneficiaries_deletion_history') . ' :: ' . $this->lang->line('subscription_detail') . ' - ' . $insured_id?>",
				},
				{
					extend: 'pdfHtml5',
					orientation: 'landscape',
					pageSize: 'LEGAL',
					title: "<?= $this->lang->line('beneficiaries_deletion_history') . ' :: ' . $this->lang->line('subscription_detail') . ' - ' . $insured_id?>",
				},
				// 'colvis'
			]
		}
	);

	$('#invoices').DataTable(
		{
			order: [[1, 'asc']],
			"columnDefs": [{
				"targets": [6],
				// "orderable": false,
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}],
		}
	);

	$('#payments').DataTable(
		{
			order: [[1, 'asc']],
			"columnDefs": [{
				"targets": [3],
				// "orderable": false,
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}],
		}
	);

</script>
