<style>
    .actionButton {
        background-color: #4c0870!important;
color:#ed9421 !important;    }
</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title">
                <h4 class="bold"><?php echo CommonLanguage::getMessage($title); ?> </h4>
            </div>
            <div class="row">
                <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('choose_customer') ?></label>
                    <input id="customer_id" type="hidden" class="full-width" name="customer_id" >
                </div>
                <?php if ($permissions->hasPermission('add_job_order_stage')) { ?>
                <div class="col-md-3 pull-right">    
                    <div class="floatingContainer ">
                        <a href="Inventory/job_order" >
                            <div class="actionButton" >
                                <p class="floatingText"><span class="floatingTextBG"><?php echo CommonLanguage::getMessage('add') ?></span></p>
                            </div>
                        </a>
                    </div>
                </div>
                <?php } ?>
            </div>
        </div>
        <div class="panel-body">
            <div class="row ">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:5%" >#</th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('customer_name'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('company'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('print_ref'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('confirm_date'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('person_in_charge'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Delivery_Date'); ?></th>
                                <th class="text-center no-print" style="width:10%" ><?php print CommonLanguage::getMessage('Action'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="change_job_order_stage" 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 text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="bold"><?php echo CommonLanguage::getMessage('change_job_order_stage'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('job_order_stage_alert'); ?></div>
                <div class="row form-group">
                    <div class="col-md-12">
                        <input type="hidden" name="job_order_id" id="job_order_id" />
                        <label><?php print CommonLanguage::getMessage('stage') ?></label>
                        <select class="full-width" id="stage_id" data-init-plugin="select2">
                            <option value="0" ><?php echo CommonLanguage::getMessage('clear_stage') ?></option>
                            <?php foreach($job_order_stages as $stage){?>
                            <option value="<?php echo $stage['stage_id'] ?>" ><?php echo $stage['stage_title']?></option>
                            <?php }?>
                        </select> 
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger text-white " data-dismiss="modal" ><?php print CommonLanguage::getMessage('Close') ?></button>
                <button type="button" class="btn  btn-info text-white " id="submit_job_order_stage" ><?php print CommonLanguage::getMessage('Save') ?></button>
            </div>
            <?php echo Forms::ModalLoader('change_job_order_stage_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function(){
        var data_session = get_storage_element(page_link);

        if (data_session.person_name != '') {
            $("#customer_id").val(data_session.customer_id).attr("data-select-name", data_session.person_name)
        }
        if (data_session.Date != '') {
            window.startDate = data_session.from_date;
            window.endDate = data_session.to_date;
            $("input[name=From_To_Date]").val(data_session.Date)
        }
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id');
            IncludeDeleteModal(common_delete_modal, {job_order_id: entry_value}, function () {
                ChangeTable()
            }, "InventoryAjax/deleteJobOrder");
        }); 
        $('#tableWithSearch').on('click', '.change_stage', function () {
            $('#job_order_id').val($(this).parents('div.Action').attr('data-id'))
            $('#stage_id').val($(this).attr('data-id')).select2()
        });
        $("#submit_job_order_stage").on("click", function () {
            $("#change_job_order_stage_loader").removeClass("hidden")
            $.ajax({
                url: "InventoryAjax/updateJobOrderStage",
                method: "POST",
                dataType:"json",
                data: {job_order_id: $("#job_order_id").val(), stage_id: $("#stage_id").val()},
                complete: function (jqXHR, v2) {
                    if (v2 == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            ChangeTable();
                            hideModalAlert('change_job_order_stage', 'job_order_stage_alert');
                        }
                        AddAlert("#job_order_stage_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    } 
                    $("#change_job_order_stage_loader").addClass("hidden")
                }
            })
        });
        build_select2($('#customer_id'), 'xhr/getOrganizations');
        ChangeTable();
        $("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: false,
            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'));
            ChangeTable();
        });
        $("input[name=From_To_Date]").on('cancel.daterangepicker', function (ev, picker) {
            window.startDate = ''
            window.endDate = ''
            $(this).val('');
            ChangeTable();
        });
    });
    function ChangeTable() {
        var Data = {
            customer_id: $('#customer_id').val(),
            person_name: ($("#customer_id").select2('data') == null ? '' : $("#customer_id").select2('data').text),
            Date: $("input[name=From_To_Date]").val(),
            from_date: window.startDate,
            to_date: window.endDate,
        }
        set_storage_element(page_link, Data)

        RebuildDataTable($('.panel-title h4').html(), [0, 1, 2, 3, 4, 5, 6], "InventoryAjax/JobOrderList", Data, '', '');

    }
</script>