<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage('delivery_list') ?></div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 pull-right">
                    <a class="btn btn-primary pull-right " id="open_online">
                        <?php print CommonLanguage::getMessage('online'); ?>
                    </a> 
                </div>
                <div class="col-md-12 m-t-10">
                    <div class="table-responsive">
                        <table class="table table-bordered inventory" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th><?php print CommonLanguage::getMessage('customer_name') ?></th>
                                    <th><?php print CommonLanguage::getMessage('address_details') ?></th>
                                    <th><?php print CommonLanguage::getMessage('mobile_phone') ?></th>
                                    <th><?php print CommonLanguage::getMessage('net_total') ?></th>
                                    <th style="width: 18%;"><?php print CommonLanguage::getMessage('Action') ?></th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
                <?php echo Forms::ModalLoader('deliveryDisabler'); ?>
            </div>
        </div>
    </div>
</div>
<!-- note modal (kitchen note, fees if takeaway and delivery ,time if delivery nd takeaway ) -->
<div class="modal fade" id="addNoteModal"  data-backdrop="static" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="text-left p-b-5"><span class="semi-bold"><?php print CommonLanguage::getMessage('print_kitchen') ?></span></h5>
            </div>
            <div class="modal-body">
                <div class="row">
                    <?php echo Forms::AlertDiv('kitchen_alert'); ?>
                    <div class="col-md-12 ">
                        <label><?php print CommonLanguage::getMessage('delivery_fees') ?></label>
                        <select id="shipping_id" class="full-width Account_Code " data-init-plugin="select2">
                            <option value="0"><?php print CommonLanguage::getMessage('free_delivery'); ?></option>
                            <option value="1000">1000</option>
                            <option value="2000">2000</option>
                            <option value="3000">3000</option>
                            <option value="4000">4000</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button data-dismiss="modal" class="btn btn-danger btn-cons"><i class="fa fa-times"></i> <?php print CommonLanguage::getMessage('Close') ?></button>
                <button id="submitDataKitchen"  class="btn bg-info btn-cons"><i class="fa fa-check"></i> <?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
            <?php echo Forms::ModalLoader('note_loader'); ?>
        </div>
    </div>
</div>
<div id="OnlineframeModal" class="modal  fade" tabindex="-1" role="dialog" style="z-index:10940; margin-left:44px;">
    <div class="modal-content">
        <div class="modal-body">
            <iframe id="checkNewFrame" src="" width="99.6%;" height="530" frameborder="0"></iframe>
        </div>
        <div class="modal-footer">
            <button class="btn bg-primary white" id="get_orders"><?php echo CommonLanguage::getMessage('get_online_orders');?></button>
            <button class="btn bg-default" data-dismiss= "modal"><?php echo CommonLanguage::getMessage('cancel');?></button>
        </div>
        <?php echo Forms::ModalLoader('online_loader'); ?>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('delivery_list')); ?>,
        smart_diner_server_url = '<?php echo SMART_DINER_URL; ?>',branch_id = '<?php echo SMARTDINER_BRANCH_ID;?>';
    $(function () {
        RebuildDataTable(title, [0,1,2,3,4], 'RestaurantAjax/delivery_list');
        $('#open_online').on('click',function(){
            $('#checkNewFrame').attr('src',smart_diner_server_url+'?is_pop=1');
            $('#OnlineframeModal').modal('show');
        });
        $('#get_orders').on('click',function(){
            $('#online_loader').removeClass('hidden')
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: 'RestaurantAjax/getOfflineIds',
                complete: function (data, vv) {
                    if (vv == "success") {
                        var response = data.responseJSON;
                        if($.type(response) == 'object'){
                            getOnlineOrders(response);
                        }
                    }
                }
            });
        });
        $('#tableWithSearch').on('click','.print_order',function(){
            var invoice_id = $(this).attr('data-id');
            $('#deliveryDisabler').removeClass('hidden')
            $.ajax({
                url: "POSAjax/reprintOrder",
                data: {Invoice_ID_Auto: invoice_id, pending_status: 3, is_edited_invoice: 0, invoice_type: 1},
                methd: "POST",
                dataType: "json",
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        var response = v1.responseJSON;
                        if (response[0] == 'OK') {
                            toastr.success('<?php echo CommonLanguage::getMessage('order_submitted_kitchen');?>')
                            $('#deliveryDisabler').addClass('hidden')
                        }
                    }
                }
            });
        });
        $('#tableWithSearch').on('click','.generate_invoice',function(){
            var invoice_id = $(this).attr('data-id');
            $('#deliveryDisabler').removeClass('hidden')
            $.ajax({
                url: "POSAjax/generateInvoice",
                data: {Invoice_ID_Auto: invoice_id},
                methd: "POST",
                dataType: "json",
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        var response = v1.responseJSON;
                        if (response[0] == 'OK') {
                            RebuildDataTable(title, [0,1,2,3,4], 'RestaurantAjax/delivery_list');
                            toastr.success('<?php echo CommonLanguage::getMessage('invoice_generated');?>')
                            $('#deliveryDisabler').addClass('hidden')
                        }
                    }
                }
            });
        });
    });
    function getOnlineOrders(data_ids){
        $.ajax({
            dataType: 'json',
            url: smart_diner_server_url + 'sync/get_order_data',
            data: {branch_id:branch_id,order_id:data_ids['order_id'],
                   account_id:data_ids['account_id'],address_id:data_ids['address_id']},
            complete: function (data, vv) {
                if (vv == "success") {
                    var response = data.responseJSON
                    if($.type(response) == 'object' && Object.keys(response).length == 3){
                        postOnlineInvoice(data.responseJSON);
                    }
                }
            }
        });
    }
    function postOnlineInvoice(data){
        $.ajax({
            url: "POSAjax/ajaxOnlineOrders",
            method: "POST",
            data: data,
            dataType: "json",
            complete: function (v1, v2) {
                if (v2 == 'success' ) {
                    if( v1.responseJSON[0] == 'OK'){
                        hideModalAlert('OnlineframeModal');
                        RebuildDataTable(title, [0,1,2,3,4], 'RestaurantAjax/delivery_list');
                    }
                    $('#online_loader').addClass('hidden');
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) { 
                $('#online_loader').addClass('hidden');
                alert("Status: " + textStatus); alert("Error: " + errorThrown); 
            }  
        });
    }
</script>