<?php $is_embedded = !empty($embedded); ?>
<div class="<?=$is_embedded ? 'promo-report-embedded' : 'container-fluid';?>"<?=$is_embedded ? '' : ' id="container-wrapper"';?>>
	<?php if ($is_embedded) { ?>
	<div class="promo-history-card">
		<div class="promo-history-head">
			<div><h2><i class="fa fa-clock-o"></i> RIWAYAT SESI PENGIRIMAN PROMOSI <small class="promo-live-badge"><i class="fa fa-circle"></i> LIVE</small></h2><p>Status diperbarui otomatis setiap 3 detik dari callback Fonnte.</p></div>
			<div class="promo-history-stats"><span>Total: <b id="report-stat-total">0</b></span><span class="success"><i class="fa fa-check-circle-o"></i> Berhasil: <b id="report-stat-success">0</b></span><span class="delivered"><i class="fa fa-check"></i> Disampaikan: <b id="report-stat-delivered">0</b></span><span class="read"><i class="fa fa-eye"></i> Dibaca: <b id="report-stat-read">0</b></span><span class="failed"><i class="fa fa-times-circle-o"></i> Gagal: <b id="report-stat-failed">0</b></span></div>
		</div>
		<div class="promo-history-filters">
			<label class="promo-history-search"><i class="fa fa-search"></i><input type="text" id="keywords" placeholder="Cari penerima, no WA, atau pesan..." onkeyup="search_LaporanGrup();"></label>
			<select id="reportStatusFilter" onchange="search_LaporanGrup()"><option value="">Semua Status</option><option value="success">Berhasil</option><option value="delivered">Disampaikan</option><option value="read">Dibaca</option><option value="failed">Gagal</option></select>
			<label class="promo-history-limit">Tampilkan:<select id="limits" name="limits" onchange="search_LaporanGrup()"><option value="10">10 / Hal</option><option value="20">20 / Hal</option><option value="50">50 / Hal</option><option value="100">100 / Hal</option></select></label>
			<input type="hidden" id="sortBy" value="DESC">
		</div>
		<div class="promo-history-table" id="data_report"><div class="promo-history-loading">Memuat riwayat pengiriman...</div></div>
	</div>
	<?php } else { ?>
	<div class="d-sm-flex align-items-center justify-content-between mb-4">
		<h1 class="h3 mb-0 text-gray-800">Report promo</h1>
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a href="./">Home</a></li>
			<li class="breadcrumb-item active" aria-current="page">Report promo</li>
		</ol>
	</div>
	<div class="row">
		<div class="col-md-12">
			
			<div class="card">
				<div class="card-header pb-0">
					<div class="input-group input-group-sm">
						<div class="input-group-prepend">
							<span class="input-group-text">SORT</span>
						</div>
						<select id="sortBy" class="form-control custom-select w-5" onchange="search_LaporanGrup()">
							<option value="ASC">ASC</option>
							<option value="DESC" selected>DESC</option>
						</select>
						<div class="input-group-prepend">
							<span class="input-group-text">LIMIT</span>
						</div>
						<select id="limits" name="limits" class="form-control custom-select" onchange="search_LaporanGrup()">
							<option value="10">10</option>
							<option value="20">20</option>
							<option value="50">50</option>
							<option value="100">100</option>
							<option value="500">500</option>
							<option value="1000">1000</option>
						</select>
						<input type="text" id="keywords" class="form-control" placeholder="Cari data" onkeyup="search_LaporanGrup();"/>
					</div>
				</div>
				
				<div class="card-body table-responsive data-report">
					<div class="card-block" id="data_report">
						
					</div><!-- /.card-body -->
				</div><!-- /.card-body -->
			</div><!-- /.card -->
			
		</div>
	</div>
	<?php } ?>
</div>

<div aria-hidden="true" aria-labelledby="promoReportDeleteLabel" class="modal fade" id="promo-report-confirm-delete" role="dialog" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h4 class="modal-title" id="promoReportDeleteLabel">Confirm Delete</h4>
				<button aria-hidden="true" class="close" data-dismiss="modal" type="button">&times;</button>
			</div>
			<div class="modal-body">
				<p>Anda akan menghapus satu url, prosedur ini tidak dapat diubah.</p>
				<p>Apakah Anda ingin melanjutkan?</p>
				<p class="debug-url"></p>
				<input type="hidden" id="report-data-hapusid">
				<input type="hidden" id="report-data-konsumen">
			</div>
			<div class="modal-footer">
				<button class="btn btn-secondary" data-dismiss="modal" type="button">Batal</button> 
				<button class="btn btn-danger hapus-report-promo" type="button">YA</button> 
			</div>
		</div>
	</div>
</div>
<script>
	
	
	
	$(document).ready(function() {
		search_LaporanGrup();
		if (window.promoReportRefresh) clearInterval(window.promoReportRefresh);
		window.promoReportRefresh = setInterval(function(){
			if (!document.hidden && $('#data_report').length) search_LaporanGrup(undefined, true);
		}, 3000);
	});
	
	function search_LaporanGrup(page_num, silent){
		page_num = page_num?page_num:0;
        var keywords = $('#keywords').val();
        var sortBy = $('#sortBy').val();
        var limits = $('#limits').val();
		var statusFilter = $('#reportStatusFilter').val() || '';
		
		$.ajax({
			type: 'POST',
			url: base_url + "promo/ajaxReport/"+page_num,
			data:{page:page_num,keywords:keywords,sortBy:sortBy,limits:limits,statusFilter:statusFilter},
			dataType: 'html',
			beforeSend: function(){
				if(!silent) $("body").loading();
			},
			success: function(data) {
				$("#data_report").html(data);
				var $stats=$("#data_report .promo-report-stats-meta");
				if($stats.length){var total=$stats.data('total'),success=$stats.data('success'),delivered=$stats.data('delivered'),read=$stats.data('read'),failed=$stats.data('failed');$('#report-stat-total').text(total);$('#report-stat-success').text(success);$('#report-stat-delivered').text(delivered);$('#report-stat-read').text(read);$('#report-stat-failed').text(failed);$('#reportStatusFilter option[value=""]').text('Semua Status ('+total+')');$('#reportStatusFilter option[value="success"]').text('Berhasil ('+success+')');$('#reportStatusFilter option[value="delivered"]').text('Disampaikan ('+delivered+')');$('#reportStatusFilter option[value="read"]').text('Dibaca ('+read+')');$('#reportStatusFilter option[value="failed"]').text('Gagal ('+failed+')');}
				if(!silent) $('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");
				}			
			}
		});
	}
	
	
	$('#promo-report-confirm-delete').on('show.bs.modal', function(e) {
		$('#report-data-hapusid').val($(e.relatedTarget).data('id'));
		$('#report-data-konsumen').val($(e.relatedTarget).data('konsumen'));
	});
	
	$(document).on('click','.hapus-report-promo',function(e){
		e.preventDefault();
		var id = $("#report-data-hapusid").val();
		var konsumen = $("#report-data-konsumen").val();
		$.ajax({
			url: base_url + 'promo/hapus_report',
			method: 'POST',
			dataType:'json',
			data:{id:id,konsumen:konsumen},
			beforeSend: function(){
				$('body').loading();
			},
			success: function(data) {
				if(data.status==200){
					$('#promo-report-confirm-delete').modal('hide');
					sweet_time(500,'Status!!!',data.msg);
					}else{
					sweet('Peringatan!!!',data.msg,'warning','warning');
				}
				search_LaporanGrup();
				$('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");
				}			
			}
		});
	});
	
</script>
