<?php
$end_day_check_fields = smc_session()['default_setting']['end_of_day_auto_checked_fields'] != '' ? explode(",", smc_session()['default_setting']['end_of_day_auto_checked_fields']) : [];
$end_day_hide_fields = smc_session()['default_setting']['end_of_day_hide_checkbox_fields'] != '' ? explode(",", smc_session()['default_setting']['end_of_day_hide_checkbox_fields']) : [];
//echo(print_r($end_day_check_fields));
preg_match("/\/Inventory\b/i", $_SERVER['REQUEST_URI'], $match);
$is_inventory = count($match) > 0 ? true : false;
?>
<div class="modal fade" id="confirm_end_of_day" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label bold"><?php echo CommonLanguage::getMessage('end_of_day'); ?></h5>
            </div>
            <form id="confirm-end-day-form" method="POST" onsubmit="return false" >
                <div class="modal-body">
                    <div class="row"><?php echo Forms::AlertDiv('end_of_day_alert'); ?></div>
                    <div class="row">
                        <div id="last_end_of_day" class="red-star"></div>
                        <div id="end_day_total_cash" class="red-star <?php echo $permissions->hasPermission($is_inventory ? 'view_total_cash_end_day' : 'view_total_cash_pos_end_day') ? '' : 'hidden'?>"></div>
                        <div id="second_currency_end_day_total_cash" class="red-star <?php echo  smc_session()['default_setting']['activate_second_currency'] == 1 ? '':'hidden'; ?> <?php echo $permissions->hasPermission($is_inventory ? 'view_total_cash_end_day' : 'view_total_cash_pos_end_day') ? '' : 'hidden'?>"></div>
                        <div id="end_day_total_sales" class="red-star <?php echo $permissions->hasPermission($is_inventory ? 'view_total_sales_end_day' : 'view_total_sales_pos_end_day') ? '' : 'hidden'?>"></div>
                    </div>
                    <div class="row form-group">
                        <!--                        <div class="col-md-12" >-->
                        <div class="col-md-3 <?php echo in_array('0', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="with_category" <?php echo in_array('0', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="with_category"><?php echo CommonLanguage::getMessage('with_category') ?></label>
                            </div>
                        </div>
                        <div class="col-md-3 <?php echo in_array('1', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="with_sold_items"  <?php echo in_array('1', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="with_sold_items"><?php echo CommonLanguage::getMessage('sold_per_date') ?></label>
                            </div>
                        </div>
                        <?php if ($permissions->hasPermission('view_manager_total_credit_card') || $permissions->hasPermission('view_pos_total_credit_card')) { ?>
                            <div class="col-md-3  <?php echo in_array('8', $end_day_hide_fields) ? 'hidden' : '' ?>">
                                <div class="checkbox check-info">
                                    <input type="checkbox"  id="with_total_credits" <?php echo in_array('8', $end_day_check_fields) ? 'checked' : '' ?>>
                                    <label for="with_total_credits"><?php echo CommonLanguage::getMessage('total_credit_card') ?></label>
                                </div>
                            </div>
                        <?php } ?>
                        <div class="col-md-3 <?php echo in_array('2', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="with_void_items"  <?php echo in_array('2', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="with_void_items"><?php echo CommonLanguage::getMessage('void_items') ?></label>
                            </div>
                        </div>
                        <!--                        </div>-->
                    </div> 
                    <div class="row form-group">
                        <div class="col-md-3 <?php echo in_array('3', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="with_discounted_items"  <?php echo in_array('3', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="with_discounted_items"><?php echo CommonLanguage::getMessage('discounted_items') ?></label>
                            </div>
                        </div>
                        <div class="col-md-3 <?php echo in_array('9', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="empty_category"  <?php echo in_array('9', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="empty_category"><?php echo CommonLanguage::getMessage('empty_category') ?></label>
                            </div>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-6">
                            <label for="is_date_checked"><?php echo CommonLanguage::getMessage('Date') ?></label>
                            <input type="checkbox"  id="is_date_checked"  <?php echo in_array('10', $end_day_check_fields) ? 'checked' : '' ?>>
                            <input name="end_of_day_range" id="end_of_day_range" class="form-control <?php echo (in_array('10', $end_day_hide_fields) && in_array('10', $end_day_check_fields)) ? '' : (in_array('10', $end_day_hide_fields) ? 'hidden' : ''); ?>" required />
                        </div>
                        <div class="col-md-6 <?php echo in_array('11', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <label><?php echo CommonLanguage::getMessage('branch'); ?></label>
                            <input type="hidden" name="end_day_branch_id" id="end_day_branch_id" class="full-width"/>
                        </div>
                    </div>

                    <div class="row form-group">
                        <div class="col-md-3 <?php echo in_array('4', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="with_invoices"  <?php echo in_array('4', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="with_invoices"><?php echo CommonLanguage::getMessage('with_invoices') ?></label>
                            </div>
                        </div>
                        <div class="col-md-3 <?php echo in_array('5', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="with_user"  <?php echo in_array('5', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="with_user"><?php echo CommonLanguage::getMessage('by_user') ?></label>
                            </div>
                        </div>
                        <div class="col-md-3 <?php echo in_array('6', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="per_day_print"  <?php echo in_array('6', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="per_day_print"><?php echo CommonLanguage::getMessage('per_day_print') ?></label>
                            </div>
                        </div>
                        <div class="col-md-3 <?php echo in_array('7', $end_day_hide_fields) ? 'hidden' : '' ?>">
                            <div class="checkbox check-info">
                                <input type="checkbox"  id="with_unpaid_invoices"  <?php echo in_array('7', $end_day_check_fields) ? 'checked' : '' ?>>
                                <label for="with_unpaid_invoices"><?php echo CommonLanguage::getMessage('with_unpaid_invoices') ?></label>
                            </div>
                        </div>
                    </div>
                    <div class="row form-group">

                    </div>
                </div>
                <div class="modal-footer">
                    <?php if ($permissions->hasPermission($is_inventory ? 'save_inv_end_of_day' : 'save_pos_end_of_day')) { ?>
                        <button  class="btn btn-info width-auto pull-left save_end_of_day save" id="save_end_of_day" ><?php print CommonLanguage::getMessage('Save') ?></button>
                    <?php }if ($permissions->hasPermission($is_inventory ? 'save_print_inv_end_of_day' : 'save_print_pos_end_of_day')) { ?>

                        <button  class="btn btn-info width-auto pull-left save_end_of_day save_print" ><?php print CommonLanguage::getMessage('save_print') ?></button>
                    <?php }if ($permissions->hasPermission($is_inventory ? 'view_inv_end_of_day' : 'view_pos_end_of_day')) { ?>

                        <button  class="btn btn-info width-auto print_end_of_day " data-type="1" ><?php print CommonLanguage::getMessage('view') ?></button>
                    <?php }if ($permissions->hasPermission($is_inventory ? 'print_inv_end_of_day' : 'print_pos_end_of_day')) { ?>

                        <button  class="btn btn-info width-auto  print_end_of_day"  data-type="2" ><?php print CommonLanguage::getMessage('print') ?></button>
                    <?php } ?>
                </div>
            </form>
            <?php echo Forms::ModalLoader('end_of_day_loader'); ?>
        </div>
    </div>
