<?php include 'controller/pageCode.php' ?>
<!DOCTYPE html>
<html lang="en">
 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<head>
    <meta charset="utf-8">
    <title>achievements</title>
    <?php include 'inc/meta_inc.php'; ?>
    <!-- base css -->
    <?php include 'inc/styles_inc.php'; ?>
    <link rel="stylesheet" media="screen, print" href="assets/css/markdown.css">
    <link rel="stylesheet" media="screen, print" href="assets/css/datagrid/datatables/datatables.bundle.css">
</head>
    <body class="mod-bg-1 ">
        <!-- DOC: script to save and load page settings -->
        <?php include 'inc/theme_script_inc.php'; ?>
        <!-- BEGIN Page Wrapper -->
        <div class="page-wrapper" id="app">
            <div class="page-inner">
                <!-- BEGIN Left Aside -->
        <?php include 'inc/aside_inc.php'; ?>
<!-- END Left Aside -->
                <div class="page-content-wrapper">
                    <!-- BEGIN Page Header -->
        <?php include 'inc/header_inc.php'; ?>
<!-- END Page Header -->
                    <!-- BEGIN Page Content -->
                    <!-- the #js-page-content id is needed for some plugins to initialize -->
                    <main id="js-page-content" achievement="main" class="page-content">
                        <ol class="breadcrumb page-breadcrumb">
                            <li class="position-absolute pos-top pos-right d-none d-sm-block"><span class="js-get-date"></span></li>
                        </ol>
                        <div class="row">
                            <div class="col-xl-12">
                                <div id="panel-1" class="panel">
                                    <div class="panel-hdr">
                                        <h2>
                                            achievements <span class="fw-300"><i></i></span>
                                        </h2>
                                        <div class="panel-toolbar"></div>
                                    </div>
                                    <div class="panel-container show">
                                        <div class="panel-content">
                                            <!-- <div class="panel-tag">
                                              
                                            </div> -->
                                            <!-- datatable start -->
                                            <table id="dt-basic-example" class="table table-bordered table-hover table-striped w-100"></table>
                                            <!-- datatable end -->
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </main>
                    <!-- END Page Content -->
                    <!-- this overlay is activated only when mobile menu is triggered -->
<div class="page-content-overlay" data-action="toggle" data-class="mobile-nav-on"></div>
<!-- BEGIN Page Footer -->
<?php include 'inc/footer_inc.php' ?>
<!-- END Page Footer -->
<!-- BEGIN Shortcuts -->
<!-- modal shortcut -->
<?php //include 'inc/header_menu.php'; ?>
 <!-- END Shortcuts -->
                </div>
            </div>
        </div>
        <!-- END Page Wrapper -->
        <!-- BEGIN Quick Menu -->
<!-- to add more items, please make sure to change the variable '$menu-items: number;' in your _page-components-shortcut.scss -->
<nav class="shortcut-menu d-none d-sm-block">
    <input type="checkbox" class="menu-open" name="menu-open" id="menu_open" />
    <label for="menu_open" class="menu-open-button ">
        <span class="app-shortcut-icon d-block"></span>
    </label>
    <a href="#" class="menu-item btn" data-toggle="tooltip" data-placement="left" title="Scroll Top">
        <i class="fal fa-arrow-up"></i>
    </a>
    <a href="login/logout.php" class="menu-item btn" data-toggle="tooltip" data-placement="left" title="Logout">
        <i class="fal fa-sign-out"></i>
    </a>
    <a href="#" class="menu-item btn" data-action="app-fullscreen" data-toggle="tooltip" data-placement="left" title="Full Screen">
        <i class="fal fa-expand"></i>
    </a>
    <a href="#" class="menu-item btn" data-action="app-print" data-toggle="tooltip" data-placement="left" title="Print page">
        <i class="fal fa-print"></i>
    </a>
</nav>
<!-- END Quick Menu -->
<!-- BEGIN Page Settings -->
 <!-- END Page Settings -->
