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

<?= $this->section('content') ?>
<div class="card border-0 m-1">
	<div class="card-header bg-success text-white">
		<div class='card-title m-0'><b>Dictionary Chapter</b></div>
	</div>
	<div class="card-body">
		<div class="table-responsive">
			<table id="myTable" class="table">
				<thead>
					<tr>
						<th>Chapter code</th>
						<th>Shorttext</th>
						<th>Text</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 Chapter</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">
							<tr class="align-middle"> <th>Chaptercode</th> <th>:</th> <td id='chapcode'></td> </tr>
							<tr class="align-middle"> <th>Shorttext</th> <th>:</th> <td id='chaptext'></td> </tr>
							<tr class="align-middle"> <th>Text 1</th> <th>:</th> <td><textarea class='form-control' id='text1'/></textarea></td> </tr>
							<tr class="align-middle"> <th>Text 2</th> <th>:</th> <td><textarea class='form-control' id='text2'/></textarea></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/dictChapters/index';
	$.ajax({
		url: url,
		method: 'GET',
		success: function(response) {
			$("#myTable").DataTable().destroy();
			$("#table-body").html("");
			var data = response['dictChapters'];
			for (var i = 0; i < data.length; i++) {
				chapcode = data[i].CHAPCODE;
				shorttext = data[i].SHORTTEXT;
				text1 = '';
				text2 = '';
				if(data[i].TEXT1 != null) { text1 = data[i].TEXT1; }
				if(data[i].TEXT2 != null) { text2 = data[i].TEXT2; }
				let editBtn =  '<button class="btn btn-sm btn-success" ' + ' onclick="edit(\'' + chapcode + '\')">Edit' + '</button> ';
				let datarow = '<tr class="align-middle">' + 
					'<td>' + chapcode + '</td>' + '<td>' + shorttext + '</td> <td> <pre class="m-0">' + text1 + '<hr/>' + text2 + '</pre> </td>' + 
					'<td>' + editBtn + '</td>' +
				'</tr>';
				$("#table-body").append(datarow);
			}			
			$("#myTable").DataTable({
				"pageLength" : 25,
			});
		},
		error: function(response) { console.log(response.responseJSON); }
	});
}

function edit(chapcode) {
	let url = '<?=base_url('');?>api/dictChapters/detail/'+chapcode;
	$("#chapcode").html('');
	$("#chaptext").html('');		
	$.ajax({
		url: url,
		method: "GET",
		success: function(response) {
			let data = response;
			$("#chapcode").html(data.CHAPCODE);
			$("#chaptext").html(data.SHORTTEXT);
			$("#text1").val(data.TEXT1);
			$("#text2").val(data.TEXT2);
			$("#modal_crud").modal('show'); 
		},
		error: function(response) {
			console.log(response.responseJSON)
		}
	});
}


function save() {
	let url = '<?=base_url('');?>api/dictChapters/save';
	var chapcode = $("#chapcode").html();
	var text1 = $("#text1").val();
	var text2 = $("#text2").val();
	let data = { chapcode: chapcode, text1:text1, text2:text2 };
	$.ajax({
		url: url,
		method: "POST",
		data: data,
		success: function(response) {
			$("#chapcode").val('');			
			$("#text1").val('');
			$("#text2").val('');
			$("#modal_crud").modal('hide');
			index();
		},
		error: function(response) {
			console.log(response.responseJSON)
		}
	});
}


</script>
<?= $this->endSection() ?>