<!-- 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" href="<?= base_url('bower_components/toastr/toastr.css') ?>">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.6.2/css/buttons.dataTables.min.css">
<div class="content-wrapper">

	<?php

//	use Carbon\Carbon;
	$fmt = new NumberFormatter( 'fr_FR', NumberFormatter::DECIMAL );

//	function toLocateDate($date , $timezone){
//		if($date){
//			$carbon = Carbon::createFromFormat('Y-m-d H:i:s', $date, 'UTC');
//			$carbon->setTimezone($timezone);
//			return $carbon->toDateTimeString();
//		}
//		return $date;
//	}

	?>
	<!-- Content Header (Page header) -->
	<section class="content-header">

		<h1>
			<?= $type == 'outgoing' ? $this->lang->line('outgoing_credits_requests') : $this->lang->line('incoming_credits_requests'); ?>
		</h1>
		<?php
		$site_url = base_url();

		if ($alert == "ok") {

			if (!$success == "ok") {
				?>
				<div class='alert alert-danger alert-dismissible col-xs-6'>
					<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>
					<h4><i class='icon fa fa-ban'></i> Erreur!</h4>
					<?= $message; ?>
				</div>

				<?php
			} else {
				?>
				<div class="alert alert-success alert-dismissible col-xs-6">
					<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
					<h4><i class="icon fa fa-check"></i> Success!</h4>
					<?= $message; ?>
				</div>

				<?php
			}
		}
		?>

	</section>
	<section class="content">
		<div class="row">
			<?php if (($type == 'incoming') || ($this->session->userdata('category') == 'hyper')) { ?>
				<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-arrow-down"></i></span>

						<div class="info-box-content">
							<span class="info-box-text"><?= $this->lang->line('Temps moyen de traitement entrant'); ?> </span>
							<span class="info-box-number">
                            <?php
							if ($temp_moyen) {
								if (duree($temp_moyen) == "") {
									echo "0s";
								} else {
									echo duree($temp_moyen);
								}
							} else {
								echo $this->lang->line('Aucune demande traitée');
							}
							?>
                        </span>
						</div>
					</div>
				</div>
			<?php } ?>
			<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%"
										   type="text" name="daterange"
										   data-lang="<?= $this->session->userdata('site_lang') ?>"
										   data-type="<?= $demand_type ?>"
										   data-category="<?= $this->session->userdata('category') ?>"
										   value="<?= ($debut != null & $fin != null) ? $debut . ' - ' . $fin : '' ?>"
									/>
                            </span>
						<span> Format : <?= $this->session->userdata('site_lang') === 'french' ? 'Jour - Mois - Année ' : 'Year - Month - Day' ?> </span>
					</div>
				</div>
			</div>
			<?php if (($this->session->userdata('category') == 'super') && ($type == 'outgoing')) { ?>
				<div class="col-md-4 col-sm-6 col-xs-12">
					<div class="info-box">
						<span class="info-box-icon bg-green"><i class="ion ion-android-arrow-up"></i></span>

						<div class="info-box-content">
							<span class="info-box-text"><?= $this->lang->line('Temps moyen de traitement sortant'); ?></span>
							<span class="info-box-number">
                            <?php
							if ($temp_moyen_mine) {
								if (duree($temp_moyen_mine) == "") {
									echo "0s";
								} else {
									echo duree($temp_moyen_mine);
								}
							} else {
								echo $this->lang->line('Aucune demande traitée');
							}
							?>
                        </span>
						</div>
					</div>
				</div>
				<?php
			}
			?>
		</div>
		<?php if ($this->session->userdata('category') == 'super') { ?>
			<div class="row">
				<?php if ($type == 'incoming') { ?>
					<div class="col-md-12">
						<div class="box">
							<div class="box-header with-border">
								<h3 class="box-title"><?= $this->lang->line('incoming_credits_requests'); ?></h3>
							</div>
							<div class="box-body" style="overflow-x:auto;">

								<?php

								$result = $list;
								if ($result){

								$numrows = $result->num_rows();
								$num = 0;
								if ($numrows > 0) {

								?>

								<table id="listeMembres1" class="table table-bordered table-striped table-modified">
									<thead>
									<?php
									echo "<tr class='label-primary'>
									<th>" . $this->lang->line('Telephone') . "</th>
									<th>" . $this->lang->line('Emetteur') . "</th>
									<th>" . $this->lang->line('Montant') . "</th>
									<th>" . $this->lang->line('Date de la demande') . "</th>
									<th>" . $this->lang->line('Date de traitement') . "</th>
									<th>" . $this->lang->line('Statut') . "</th>
									<th>" . $this->lang->line('Délai de traitement') . "</th>
									<th>" . $this->lang->line('Délai de traitement')."(en minutes)" . "</th>
								</tr>";

									?>
									</thead>
									<tbody>


									<?php
									// output data of each row
									foreach ($result->result() as $row) {
										$num++;
										$etat = $this->lang->line("Non traitée");
										$disabled = "disabled";
										if ($row->statut == 1) {
											$disabled = "";
											$etat = $this->lang->line("Acceptée");
										} else if ($row->statut == 2) {
											$etat = $this->lang->line("Annulée");
										}
										$origin = $row->agent;
										$destination = $row->codeParrain;
										if ($row->codeMembre == $this->session->userdata('member_code')) {
											$origin = $this->lang->line('Vous');
										}
										if ($row->codeParrain == $this->session->userdata('member_code')) {
											$destination = $this->lang->line('Vous');
										}
										echo "<tr>
										<td>  $row->phone</td>
										<td >$origin </td>
										<td >".$fmt->format($row->montant)."</td>
										<td>" . $row->dateAjout . "</td>
										<td > " . $row->dateModif . "</td>
										<td>$etat</td>
										<td>" . traitementTemps($row->temps, $row->dateAjout,$this) . "</td>
										<td>" . getDelayOfTreatmentInSeconds($row->temps, $row->dateAjout)/60 . "</td>
										 </tr>";

									}
									?>
									</tbody>


									<?php
									} else {
										echo $this->lang->line('Aucune demande');
									}
									} else {
										echo $this->lang->line('Aucune demande');
									}


									?>

								</table>


							</div>
						</div>
					</div>
				<?php } else { ?>
					<div class="col-md-12">
						<div class="box">
							<div class="box-header with-border">
								<h3 class="box-title"><?= $this->lang->line('outgoing_credits_requests'); ?></h3>
							</div>
							<div class="box-body">

								<?php

								$result = $mydem;
								if ($result){

								$numrows = $result->num_rows();
								$num = 0;
								if ($numrows > 0) {

								?>

								<table id="listeMembres2" class="table table-bordered table-striped table-modified">
									<thead>
									<?php

									echo "<tr class='label-primary'>
								   <th>" . $this->lang->line('Montant') . "</th>
									<th>" . $this->lang->line('Date de la demande') . "</th>
									<th>" . $this->lang->line('Date de traitement') . "</th>
									<th>" . $this->lang->line('Statut') . "</th>
									<th>" . $this->lang->line('Délai de traitement') . "</th>
									<th>" . $this->lang->line('Délai de traitement')."(en minutes)" . "</th>
								</tr>";

									?>
									</thead>
									<tbody>


									<?php
									// output data of each row
									foreach ($result->result() as $row) {
										$num++;

										$etat = $this->lang->line("Non traitée");
										if ($row->statut == 1) {
											$etat = $this->lang->line("Acceptée");
										} else if ($row->statut == 2) {
											$etat = $this->lang->line("Annulée");
										}
										$origin = $row->codeMembre;
										$destination = $row->codeParrain;
										if ($row->codeMembre == $this->session->userdata('member_code')) {
											$origin = $this->lang->line('Vous');
										}
										if ($row->codeParrain == $this->session->userdata('member_code')) {
											$destination = $this->lang->line('Vous');
										}
										echo "<tr>
	
										<td >".$fmt->format($row->montant)."</td>
										<td>" . $row->dateAjout . "</td>
										<td > " . $row->dateModif . "</td>
										<td>$etat</td>
										<td>" . traitementTemps($row->temps, $row->dateAjout,$this) . "</td>
										<td>" . getDelayOfTreatmentInSeconds($row->temps, $row->dateAjout)/60 . "</td>
										
										 </tr>";

									}
									?>
									</tbody>


									<?php
									} else {
										echo $this->lang->line('Aucune demande');
									}
									} else {
										echo $this->lang->line('Aucune demande');
									}
									?>

								</table>


							</div>
						</div>
					</div>
				<?php } ?>
			</div>
			<div class="row">
				<?php if ($type == 'incoming') { ?>
					<div class="col-md-12">
						<div class="box">

							<div class="box-header with-border">
								<h3 class="box-title"><?= $this->lang->line('incoming_credits_requests').' '.$this->lang->line('les plus rapidement traitées'); ?></h3>
							</div>
							<div class="box-body">

								<?php

								$result = $tab_fastDemande;
								if ($result){

								$numrows = $result->num_rows();
								$num = 0;
								if ($numrows > 0) {

								?>

								<table id="listeDemandesRapides"
									   class="table table-bordered table-striped table-modified">
									<thead>
									<?php
									echo "<tr class='label-primary'>
									<th>N°</th>
									<th>" . $this->lang->line('Membre') . "</th>
									<th>" . $this->lang->line('Parrain') . "</th>
									<th>" . $this->lang->line('Montant') . "</th>
									<th>" . $this->lang->line('Date de la demande') . "</th>
									<th>" . $this->lang->line('Date de traitement') . "</th>
									<th>" . $this->lang->line('Délai de traitement') . "</th>
								</tr>";

									?>
									</thead>
									<tbody>


									<?php

									// output data of each row
									foreach ($result->result() as $row) {
										$num++;
										$superviseur = $row->superviseur;
										if ($row->superviseur == $this->session->userdata('member_code')) {
											$superviseur = $this->lang->line('Vous');
										}

										echo "<tr>
	
										<td>$num</td>
										<td>$row->agent</td>
										<td>$superviseur</td>
										<td>".$fmt->format($row->montant)."</td>
										<td>" . $row->dateA . "</td>
										<td> " . $row->dateM . "</td>
										<td>" . duree($row->temps) . "</td>
										
										 </tr>";

									}
									?>
									</tbody>


									<?php
									} else {
										echo $this->lang->line('Aucune demande');
									}
									} else {
										echo $this->lang->line('Aucune demande');
									}

									?>

								</table>


							</div>
						</div>
					</div>
				<?php } else { ?>
					<div class="col-md-12">
						<div class="box">
							<div class="box-header with-border">
								<h3 class="box-title"><?= $this->lang->line('outgoing_credits_requests').' '.$this->lang->line('les plus rapidement traitées'); ?></h3>
							</div>
							<div class="box-body">

								<?php

								$result = $tab_fastDemande_s;
								if ($result){

								$numrows = $result->num_rows();
								$num = 0;
								if ($numrows > 0) {

								?>

								<table id="listeDemandesRapides_s"
									   class="table table-bordered table-striped table-modified">
									<thead>
									<?php
									echo "<tr class='label-primary'>
									<th>N°</th>
									<th>" . $this->lang->line('Montant') . "</th>
									<th>" . $this->lang->line('Date de la demande') . "</th>
									<th>" . $this->lang->line('Date de traitement') . "</th>
									<th>" . $this->lang->line('Délai de traitement') . "</th>
								</tr>";

									?>
									</thead>
									<tbody>


									<?php

									// output data of each row
									foreach ($result->result() as $row) {
										$num++;
										$superviseur = $row->superviseur;
										if ($row->superviseur == $this->session->userdata('member_code')) {
											$superviseur = $this->lang->line('Vous');
										}

										echo "<tr>
	
										<td>$num</td>
										<td>".$fmt->format($row->montant)."</td>
										<td>" . $row->dateA . "</td>
										<td> " . $row->dateM . "</td>
										<td>" . duree($row->temps) . "</td>
										
										 </tr>";

									}
									?>
									</tbody>


									<?php
									} else {
										echo $this->lang->line('Aucune demande');
									}
									} else {
										echo $this->lang->line('Aucune demande');
									}


									?>

								</table>


							</div>
						</div>
					</div>
				<?php } ?>
			</div>
			<div class="row">
				<?php if ($type == 'incoming') { ?>
					<div class="col-md-12">
						<div class="box">
							<div class="box-header with-border">
								<h3 class="box-title"><?= $this->lang->line('incoming_credits_requests').' '.$this->lang->line('les plus tardivement traitées'); ?></h3>
							</div>
							<div class="box-body">

								<?php

								$result = $tab_slowDemande;
								if ($result){

								$numrows = $result->num_rows();
								$num = 0;
								if ($numrows > 0) {

								?>

								<table id="listeDemandesTardives"
									   class="table table-bordered table-striped table-modified">
									<thead>
									<?php
									echo "<tr class='label-primary'>
									<th>N°</th>
									<th>" . $this->lang->line('Membre') . "</th>
									<th>" . $this->lang->line('Parrain') . "</th>
									<th>" . $this->lang->line('Montant') . "</th>
									<th>" . $this->lang->line('Date de la demande') . "</th>
									<th>" . $this->lang->line('Date de traitement') . "</th>
									<th>" . $this->lang->line('Délai de traitement') . "</th>
								</tr>";
									?>
									</thead>
									<tbody>


									<?php

									// output data of each row
									foreach ($result->result() as $row) {
										$num++;
										$superviseur = $row->superviseur;
										if ($row->superviseur == $this->session->userdata('member_code')) {
											$superviseur = $this->lang->line('Vous');
										}

										echo "<tr>
	
										<td>$num</td>
										<td>$row->agent</td>
										<td>$superviseur</td>
										<td>".$fmt->format($row->montant)."</td>
										<td>" . $row->dateA . "</td>
										<td> " . $row->dateM . "</td>
										<td>" . duree($row->temps) . "</td>
										
										 </tr>";

									}
									?>
									</tbody>


									<?php
									} else {
										echo $this->lang->line('Aucune demande');
									}
									} else {
										echo $this->lang->line('Aucune demande');
									}


									?>

								</table>


							</div>
						</div>
					</div>
				<?php } else { ?>
					<div class="col-md-12">
						<div class="box">
							<div class="box-header with-border">
								<h3 class="box-title"><?= $this->lang->line('outgoing_credits_requests').' '.$this->lang->line('les plus tardivement traitées'); ?></h3>
							</div>
							<div class="box-body">

								<?php

								$result = $tab_slowDemande_s;
								if ($result){

								$numrows = $result->num_rows();
								$num = 0;
								if ($numrows > 0) {

								?>

								<table id="listeDemandesTardives_s"
									   class="table table-bordered table-striped table-modified">
									<thead>
									<?php
									echo "<tr class='label-primary'>
									<th>N°</th>
									<th>" . $this->lang->line('Montant') . "</th>
									<th>" . $this->lang->line('Date de la demande') . "</th>
									<th>" . $this->lang->line('Date de traitement') . "</th>
									<th>" . $this->lang->line('Délai de traitement') . "</th>
								</tr>";
									?>
									</thead>
									<tbody>


									<?php

									// output data of each row
									foreach ($result->result() as $row) {
										$num++;
										$superviseur = $row->superviseur;
										if ($row->superviseur == $this->session->userdata('member_code')) {
											$superviseur = $this->lang->line('Vous');
										}

										echo "<tr>
	
										<td>$num</td>
										<td>".$fmt->format($row->montant)."</td>
										<td>" . $row->dateA . "</td>
										<td> " . $row->dateM . "</td>
										<td>" . duree($row->temps) . "</td>
										
										 </tr>";

									}
									?>
									</tbody>


									<?php
									} else {
										echo $this->lang->line('Aucune demande');
									}
									} else {
										echo $this->lang->line('Aucune demande');
									}

									?>

								</table>


							</div>
						</div>
					</div>
				<?php } ?>
			</div>
		<?php } else { ?>
			<div class="row">
				<div class="col-md-12">
					<div class="box">

						<div class="box-header with-border">
							<h3 class="box-title"><?= $this->lang->line('incoming_credits_requests'); ?></h3>
						</div>
						<div class="box-body" style="overflow-x:auto;">

							<?php

							$result = $list;
							if ($result){

							$numrows = $result->num_rows();
							$num = 0;
							if ($numrows > 0) {

							?>
							<table id="listeMembres" class="table table-bordered table-striped table-modified">
								<thead>
								<?php

								echo "<tr class='label-primary'>
                                            <th>" . $this->lang->line('Reseau') . "</th>
                                            <th>" . $this->lang->line('Téléphone utilisateur') . "</th>
                                            <th>" . $this->lang->line('Emetteur') . "</th>
                                            <th>" . $this->lang->line('Parrain') . "</th>
                                            <th>" . $this->lang->line('Montant') . "</th>
                                            <th>" . $this->lang->line('Date de la demande') . "</th>
                                            <th>" . $this->lang->line('Date de traitement') . "</th>
                                            <th>" . $this->lang->line('Statut') . "</th>
                                            <th>" . $this->lang->line('Délai de traitement') . "</th>
                                            <th>" . $this->lang->line('Délai de traitement')."(en minutes)" . "</th>
                                        </tr>";
								?>
								</thead>
								<tbody>

								<?php

								// output data of each row
								foreach ($result->result() as $row) {
									$num++;
									$disabled = "disabled";
									$etat = $this->lang->line("Non traitée");
									if ($row->statut == 1) {
										$disabled = "";
										$etat = $this->lang->line("Acceptée");
									} else if ($row->statut == 2) {
										$etat = $this->lang->line("Annulée");
									}
									$origin = $row->agent;
									$destination = $row->codeParrain;
									if ($row->codeMembre == $this->session->userdata('member_code')) {
										$origin = $this->lang->line('Vous');
									}
									if ($row->codeParrain == $this->session->userdata('member_code')) {
										$destination = $this->lang->line('Vous');
									}
									echo "<tr>

                                    <td>  $row->reseau</td>
                                    <td>  $row->phone </td>
                                    <td >$origin </td>
                                    <td>$destination</td>
                                    <td >".$fmt->format($row->montant)."</td>
                                    <td>" . $row->dateAjout . "</td>
									<td> " . $row->dateModif . "</td>
                                    <td>$etat</td>
                                    <td>" . traitementTemps($row->temps, $row->dateAjout,$this) . "</td>
                                    <td>" . getDelayOfTreatmentInSeconds($row->temps, $row->dateAjout)/60 . "</td>
                                     </tr>";

								}
								?>
								</tbody>

								<?php
								} else {
									echo $this->lang->line('Aucune demande');
								}
								} else {
									echo $this->lang->line('Aucune demande');
								}

								?>

							</table>


						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-md-12">
					<div class="box">

						<div class="box-header with-border">
							<h3 class="box-title"><?= $this->lang->line('incoming_credits_requests').' '.$this->lang->line('les plus rapidement traitées'); ?></h3>
						</div>
						<div class="box-body">

							<?php

							$result = $tab_fastDemande;
							if ($result){

							$numrows = $result->num_rows();
							$num = 0;
							if ($numrows > 0) {

							?>

							<table id="listeDemandesRapides" class="table table-bordered table-striped table-modified">
								<thead>
								<?php
								echo "<tr class='label-primary'>
                                <th>N°</th>
                                <th>" . $this->lang->line('Membre') . "</th>
                                <th>" . $this->lang->line('Parrain') . "</th>
                                <th>" . $this->lang->line('Montant') . "</th>
                                <th>" . $this->lang->line('Date de la demande') . "</th>
                                <th>" . $this->lang->line('Date de traitement') . "</th>
                                <th>" . $this->lang->line('Délai de traitement') . "</th>
                            </tr>";
								?>
								</thead>
								<tbody>


								<?php

								// output data of each row
								foreach ($result->result() as $row) {
									$num++;
									$superviseur = $row->superviseur;
									if ($row->superviseur == $this->session->userdata('member_code')) {
										$superviseur = $this->lang->line('Vous');
									}

									echo "<tr>

                                    <td>$num</td>
                                    <td>$row->emetteur</td>
                                    <td>$superviseur</td>
                                    <td>".$fmt->format($row->montant)."</td>
                                    <td>" . $row->dateA . "</td>
									<td> " . $row->dateM . "</td>
                                    <td>" . duree($row->temps) . "</td>
                                    
                                     </tr>";

								}
								?>
								</tbody>


								<?php
								} else {
									echo $this->lang->line('Aucune demande');
								}
								} else {
									echo $this->lang->line('Aucune demande');
								}

								?>

							</table>


						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-md-12">
					<div class="box">
						<div class="box-header with-border">
							<h3 class="box-title"><?= $this->lang->line('incoming_credits_requests').' '.$this->lang->line('les plus tardivement traitées'); ?></h3>
						</div>
						<div class="box-body">

							<?php

							$result = $tab_slowDemande;
							if ($result){

							$numrows = $result->num_rows();
							$num = 0;
							if ($numrows > 0) {

							?>

							<table id="listeDemandesTardives" class="table table-bordered table-striped table-modified">
								<thead>
								<?php

								echo "<tr class='label-primary'>
                                <th>N°</th>
                                <th>" . $this->lang->line('Membre') . "</th>
                                <th>" . $this->lang->line('Parrain') . "</th>
                                <th>" . $this->lang->line('Montant') . "</th>
                                <th>" . $this->lang->line('Date de la demande') . "</th>
                                <th>" . $this->lang->line('Date de traitement') . "</th>
                                <th>" . $this->lang->line('Délai de traitement') . "</th>
                            </tr>";

								?>
								</thead>
								<tbody>
								<?php

								// output data of each row
								foreach ($result->result() as $row) {
									$num++;
									$superviseur = $row->superviseur;
									if ($row->superviseur == $this->session->userdata('member_code')) {
										$superviseur = $this->lang->line('Vous');
									}

									echo "<tr>

                                    <td>$num</td>
                                    <td>$row->emetteur</td>
                                    <td>$superviseur</td>
                                    <td>".$fmt->format($row->montant)."</td>
                                   <td>" . $row->dateA . "</td>
									<td> " . $row->dateM . "</td>
                                    <td>" . duree($row->temps) . "</td>
                                    
                                     </tr>";

								}
								?>
								</tbody>

								<?php
								} else {
									echo $this->lang->line('Aucune demande');
								}
								} else {
									echo $this->lang->line('Aucune demande');
								}

								?>

							</table>


						</div>
					</div>
				</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>


<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css"/>
<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/plug-ins/1.10.20/dataRender/datetime.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

		$('#listeMembres').DataTable({
			"aaSorting": [[5, "desc"]],
			"columnDefs": [{
				targets: [5, 6],
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format),
			},
				{
					"targets": [ 9 ],
					"visible": false
				}
			],
			dom: 'Bfrtip',
			"buttons": [
				'pageLength',
				{
					extend: 'excelHtml5',
					title: "<?=$this->lang->line('incoming_credits_requests')?>"
				},
				{
					extend: 'csvHtml5',
					title: "<?=$this->lang->line('incoming_credits_requests')?>"
				},
				{
					extend: 'pdfHtml5',
					orientation: 'landscape',
					pageSize: 'LEGAL',
					title: "<?=$this->lang->line('incoming_credits_requests')?>"
				}
			]
		});

		$('#listeMembres1').DataTable({
			"aaSorting": [[3, "desc"]],
			"columnDefs": [{
				targets: [3, 4],
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format),
			},
				{
					"targets": [ 7 ],
					"visible": false
				}
			],
			dom: 'Bfrtip',
			"buttons": [
				'pageLength',
				{
					extend: 'excelHtml5',
					title: "<?=$this->lang->line('incoming_credits_requests')?>"
				},
				{
					extend: 'csvHtml5',
					title: "<?=$this->lang->line('incoming_credits_requests')?>"
				},
				{
					extend: 'pdfHtml5',
					orientation: 'landscape',
					pageSize: 'LEGAL',
					title: "<?=$this->lang->line('incoming_credits_requests')?>"
				}
			]
		});
		$('#listeMembres2').DataTable({
			"aaSorting": [[1, "desc"]],
			"columnDefs": [{
				targets: [1, 2],
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			},
				{
					"targets": [ 5 ],
					"visible": false
				}
			],
			dom: 'Bfrtip',
			"buttons": [
				'pageLength',
				{
					extend: 'excelHtml5',
					title: "<?=$this->lang->line('outgoing_credits_requests')?>"
				},
				{
					extend: 'csvHtml5',
					title: "<?=$this->lang->line('outgoing_credits_requests')?>"
				},
				{
					extend: 'pdfHtml5',
					orientation: 'landscape',
					pageSize: 'LEGAL',
					title: "<?=$this->lang->line('outgoing_credits_requests')?>"
				}
			]
		});

		$('#listeDemandesRapides').DataTable({
			searching: false, paging: false, info: false,
			"aaSorting": [[4, "desc"]],
			"columnDefs": [{
				targets: [4, 5],
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}]
		});

		$('#listeDemandesTardives').DataTable({
			searching: false, paging: false, info: false,
			"aaSorting": [[4, "desc"]],
			"columnDefs": [{
				targets: [4, 5],
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}]
		});

		$('#listeDemandesRapides_s').DataTable({
			searching: false, paging: false, info: false,
			"aaSorting": [[2, "desc"]],
			"columnDefs": [{
				targets: [2, 3],
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}]
		});

		$('#listeDemandesTardives_s').DataTable({
			searching: false, paging: false, info: false,
			"aaSorting": [[2, "desc"]],
			"columnDefs": [{
				targets: [2, 3],
				render: $.fn.dataTable.render.moment('YYYY-MM-DD HH:mm:ss', 'D MMMM YYYY HH:mm:ss', format)
			}]
		});

	});
</script>

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

	function GetURLParameter(sParam) {
		var sPageURL = window.location.search.substring(1);
		var sURLVariables = sPageURL.split('&');
		for (var i = 0; i < sURLVariables.length; i++) {
			var sParameterName = sURLVariables[i].split('=');
			if (sParameterName[0] == sParam) {
				return decodeURIComponent(sParameterName[1]);
			}
		}
	}


	$(function () {
		const category = $('#picker').data('category');
		const lang = $('#picker').data('lang');
		$('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');
			const user_geo = GetURLParameter("u");
			const type = GetURLParameter("type");
			if (category == 'super') {
				window.location.replace("<?= base_url('Superviseur_dash/getDemandes?type=')?>" + type + "&d=" + debut + "&f=" + fin);
			} else if (category == 'hyper') {
				window.location.replace("<?= base_url('Hyperviseur_dash/getDemandes')?>" + "?d=" + debut + "&f=" + fin);
			}
		});
		$('input[name="daterange"]').on('cancel.daterangepicker', function (ev, picker) {
			//do something, like clearing an input
			$('#daterange').val('');
		});
	});

</script>
