<?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><?php echo $_SESSION['employee_name'] ?>'s Approvals</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" role="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>
                                            My Approved Requests <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">
                                                <tfoot>
                                                    <tr>
                                                        <th>RowId</th>
                                                        <th>Company</th>
                                                        <th>Received By</th>
                                                        <th>Approved By</th>
                                                        <th>Quantity</th>
                                                        <th>Amount to Pay </th>
                                                        <th>Amount Paid</th>
                                                        <th>Commission</th>
                                                        <th>Commission Value</th>
                                                        <th>Discount</th>
                                                        <th>Tax</th>
                                                        <th>Completed</th>
                                                        <th>Requested On</th>
                                                        <th>Return Qty</th>
                                                        <th>Return Worth</th>
                                                        <th>Action</th>
                                                    </tr>
                                                </tfoot>
                                            </table>
                                            <!-- datatable end -->
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xl-6">
                                <div id="panel-1" class="panel">
                                    <div class="panel-hdr">
                                        <h2>
                                            My Approved Request Statistics <span class="fw-300"><i></i></span>
                                        </h2>
                                        <div class="panel-toolbar"></div>
                                    </div>
                                    <div class="panel-container show">
                                        <div class="panel-content">
                                            <div id="chartdiv" style="width:100%;height:350px;"></div>
                                        </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 src="assets/js/datagrid/datatables/datatables.export.js"></script>
        <script src="plugin//amcharts4/core.js"></script>
        <script src="plugin//amcharts4/charts.js"></script>
        <script src="plugin//amcharts4/themes/kelly.js"></script>
        <script src="plugin//amcharts4/themes/animated.js"></script>
        <script>
            $(document).ready(function()
            {
                // Event Lot
                var events = $("#app-eventlog");

                // Column Definitions
                var columnSet = [
                {
                    title: "RowId",
                    id: "item_request_id",
                    data: "item_request_id",
                    placeholderMsg: "Server Generated ID",
                    "visible": false,
                    "searchable": false,
                    type: "readonly"
                },
                {
                    title: "Company",
                    id: "customer_company",
                    data: "customer_company",
                    type: "text",
                    placeholderMsg: "Company",
                    unique: false,
                    required: true
                },
                {
                    title: "Received By",
                    id: "customer_name",
                    data: "customer_name",
                    type: "text",
                    placeholderMsg: "Received By",
                    unique: false,
                    required: true
                },
                {
                    title: "Approved By",
                    id: "employee_name",
                    data: "employee_name",
                    type: "text",
                    placeholderMsg: "Approved By",
                    unique: false,
                    required: true
                },
                {
                    title: "Quantity",
                    id: "item_request_quantity",
                    data: "item_request_quantity",
                    type: "text",
                    placeholderMsg: "Quantity",
                    unique: false,
                    required: true
                },
                {
                    title: "Amount to Pay ₵",
                    id: "item_request_amount",
                    data: "item_request_amount",
                    type: "text",
                    placeholderMsg: "Apount to Pay",
                    unique: false,
                    required: true
                },
                {
                    title: "Amount Paid ₵",
                    id: "amount_payed",
                    data: "amount_payed",
                    type: "text",
                    placeholderMsg: "Apount to Pay",
                    unique: false,
                    required: true
                },
                {
                    title: "Commission%",
                    id: "commission",
                    data: "commission",
                    type: "readonly",
                    placeholderMsg: "Commission",
                    unique: false,
                    required: true
                },
                {
                    title: "Commission₵",
                    id: "commission_value",
                    data: "commission_value",
                    type: "readonly",
                    placeholderMsg: "Commission Value",
                    unique: false,
                    required: true
                },
                {
                    title: "Discount%",
                    id: "discount",
                    data: "discount",
                    type: "readonly",
                    placeholderMsg: "Discount",
                    unique: false,
                    required: true
                },
                {
                    title: "Tax%",
                    id: "tax",
                    data: "tax",
                    type: "readonly",
                    placeholderMsg: "Tax%",
                    unique: false,
                    required: true
                },
                {
                    title: "Completed",
                    id: "completed",
                    data: "completed",
                    type: "readonly",
                },
                {
                    title: "Requested On",
                    id: "item_request_datetime",
                    data: "item_request_datetime",
                    type: "text",
                    placeholderMsg: "Requested On",
                    unique: false,
                    required: true
                },
                {
                    title: "Return Qty ",
                    id: "items_returned",
                    data: "items_returned",
                    type: "readonly"
                },
                {
                    title: "Return Worth ₵",
                    id: "refund",
                    data: "refund",
                    type: "readonly"
                },
                {
                    title: "Action",
                    id: "item_request_id",
                    data: "item_request_id",
                    type: "text",
                    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/requestCode.php?my_approved_requests",
                    columns: columnSet,
                    /* selecting multiple rows will not work */
                    select: 'single',
                    /* altEditor at work */
                    altEditor: true,
                    responsive: true,
                    pageLength: 15,
                    order: [
                        [8, 'desc']
                    ],
                    /* 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: 'pdfHtml5',
                        text: 'PDF',
                        titleAttr: 'Generate PDF',
                        className: 'btn-danger btn-xs mr-1'
                    },
                    
                    {
                        extend: 'csvHtml5',
                        text: 'CSV',
                        titleAttr: 'Generate CSV',
                        className: 'btn-success btn-xs mr-1'
                    },
                    {
                        extend: 'copyHtml5',
                        text: 'Copy',
                        titleAttr: 'Copy to clipboard',
                        className: 'btn-dark btn-xs mr-1'
                    },
                    {
                        extend: 'print',
                        text: 'Print',
                        titleAttr: 'Print Table',
                        className: 'btn-outline-primary btn-xs'
                    },
                    {
                        text: '<i class="fal fa-sync mr-1"></i> Refresh',
                        name: 'refresh',
                        className: 'btn-xs btn-primary btn-sm'
                    }],
                    columnDefs: [
                        {
                            targets: 5,
                            render: function(data, type, full, meta)
                            {
                                
                                return '<span class="badge badge-xs badge-primary">'+data+'</span>';
                            }
                            
                        },
                        {
                            targets: 6,
                            render: function(data, type, full, meta)
                            {
                                
                                return '<span class="badge badge-xs badge-dark">'+data+'</span>';
                            }
                            
                        },
                        {
                            targets: 11,
                            render: function(data, type, full, meta)
                            {
                                if(data=="Yes"){
                                    return '<span class="badge badge-xs badge-success">'+data+'</span>';
                                }else if(data=="No"){
                                    return '<span class="badge badge-xs badge-warning">'+data+'</span>';
                                }
                                
                                
                            }
                            
                        },
                        {
                            targets: 15,
                            render: function(data, type, full, meta)
                            {
                                
                                return '<a href="invoice.php?view_invoice=&&id='+data+'" class="btn btn-xs btn-secondary">Invoice</a>';
                            }
                            
                        }
                    ],
                    footerCallback:function(tfoot,data,start,end,display){
                        var api = this.api();
                        pageTotal = api
                        .column( 5, { page: 'current'} )
                        .data()
                        .reduce( function (a, b) {
                            var result = parseFloat(a)+parseFloat(b);
                            return result.toFixed(2);
                        }, 0 );
                        // Update footer
                        
                        $( api.column( 5 ).footer() ).html(
                            `<span style="font-size:18px" class="badge badge-info">₵${pageTotal}</span>`
                        );
                        var api = this.api();
                        pageTotal = api
                        .column( 6, { page: 'current'} )
                        .data()
                        .reduce( function (a, b) {
                            var result = parseFloat(a)+parseFloat(b);
                            return result.toFixed(2);
                        }, 0 );
                        // Update footer
                        
                        $( api.column( 6 ).footer() ).html(
                            `<span style="font-size:18px" class="badge badge-warning">₵${pageTotal}</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_role'] = '';
                        //success(rowdata);
                        $.post('controller/roleCode.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: "Role 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_role'] = '';
                        //success(rowdata);
                        $.post('controller/roleCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "Role 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_role'] = '';
                        //success(rowdata);
                        $.post('controller/roleCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "Role 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>');
                    },
                });
            });

am4core.ready(function() {

    // Themes begin
    am4core.useTheme(am4themes_kelly);
    am4core.useTheme(am4themes_animated);
    // Themes end

    // Create chart instance
    var chart = am4core.create("chartdiv", am4charts.PieChart);
    var completed = '<?php echo $no_of_completed_request; ?>';
    var uncompleted = '<?php echo $no_of_uncompleted_request; ?>';
    // Add data
    chart.data = [{
                "request": "Completed",
                "number": completed
            }, {
                "request": "Uncompleted",
                "number": uncompleted
            }]

    // Add and configure Series
    var pieSeries = chart.series.push(new am4charts.PieSeries());
    pieSeries.dataFields.value = "number";
    pieSeries.dataFields.category = "request";
    pieSeries.slices.template.stroke = am4core.color("#fff");
    pieSeries.slices.template.strokeWidth = 2;
    pieSeries.slices.template.strokeOpacity = 1;

    // This creates initial animation
    pieSeries.hiddenState.properties.opacity = 1;
    pieSeries.hiddenState.properties.endAngle = -90;
    pieSeries.hiddenState.properties.startAngle = -90;

}); // end am4core.ready()
            
        </script>
<!-- Modal Large -->
    </body>

</html>