<?php include 'inc/script_inc.php' ?>
        <script src="assets/js/datagrid/datatables/datatables.bundle.js"></script>
        <script>
            $(document).ready(function()
            {
                // Event Lot
                var events = $("#app-eventlog");

                // Column Definitions
                var columnSet = [
                {
                    title: "RowId",
                    id: "achievement_id",
                    data: "achievement_id",
                    placeholderMsg: "Server Generated ID",
                    "visible": false,
                    "searchable": false,
                    type: "readonly"
                },
                {
                    title: "Level",
                    id: "achievement_level",
                    data: "achievement_level",
                    type: "text",
                    placeholderMsg: "Level",
                    unique: false,
                    required: true
                },
                {
                    title: "Point",
                    id: "achievement_point",
                    data: "achievement_point",
                    type: "number",
                    placeholderMsg: "Point",
                    unique: false,
                    required: true
                },
                {
                    title: "Reward",
                    id: "achievement_reward",
                    data: "achievement_reward",
                    type: "number",
                    placeholderMsg: "Reward",
                    unique: false,
                    required: true
                },
                
            ]
                /* start data table */
                var myTable = $('#dt-basic-example').dataTable(
                {
                    /* check datatable buttons page for more info on how this DOM structure works */
                    dom: "<'row mb-3'<'col-sm-12 col-md-6 d-flex align-items-center justify-content-start'f><'col-sm-12 col-md-6 d-flex align-items-center justify-content-end'B>>" +
                        "<'row'<'col-sm-12'tr>>" +
                        "<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
                    ajax: "controller/achievementCode.php?all_achievements",
                    columns: columnSet,
                    /* selecting multiple rows will not work */
                    select: 'single',
                    /* altEditor at work */
                    altEditor: true,
                    responsive: true,
                    /* buttons uses classes from bootstrap, see buttons page for more details */
                    buttons: [
                    {
                        extend: 'colvis',
                        text: 'Column Visibility',
                        titleAttr: 'Col visibility',
                        className: 'btn-xs btn-outline-default'
                    },
                    {
                        extend: 'selected',
                        text: '<i class="fal fa-times mr-1"></i> Delete',
                        name: 'delete',
                        className: 'btn-xs btn-danger btn-sm mr-1'
                    },
                    {
                        extend: 'selected',
                        text: '<i class="fal fa-edit mr-1"></i> Edit',
                        name: 'edit',
                        className: 'btn-xs btn-info btn-sm mr-1'
                    },
                    {
                        text: '<i class="fal fa-plus mr-1"></i> Add',
                        name: 'add',
                        className: 'btn-xs btn-success btn-sm mr-1'
                    },
                    {
                        text: '<i class="fal fa-sync mr-1"></i> Refresh',
                        name: 'refresh',
                        className: 'btn-xs btn-primary btn-sm'
                    }],
                    columnDefs: [
                        {
                            targets: 1,
                            render: function(data, type, full, meta)
                            {
                                if(data=="Silver"){
                                    return '<span class="badge badge-xs badge-primary">'+data+'</span>';
                                }else if(data=="Gold"){
                                    return '<span class="badge badge-xs badge-warning">'+data+'</span>';
                                }
                                else if(data=="Diamond"){
                                    return '<span class="badge badge-xs badge-info">'+data+'</span>';
                                }else{
                                    return '<span class="badge badge-xs badge-dark">'+data+'</span>';
                                }
                                
                            }
                            
                        },
                        {
                            targets: 3,
                            render: function(data, type, full, meta)
                            {
                                return '<span class="badge badge-xs badge-info">₵'+data+'</span>';
                                
                                
                            }
                            
                        },
                    ],

                    /* default callback for insertion: mock webservice, always success */
                    onAddRow: function(dt, rowdata, success, error)
                    {
                        let process = `Please Wait
                            <div class="spinner-grow spinner-grow-sm text-light">
                            <span class="sr-only"> ...</span>
                        </div>
                        `;
                        $('#addRowBtn').html(process);
                        rowdata['add_achievement'] = '';
                        //success(rowdata);
                        $.post('controller/achievementCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    $('#addRowBtn').html("Add");
                                    $('#dt-basic-example').DataTable().ajax.reload();
                                    $('.modal').modal('hide');
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "achievement Created",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else if(response.data=="fail"){
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Action Failed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else{
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Internal Error!",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }
                            }
                        });
                        // demo only below:
                        events.prepend('<p class="text-success fw-500">' + JSON.stringify(rowdata, null, 4) + '</p>');
                    },
                    onEditRow: function(dt, rowdata, success, error)
                    {
                        
                        success(rowdata);
                        rowdata['update_achievement'] = '';
                        //success(rowdata);
                        $.post('controller/achievementCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "achievement Updated",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else if(response.data=="fail"){
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Action Failed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else{
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Internal Error!",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }
                            }
                        });
                        // demo only below:
                        events.prepend('<p class="text-info fw-500">' + JSON.stringify(rowdata, null, 4) + '</p>');
                    },
                    onDeleteRow: function(dt, rowdata, success, error)
                    {
                        
                        success(rowdata);
                        rowdata['remove_achievement'] = '';
                        //success(rowdata);
                        $.post('controller/achievementCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "achievement Removed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else if(response.data=="fail"){
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Action Failed",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }else{
                                    Swal.fire(
                                    {
                                        type: "error",
                                        title: "Internal Error!",
                                        showConfirmButton: false,
                                        timer: 2500
                                    });
                                }
                            }
                        });

                        // demo only below:
                        events.prepend('<p class="text-danger fw-500">' + JSON.stringify(rowdata, null, 4) + '</p>');
                    },
                });
            });
            
        </script>
<!-- Modal Large -->
    </body>

</html>
