<div class="container-fluid" id="container-wrapper">
	<div class="d-sm-flex align-items-center justify-content-between mb-4">
		<h1 class="h3 mb-0 text-gray-800">Backup Database</h1>
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a href="./">Home</a></li>
			<li class="breadcrumb-item active" aria-current="page">Backup</li>
		</ol>
	</div>
	<style>
	.autobackup-card{border:1px solid #dbeafe;border-radius:10px;background:#f8fbff;box-shadow:0 8px 22px rgba(15,23,42,.05);margin-bottom:1rem;padding:1rem 1.15rem}.autobackup-grid{display:grid;grid-template-columns:minmax(250px,1.45fr) minmax(170px,.65fr) minmax(135px,.45fr) minmax(220px,1fr) auto;gap:1rem;align-items:center}.autobackup-title{font-weight:700;color:#172033}.autobackup-note,.autobackup-meta{font-size:.78rem;color:#64748b;margin-top:.25rem}.autobackup-label{display:block;font-size:.75rem;font-weight:700;color:#475569;margin-bottom:.35rem}.autobackup-select,.autobackup-time{height:38px;border:1px solid #d1d9e6;border-radius:5px;background:#fff}.autobackup-time{padding:.375rem .75rem;width:100%;color:#495057}.autobackup-switch{position:relative;width:48px;height:26px;margin:0}.autobackup-switch input{opacity:0;width:0;height:0}.autobackup-slider{position:absolute;inset:0;border-radius:99px;background:#cbd5e1;cursor:pointer;transition:.2s}.autobackup-slider:before{content:"";position:absolute;width:20px;height:20px;left:3px;top:3px;border-radius:50%;background:#fff;box-shadow:0 1px 4px rgba(15,23,42,.25);transition:.2s}.autobackup-switch input:checked+.autobackup-slider{background:var(--app-theme-color,#2563eb)}.autobackup-switch input:checked+.autobackup-slider:before{transform:translateX(22px)}@media(max-width:1100px){.autobackup-grid{grid-template-columns:1fr 1fr}.autobackup-switch{margin-top:.25rem}}@media(max-width:650px){.autobackup-grid{grid-template-columns:1fr}}
	</style>
	<?php
	$auto = isset($autobackup) && is_array($autobackup) ? $autobackup : array();
	$autoAktif = !empty($auto['autobackup_aktif']);
	$autoFrekuensi = !empty($auto['autobackup_frekuensi']) ? $auto['autobackup_frekuensi'] : 'harian';
	$autoJam = !empty($auto['autobackup_jam']) ? substr($auto['autobackup_jam'], 0, 5) : '02:00';
	$autoTerakhir = !empty($auto['autobackup_terakhir']) ? dtimes($auto['autobackup_terakhir'],true,false) : 'Belum pernah';
	$autoBerikutnya = !empty($auto['autobackup_berikutnya']) ? dtimes($auto['autobackup_berikutnya'],true,false) : '-';
	?>
	<div class="autobackup-card">
		<div class="autobackup-grid">
			<div><div class="autobackup-title"><i class="fa fa-clock-o mr-1"></i> Auto Backup Database</div><div class="autobackup-note">Backup tervalidasi dibuat otomatis pada aktivitas aplikasi pertama setelah jadwal jatuh tempo.</div></div>
			<div><label class="autobackup-label" for="autobackup_frekuensi">FREKUENSI</label><select id="autobackup_frekuensi" class="form-control autobackup-select" <?= empty($boleh_atur_autobackup)?'disabled':''; ?>><option value="harian" <?=$autoFrekuensi==='harian'?'selected':'';?>>Setiap Hari</option><option value="mingguan" <?=$autoFrekuensi==='mingguan'?'selected':'';?>>Setiap Minggu</option><option value="bulanan" <?=$autoFrekuensi==='bulanan'?'selected':'';?>>Setiap Bulan</option></select></div>
			<div><label class="autobackup-label" for="autobackup_jam">JAM BACKUP</label><input type="time" id="autobackup_jam" class="autobackup-time" value="<?=html_escape($autoJam);?>" step="60" <?=empty($boleh_atur_autobackup)?'disabled':'';?>></div>
			<div><div class="autobackup-meta">Backup terakhir: <strong id="auto_terakhir"><?=$autoTerakhir;?></strong></div><div class="autobackup-meta">Jadwal berikutnya: <strong id="auto_berikutnya"><?=$autoBerikutnya;?></strong></div><div class="autobackup-meta">Status: <strong id="auto_status"><?=html_escape(isset($auto['autobackup_status'])?$auto['autobackup_status']:'nonaktif');?></strong></div></div>
			<label class="autobackup-switch" title="Aktifkan Auto Backup"><input type="checkbox" id="autobackup_aktif" <?=$autoAktif?'checked':'';?> <?=empty($boleh_atur_autobackup)?'disabled':'';?>><span class="autobackup-slider"></span></label>
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			
			<div class="card">
				<div class="card-header  d-flex flex-row align-items-center justify-content-between">
					<div class="input-group input-group-sm">
						<div class="input-group-prepend">
							<span class="input-group-text" for"limits">LIMIT</span>
						</div>
						<select id="limits" name="limits" class="form-control custom-select w-1" onchange="FilterBackup()">
							<option value="10">10</option>
							<option value="20">20</option>
							<option value="50">50</option>
							<option value="100">100</option>
						</select>
						<div class="input-group-prepend">
							<span class="input-group-text" for="sortBy">SORT</span>
						</div>
						<select id="sortBy" class="form-control custom-select w-1" onchange="FilterBackup()">
							<option value="ASC">ASC</option>
							<option value="DESC" selected>DESC</option>
						</select>
						<input type="text" id="keywords" class="form-control w-40" placeholder="Cari data" onkeyup="FilterBackup();"/>
						<div class="input-group-append">
							<button class="btn btn-danger clear_backup" type="button"><i class="fa fa-times"></i></button>
							<button type="button" class="btn btn-info backup"><i class="fa fa-plus"></i> Backup DB</button>
						</div>
					</div>
				</div>
				
				<div class="card-body">
					<div id="filter_backup">
						
					</div><!-- /.card -->
				</div><!-- /.card -->
			</div><!-- /.card -->
			
		</div>
	</div>
</div>
<div aria-hidden="true" aria-labelledby="myModalLabel" class="modal fade" id="confirm-delete" role="dialog" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h4 class="modal-title" id="myModalLabel">Konfirmasi Hapus</h4>
				<button aria-hidden="true" class="close" data-dismiss="modal" type="button">&times;</button>
			</div>
			<div class="modal-body">
				<p>Anda akan menghapus file database backup, prosedur ini tidak dapat diubah.</p>
				<p>Apakah Anda ingin melanjutkan?</p>
				<p class="debug-url"></p>
				<input type="hidden" id="data-hapus">
			</div>
			<div class="modal-footer">
				<button class="btn btn-secondary" data-dismiss="modal" type="button">Batal</button> 
				<button class="btn btn-danger hapus" type="button">YA</button> 
			</div>
		</div>
	</div>
</div>
<script>
	function simpanAutoBackup(){
		var aktif=$('#autobackup_aktif').is(':checked')?1:0, frekuensi=$('#autobackup_frekuensi').val(), jam=$('#autobackup_jam').val();
		$('#autobackup_aktif,#autobackup_frekuensi,#autobackup_jam').prop('disabled',true);
		$.ajax({url:base_url+'Backupdata/simpan_autobackup',method:'POST',dataType:'json',data:{aktif:aktif,frekuensi:frekuensi,jam:jam},success:function(res){
			$('#autobackup_aktif,#autobackup_frekuensi,#autobackup_jam').prop('disabled',false);
			if(res.status){$('#auto_status').text(res.data.autobackup_status);$('#auto_berikutnya').text(res.data.autobackup_berikutnya||'-');sweet('Auto Backup',res.msg,'success','success');}else sweet('Peringatan!!!',res.msg,'warning','warning');
		},error:function(xhr){$('#autobackup_aktif,#autobackup_frekuensi,#autobackup_jam').prop('disabled',false);sweet('Peringatan!!!',(xhr.responseJSON&&xhr.responseJSON.msg)||'Pengaturan auto backup gagal disimpan','warning','warning');}});
	}
	$(document).on('change','#autobackup_aktif,#autobackup_frekuensi,#autobackup_jam',simpanAutoBackup);
	$('#confirm-delete').on('show.bs.modal', function(e) {
		$('#data-hapus').val($(e.relatedTarget).data('id'));
	});
	FilterBackup(); 
	function FilterBackup(page_num){
		page_num = page_num?page_num:0;
		var keywords = $('#keywords').val();
		var sortBy = $('#sortBy').val();
		$.ajax({
			type: 'POST',
			url: base_url+'backupdata/ajaxDatabase/'+page_num,
			data:'page='+page_num+'&keywords='+keywords+'&sortBy='+sortBy,
			beforeSend: function(){
				$('body').loading();
			},
			success: function(html){
				$('#filter_backup').html(html);
				$('body').loading('stop');
			},
			error : function(res, status, httpMessage) {
				$('body').loading('stop');
				console.log(res.status)
				if(res.status==401){
					sweet_login(httpMessage,'warning',base_url);
					}else{
					sweet("Peringatan!!!", httpMessage, "warning", "warning");
				}			
			}
		});
	}
	
	$(document).ready(function() {
		$('body').tooltip({selector: '[data-toggle="tooltip"]'});
		var dataTable1 = $('#dataTableHover').DataTable({   
			"ajax":{  
				url:base_url + 'Backupdata/list_data',
				type:"POST"             
			},
			"order": [[ 0, 'desc' ]],
			"columnDefs": [
			{ "targets": [2,3], "orderable": false },
			{ "className": "text-right", "targets": [ 3 ] }
			]
			// dom: 'Bfrtip',
			// buttons: [
			// 'copy', 'csv', 'excel', 'pdf', 'print', 'colvis'
			// ]
		});
		
		$(document).on('click', '.backup', function() {
			$.ajax({
				'url': base_url + 'Backupdata/backupdb',
				'method': 'POST',
				'dataType':'json',
				beforeSend: function(){	 
					$('body').loading();
				},
				success: function(data) {
					$('body').loading('stop');
					if(data.status==200){
						sweet('Backup File!!!',data.msg,'success','success');
						}else{
						sweet('Peringatan!!!',data.msg,'warning','warning');
					}
					FilterBackup();  
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}
			})
		});
		$(document).on('click', '.unzipdb', function() {
			var file = $(this).attr('data-id');
			// alert(file);
			$.ajax({
				'url': base_url + 'Backupdata/unzipdb',
				'method': 'POST',
				'data': {file:file},
				'dataType':'json',
				beforeSend: function(){	 
					$('body').loading();
				},
				success: function(data) {
					$('body').loading('stop');
					if(data.status==200){
						sweet('Extract DB!!!',data.msg,'success','success');
						}else{
						sweet('Peringatan!!!',data.msg,'warning','warning');
					}
					FilterBackup();  
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}
			})
		});
		
		$(document).on('click', '.downloaddb', function() {
			var file = $(this).attr('data-file');
			// alert(file);
			$.ajax({
				'url': base_url + 'Backupdata/download_db',
				'method': 'POST',
				'data': {file:file},
				'dataType': 'binary',
				'xhrFields': {
					'responseType': 'blob'
				},
				beforeSend: function(){	 
					$('body').loading();
				},
				success: function(data) {
					var link = document.createElement('a'),
					filename = file;
					link.href = URL.createObjectURL(data);
					link.download = filename;
					link.click();
					$('body').loading('stop');
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}
			})
		});
		
		$(document).on('click', '.restoredb', function() {
			var file = $(this).attr('data-id');
			 
			$.ajax({
				'url': base_url + 'Backupdata/restoredb',
				'method': 'POST',
				'data': {file:file},
				'dataType':'json',
				beforeSend: function () {
					$('body').loading();
				},
				success: function(data) {
					$(".loadings").hide();
					if(data.status==200){
						sweet('Restore DB!!!',data.msg,'success','success');
						}else{
						sweet('Restore DB!!!',data.msg,'warning','warning');
					}
					// dataTable1.ajax.reload();  
					$('body').loading('stop');
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}
			});
		});
		$(document).on('click', '.hapus', function(e) {
			e.preventDefault();
			var file = $("#data-hapus").val();
			$.ajax({
				'url': base_url + 'Backupdata/hapusdb',
				'method': 'POST',
				'data':{file:file},
				'dataType':'json',
				beforeSend: function(){	 
					$('body').loading();
				},
				success: function(data) {
					$('body').loading('stop');
					if(data.status==200){
						sweet('Hapus File!!!',data.msg,'success','success');
						FilterBackup();  
						}else{
						sweet('Hapus File!!!',data.msg,'warning','warning');
						FilterBackup();  
					}
					$('#confirm-delete').modal('hide');
				},
				error : function(res, status, httpMessage) {
					$('body').loading('stop');
					console.log(res.status)
					if(res.status==401){
						sweet_login(httpMessage,'warning',base_url);
						}else{
						sweet("Peringatan!!!", httpMessage, "warning", "warning");
					}			
				}
			})
		});
	});
	$(document).on('click', '.clear_backup', function(e) {
		$('#keywords').val('');
		FilterBackup();  
	});
	
	
</script>	
