<?php $status = array('Pending', 'Confirmed', 'Cancelled', 'Wating For Online Payment') ?>
<?php $status_color = array('color:#FF9900;', 'color:#219921;', 'color:#D52229;', 'color:#FF9900;') ?>
<fieldset style="margin-top:0px; margin-bottom: 100px; padding-bottom: 0px; border-radius: 0px 0px 10px 10px">
    <div class="row report-row">
        <div id="entireReport">
            <table class="table table-responsive table-bordered" id="reportTableContent">
                <tr class="table-bg-color">
                    <th class="text-center text-capitalize">Ref#</th>
                    <th class="text-center text-capitalize">Date</th>
                    <th class="text-center text-capitalize">Type</th>
                    <th class="text-center text-capitalize">Name</th>
                    <!--<th class="text-center text-capitalize">Phone</th>-->
                    <th class="text-center text-capitalize">Amount</th>
                    <th class="text-center text-capitalize">Paid/Pay</th>
                    <th class="text-center text-capitalize">ccFee</th>
                    <th class="text-center text-capitalize">Status</th>
                    <!--<th class="text-center text-capitalize">Del.Time</th>-->
                    <th class="text-center text-capitalize">Update</th>
                    <th class="text-center text-capitalize">Action</th>
                </tr>
                <?php $grand_total = 0; ?>
                <?php $paid_total = 0; ?>
                <?php $cc_fee_total = 0; ?>
                <?php if (!empty($orderList)): ?>
                    <?php foreach ($orderList as $obj): ?>
                        <tr class="table-bg-color-white table-content">
                            <td class="text-center text-capitalize"><a href="javascript:void(0)" onclick="showOnlineOrderDetail('<?php echo site_url('gkpos/report/viewdetail/' . $obj->id) ?>', 'View Detail')" style='color: #002a80'><?php echo strtoupper($obj->id) ?></a></td>
                            <td class="text-center text-capitalize"><?php echo date($this->config->item('dateformat'), strtotime($obj->created)) ?></td>
                            <td class="text-center text-capitalize"><?php echo $obj->online_type ?></td>
                            <td class="text-center text-capitalize"><?php echo $obj->namealt ? $obj->namealt : $obj->name ?></td>
                            <!--<td class="text-center text-capitalize"><?php echo $obj->phonealt ? $obj->phonealt : $obj->phone ?></td>-->
                            <td class="text-center text-capitalize"><?php echo to_currency($obj->grand_total) ?></td>
                            <?php if ($obj->online_method != 'cod'): ?>
                                <td class="text-center text-capitalize"><?php $obj->paid_status == 1 ? print to_currency($obj->grand_total) : 'unpaid' ?></td>
                                <?php $order_grand_total = abs($obj->change_due) > 0 && $obj->pay_tip == 'yes' ? $obj->grand_total + abs($obj->change_due) : $obj->grand_total ?>
                                <?php $paid_total += $order_grand_total; ?>
                            <?php else: ?>
                                <?php if ($obj->paid_status == 1): ?>
                                    <?php $order_grand_total = abs($obj->change_due) > 0 && $obj->pay_tip == 'yes' ? $obj->grand_total + abs($obj->change_due) : $obj->grand_total ?>
                                    <?php $paid_total += $order_grand_total; ?>
                                    <td class="text-center text-capitalize"><?php echo to_currency($order_grand_total) ?></td>
                                <?php else: ?>
                                    <?php if ($obj->online_status == 1): ?>
                                        <td class="text-center text-capitalize"><a href="javascript:void(0)" onclick="payOpener('<?php echo $obj->id ?>')" style="color: #002a80">COD</a></td>
                                    <?php else: ?>
                                        <td class="text-center text-capitalize">pending</td>
                                    <?php endif; ?>
                                <?php endif; ?>
                            <?php endif; ?>
                            <?php if ($obj->cc_fee != null && $obj->cc_fee > 0): ?>
                                <?php $cc_fee_total += $obj->cc_fee; ?>
                                <td class="text-center"><?php print to_currency($obj->cc_fee) ?></td>           
                            <?php else: ?>
                                <td class="text-center"><?php print to_currency(0) ?></td>           
                            <?php endif; ?>
                            <td style="<?php echo $status_color[$obj->online_status] ?>"><?php echo $status[$obj->online_status] ?></td>
                            <!--<td class="text-center"><?php echo $obj->delivery_time ?></td>-->
                            <td class="text-center text-capitalize">
                                <select id="updateStatus_<?php echo $obj->id ?>" class="form-control" style="height: 20px; padding: 2px; <?php echo $status_color[$obj->online_status] ?>;"   onchange="updateStatus('<?php echo $obj->id ?>', '<?php echo $segment ?>')">
                                    <option value="1" <?php $obj->online_status == 1 ? print 'selected="true"' : print'' ?>>Confirm</option>
                                    <option value="2" <?php $obj->online_status == 2 ? print 'selected="true"' : print'' ?>>Cancel</option>
                                    <option value="0" <?php $obj->online_status == 0 ? print 'selected="true"' : print'' ?>>Pending</option>
                                </select>
                            </td>
                            <td class="text-center text-capitalize"><img src="<?php echo ASSETS_GKPOS_PATH ?>images/delete.png" width="24px" height="24px" onclick="deleteReportRow('<?php echo $obj->id ?>', '<?php echo $segment ?>')">
                                <?php if ($this->config->item('gk_only_online_admin') == 'no'): ?>
                                    | 
                                    <?php if ($this->config->item('gk_guest_bill_from_browser') == 'yes'): ?>
                                        <img src="<?php echo ASSETS_GKPOS_PATH ?>images/view.png" width="24px" height="24px" onclick="printGuestBillServer('<?php echo $obj->id ?>', 4)">
                                    <?php else: ?>
                                        <img src="<?php echo ASSETS_GKPOS_PATH ?>images/view.png" width="24px" height="24px" onclick="printGuestBill('<?php echo $obj->id ?>', 4)"> 
                                    <?php endif; ?>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php $grand_total += $obj->grand_total; ?>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="8">No Orders Found</td></tr>
                <?php endif; ?>
                <tr class="table-bg-color table-content">
                    <td colspan="4" class="text-right">Grand Total&nbsp;</td>
                    <td class="text-center"><?php echo to_currency($grand_total) ?></td>
                    <td class="text-center"><?php echo to_currency($paid_total) ?></td>
                    <td class="text-center"><?php echo to_currency($cc_fee_total) ?></td>
                    <td colspan="4">&nbsp;</td>
                </tr>
                <?php if (!empty($orderList) && $maxCounter > 1): ?>
                    <tr>
                        <td colspan="11">
                            <div class="table-bg-color">
                                <?php if (isset($maxCounter) && $maxCounter > 1): ?>
                                    <div class="main-arrowbg text-center pull-left">
                                        <ul class="pagination">
                                            <?php for ($i = 0; $i < $maxCounter; $i++): ?>
                                                <li <?php $segment == $i ? print'class="active"' : '' ?>><a href="javascript:void(0)" onclick="paginateReport('<?php echo $i ?>')" style="padding: 5px 10px !important"><?php echo $i + 1; ?></a></li>
                                            <?php endfor; ?>
                                        </ul>
                                    </div>
                                <?php endif ?>
                            </div>
                        </td>
                    </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
