<?php
session_start();
require_once '../../config.php';
load('middleware', 'user');
if ($_SESSION['user']['level'] !== "Admin") {
    header('location:' . base_url('id'));
}
if (isset($_POST['edit'])) {
    $ppid =  $db->real_escape_string(e(@$_POST['id']));
    $nickname =  $db->real_escape_string(e(@$_POST['nick']));
    $userid =  $db->real_escape_string(e(@$_POST['userid']));
    $status = $db->real_escape_string(e(@$_POST['status']));
    $name = $_POST['name_skin'];
	    if(isset($ppid)){
	        
	        $insert = $db->query("UPDATE gift_skin SET name_skin='$name', userid= '$userid', nickname='$nickname',status='$status' WHERE id = '$ppid'");
	        if($insert == TRUE){
	            $_SESSION['alert'] = ['success', 'Success!', 'Jadwal Gift Skin Berhasil Di Edit'];
	            exit(header("Location: ".base_url('admin/setting/gift-skin')));
	        }else{
	            $_SESSION['alert'] = ['danger', 'Failed!', 'Jadwal Gift Skin Gagal Di Edit'];
		        exit(header("Location: ".base_url('admin/setting/gift-skin')));
	        }

		}else{
		    $_SESSION['alert'] = ['danger', 'Failed!', 'System is busy, please try again later.'];
		    exit(header("Location: ".base_url('admin/setting/gift-skin')));
	        
		}
	} 
	
if (isset($_POST['add'])) {
	$date_create =  $db->real_escape_string(e(@$_POST['date_create'])).' '.date("H:i:s");
	$date_gift =  $db->real_escape_string(e(@$_POST['date_gift'])).' '.date("H:i:s");
	$userid =  $db->real_escape_string(e(@$_POST['userid']));
	$status =  $db->real_escape_string(e(@$_POST['status']));
	$nick =  $db->real_escape_string(e(@$_POST['nick']));
	$name = $_POST['name_skin'];
	
	if (!$date_create || !$date_gift || !$userid || !$status || !$nick) {
	    $_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada form kosong.'];
	    exit(header("Location: ".base_url('admin/setting/gift-skin')));
	} else {
	       $insert_srvc = $db->query("INSERT INTO gift_skin VALUES('','$userid', '$nick','$status','$date_create', '$date_gift','$name')");
			if ($insert_srvc === true) {
				$_SESSION['alert'] = ['success', 'Success!', 'Jadwal Gift Skin Berhasil Di Tambah'];
				exit(header("Location: ".base_url('admin/setting/gift-skin')));
		
			} else {
				$_SESSION['alert'] = ['danger', 'Failed!', 'System is busy, please try again later.'];
				exit(header("Location: ".base_url('admin/setting/gift-skin')));
		
			}
		}
	}

if (isset($_POST['delete'])) {
	$pid =  $db->real_escape_string(e(@$_POST['id']));
        $delete_srvc = $db->query("DELETE FROM gift_skin WHERE id = '$pid'");
                if ($delete_srvc === true) {
                    $_SESSION['alert'] = ['success', 'Success!', 'Jadwal Gift Ski Berhasil Di Hapus'];
                    exit(header("Location: ".base_url('admin/setting/gift-skin')));
                } else {
                    $_SESSION['alert'] = ['danger', 'Failed!', 'System is busy, please try again later.'];
                    exit(header("Location: ".base_url('admin/setting/gift-skin')));
                }
           }
require '../../layouts/header-admin.php';
?>
<div class="row justify-content-center">
    <div class="col-md-9 col-12">
        <div class="card bg-dark shadow">
            <div class="card-header">
                <h4 class="card-title float-left">Jadwal Gift Skin</h4>
                <button class="btn btn-success float-right" onclick="modal('Tambah Jadwal','<?= base_url(); ?>admin/setting/service/add')">Tambah</button>
            </div>
            <div class="card-body card-dashboard">
                <div class="table-responsive">
                    <table class="table" style="font-size:14px;" id="tbb">
                        <thead>
                            <tr>
                                <th>Date Create</th>
                                <th>User ID</th>
                                <th>Nickname</th>
                                <th>Name Skin</th>
                                <th>Gift Date</th>
                                <th>Status</th>
                                <th>Act.</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $checkk = $db->query("SELECT * FROM gift_skin");
                            while($check = $checkk->fetch_assoc()):?>
                            <tr>
                            <td><?= $check['date_create'];?></td>
                            <td><?= $check['userid'];?></td>
                            <td><?= $check['nickname'];?></td>
                            <td><?= $check['name_skin'];?></td>
                            <td><?= $check['date_gift'];?></td>
                            <?php if($check['status'] == "Success"){
                                $bg = "success";
                                $stts = "SUCCESS";
                            }else if($check['status'] == "Waiting"){
                                $bg = "secondary";
                                $stts = "WAITING";
                            }else if($check['status'] == "Pending"){
                                $bg = "warning";
                                $stts = "PENDING";
                            }else if($check['status'] == "Processing"){
                                $bg = "info";
                                $stts = "PROCESSING";
                            }else if($check['status'] == "Canceled"){
                                $bg = "danger";
                                $stts = "CANCELED";
                            }?>
                            <td><span class="badge p-2 bg-<?=$bg;?>"><?= $stts;?></span></td>
                            <td style="white-space: nowrap;"><span class="btn btn-warning btn-sm" onclick="modal('Edit Jadwal Gift Skin','<?= base_url('admin/setting/service/edit?id='.$check['id']);?>')">Edit</span> <span class="btn btn-danger btn-sm" onclick="modal('Delete Jadwal Gift Skin','<?= base_url('admin/setting/service/delete?id='.$check['id']);?>')">Delete</span></td>
                            </tr>
                            <?php endwhile;?>
                        </tbody>		
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php include '../../layouts/footer-admin.php';?>
<script>
    $(document).ready(function(){
       $("#tbb").dataTable(); 
    });
</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>