<script>
    function printPage(){
        window.print();
    }
</script>
<style type="text/css">
    .showHideSpan{
        font-size: 14px;
    }
    input[type="checkbox"]{
        width: 20px;height: 18px;
    }
    .displaynone{
        display: none;
    }
    .floatRight{
        float: right;
    }
    #generateCSVBtn,
    #generatePdfBtn{
        padding:5px 50px;
        margin-right: 40px;
    }
    @media print{
        .showHideSpan{
            display: none;
        }
    }
</style>
<div class="wrapper" id="printWrapper">

            <div class="print" style="overflow: hidden;">
                <form action="<?php echo $this->webroot . 'Expense/printReport/' . $data['Expense']['unique_url']; ?>" class="form-inline hidden-print" id="participantData" method="post">
                    <div class="form-group">
                        <label for="participantId"><?php echo __("LBL_SELECT_PARTICIPANT"); ?> </label>
                        <input type="hidden" name="unique_url" value="<?php echo $data['Expense']['unique_url']; ?>">
                         <select class="form-control" id="participantId" name="participant_id" placeholder="Select any">
                            <option value="" ><?php echo __("LBL_ALL"); ?></option>
                            <?php foreach ($data['Participant'] as $participant) { ?>
                            <option
                                    value="<?php echo $participant['participant_id']; ?>"
                                    <?php if (!empty($participant_id) && $participant_id == $participant['participant_id']) {
                                        echo "Selected";
                                        $participant_name = $participant['name'];
                                    } ?>
                            ><?php echo $participant['name']; ?></option>
                           <?php 
                        } ?>
                        </select>
                    </div>
                </form>
                <button type="submit" class="btn btn-info floatRight hidden-print" id="generateCSVBtn"><?php echo __("LBL_CSV"); ?> <img src="<?php echo HTTP_SITE_URL; ?>images/download.png" style="height: 18px;width: 18px;"/></button>
                <button type="submit" class="btn btn-info floatRight hidden-print" id="generatePdfBtn"><?php echo __("LBL_PDF"); ?> <img src="<?php echo HTTP_SITE_URL; ?>images/download.png" style="height: 18px;width: 18px;"/></button>
                <!-- <span><?php //echo date("d/m/Y - H:i"); ?></span> -->
            </div>

    <div id="exportPrintTable">
        <div class="exportPrintTable-inner">


            <div style="width: 100%; overflow: hidden;">
                <div style="width: 40%;float: left; ">
                    <!-- <p><a href="<?php echo $this->webroot; ?>"><img src="<?php echo CDN_URL; ?><?php echo __('LBL_LOGO_PDF'); ?>" width="270" height="72"  alt=""/></a> </p> -->
                    <div class="description">
                        <p><strong> ExpenseCount "<?php echo $data['Expense']["title"]; ?>"</strong></p>
                        <?php if (isset($data['Expense']["description"])) { ?>
                        <p><?php echo $data['Expense']["description"]; ?></p>
                        <?php 
                    }
                    $create_date = strtotime($data['Expense']["create_date"]);
                    ?>
                        <p><?php echo __("LBL_CREATED_BY"); ?> <?php echo $data['Participant'][0]['name']; ?>, <?php echo __("LBL_ON"); ?> <?php echo date('d-M-Y', $create_date); ?></p>
                    </div>
                </div>
                <div style="width: 60%;float: left;">
                    <span class="showHideSpan" style=""> (<?php echo __("LBL_HIDE_SHOW"); ?> <input type="checkbox" value="1" id="isChartShow" checked="checked">)</span>
                    <div style="border: 1px solid #bbb; margin: 5px 0; " id="chartDiv">
                    <!-- <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> -->
                        <script src="<?php echo CDN_URL;?>js/highcharts/highcharts.js"></script>
                    <!-- <script src="https://code.highcharts.com/modules/exporting.js"></script> -->
                    <!-- <script src="https://code.highcharts.com/modules/export-data.js"></script> -->

                    <div id="containerPie" style=""></div>
                    <script type="text/javascript">
                        
                        // Build the chart
                        Highcharts.chart('containerPie', {
                            chart: {
                                height:280,
                                width:540,
                                plotBackgroundColor: null,
                                plotBorderWidth: null,
                                plotShadow: false,
                                type: 'pie'
                            },
                            title: {
                                text: 'Participant wise expense ratio'
                            },
                            tooltip: {
                                pointFormat: '<b>{point.percentage:.1f}%</b>'//{series.name}: 
                            },
                            plotOptions: {
                                pie: {
                                    allowPointSelect: true,
                                    cursor: 'pointer',
                                    dataLabels: {
                                        enabled: true,
                                        format:'<b>{point.percentage:.1f}%</b>'
                                    },
                                    showInLegend: true
                                }
                            },
                            series: [{
                                name: 'Brands',
                                colorByPoint: true,
                                data: <?php echo $pieChartDataFinal; ?>
                                /*[{
                                    name: 'Chrome',
                                    y: 31.41
                                }, {
                                    name: 'Internet Explorer',
                                    y: 11.84
                                }, {
                                    name: 'Firefox',
                                    y: 10.85
                                }, {
                                    name: 'Edge',
                                    y: 4.67
                                }, {
                                    name: 'Safari',
                                    y: 4.18
                                }, {
                                    name: 'Other',
                                    y: 7.05
                                }]*/
                            }],
                            legend: {
                                // layout: 'vertical',
                                backgroundColor: '',
                                align: 'right',
                                verticalAlign: 'middle',
                                // alignColumns:false,
                                floating: false,
                                itemWidth:100,
                                width:200,
                                x: 0,
                                y: 0,
                                // maxHeight:200,
                                // navigation:{
                                //     enabled:true
                                // }
                                symbolHeight:10,
                                symbolWidth:10,
                                squareSymbol:true
                            },
                            credits: {
                                enabled:false
                            },
                            exporting: {
                                enabled:false
                            }
                        });
                    </script></div>
                </div>
            </div>

        
            <span class="showHideSpan" style="width: 100%; "> (<?php echo __("LBL_HIDE_SHOW"); ?>: 
                <?php echo __("LBL_WHO_PAID"); ?>
                <input type="checkbox" value="1" id="isWhoPaidShow" checked="checked">
                &nbsp;&nbsp;&nbsp;&nbsp;<?php echo __("LBL_FOR_WHAT_REASONS"); ?>
                <input type="checkbox" value="1" id="isWhatReasonShow" checked="checked">
                &nbsp;&nbsp;&nbsp;&nbsp;<?php echo __("LBL_HOW_MUCH"); ?>
                <input type="checkbox" value="1" id="isHowMuchShow" checked="checked">
                &nbsp;&nbsp;&nbsp;&nbsp;<?php echo __("LBL_WHEN"); ?>
                <input type="checkbox" value="1" id="isWhenShow" checked="checked">
                &nbsp;&nbsp;&nbsp;&nbsp;<?php echo __("LBL_INVOLVES"); ?>
                <input type="checkbox" value="1" id="isInvolvesShow" checked="checked">
            )
            </span>
            <table class="table printTable perticipantTable" id="exportTable" width="100%">
                <thead>
                    <tr>
                        <th><?php echo __("LBL_WHO_PAID"); ?></th>
                        <th><?php echo __("LBL_FOR_WHAT_REASONS"); ?></th>
                        <th><?php echo __("LBL_HOW_MUCH"); ?>?</th>
                        <th><?php echo __("LBL_WHEN"); ?></th>
                        <th><?php echo __("LBL_INVOLVES"); ?></th>
                        <?php if (!empty($participant_id)) { ?>
                            <th><?php echo __("LBL_FOR"); ?> <?php echo $participant_name; ?>?</th>
                        <?php } ?>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $personal_total_expense = 0;
                    $total_expense = 0;
                    $finalExpenseList = array();
                    foreach ($data['GroupExpense'] as $key => $value) {
                        $finalExpenseList[$key]['participent_name'] = $value["participent_name"];
                        $finalExpenseList[$key]['amount'] = $value["amount"];
                        $finalExpenseList[$key]['what'] = $value["what"];
                        $finalExpenseList[$key]['expense_date_new'] = $value["expense_date_new"];
                        $finalExpenseList[$key]['involve_list'] = $value["involve_list"];
                        $finalExpenseList[$key]['my_expense'] = '';
                        $finalExpenseList[$key]['currency'] = $data['Expense']["currency"];
                        if (!empty($participant_id)) {
                            if (!isset($data['Meal']) && empty($data['Meal'])) {
                                $finalExpenseList[$key]['my_expense'] = number_format($value["my_expense"], 2);
                            } 
                        }
                        ?>
                    <tr>
                        <td><?php echo $value["participent_name"]; ?></td>
                        <td><?php echo $value["what"]; ?></td>
                        <td><?php
                            $total_expense = $total_expense + $value["amount"];
                            echo $value["amount"] . " " . $data['Expense']["currency"]; ?></td>
                        <td><?php echo $value["expense_date_new"]; ?></td>
                        <td><?php echo $value["involve_list"]; ?></td>
                        <?php if (!empty($participant_id)) { ?>
                            <td><?php
                                if (!isset($data['Meal']) && empty($data['Meal'])) {
                                    $personal_total_expense = $personal_total_expense + $value["my_expense"];
                                    echo number_format($value["my_expense"], 2) . " " . $data['Expense']["currency"]; 
                                }
                                ?></td>
                        <?php 
                    } ?>
                    </tr>

                    <?php

                    }
                    ?>
                </tbody>
                <tfoot>
                    <tr style="background: transparent;">
                        <?php if (!empty($participant_id)) { ?>
                            <td style="border: 0px;"></td>
                            <td align="right" style="font-weight: bold;border: 0px;"><?php echo __("LBL_TOTAL"); ?> : </td>
                            <td align="left" style="border: 0px;"><?php echo $total_expense . " " . $data['Expense']["currency"]; ?></td>
                            <td style="border:0px;"></td>
                            <td align="right" style="font-weight: bold;border: 0px;"><?php echo __("LBL_TOTAL"); ?> : </td>
                            <td align="left" style="border: 0px;"><?php echo number_format($personal_total_expense, 2) . " " . $data['Expense']["currency"]; ?></td>
                            <?php 
                        } else { ?>
                            <td style="border:0px;"></td>
                            <td align="right" style="font-weight: bold; border: 0px;"><?php echo __("LBL_TOTAL"); ?> : </td>
                            <td align="left" style="border:0px;"><?php echo $total_expense . " " . $data['Expense']["currency"]; ?></td>
                            <td style="border:0px;"></td>
                            <td style="border:0px;"></td>
                        <?php } ?>
                    </tr>
                </tfoot>
            </table>

            <?php $finalMealList = array();
            if (isset($data['Meal']) && !empty($data['Meal'])) { ?>
                <h5 id="summaryHeader" style="text-align: left;"><?php echo __("LBL_MEAL"); ?>
                    <span class="showHideSpan" style=""> (<?php echo __("LBL_HIDE_SHOW"); ?> <input type="checkbox" value="1" name="isMealReportShow" id="isMealReportShow" checked="checked">)</span>
                </h5>
                <table class="table printTable mealTable" id="exportTable" width="100%">
                    <thead>
                        <tr>
                            <th><?php echo __("LBL_DATE"); ?></th>
                            <th><?php echo __("LBL_DAY"); ?></th>
                            <th><?php echo __("LBL_BREAKFAST"); ?></th>
                            <th><?php echo __("LBL_LUNCH"); ?></th>
                            <th><?php echo __("LBL_DINNER"); ?></th>
                            <th><?php echo __("LBL_TOTAL"); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $total = 0; $tempParticipantMeal = array();
                    
                    foreach ($data['Meal'] as $zz => $value) {
                        $subtotal = 0;
                        $breakfast = 0;
                        $lunch = 0;
                        $dinner = 0;
                        $meal_participants = explode(',', $value['meal_amount']);
                        foreach ($meal_participants as $meal_participant) {
                            $meal_amounts = explode('|', $meal_participant);
                            $breakfast += $meal_amounts[1];
                            $lunch += $meal_amounts[2];
                            $dinner += $meal_amounts[3];
                            if(!isset($tempParticipantMeal[$meal_amounts[0]]['meal'])){
                                $tempParticipantMeal[$meal_amounts[0]]['meal'] = 0;
                            }
                            $tempParticipantMeal[$meal_amounts[0]]['meal'] += $meal_amounts[1] + $meal_amounts[2] + $meal_amounts[3];
                        }
                        $subtotal = $breakfast + $lunch + $dinner;
                        $total += $subtotal;
                        $finalMealList[$zz]['create_date'] = date('d-M-Y', strtotime($value["create_date"]));
                        $finalMealList[$zz]['day'] = date('D', strtotime($value["create_date"]));
                        $finalMealList[$zz]['breakfast'] = $breakfast;
                        $finalMealList[$zz]['lunch'] = $lunch;
                        $finalMealList[$zz]['dinner'] = $dinner;
                        $finalMealList[$zz]['subtotal'] = $subtotal;
                        ?>
                    <tr>
                        <td><?php echo date('d-M-Y', strtotime($value["create_date"])); ?></td>
                        <td><?php echo date('D', strtotime($value["create_date"])); ?></td>
                        <td><?php echo $breakfast; ?></td>
                        <td><?php echo $lunch; ?></td>
                        <td><?php echo $dinner; ?></td>
                        <td><?php echo $subtotal; ?></td>
                    </tr>
                    <?php
                    }
                    ?>
                    </tbody>
                    <tfoot>
                        <tr style="background: transparent;">
                            <td style="border: 0px;"></td>
                            <td style="border:0px;"></td>
                            <td style="border:0px;"></td>
                            <td style="border:0px;"></td>
                            <td align="right" style="font-weight: bold;border: 0px;"><?php echo __("LBL_TOTAL"); ?> : </td>
                            <td style="border:0px;"><?php echo $total; ?></td>
                        </tr>
                    </tfoot>
                </table>
            <?php } ?>

        <h5 id="summaryHeader" style="text-align: left;"><?php echo __("LBL_SUMMARY_REPORT"); ?>
            <span class="showHideSpan" style=""> (<?php echo __("LBL_HIDE_SHOW"); ?> <input type="checkbox" value="1" name="isSummaryReportShow" id="isSummaryReportShow" checked="checked">)</span>
        </h5>
        <table class="printTable table" id="exportTableBalance" style="text-align: center;">
            <thead>
                    <th style="text-align: center;"><?php echo __("LBL_PARTICIPANTS"); ?></th>
                    <th style="text-align: center;"><?php echo __("LBL_CHARGED"); ?></th>
                    <th style="text-align: center;"><?php echo __("LBL_PAID"); ?></th>
                    <th style="text-align: center;"><?php echo __("LBL_DUE"); ?></th>
                </tr>
            </thead>
            <tbody>
            <?php
            $due = 0;
            $count = 1; 
            $finalSummaryList = array();
            foreach($allParticipant as $key=>$value) {
                $due = $value["total_paid"] - $value["total_charge"];
                if( (($current_perticipants != '') && ($value['participant_id'] == $current_perticipants)) || ($current_perticipants == '') ) {
                    $finalSummaryList[$key]['name'] = $value['name'];
                    $finalSummaryList[$key]['total_charge'] = $value['total_charge'];
                    $finalSummaryList[$key]['total_paid'] = $value['total_paid'];
                    $finalSummaryList[$key]['currency'] = $value['currency'];
                    $finalSummaryList[$key]['due'] = $value["total_paid"] - $value["total_charge"];
                    ?>
                    <tr>
                        <td><?php echo $value["name"];?></td>
                        <td><?php echo $value["total_charge"].' '.$value["currency"];?></td>
                        <td><?php echo $value["total_paid"].' '.$value["currency"];?></td>
                        <td><?php echo $due.' '.$value["currency"];?></td>
                    </tr>
                    
                    <?php 
                }
            }
            ?>
            </tbody>
        </table>
        




        <div style="width: 100%; overflow: hidden;margin-top: 50px;">
            <div style="width: 40%;float: right; text-align: right;margin-right: 20px;">
                <?php if (isset($data['Meal']) && !empty($data['Meal'])) : ?>
                    <p><a href="<?php echo $this->webroot; ?>"><img src="<?php echo HTTP_SITE_URL; ?><?php echo __('LBL_MESS_EXPENSE_LOGO'); ?>" width="270" height="72"  alt=""/></a> </p>
                <?php else : ?>
                    <p><a href="<?php echo $this->webroot; ?>"><img src="<?php echo HTTP_SITE_URL; ?><?php echo __('LBL_GROUP_EXPENSE_LOGO'); ?>" width="270" height="72"  alt=""/></a> </p>
                <?php endif; ?>
            </div>
        </div>

    </div>
    </div>
   <!-- <div class="footerText">To add, delete or comment on the expenses, balance an ExpenseCount or print this page, it is necessary to access the ExpenseCount.com via a traditional browser.<br/>
        click <a href="#" style="cursor:pointer" onclick="printPage()"> here</a> to print the page.</div>-->
    <div class="footer-pdf-print-link hidden-print displaynone">
        <a href="#" onclick="printPage()" class="btn btn-info"><?php echo __("LBL_PRINT"); ?></a>
        <form action="<?php echo $this->Html->url(array('controller' => 'Expense', 'action' => 'generatePdf')); ?>" class="form-inline" method="post" id="generatePdf">
            <input type="hidden" name="whole_content" id="wholeContent">
            <div class="form-group">
                <button type="submit" class="btn btn-info" id="generatePdfBtn2"><?php echo __("LBL_DOWNLOAD"); ?></button>
            </div>
        </form>

        <form action="<?php echo $this->Html->url(array('controller'=>'Expense','action'=>'generateCSV'));?>" class="form-inline" method="post" id="generateCSV" style="display: inline-flex;">
            <input type="hidden" name="csv_content" value='<?php echo serialize($finalExpenseList); ?>'>
            <input type="hidden" name="csv_summary_content" value='<?php echo serialize($finalSummaryList); ?>'>
            <input type="hidden" name="csv_meal_content" value='<?php echo serialize($finalMealList); ?>'>
            <div class="form-group">
                <button type="submit" class="btn btn-info" id="generateCSVBtn2"><?php echo __("LBL_CSV"); ?></button>
            </div>
        </form>
    </div>