</fieldset>
<a class='warningPopup' href="#warningPopup" style="display:none"></a>
<div style="display:none">
    <div id="warningPopup" class="popupouter">
        <div class="popup-header row">
            <div id="warningPopupHeader" class="warningPopupHeader col-lg-10 col-md-10 col-sm-10 col-xs-10 pull-left "></div>
            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pull-left closeWarningPopup text-center times">&times;</div>
        </div>
        <div class="popup-body row">
            <div id="warningPopupContent" class="warningPopupContent col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <input type="hidden" name="onlineStatus" id="onlineStatus">
                <input type="hidden" name="OrderId" id="OrderId">
                <div class="form-group" id="deliveryTimeBlock">
                    <label style="text-transform: uppercase">Default Delivery Time:<span id="defaultDeliveryTime"></span></label>
                    <label for="deliveryTime" style="text-transform: uppercase">Revised:Default + Given Minutes </label>
                    <div class="input-group">
                        <span class="input-group-addon"><a href="#"><i class="fa fa-clock-o" aria-hidden="true"></i></a></span>
                        <div class="stylish">
                            <span>
                                <input onclick="$(this).closest('div').find('select').slideToggle(110)" class="form-control customernambg" readonly="1" type="text" name="deliveryTime" id="deliveryTime"  placeholder="<?php echo 'Additional minutes' ?>" style="left: 0; width: 220px;"/>
                                <br>
                                <select size=15 onclick="$(this).hide().closest('div').find('input').val($(this).find('option:selected').val());" class="customernambg" style="min-height:150px; margin: 0 auto; left:0px; margin-top: -20px">
                                    <option value="5" class="btn btn-default btn-lg btn-block text-lowercase">5</option>
                                    <option value="10" class="btn btn-default btn-lg btn-block text-lowercase">10</option>
                                    <option value="15" class="btn btn-default btn-lg btn-block text-lowercase">15</option>
                                    <option value="20" class="btn btn-default btn-lg btn-block text-lowercase">20</option>
                                    <option value="25" class="btn btn-default btn-lg btn-block text-lowercase">25</option>
                                    <option value="30" class="btn btn-default btn-lg btn-block text-lowercase">30</option>
                                    <option value="35" class="btn btn-default btn-lg btn-block text-lowercase">35</option>
                                    <option value="40" class="btn btn-default btn-lg btn-block text-lowercase">40</option>
                                    <option value="45" class="btn btn-default btn-lg btn-block text-lowercase">45</option>
                                    <option value="50" class="btn btn-default btn-lg btn-block text-lowercase">50</option>
                                    <option value="55" class="btn btn-default btn-lg btn-block text-lowercase">55</option>
                                </select>
                            </span>
                        </div>
                    </div>
                    <ul id="checkDeliveryPlanError" class="error_message_box"></ul>
                    <div style="text-align: center; margin-top: 80px; ">
                        <input class="btn btn-danger" style="width: 90%;padding: 15px; margin: 0 auto;" type="submit" name="submit_form" value="<?php echo $this->lang->line('gkpos_numpad_key_enter') ?>" onclick="update()">
                    </div>
                </div>
                <div id="deliveryRejectMessge" >
                    <p><strong>Reject Reason</strong></p>
                    <p><textarea rows="3" id="rejectMessage" style="width: 100%;color: #000; border-radius:10px; border: none; padding: 10px;" autofocus="true"></textarea></p>
                    <ul id="checkDeliveryPlanError" class="error_message_box"></ul>
                    <div style="text-align: center; margin-bottom: 30px;">
                        <input class="btn btn-danger" style="width: 100%;padding: 15px; margin: 0 auto;" type="submit" name="submit_form" value="<?php echo "Enter" ?>" onclick="update()">
                    </div>
                    <?php if ($this->config->item('is_touch') == 'enable'): ?>
                        <div class="main-keyboardbg">
                            <div id="virtualKeyboard"></div>
                        </div>
                    <?php endif ?>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="popupContentHolder"></div>
