<?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>Payment</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>
                                            Pay For Items <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="row">
                                                <div class="col-md-4"></div>
                                                <div class="col-md-4">
                                                    <div class="p-5">
                                                        <label for="invoice_no" style="font-weight:bold">Invoice No.#</label>
                                                        <input type="number" id="invoice_no" placeholder="Enter invoice No.#" class="form-control">
                                                        <div class="summary d-none">
                                                            
                                                        </div>
                                                        <div class="pay_section d-none">
                                                            <hr>
                                                            <label for="amount" style="font-weight:bold">Amount</label>
                                                            <input type="number" id="amount" placeholder="Enter Amount" class="form-control">
                                                            <hr>
                                                            <center>
                                                                <button class="btn btn-xs btn-info" id="add_payment">Add Payment</button>
                                                                <a href="" class="btn btn-xs btn-secondary invoice d-none" id="add_payment">View Invoice</a>
                                                            </center>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="col-md-4"></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-xl-12">
                                <div id="panel-1" class="panel">
                                    <div class="panel-hdr">
                                        <h2>
                                            All Approvals <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()
            {
                toastr.options = {
                    "closeButton": true,
                    "debug": false,
                    "newestOnTop": true,
                    "progressBar": true,
                    "positionClass": "toast-top-right",
                    "preventDuplicates": true,
                    "onclick": null,
                    "showDuration": 300,
                    "hideDuration": 5000,
                    "timeOut": 5000,
                    "extendedTimeOut": 1000,
                    "showEasing": "swing",
                    "hideEasing": "linear",
                    "showMethod": "fadeIn",
                    "hideMethod": "fadeOut"
                }
                let btn_template = `
                                    <div class="process">
                                        <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
                                        <span class="sr-only"> ...</span>
                                    </div> `;
                $('#invoice_no').on('change',function(){
                    let = amount_payed = 0;
                    var summary_template = '';
                    var invoice_no = $('#invoice_no').val();
                    var serverData = {
                        invoice_no:invoice_no,
                        find_invoice:''
                    }
                    $.post('controller/requestCode.php',serverData,function(serverResponse){
                        if(serverResponse){
                            var response = JSON.parse(serverResponse);
                            if(response.data!="null"){
                                $('.summary').removeClass('d-none');
                                $('.pay_section').removeClass('d-none');
                                amount_payed = response.data[0].amount_payed;
                                var amount_left = parseFloat(response.data[0].item_request_amount)-parseFloat(amount_payed);
                                $('#amount').attr('maxValue',amount_left)
                                $('#amount').val(amount_left)
                                if(amount_left<=0){
                                    $('#add_payment').addClass("d-none");
                                    $('#amount').addClass("d-none");
                                }else{
                                    $('#add_payment').removeClass("d-none");
                                    $('#amount').removeClass("d-none");
                                }
                                console.log(amount_left)
                                var summary_template = `
                                <span style="font-weight:bold">Company</span>:<span class="text-muted">${response.data[0].customer_company}</span><br>
                                <span style="font-weight:bold">Attn</span>:<span class="text-muted">${response.data[0].customer_name}</span><br>
                                <span style="font-weight:bold">Amount Due:</span><span class="text-muted">₵${response.data[0].item_request_amount}</span><br>
                                <span style="font-weight:bold">Amount Payed</span>:₵<span class="text-muted amount_payed">${response.data[0].amount_payed}</span><br>
                                <span style="font-weight:bold">Amount Left</span>:₵<span style="color:red;font-weight:bolder">${amount_left}</span><br>
                                <span style="font-weight:bold">Date Issued</span>:<span class="text-muted">${response.data[0].item_approval_datetime}</span>
                                `;
                                $('.summary').html(summary_template);
                            }else{
                                $('.pay_section').addClass('d-none');
                                $('.summary').html('');
                            }
                        }
                    });
                });

                $(document).on('click','#add_payment',function(){
                    $('#add_payment').html(btn_template);
                    var invoice_no = $('#invoice_no').val();
                    if(!invoice_no){
                        $('#add_payment').html('Add Payment');
                        toastr.warning('Kindly provide an invoice No.#!', 'Admin');
                        return false;
                    }
                    var amount = $('#amount').val();
                    var serverData = {
                        invoice_no:invoice_no,
                        amount:amount,
                        amount_payed:amount_payed,
                        add_payment:''
                    }
                    $.post('controller/requestCode.php',serverData,function(serverResponse){
                        if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    $('#add_payment').html('Add Payment');
                                    $('.invoice').removeClass("d-none");
                                    $('.invoice').attr("href","invoice.php?view_invoice=&&id="+invoice_no)
                                    $('.amount_payed').text(response.amount_payed)
                                    toastr.success('Payment Made Successfully.', 'Admin');
                                    toastr.clear()
                                }else if(response.data=="fail"){
                                    $('#add_payment').html('Add Payment');
                                    toastr.warning('Action Failed!', 'Admin');
                                    toastr.clear()
                                }else{
                                    $('#add_payment').html('Add Payment');
                                    toastr.error('Internal Error!', 'Admin');
                                    toastr.clear()
                                }
                            }
                    });
                });
                // 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_paid",
                    data: "amount_payed",
                    type: "text",
                    placeholderMsg: "Apount to Pay",
                    unique: false,
                    required: true
                },
                {
                    title: "Commission%",
                    id: "commission",
                    data: "commission",
                    type: "text",
                    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: "text",
                    placeholderMsg: "Discount",
                    unique: false,
                    required: true
                },
                {
                    title: "Requested On",
                    id: "item_request_datetime",
                    data: "item_request_datetime",
                    type: "text",
                    placeholderMsg: "Requested On",
                    unique: false,
                    required: true
                },
                {
                    title: "Completed",
                    id: "completed",
                    data: "completed",
                    type: "readonly"
                },
                {
                    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?all_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'
                    },
                    {
                        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-warning">'+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: 14,
                            render: function(data, type, full, meta)
                            {
                                
                                return '<a href="invoice.php?view_invoice=&&id='+data+'" class="btn btn-xs btn-secondary">Invoice</a>';
                            }
                            
                        }
                    ],

                    /* 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>');
                    },
                });
            });
            
        </script>
<!-- Modal Large -->
    </body>

</html>
