<?php
session_start();
require_once '../../config.php';
load('middleware', 'user');

if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}

if (isset($_POST['edit_layanan'])) {
    $post_pid = $db->real_escape_string(e(@$_POST['pid']));
	$post_layanan = $db->real_escape_string(e(@$_POST['layanan']));
	$post_note = $db->real_escape_string(e(@$_POST['note']));
	$post_harga = $db->real_escape_string(e(@$_POST['harga']));
	$post_harga_ress = $db->real_escape_string(e(@$_POST['harga_ress']));
	$post_harga_prem = $db->real_escape_string(e(@$_POST['harga_prem']));
	$post_gambarr = $db->real_escape_string(e(@$_POST['gambar']));
    $post_tipe = $db->real_escape_string(e(@$_POST['tipe']));
    $post_status = $db->real_escape_string(e(@$_POST['status']));
    $post_provider = $db->real_escape_string(e(@$_POST['provider']));
        
	if (!$post_pid) {
		$_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada form kosong.'];
		exit(header('location: /admin/guest/layanan'));
	} else {
	        $edit_layanan = $db->query("UPDATE layanan_pulsa SET img='{$post_gambarr}', layanan = '{$post_layanan}', note = '{$post_note}', harga = '{$post_harga}', harga_reseller='$post_harga_ress}', harga_premium='{$post_harga_prem}', tipe = '{$post_tipe}', status = '{$post_status}', provider = '{$post_provider}' WHERE id = '{$post_pid}'");
	        if ($edit_layanan === true) {
				$_SESSION['alert'] = ['success', 'Success!', 'Layanan successfully change.'];
				exit(header('location: /admin/guest/layanan'));
			        } 
			}
	} 
	
if (isset($_POST['addlayanan'])) {
	$post_provider = $db->real_escape_string(e(@$_POST['provider']));
	$post_kategori = $db->real_escape_string(e(@$_POST['kategori']));
	$post_code = $db->real_escape_string(e(@$_POST['code']));
	$post_gambar = $db->real_escape_string(e(@$_POST['gambar']));
	$post_layanan = $db->real_escape_string(e(@$_POST['layanan']));
	$post_note = $db->real_escape_string(e(@$_POST['note']));
	$post_harga_web = $db->real_escape_string(e(@$_POST['harga_web']));
	$post_harga_web_ress = $db->real_escape_string(e(@$_POST['harga_web_ress']));
	$post_harga_web_prem = $db->real_escape_string(e(@$_POST['harga_web_prem']));
    $post_tipe = $db->real_escape_string(e(@$_POST['tipe']));

	if (!$post_provider || !$post_kategori || !$post_code || !$post_layanan || !$post_note || !$post_harga_web || !$post_harga_web_ress || !$post_harga_web_prem || !$post_tipe) {
	$_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada form kosong.'];
	exit(header('location: /admin/guest/layanan'));
	} else {
			$insert_srvc = $db->query("INSERT INTO `layanan_pulsa` (`id`, `service_id`, `provider_id`, `operator`, `layanan`, `img`,`harga`, `harga_reseller`, `harga_premium`, `harga_h2h`, `profit`, `status`, `provider`, `tipe`, `note`) VALUES ('', '{$post_code}', '{$post_code}', '{$post_kategori}', '{$post_layanan}', '{$post_gambar}','{$post_harga_web}', '{$post_harga_web_ress}','{$post_harga_web_prem}','0','0', 'Normal', '{$post_provider}', '{$post_tipe}', '{$post_note}')");
			if ($insert_srvc === true) {
				$_SESSION['alert'] = ['success', 'Success!', 'layanan successfully add.'];
				exit(header('location: /admin/guest/layanan'));
			} else {
				$_SESSION['alert'] = ['danger', 'Failed!', 'System is busy, please try again later.'];
				exit(header('location: /admin/guest/layanan'));
			}
		}
	}

