<div class="container-fluid" style="padding-bottom: 2rem;">
    <div class="card bg-light-transparent">
        <div class="card-header bg-white">
            <div class="float-left">
                <h4>Report Summary</h4>
            </div>
            <div class="float-right">
                <button type="button" class="btn btn-outline-dark btn-sm"  data-trigger="hover" data-tooltip="tooltip" title="Update Form Value" onclick="open_modal()"><i class="fas fa-edit"></i> Update Booking Form Value</button>

                <a href="<?php echo base_url('hr/Serviceform_report/summary_print/fee')?>" target="_blank" class="btn btn-outline-dark btn-sm" title="Print"><i class="fa fa-print"></i>Print</a>

                <a id="btnShowFilters" type="button" class="btn btn-outline-dark btn-sm collapsed" data-trigger="hover" title="Show Filters Box" data-toggle="collapse" data-target="#collapseFilter" aria-expanded="false"><i class="fas fa-filter"></i>Show Filters</a>
            </div>
        </div>

        <div class="collapse show active" id="collapseFilter">
            <div class="card-header">
                <div class="row">
                    <div class="col">
                        <div class="card no-shadow">
                            <form class="form" action="<?php echo base_url('/hr/serviceform_report/sv_summary_fee_report'); ?>" method="POST">
                                <div class="card-body">
                                    <div class="form-group row">
                                        <label for="sv_summary_filter_form" class="col-lg-2">Booking Form</label>                           
                                        <div class="col-lg-5" >
                                            <select class="form-control select" name="sv_summary_filter_form" id="sv_summary_filter_form" required onchange="change_department_input()">
                                                <option value="all" selected disabled>-- Please Select --</option>
                                                <?php if($getBookingName) foreach($getBookingName as $key => $value){ ?>
                                                    <option value="<?php echo $value['id']; ?>" <?php echo (isset($_SESSION['sv_summary_fee_filter_title']) && $_SESSION['sv_summary_fee_filter_title'] != 'all' && $_SESSION['sv_summary_fee_filter_title'] == $value['id'])?'selected':''; ?>><?php echo $value['title'].' - ['.$value['category'].']'; ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="sv_summary_filter_year" class="col-lg-2">Year</label>                           
                                        <div class="col-lg-5" >
                                            <input type="text" name="sv_summary_filter_year" id="sv_summary_filter_year" class="form-control datepicker-yyyy" value="<?php echo (isset($_SESSION['sv_summary_fee_filter_year']))?$_SESSION['sv_summary_fee_filter_year']:date('Y'); ?>">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="sv_summary_filter_month" class="col-lg-2">Month</label>                           
                                        <div class="col-lg-5" >
                                            <input type="text" name="sv_summary_filter_month" id="sv_summary_filter_month" class="form-control datepicker-mm" value="<?php echo (isset($_SESSION['sv_summary_fee_filter_month']))?$_SESSION['sv_summary_fee_filter_month']:''; ?>">
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label for="sv_summary_filter_department" class="col-lg-2">Department</label>                           
                                        <div class="col-lg-5" >
                                            <select class="form-control select" name="sv_summary_filter_department" id="sv_summary_filter_department">
                                                <option value="" selected>-- Please Select --</option>
                                                <?php if($department) foreach($department as $key => $value){ ?>
                                                    <option value="<?php echo $value['id']; ?>" <?php echo (isset($_SESSION['sv_summary_fee_filter_department']) && $_SESSION['sv_summary_fee_filter_department'] != 'all' && $_SESSION['sv_summary_fee_filter_department'] == $value['id'])?'selected':''; ?>><?php echo $value['name'].' - ['.$value['branch_name'].']'; ?></option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="row float-right" style="margin-bottom: 1rem;">
                                        <div class="col d-print-none">
                                            <button class="btn btn-info" id="searchBtn" disabled style="cursor: not-allowed;">Search</button>
                                        </div>
                                    </div>
                                </div>
                            </form>    
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="tab-content">
            <div class="tab-pane fade <?php echo (!empty($booking_form))?'active show':''; ?>" id="tableView" role="tabpanel" aria-labelledby="nav-table-tab">
                <div class="card-body">
                    <div class="card no-shadow">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-4 mx-auto py-5 text-center">
                                    <img src="<?php echo base_url('assets/images/logo/salihin-logo.png'); ?>" class="img img-fluid">
                                    <h5 class="text-uppercase mt-3">
                                        <b>
                                            Service Form 
                                            <?php 
                                                if($_SESSION['sv_summary_fee_filter_title'] == 24) $title = str_replace($booking_form[0]['type'],'', $title_form);
                                                else $title = $title_form;
                                                echo (isset($title_form))?'- '.$title:''; 
                                            ?> 
                                            Summary 
                                            <?php echo (!empty($title_month)?date('M',strtotime('01-'.$title_month.'-'.$title_year)):'').' '.$title_year; ?>
                                        </b>
                                    </h5>
                                </div>
                            </div>
                            <div class="table-responsive">
                                <table id="filter_table_sv_summary" class="table table-bordered p-0 m-0" width="100%">
                                    <thead>
                                        <th class="text-center" width="5%">#</th>
                                        <th class="text-center" width="5%">Year</th>
                                        <th class="text-center" width="30%">Service Form</th>
                                        <th class="text-center" width="30%">Department</th>
                                        <th class="text-center" width="10%">
                                            <?php 
                                                if($_SESSION['user']['division_id'] == 4 || $_SESSION['user']['division_id'] == 10)
                                                {
                                                    if($_SESSION['sv_summary_fee_filter_title'] == 20) echo "Price per request";
                                                    elseif($_SESSION['sv_summary_fee_filter_title'] == 21) echo "Per Trip";
                                                    elseif($_SESSION['sv_summary_fee_filter_title'] == 23) echo "Per Pax";
                                                    elseif($_SESSION['sv_summary_fee_filter_title'] == 22) echo "Per SQFT";
                                                    else echo "Unit Price";
                                                }else{
                                                    echo "Price Per Pax"; 
                                                }
                                            ?>
                                        </th>
                                        <th class="text-center" width="10%">
                                            <?php 
                                                if($_SESSION['user']['division_id'] == 4 || $_SESSION['user']['division_id'] == 10)
                                                {
                                                    if($_SESSION['sv_summary_fee_filter_title'] == 20) echo "Requested";
                                                    elseif($_SESSION['sv_summary_fee_filter_title'] == 23) echo "Pax";
                                                    elseif($_SESSION['sv_summary_fee_filter_title'] == 22) echo "Unit";
                                                    else echo "Total Request";
                                                }else{
                                                    echo "Pax"; 
                                                }
                                            ?>
                                        </th>
                                        <th class="text-center" width="10%">Total</th>
                                    </thead>

                                    <tbody>
                                        <?php $total_pax = 0; $total_charge = 0;
                                        if($booking_form) foreach($booking_form as $key => $row){ 
                                            $charge_per_pax = $row['price'];
                                        ?>
                                            <tr>
                                                <td class="text-center"><?php echo $key+1; ?></td>
                                                <td class="text-center"><?php echo date('Y',strtotime($row['requested_date'])); ?></td>
                                                <td>
                                                    <?php 
                                                        if($_SESSION['sv_summary_fee_filter_title'] == 20) echo $row['title'].' - '.$row['type'];
                                                        else echo $row['title']; 
                                                    ?>
                                                </td>
                                                <td><?php echo $row['department_name']; ?></td>
                                                <td class="text-center"><?php echo 'RM'.number_format($row['price'],2); ?></td>
                                                <td class="text-center">
                                                    <?php 
                                                        if(isset($_SESSION['sv_summary_fee_filter_title']))
                                                        {
                                                            if($_SESSION['sv_summary_fee_filter_title'] == 22)
                                                            {
                                                                $total_pax += $row['pax'];
                                                                echo $row['pax'];
                                                            }
                                                            elseif($_SESSION['sv_summary_fee_filter_title'] == 23)
                                                            {
                                                                $total_pax += $row['pax'];
                                                                echo $row['pax'];
                                                            }
                                                            // elseif($_SESSION['sv_summary_fee_filter_title'] == 20)
                                                            // {
                                                            //     if($row['type'])
                                                            //     {
                                                            //         $type_explode = explode('-', $row['type']);
                                                            //         $total_pax += end($type_explode); 
                                                            //         echo end($type_explode);
                                                            //     }                                                            
                                                            // }
                                                            else
                                                            {
                                                                $total_pax += $row['total']; 
                                                                echo $row['total'];
                                                            }
                                                        }else{
                                                            $total_pax += $row['total']; 
                                                            echo $row['total'];
                                                        }
                                                    ?>
                                                </td>
                                                <td class="text-center">
                                                    <?php
                                                        if(isset($_SESSION['sv_summary_fee_filter_title']))
                                                        {
                                                            if($_SESSION['sv_summary_fee_filter_title'] == 22)
                                                            {
                                                                $total_charge += ($charge_per_pax * $row['pax']);
                                                                echo 'RM'.number_format(($charge_per_pax * $row['pax']),2);
                                                            }
                                                            elseif($_SESSION['sv_summary_fee_filter_title'] == 23)
                                                            {
                                                                $total_charge += ($charge_per_pax * $row['pax']);
                                                                echo 'RM'.number_format(($charge_per_pax * $row['pax']),2);
                                                            }
                                                            // elseif($_SESSION['sv_summary_fee_filter_title'] == 20)
                                                            // {
                                                                // if($row['type'])
                                                                // {
                                                                //     $type_explode = explode('-', $row['type']);
                                                                //     $total_pax += end($type_explode); 
                                                                //     $total_charge += ($row['price'] * end($type_explode));
                                                                //     echo 'RM'.number_format($row['price'] * end($type_explode),2);
                                                                // }
                                                                // $total_charge += $row['price'];
                                                                // echo 'RM'.number_format($row['price'],2);
                                                            // }
                                                            else
                                                            {
                                                                $total_charge += ($charge_per_pax * $row['total']);
                                                                echo 'RM'.number_format(($charge_per_pax * $row['total']),2);
                                                            }
                                                        }else{
                                                            $total_charge += ($charge_per_pax * $row['total']);
                                                            echo 'RM'.number_format(($charge_per_pax * $row['total']),2);
                                                        }
                                                    ?>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                        <tr>
                                            <td style="border-right: none;"></td>
                                            <td style="border-left: none; border-right: none;"></td>
                                            <td style="border-left: none; border-right: none;"></td>
                                            <td style="padding-left: 100px; border-left: none; border-right: none;"><strong>TOTAL</strong></td>
                                            <td style="border-left: none;"></td>
                                            <td class="text-center"><strong><?php echo $total_pax; ?></strong></td>
                                            <td class="text-center"><strong><?php echo 'RM'.number_format($total_charge,2); ?></strong></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="updateFormValue" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Update Booking Form Value</h5>&emsp;
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form class="form" id="bookingDetailForm" action="<?php echo base_url('hr/serviceform_report/update_form_value') ?>" method="POST" onsubmit="return confirm('Confirm to update this booking form value?')">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-12">
                            <label for="form_id">Booking Form</label>
                            <select class="form-control select" name="form_id" id="form_id" onchange="get_current_form_value()">
                                <option value="-1" selected disabled>-- Please Select --</option>
                                <?php if($getBookingName) foreach($getBookingName as $key => $value){ ?>
                                    <?php if($value['category_id'] != 7){ ?>
                                        <option value="<?php echo $value['id']; ?>"><?php echo $value['title']; ?></option>
                                    <?php } ?>
                                <?php } ?>
                            </select>
                        </div>
                        <div style="margin-bottom: 6rem;"></div>
                        <div class="col-12 col-sm-6">
                            <label for="form_value">Booking Form Value</label>
                            <div class="input-group mb-3">
                                <div class="input-group-append">
                                    <select class="form-control" name="value_type_id" id="value_type_id" required>
                                        <option value="0">RM</option>
                                        <option value="1">%</option>
                                    </select>
                                </div>
                                <input type="text" class="form-control" name="form_value" id="form_value">
                            </div>
                        </div>
                        <div class="col-12 col-sm-6">
                            <label for="form_value">Booking Form Current Value</label>
                            <input type="text" class="form-control" name="current_form_value" id="current_form_value" disabled style="cursor: not-allowed;">
                        </div>
                    </div>
                    <br>
                    <div class="float-right" style="padding-bottom: 1rem;">
                        <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    $(document).ready(function()
    {
        $('#filter_table_sv_summary').DataTable().destroy();
        change_department_input();
    });

    function open_modal()
    {
        // data-toggle="modal" data-target="#updateFormValue"
        // $('#form_id').prop('selectedIndex',-1);
        $("#form_id").select2("val", "-1");
        $('#current_form_value').val('');
        $('#updateFormValue').modal('show');

    }

    function get_current_form_value()
    {
        var form_id = $('#form_id').val();
        $('#current_form_value').empty();

        $.ajax({
            url:'<?php echo base_url('hr/serviceform_report/get_current_form_value') ?>',
            data: {
                form_id   : form_id,
            },
            type: 'post',
            dataType: 'json',
            success: (data) => {
            if(data.status == "Success") 
            {
                var result = data.result;
                
                if(result[0]['type'] == 1)
                {
                    var price = result[0]['price'] * 100+'%';
                }else{
                    var price = result[0]['price'];
                }
                $('#current_form_value').val(price);
            }else{
                $('#current_form_value').val('');
            }

          }
       });
    }

    function change_department_input()
    {
        var form_id = $('#sv_summary_filter_form').val();

        if(form_id > 0)
        {
            // if(form_id == 20)
            // {
            //     $('#sv_summary_filter_department').attr('required','required');
            // }else{
            //     $('#sv_summary_filter_department').removeAttr('required');
            // }

            $('#searchBtn').removeAttr('disabled');
            $('#searchBtn').css('cursor','pointer');
        }
    }
</script>