<?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">
    
    <link rel="stylesheet" href="assets/css/select2.bundle.css">
    <style>
        .select2-selection__rendered {
            line-height: 32px !important;
        }
        .select2-container .select2-selection--single {
            height: 39px !important;
        }
        .select2-selection__arrow {
            height: 35px !important;
        }
    </style>
</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-4">
                                <div id="panel-1" class="panel">
                                    <div class="panel-hdr">
                                        <h2>
                                           Add Payment <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="form-row form-group">
                                                <div class="col-md-12 mb-3">
                                                    <label class="form-label" for="payer">Who is Paying?<span class="text-danger">*</span> </label><br>  
                                                    <select class="custom-select select2"  id="payer" style="width:300px" name="payer" required>
                                                        <option value="">Select Payer</option>
                                                        <?php foreach($students as $key): ?>
                                                        <option value="<?php echo $key['student_id']; ?>"><?php echo $key['student_name'].' | '.$key['student_email'].' | '.$key['student_phone']; ?></option>
                                                        <?php endforeach ?>
                                                    </select>
                                                    <div class="invalid-feedback">
                                                        Please provide a valid value.
                                                    </div>
                                                </div>
                                                <!--  -->
                                                <div class="col-md-12 mb-3">
                                                    <label class="form-label" for="payment_type">Paying For<span class="text-danger">*</span> </label><br>  
                                                    <select class="custom-select select2"  id="payment_type" style="width:300px" name="payment_type" required>
                                                        <option value="">Select Fee Type</option>
                                                        <?php foreach($payment_types as $key): ?>
                                                        <option value="<?php echo $key['payment_type_id']; ?>"><?php echo $key['payment_type_name']; ?></option>
                                                        <?php endforeach ?>
                                                    </select>
                                                    <div class="invalid-feedback">
                                                        Please provide a valid value.
                                                    </div>
                                                </div>
                                                <!--  -->
                                                <div class="col-md-12 mb-3">
                                                    <label class="form-label" for="choice">Amount<span class="text-danger">*</span> </label><br>  
                                                    <input type="number" id="amount" class="form-control">
                                                    <div class="invalid-feedback">
                                                        Please provide a valid value.
                                                    </div>
                                                </div>
                                                <!--  -->
                                                <div class="col-md-12 mb-3">
                                                    <label class="form-label" for="payment_date">Payment Date <span class="text-danger">*</span> </label><br>  
                                                    <input type="date" id="payment_date" class="form-control" value="<?php echo date("Y-m-d") ?>">
                                                    <div class="invalid-feedback">
                                                        Please provide a valid value.
                                                    </div>
                                                </div>
                                                <div class="col-md-12 mb-3 save_btn">
                                                <button class="btn btn-primary ml-auto" id="save_btn" type="submit" >Save Payment</button>
                                            </div>
                                                <!--  -->
                                            </div>
                                            
                                                
                                        </div>
                                    </div>
                                </div>  
                            </div>
                            <div class="col-xl-8">
                                <div id="panel-1" class="panel">
                                    <div class="panel-hdr">
                                        <h2>
                                        Payments <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>

                        <div class="row">
                            <div class="col-xl-6"></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/select2.bundle.js"></script>
        <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()
            {
                $('#select2').select2();
                $('.select2').select2();

                $('#save_btn').click(function(){
                    var payer = $('#payer').val();
                    var payment_type = $('#payment_type').val();
                    var amount = $('#amount').val();
                    var payment_date = $('#payment_date').val();   
                    var serverData = {
                        payer:payer,
                        payment_type:payment_type,
                        amount:amount,
                        payment_date:payment_date,
                        add_payment:''
                    }
                    $.post('controller/paymentCode.php',serverData,function(serverResponse){
                        var response = JSON.parse(serverResponse)
                        console.log(response)
                        if(response.data=="success"){
                            Swal.fire(
                            {
                                type: "success",
                                title: "Payment Saved",
                                showConfirmButton: false,
                                timer: 2500
                            });
                            $('#dt-basic-example').DataTable().ajax.reload();
                        }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
                            });
                        }
                    });
                });
                // $(".select2-placeholder-multiple").select2(
                // {
                //     placeholder: "Select State"
                // });
                // $(".js-hide-search").select2(
                // {
                //     minimumResultsForSearch: 1 / 0
                // });
                // $(".js-max-length").select2(
                // {
                //     maximumSelectionLength: 2,
                //     placeholder: "Select maximum 2 items"
                // });
                // $(".select2-placeholder").select2(
                // {
                //     placeholder: "Select a state",
                //     allowClear: true
                // });

                // $(".js-select2-icons").select2(
                // {
                //     minimumResultsForSearch: 1 / 0,
                //     templateResult: icon,
                //     templateSelection: icon,
                //     escapeMarkup: function(elm)
                //     {
                //         return elm
                //     }
                // });

                // function icon(elm)
                // {
                //     elm.element;
                //     return elm.id ? "<i class='" + $(elm.element).data("icon") + " mr-2'></i>" + elm.text : elm.text
                // }
                // Event Lot
                var events = $("#app-eventlog");

                // Column Definitions
                var columnSet = [
                {
                    title: "RowId",
                    id: "payment_id",
                    data: "payment_id",
                    placeholderMsg: "Server Generated ID",
                    "visible": false,
                    "searchable": false,
                    type: "hidden"
                },
                {
                    title: "Payment Type",
                    id: "payment_type_name",
                    data: "payment_type_name",
                    type: "select",
                    "options": [<?php echo $all_payment_types ?>]
                },
                {
                    title: "Entry By",
                    id: "entry_by",
                    data: "entry_by",
                    type: "hidden",
                    placeholderMsg: "Entry By",
                    unique: false,
                    required: true
                },
                {
                    title: "Payer",
                    id: "student_name",
                    data: "student_name",
                    options:[<?php echo $all_students ?>],
                    type: "hidden"
                },
                {
                    title: "Amount ₵",
                    id: "amount",
                    data: "amount",
                    type: "number",
                    placeholderMsg: "Amount",
                    unique: false,
                    required: true
                },
                {
                    title: "Payment Date",
                    id: "payment_date",
                    data: "payment_date",
                    type: "date",
                    placeholderMsg: "Payment Date",
                    unique: false,
                    required: true
                },
                {
                    title: "Invoice",
                    id: "payment_id",
                    data: "payment_id",
                    type: "hidden",
                    unique: 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/paymentCode.php?all_payments",
                    columns: columnSet,
                    /* selecting multiple rows will not work */
                    select: 'single',
                    /* altEditor at work */
                    altEditor: true,
                    responsive: true,
                    pageLength: 10,
                    order: [
                        [0, 'desc']
                    ],
                    /* buttons uses classes from bootstrap, see buttons page for more details */
                    buttons: [
                    {
                        extend: 'colvis',
                        text: 'Column',
                        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: '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-sync mr-1"></i> Refresh',
                        name: 'refresh',
                        className: 'btn-xs btn-primary btn-sm'
                    }],
                    columnDefs: [
                        {
                            targets: 6,
                            render: function(data, type, full, meta)
                            {
                                
                                return '<a href="invoice.php?view_invoice&&id='+data+'" class="btn badge badge-xs badge-primary">View</a>';
                            }
                        }
                        // },
                        // {
                        //     targets: 6,
                        //     render: function(data, type, full, meta)
                        //     {
                                
                        //         return '<span class="badge badge-xs badge-dark">'+data+'</span>';
                        //     }
                            
                        // },
                        // {
                        //     targets: 7,
                        //     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: 8,
                        //     render: function(data, type, full, meta)
                        //     {
                                
                        //         return '<a href="installment_invoice.php?view_installment_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_payment'] = '';
                        //success(rowdata);
                        $.post('controller/paymentCode.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_payment'] = '';
                        //success(rowdata);
                        $.post('controller/paymentCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "Payment 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_payment'] = '';
                        //success(rowdata);
                        $.post('controller/paymentCode.php',rowdata,function(serverResponse){
                            console.log(serverResponse)
                            if(serverResponse){
                                let response = JSON.parse(serverResponse);
                                if(response.data=="success"){
                                    Swal.fire(
                                    {
                                        type: "success",
                                        title: "Payment 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>