</div>
<button type="" class="openFrame hidden" id="view_end_of_day_frame" data-src=""/>
<script>
    var default_start_time = <?php echo json_encode(smc_session()['default_setting']['default_start_time']); ?>;
    var is_actual = <?php echo json_encode(!empty($is_actual) ? 1 : 0); ?>;
    $(function () {
        build_select2($("#end_day_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");
        var default_end_time = subtractFromTime(default_start_time, 1, 'seconds'),
                twelve_hours = subtractFromTime(default_start_time, 12, 'hours');
        $("#is_date_checked").on("click", function () {
            if ($(this).is(":checked"))
                $("#end_of_day_range").removeClass("hidden")
            else
                $("#end_of_day_range").addClass("hidden")
        })
        $("input[name=end_of_day_range]").daterangepicker({
            timePicker: true,
            locale: {
                format: 'YYYY-MM-DD HH:mm:ss'
            },
            startDate: moment(moment().format('YYYY-MM-DD') + ' ' + default_start_time),
            endDate: moment(moment().add(1, 'days').format('YYYY-MM-DD') + ' ' + default_end_time),
            ranges: {
                'Today': [moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment().add(1, 'days').format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Yesterday': [moment(moment().subtract(1, 'days').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Last 7 Days': [moment(moment().subtract(6, 'days').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Last 30 Days': [moment(moment().subtract(29, 'days').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment(new Date().getTime()).format('YYYY-MM-DD') + ' ' + default_end_time)],
                'This Month': [moment(moment().startOf('month').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment().endOf('month').format('YYYY-MM-DD') + ' ' + default_end_time)],
                'Last Month': [moment(moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD') + ' ' + default_start_time),
                    moment(moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD') + ' ' + default_end_time)]
            }
        });
        $(".print_end_of_day").on("click", function () {
            myurl = "Inventory/pos_view_end_day?end_of_day_range=" + $("#end_of_day_range").val() + "&with_inv=" + ($("#with_invoices").is(":checked") ? 1 : 0) + "&with_user=" + ($("#with_user").is(":checked") ? 1 : 0) + "&with_category=" + ($("#with_category").is(":checked") ? 1 : 0) + "&with_sold_items=" + ($("#with_sold_items").is(":checked") ? 1 : 0) + "&with_total_credits=" + ($("#with_total_credits").is(":checked") ? 1 : 0) + '&template_title=' + $('#confirm_end_of_day .add-label').html() + '&per_day_print=' + ($("#per_day_print").is(":checked") ? 1 : 0) + '&is_actual=' + (is_actual) + '&type_preview=' + $(this).attr('data-type') + '&with_unpaid_invoices=' + ($("#with_unpaid_invoices").is(":checked") ? 1 : 0) + '&with_void_items=' + ($("#with_void_items").is(":checked") ? 1 : 0) + '&with_discounted_items=' + ($("#with_discounted_items").is(":checked") ? 1 : 0) + '&empty_category=' + ($("#empty_category").is(":checked") ? 1 : 0) + '&is_date_checked=' + ($("#is_date_checked").is(":checked") ? 1 : 0) + '&branch_id=' + $('#end_day_branch_id').val();
            if ($(this).attr('data-type') == 2) {
                printPOSExternal(myurl)
            } else {
                $('#view_end_of_day_frame').attr('data-src', myurl + '&is_pop=1');
                $('#view_end_of_day_frame').trigger('click');
            }
        });
        $(".save_end_of_day").on("click", function () {
            //            var with_print = $(this).hasClass("save_print") ? 1 : 0;
            var with_print = $(this).hasClass("save_print");
            $('#end_of_day_loader').removeClass('hidden')
            $.ajax({
                url: "POSAjax/end_of_day",
                data: {is_actual: is_actual || 0, branch_id: $('#end_day_branch_id').val()},
                method: "POST",
                dataType: 'JSON',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        if (v1.responseJSON[0] === 'OK') {
                            AddAlert("#end_of_day_alert", Submitted_Successfully, 'success');
                            //                            if (with_print == 1) {
                            //                                myurl = "Inventory/pos_view_end_shift/" + v1.responseJSON[1];
                            //                                printPOSExternal(myurl)
                            //                            }
                            if (with_print) {
                                //                                            myurl = "Inventory/pos_view_end_day?end_of_day_range=" + $("#end_of_day_range").val() + "&with_inv=" + ($("#with_invoices").is(":checked") ? 1 : 0) + "&with_user=" + ($("#with_user").is(":checked") ? 1 : 0) + "&with_category=" + ($("#with_category").is(":checked") ? 1 : 0) + "&with_sold_items=" + ($("#with_sold_items").is(":checked") ? 1 : 0) + "&with_total_credits=" + ($("#with_total_credits").is(":checked") ? 1 : 0) + '&template_title=' + $('#confirm_end_of_day .add-label').html() + '&per_day_print=' + ($("#per_day_print").is(":checked") ? 1 : 0) + '&is_actual=' + (is_actual) + '&type_preview=' + $(this).attr('data-type') + '&with_unpaid_invoices=' + ($("#with_unpaid_invoices").is(":checked") ? 1 : 0)+ '&with_void_items=' +($("#with_void_items").is(":checked") ? 1 : 0) + '&with_discounted_items=' + ($("#with_discounted_items").is(":checked") ? 1 : 0);

                                myurl = "Inventory/pos_view_end_day?shift_id=" + v1.responseJSON[1] + '&type_preview=2' + "&with_inv=" + ($("#with_invoices").is(":checked") ? 1 : 0) + "&with_user=" + ($("#with_user").is(":checked") ? 1 : 0) + "&with_category=" + ($("#with_category").is(":checked") ? 1 : 0) + "&with_sold_items=" + ($("#with_sold_items").is(":checked") ? 1 : 0) + "&with_total_credits=" + ($("#with_total_credits").is(":checked") ? 1 : 0) + '&template_title=' + $('#confirm_end_of_day .add-label').html() + '&per_day_print=' + ($("#per_day_print").is(":checked") ? 1 : 0) + '&is_actual=' + (is_actual) + '&with_unpaid_invoices=' + ($("#with_unpaid_invoices").is(":checked") ? 1 : 0) + '&with_void_items=' + ($("#with_void_items").is(":checked") ? 1 : 0) + '&with_discounted_items=' + ($("#with_discounted_items").is(":checked") ? 1 : 0) + '&empty_category=' + ($("#empty_category").is(":checked") ? 1 : 0) + '&branch_id=' + $('#end_day_branch_id').val();
                                printPOSExternal(myurl)
                            }
                            setTimeout(function () {
                                $("#FunctionsModal,#confirm_end_of_day").modal("hide")
                            }, 1500)
                        } else {
                            AddAlert("#end_of_day_alert", v1.responseJSON[1], 'danger');
                        }
                        $('#end_of_day_loader').addClass('hidden')
                    }
                }
            })
        });
    })
    function printPOSExternal(url, data) {
        $("#print_iframe").remove();
        if (url == undefined || url == false || url == null || url == '')
            url = 'about::blank';
        $("<iframe id='print_iframe' style='position:absolute;top:0px;left:0;width:0px;height:0px;opacity:0;' src='" + url + "' ></iframe>").appendTo("body");
        if (data !== undefined) {
            $("#print_iframe")[0].contentWindow.document.write(data);
            $("#print_iframe")[0].contentWindow.document.close();   //important to make close or window load as example will not triggered
        }
    }
</script>
