<?= $this->extend('admin/layout/main.php') ?>

<?= $this->section('content') ?>
<div class="card border-0 m-1">
	<div class="card-body">
		<div class="card-header bg-success text-white">
			<div class='card-title m-0'><b>Dictionary Tubes</b></div>
		</div>
		<button class='btn btn-sm btn-success m-3' onclick="create()"><i class="bi bi-plus-circle"></i> Create</button>
		<div class="table-responsive">
			<table id="myTable" class="table">
				<thead>
					<tr>
						<th>Tube code</th>
						<th>Tube name</th>
						<th>Action</th>
					</tr>
				</thead>
				<tbody id='table-body'>
				</tbody>
			</table>
		</div>
	</div>
</div>

<div class="modal fade" id="modal_crud" aria-hidden="true" tabindex="-1">
	<div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
		<div class="modal-content">
			<div class="modal-header">
				<h1 class="modal-title fs-5" id="exampleModalToggleLabel">Edit Tube</h1>
				<button type="button" class="btn-close" data-bs-dismiss="modal" ></button>
			</div>
			<div class="modal-body" style='background-color:#F4F6FF'>
				<div class="row">
					<div class="col-12">
						<table class="table table-sm table-borderless">
							<input type='hidden' id='tubeid' value='0' />
							<tr class="align-middle"> <th>Tubecode</th> <th>:</th> <td><input class='form-control' type='text' id='tubecode' oninput='this.value = this.value.toUpperCase();' /></td> </tr>
							<tr class="align-middle"> <th>Tubename</th> <th>:</th> <td><input class='form-control' type='text' id='tubename' /></td> </tr>
						</table>
						<button class='btn btn-sm btn-primary' onclick='save()'>Save</button>
						<button class='btn btn-sm btn-secondary' data-bs-dismiss="modal">Cancel</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<?= $this->endSection() ?>

<?= $this->section('script') ?>
<script>
index();
function index() {
	let url = '<?=base_url('');?>api/dictTubes/index';
	$.ajax({
		url: url,
		method: 'GET',
		success: function(response) {
			$("#table-body").html("");
			var data = response['dictTubes'];
			for (var i = 0; i < data.length; i++) {
				tubeid = data[i].TUBEID;
				tubecode = data[i].TUBECODE;
				tubename = data[i].TUBENAME;
				let editBtn =  '<button class="btn btn-sm btn-success" ' + ' onclick="edit(\'' + tubeid + '\')">Edit' + '</button> ';
				let datarow = '<tr class="align-middle">' + 
					'<td>' + tubecode + '</td>' + '<td>' + tubename+ '</td> ' + '<td>' + editBtn + '</td>' +
				'</tr>';
				$("#table-body").append(datarow);
			}			
		},
		error: function(response) { console.log(response.responseJSON); }
	});
}

function create() {
	$("#alertSingle").html();
	$("#tubeid").val("0");
	$("#tubecode").val("");
	$("#tubename").val("");
	$("#modal_crud").modal('show'); 
}

function edit(tubeid) {
	let url = '<?=base_url('');?>api/dictTubes/detail/'+tubeid;
	$.ajax({
		url: url,
		method: "GET",
		success: function(response) {
			let data = response;
			$("#alert-div").html("");
			$("#error-div").html("");   
			$("#tubeid").val(tubeid);
			$("#tubecode").val(data.TUBECODE);
			$("#tubename").val(data.TUBENAME);
			$("#modal_crud").modal('show'); 
		},
		error: function(response) {
			console.log(response.responseJSON)
		}
	});
}


function save() {
	let url = '<?=base_url('');?>api/dictTubes/save';
	var tubeid = $("#tubeid").val();
	var tubecode = $("#tubecode").val();
	var tubename = $("#tubename").val();
	let data = { tubeid: tubeid, tubecode:tubecode, tubename:tubename };
	$.ajax({
		url: url,
		method: "POST",
		data: data,
		success: function(response) {
			$("#alert-div").html("");
			$("#error-div").html("");   
			$("#tubeid").val('');
			$("#tubecode").val('');
			$("#tubename").val('');
			$("#modal_crud").modal('hide');
			index();
		},
		error: function(response) {
			console.log(response.responseJSON)
		}
	});
}
</script>
<?= $this->endSection() ?>