<div class="wrapper">
    <div class="panel invoice">
        <input type="hidden" name="is_quotation" id="is_quotation" value="<?php echo $IsQuotation ? 1 : 0; ?>" />
        <div class="panel-title col-md-12">
            <div class="row">
                <h4 class="bold"><?php print CommonLanguage::getMessage($IsQuotation ? ($IsSales ? 'sales_quotation_list' : 'purchase_quotation_list') : ($is_actual ? ($IsSales ? 'actual_sales_order_list' : 'actual_purchase_order_list') : ($IsSales ? 'sales_order_list' : 'purchase_order_list'))); ?> </h4>
            </div>
            <div class="row form-group">
                <div class="  col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Date'); ?></label>
                    <div class="input-group">
                        <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                        <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" autocomplete="off">
                    </div>
                </div>
                <div class="  col-md-3">
                    <label><?php echo CommonLanguage::getMessage('status'); ?></label>
                    <select required name="status_id" class="full-width status_id " id="status_id" data-init-plugin="select2">
                        <option value="0"><?php echo CommonLanguage::getMessage('All'); ?></option>
                        <option value="1" selected><?php echo CommonLanguage::getMessage('pending'); ?></option>
                        <option value="2"><?php echo CommonLanguage::getMessage('converted'); ?></option>
                    </select>
                </div>
                <div class="  col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Delivery_Date'); ?></label>
                    <div class="input-group">
                        <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                        <input id="From_To_Delivery_Date" type="text" class="form-control" name="From_To_Delivery_Date" autocomplete="off">
                    </div>
                </div>
                <div class="col-md-3 <?php echo $permissions->hasPermission('Branches') ? '' : 'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id" >
                </div>

            </div>
            <!-- <div class="row form-group">
                <div class="col-md-3  ">
                    <label><?php echo CommonLanguage::getMessage('search_note') ?></label>
                    <input id="search_note" type="hidden" class="full-width" name="search_note" >
                </div>
            </div> -->

        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 m-t-10 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch">
                        <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th class="text-center"><?php print CommonLanguage::getMessage($IsSales ? 'customer_name' : 'vendor_name'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('date_created'); ?></th>
                                <th class="text-center"><?php print CommonLanguage::getMessage('note'); ?></th>
                                <?php if (!$IsQuotation) { ?>
                                    <th class="text-center"><?php print CommonLanguage::getMessage('Delivery_Date'); ?></th>
                                <?php } ?>
                                <?php if (smc_session()['default_setting']['activate_postponed_sales_order'] == 1 && !$IsQuotation && $IsSales) { ?>
                                    <th class="text-center"><?php print CommonLanguage::getMessage('posponted_status'); ?></th>
                                    <th class="text-center"><?php print CommonLanguage::getMessage('posponted_date'); ?></th>
                                <?php } ?>
                                <th class="text-center"><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="sq_revesions" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <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>
                <h4 class="modal-title text-center"></h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div id="col-md-12">
                        <div class="table-responsive">
                            <table class="table table-hover demo-table-search " id="sq_revision_list" style="width:100%;">
                                <thead>
                                    <tr>
                                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('Date'); ?></th>

                                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('revision') . ' #'; ?></th>
                                        <th class="text-center" style="width:10%"><?php print CommonLanguage::getMessage('Action'); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var IsSales = <?php echo json_encode($IsSales ? 1 : 0); ?>,
        is_actual = <?php echo json_encode($is_actual ? 1 : 0); ?>,
        data_linked_to_app = <?php echo json_encode(CommonLanguage::getMessage('data_linked_to_app')); ?>,
        startDeliveryDate = endDeliveryDate = '';
    $(function() {
        build_select2($("#search_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");
        // build_select2($("#search_note"), "xhr/CommonSearch?col_name=order_description&col_id=Order_ID&table=tbl_purchase_order");

        var data_session = get_storage_element(page_link);
        if (data_session.Date != '') {
            window.startDate = data_session.from_date;
            window.endDate = data_session.to_date;
            window.startDeliveryDate = data_session.from_delivery_date;
            window.endDeliveryDate = data_session.to_delivery_date;
            $("input[name=From_To_Date]").val(data_session.Date).trigger('apply.daterangepicker')
        }
        if (data_session.status_id != '' && data_session.status_id != null) {
            $('#status_id').val(data_session.status_id).select2()
        } else {
            $('#status_id').val(1).select2()
        }
        $('#status_id,#search_branch_id').on('change', function() {
            AjaxReq();
        });
        AjaxReq();
        $("input[name=From_To_Date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            locale: {
                cancelLabel: 'Clear'
            },
            allowEmpty: true,
            autoUpdateInput: false
        });
        $("input[name=From_To_Date]").on('apply.daterangepicker', function(ev, picker) {
            window.startDate = picker.startDate.format('YYYY-MM-DD'),
                window.endDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
            AjaxReq();
        });
        $("input[name=From_To_Date]").on('cancel.daterangepicker', function(ev, picker) {
            window.startDate = ''
            window.endDate = ''
            $(this).val('');
            AjaxReq();
        });
        $("input[name=From_To_Delivery_Date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            locale: {
                cancelLabel: 'Clear'
            },
            allowEmpty: true,
            autoUpdateInput: false
        });
        $("input[name=From_To_Delivery_Date]").on('apply.daterangepicker', function(ev, picker) {
            window.startDeliveryDate = picker.startDate.format('YYYY-MM-DD'),
                window.endDeliveryDate = picker.endDate.format('YYYY-MM-DD');
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
            AjaxReq();
        });
        $("input[name=From_To_Delivery_Date]").on('cancel.daterangepicker', function(ev, picker) {
            window.startDeliveryDate = ''
            window.endDeliveryDate = ''
            $(this).val('');
            AjaxReq();
        });

        $("#tableWithSearch").on("click", ".go_to_invoice,.go_delivery,.go_order", function() {
            var type = '';
            if ($(this).hasClass('go_to_invoice'))
                type = IsSales ? 'inv' : 'pur';
            else if ($(this).hasClass('go_delivery'))
                type = IsSales ? 'dlc' : 'dls';
            else if ($(this).hasClass('go_order'))
                type = IsSales ? 'oin' : 'opr';
            else if ($(this).hasClass('go_noqty'))
                type = IsSales ? '' : 'pnq';
            $("#invoice_type_id").val('').attr('data-select-name', '')
            build_select2($("#invoice_type_id"), "xhr/invoice_type_search?type=" + type + "&is_actual=" + (is_actual ? 1 : 0));

        })
        $("#tableWithSearch").on("click", ".revision", function() {
            var sq_id = $(this).parents(".Action").attr("data-id");
            RebuildDataTable('<?php echo CommonLanguage::getMessage('revisions'); ?>', [0, 1, 2],
                'InventoryAjax/sales_quotatin_revision_ajax', {
                    id: sq_id
                }, '#sq_revision_list');
            $("#sq_revesions").modal("show")

        });
        $('#tableWithSearch').on('click', '.delete', function() {
            var entry_value = $(this).parents('div.Action').attr('data-id'),
                data_linked = $(this).attr('data-sale-app-id') == 0 ? '' : data_linked_to_app;
            IncludeDeleteModal(common_delete_modal, {
                Order_ID: entry_value,
                is_quotation: $('#is_quotation').val(),
                is_sales: IsSales
            }, function() {
                AjaxReq();
            }, "InventoryAjax/deleteQuotationOrder", data_linked);
        });
    });

    function AjaxReq() {
        var OrderDate = {
            IsSales: IsSales,
            Date: $("input[name=From_To_Date]").val(),
            is_quotation: $('#is_quotation').val(),
            from_date: window.startDate,
            to_date: window.endDate,
            from_delivery_date: window.startDeliveryDate,
            to_delivery_date: window.endDeliveryDate,
            is_actual: is_actual,
            status_id: $('#status_id').val(),
            search_branch_id: $('#search_branch_id').val()
        }
        set_storage_element(page_link, OrderDate)
        RebuildDataTable($('#page_main_header').html(), [0, 1, 2], 'InventoryAjax/quotationRequest', OrderDate);
    }
</script>
<?php
include 'Views/Inventory/general_invoice_type_modal.php';
