<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) -->
	<section class="content-header">
		<h1>
			<?= $this->lang->line('manage_drugs_and_devices')?>
		</h1>
	</section>

	<section class="content">
		<div class="row">
			<div class="col-xs-8">
				<div class="box">
					<div class="box-header">
						<h3 class="box-title"><?= $this->lang->line( 'drugs_and_devices') ?></h3>
					</div>
					<div class="box-body" style="overflow-x:auto;">
						<table id="institutions" class="table table-bordered table-striped">
							<thead>
							<tr>
								<th align='center'>#</th>
								<th>Code</th>
								<th><?= $this->lang->line('Nom'); ?></th>
								<th>Type</th>
								<th><?= $this->lang->line('on_prescription'); ?></th>
								<th>Action</th>
							</tr>
							</thead>
						</table>
					</div>
				</div>
			</div>
				<div class="col-xs-4">
					<div class="box">
						<div class="box-header">
							<h3 class="box-title"><?= $this->lang->line('add_drug_and_device'); ?></h3>
						</div>
						<div class="box-body">
							<form id="create-form" class="bottom-75 center-block">
								<div class="form-group">
									<label>Code</label>
									<input type="text" class="form-control input-lg" name="code" >
								</div>
								<div class="form-group">
									<label for="firstname"><?= $this->lang->line('Nom'); ?></label>
									<input type="text" class="form-control input-lg" name="name">
								</div>
								<div class="form-group">
									<label  class="col-form-label">Type</label>
									<select class="form-control input-lg" name="provider_billing_period" required>
										<?php $array = array_values($types);
										foreach ($array as $value) { ?>
											<option value="<?=$value?>"> <?=$this->lang->line($value)?> </option>
										<?php } ?>
									</select>
								</div>
								<div class="form-group">
									<input type="checkbox" name="on_prescription">
									<label ><?= $this->lang->line('on_prescription'); ?></label>
								</div>
								<div class="form-group">
									<input type="submit"
										   value="<?= $this->lang->line('save'); ?>"
										   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>Code</label>
								<input type="text" class="form-control input-lg" name="code" >
							</div>
							<div class="form-group">
								<label><?= $this->lang->line('Nom'); ?></label>
								<input type="text" class="form-control input-lg" name="name" >
							</div>
							<div class="form-group">
								<label  class="col-form-label">Type</label>
								<select class="form-control input-lg" name="provider_billing_period" required>
									<?php $array = array_values($types);
									foreach ($array as $value) { ?>
										<option value="<?=$value?>"> <?=$this->lang->line($value)?> </option>
									<?php } ?>
								</select>
							</div>
							<div class="form-group">
								<input type="checkbox" name="on_prescription">
								<label ><?= $this->lang->line('on_prescription'); ?></label>
							</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',['title' => $this->lang->line('delete_drug_and_device')]) ?>
		<?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 () {
		$('#institutions').DataTable({
			// Processing indicator
			"processing": true,
			"language": {
				"processing": "<?= $this->lang->line('loading') ?>",
				"emptyTable" : "<?= $this->lang->line('no_drug_and_device') ?>"
			},
			// DataTables server-side processing mode
			"serverSide": true,
			// Initial no order.
			"order": [],
			// Load data from an Ajax source
			"ajax": {
				"url": "<?= base_url('pagination/DrugAndDevices/getLists'); ?>",
				"data":{
					"id_network" : "<?= $network_id ?>",
				},
				"type": "POST"
			},
			"aaSorting": [],
			dom: 'Bfrtip',
		});
	})

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

	$(document).on("click", ".edit", function () {
		selectedId = $(this).data('id');
		$("#update-form input[name='code']").val($(this).data('code'));
		$("#update-form input[name='name']").val($(this).data('name'));
		$("#update-form input[name='type']").val($(this).data('type'));
		$("#update-form input[name='on_prescription']").prop('checked', $(this).data('on_prescription'));
		$('#editModal').modal('show');

	});

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

	$("#update-form").submit(function () {
		$.ajax({
			url: '<?= base_url('NanoHealthController/storeDrugAndDevice')?>',
			type: 'post',
			data: {
				id: selectedId,
				network_id : networkId,
				code:  $("input[name=code]",this).val(),
				name:  $("input[name=name]",this).val(),
				type:  $("input[name=type]",this).val(),
				on_prescription : $("input[name=on_prescription]",this).is(':checked') ? 1 : 0
			},
			dataType: 'json',
			success: function (data) {
				if(data.code === 200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('drug_and_device_updated')?>",
						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();
	});

	$("#create-form").submit(function (event) {

		$.ajax({
			url: '<?= base_url('NanoHealthController/storeDrugAndDevice')?>',
			type: 'post',
			data: {
				network_id : networkId,
				code:  $("input[name=code]",this).val(),
				name:  $("input[name=name]",this).val(),
				type:  $("input[name=type]",this).val(),
				on_prescription : $("input[name=on_prescription]",this).is(':checked') ? 1 : 0
			},
			dataType: 'json',
			success: function (data) {
				if(data.code === 200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('drug_and_device_created')?>",
						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();
	});


	$("#delete-btn").click(function () {
		$.ajax({
			url: '<?= base_url('NanoHealthController/deleteDrugAndDevice')?>',
			type: 'post',
			data: {id: selectedId},
			dataType: 'json',
			success: function (data) {
				if(data.code === 200){
					Swal.fire({
						icon: 'success',
						title: "<?= $this->lang->line('drug_and_device_deleted')?>",
						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>