<script>
    $(document).ready(function () {
<?php if ($this->config->item('is_touch') == 'enable') { ?>
            keyboard('virtualKeyboard');
<?php } ?>
        $("#name").autocomplete({
            delay: 0,
            source: '<?php echo site_url('gkpos/get_customer') ?>',
            minLength: 2
        });
        $("#phone").autocomplete({
            delay: 0,
            source: '<?php echo site_url('gkpos/get_customer_phone') ?>',
            minLength: 4
        });
        $(".closeWarningPopup").click(function () {
            $.colorbox.close();
            return false;
        });
        $('.date_filter').datepicker({
            dateFormat: "dd/mm/yy",
             maxDate: "<?php echo date('d/m/Y') ?>"
        });
        $('.closeServiceChargePopup').click(function () {
            $.colorbox.close();
            return false;
        });
    });
    function filterReport() {
        $('#reportFilterForm').validate({
            submitHandler: function () {
                var myform = document.getElementById('reportFilterForm');
                var formdata = new FormData(myform);
                $.ajax({
                    url: $('#reportFilterForm').attr('action'),
                    data: formdata,
                    cache: false,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    beforeSend: function () {
                        $('#GkposAjaxPageLoader').show();
                    },
                    success: function (response) {
                        $("#filterContent2").html(response)
                        manageWindowHeight();
                    },
                    complete: function (xhr, status) {
                        $('#GkposAjaxPageLoader').hide();
                    }
                });
            }
        });
    }

    function paginateReport(segment) {
        $.ajax({
            url: "<?php echo site_url('gkpos/report/onlinefilter/') ?>" + segment,
            data: {
                start_date: $('#onlineFilterStartDate').val(),
                end_date: $('#onlineFilterEndDate').val(),
                order_type: $('#onlineFilterOrderType').val(),
                name: $('#onlineFilterName').val(),
                phone: $('#onlineFilterPhone').val(),
                id: $('#onlineFilterId').val()
            },
            type: "POST",
            beforeSend: function () {
                $('#GkposAjaxPageLoader').show();
            },
            success: function (response) {
                $('#GkposAjaxPageLoader').hide();
                $("#filterContent2").html(response)
                manageWindowHeight();
            }
        });
    }


    function updateStatus(id, segment) {
        var status = $('#updateStatus_' + id).val();
        $('#onlineStatus').val(status);
        $('#OrderId').val(id)
        if (status == '1') {
            $('#deliveryRejectMessge').hide();
            $('#rejectMessage').val('');
            $('#deliveryTimeBlock').show();
            $.ajax({
                url: '<?php echo site_url("gkpos/report/get_delivery_time") ?>',
                data: {
                    id: id
                },
                type: "POST",
                dataType: "json",
                beforeSend: function () {
                    $('#GkposAjaxPageLoader').show();
                },
                success: function (output) {
                    $('#defaultDeliveryTime').text(output.delivery_time);
                },
                complete: function (xhr, status) {
                    $('#GkposAjaxPageLoader').hide();
                }
            });
            jQuery(".warningPopup").colorbox({inline: true, slideshow: false, scrolling: false, height: "300px", open: true, width: '100%', maxWidth: '450px'});
            return false;
        } else if (status == '2') {
            $('#deliveryTimeBlock').hide();
            $('#deliveryTime').val('');
            $('#deliveryRejectMessge').show();
            jQuery(".warningPopup").colorbox({inline: true, slideshow: false, scrolling: false, height: "auto", open: true, width: 'auto', maxWidth: '100%', onComplete: function () {
                    $('#rejectMessage').focus();
<?php if ($this->config->item('is_touch') == 'enable') { ?>
                        jsKeyboard.currentElement = $('#rejectMessage');
<?php } ?>
                }
            });
            return false;
        } else {
            update(segment);
        }
    }
    function update(segment) {
        $.ajax({
            url: '<?php echo site_url("gkpos/report/update") ?>',
            data: {
                id: $('#OrderId').val(),
                online_status: $('#onlineStatus').val(),
                message: $('#rejectMessage').val(),
                delivery_time: $('#deliveryTime').val()
            },
            type: "POST",
            dataType: "json",
            success: function (output) {
                if (output.success) {
                    $.colorbox.close();
                    paginateReport(segment);
                } else {
                    paginateReport(segment);
                }
            }
        });
    }
    function deleteReportRow(id, segment) {
        $.confirm({
            'title': 'Order Delete Warning',
            'message': 'Are you sure? You want to delete this order ',
            'buttons': {
                'Yes': {
                    'class': 'btn btn-danger',
                    'action': function () {
                        $.ajax({
                            url: "<?php echo site_url('gkpos/report/deleteReportRow') ?>",
                            type: "POST",
                            dataType: "json",
                            data: {
                                id: id
                            },
                            success: function (response) {
                                if (response.status) {
                                    paginateReport(segment);
                                }
                            }
                        });
                    }
                },
                'NO': {
                    'class': 'btn btn-success',
                    'action': function () {}
                }
            }
        });
    }

    function showOnlineOrderDetail(action) {
        $.ajax({
            url: action,
            type: 'Get',
            success: function (response) {
                $('#popupContentHolder').html(response);
                if ($('#onlineOrderContent').length > 0) {
                    showpop('onlineOrderContent');
                }
            }
        });
    }

    function showpop(id) {
        $.colorbox({
            inline: true,
            scrolling: false,
            href: "#" + id,
            width: '90%',
            maxWidth: "100%",
            open: true,
            closeButton: false,
            transition: 'elastic',
            speed: '5000',
            onOpen: function () {
            },
            onLoad: function () {
                //console.log('on onLoad action');
            },
            onComplete: function () {
                //console.log('on onComplete action');
            },
            onCleanup: function () {
                //console.log('on cleandup action');
            },
            onClosed: function () {
                $('#popupContentHolder').html('');
            }

        });
    }
</script>
<style>
    .button .key{
        color:#000000;
    }
</style>