</div>
<script>
    $(document).ready(function(){
        $("#participantId").on('change', function () {
            //alert("selection changed");
            $("form#participantData").submit();
        });
        $('#exportTable').DataTable({
            "paging":   false,
            "searching": false,
            "info":     false
        });
        $("#generateCSVBtn").on('click', function () {
            $("form#generateCSV").submit();
        });
        $("#generatePdfBtn").on('click', function () {
            $('.showHideSpan').hide();
            $('input[type="checkbox"]').hide().css('visibility','hidden');
            var Content = $('#exportPrintTable').html();
            var ContentWrap = "<!doctype html><html><body>"+Content+"</body></html>";
            var currentHtml = $("#wholeContent").val(ContentWrap);
            // alert($("#wholeContent").val());
            $("form#generatePdf").submit();
        });

        $("#isSummaryReportShow").on('click', function () {
            if(this.checked) {
                $('#exportTableBalance').show();
            } else {
                $('#exportTableBalance').hide();
            }
        });

        $("#isMealReportShow").on('click', function () {
            if(this.checked) {
                $('.mealTable').show();
            } else {
                $('.mealTable').hide();
            }
        });

        $("#isWhoPaidShow").on('click', function () {
            if(this.checked) {
                $('table.perticipantTable th:nth-child(1)').show();
                $('table.perticipantTable td:nth-child(1)').show();
            } else {
                $('table.perticipantTable th:nth-child(1)').hide();
                $('table.perticipantTable td:nth-child(1)').hide();
            }
        });

        $("#isHowMuchShow").on('click', function () {
            if(this.checked) {
                $('table.perticipantTable th:nth-child(3)').show();
                $('table.perticipantTable td:nth-child(3)').show();
            } else {
                $('table.perticipantTable th:nth-child(3)').hide();
                $('table.perticipantTable td:nth-child(3)').hide();
            }
        });

        $("#isWhatReasonShow").on('click', function () {
            if(this.checked) {
                $('table.perticipantTable th:nth-child(2)').show();
                $('table.perticipantTable td:nth-child(2)').show();
            } else {
                $('table.perticipantTable th:nth-child(2)').hide();
                $('table.perticipantTable td:nth-child(2)').hide();
            }
        });

        $("#isWhenShow").on('click', function () {
            if(this.checked) {
                $('table.perticipantTable th:nth-child(4)').show();
                $('table.perticipantTable td:nth-child(4)').show();
            } else {
                $('table.perticipantTable th:nth-child(4)').hide();
                $('table.perticipantTable td:nth-child(4)').hide();
            }
        });

        $("#isInvolvesShow").on('click', function () {
            if(this.checked) {
                $('table.perticipantTable th:nth-child(5)').show();
                $('table.perticipantTable td:nth-child(5)').show();
            } else {
                $('table.perticipantTable th:nth-child(5)').hide();
                $('table.perticipantTable td:nth-child(5)').hide();
            }
        });

        $("#isChartShow").on('click', function () {
            if(this.checked) {
                $('#chartDiv').show();
            } else {
                $('#chartDiv').hide();
            }
        });
    });
</script>