if (isset($_POST['delete'])) {
	$post_id = $db->real_escape_string(e(@$_POST['id']));
	if (!$post_id) {
		$_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada form kosong.'];
		exit(header('location: /admin/guest/layanan'));
	} else { 
                $delete_srvc = $db->query("DELETE FROM layanan_pulsa WHERE id = '{$post_id}'");
                if ($delete_srvc === true) {
                    $_SESSION['alert'] = ['success', 'Success!', 'Layanan successfully delete.'];
                    exit(header('location: /admin/guest/layanan'));
                } else {
                    $_SESSION['alert'] = ['danger', 'Failed!', 'System is busy, please try again later.'];
                    exit(header('location: /admin/guest/layanan'));
                }
           }
      }
include_once '../../layouts/header-admin.php'; ?>
<div class="row">
        <div class="col-12">
            <div class="card bg-dark shadow">
                <div class="card-header">
                    <h4 class="card-title float-left">Kelola Layanan</h4>
                    <button class="btn btn-success float-right" onclick="modal('Tambah Layanan','<?= base_url(); ?>/admin/guest/service_data/add-layanan')">Add</button>
                </div>
                <div class="card-body card-dashboard">
                    <div class="table-responsive">
                        	<table class="table table-striped table-dark table-bordered table-hover" id="datatabl3s">
                            <thead>
                                <tr>
                                    <th>Code</th>
                                    <th>Layanan</th>
                                    <th>Provider</th>
                                    <th>Act.</th>
                                </tr>
                            </thead>
                            <tbody>
                            <td colspan="4" class="text-center">Loading data from server...</td>
                            </tbody>		
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php include_once '../../layouts/footer-admin.php'; ?>
<script type="text/javascript">
$(document).ready(function() {
    $('#datatabl3s').DataTable({
        "order": [[0, 'desc']],
        "processing": false,
        "serverSide": true,
        "paging": true,
        "pagingType": "simple_numbers",
        "ajax": "<?= base_url(); ?>/admin/class/layanan_pulsa.php",
        columnDefs : [
        {
        "searhable" : false,
        "orderable" : false,
        "targets" : 3,
        "render" : function(data, type, row) {
            var btn = "<button class=\"btn btn-info ms-1\" onclick=\"modal('Detail Layanan','<?= base_url(); ?>/admin/guest/service_data/detail_layanan?id="+data+"')\">Detail</button><button class=\"btn btn-warning ms-1\" onclick=\"modal('Edit Layanan','<?= base_url(); ?>/admin/guest/service_data/edit_layanan?id="+data+"')\">Edit</button><button class=\"btn btn-danger ms-1\" onclick=\"modal('Delete Layanan','<?= base_url(); ?>/admin/guest/service_data/delete_layanan?id="+data+"')\">Delete</button>"
            return btn;
        }
        } 
                     ]
        
    });
});
</script>
<script type="text/javascript">
    function modal(name, link, size) {
        var sizes = '';
        if (size == 'smaller' || size == 'xs') sizes = 'modal-xs';
        if (size == 'small' || size == 'sm') sizes = 'modal-sm';
        if (size == 'large' || size == 'lg') sizes = 'modal-lg';
        if (size == 'larger' || size == 'xl') sizes = 'modal-xl';

        $.ajax({
            type: "GET",
            url: link,
            beforeSend: function() {
                $('#SModal-body').html('Loading...');
            },
            success: function(result) {
                $('#SModal-body').html(result);
            },
            error: function() {
                $('#SModal-body').html('Failed to get contents...');
            }
        });

        $('#SModal-title').html(name);
        $('#SModal-size').removeClass('modal-xs');
        $('#SModal-size').removeClass('modal-sm');
        $('#SModal-size').removeClass('modal-lg');
        $('#SModal-size').removeClass('modal-xl');
        $('#SModal-size').addClass(sizes);
        $('#SModal').modal('show');
    }
</script>
<div class="modal fade text-left" id="SModal" tabindex="-1" role="dialog" aria-labelledby="SModal" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog" id="SModal-size" role="document">
        <div class="modal-content bg-dark">
            <div class="modal-header">
                <h4 class="modal-title" id="SModal-title"></h4>
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"><span aria-hidden="true" style="text-shadow: none; color:white;">&times;</span></button>
            </div>
            <div class="modal-body" id="SModal-body"></div>
        </div>
    </div>
</div>