<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php echo $title; ?></div>
            <?php if ($permissions->hasPermission("add_route_schedule")) {
    echo Forms::FloatingAddButton('4c0870', '#route_schedule_modal', CommonLanguage::getMessage('add'), 'new_route_schedule'); }?>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered general-table" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('routes') ?></th>
                            <th><?php print CommonLanguage::getMessage('is_one_time') ?></th>
                            <th><?php print CommonLanguage::getMessage('Date') ?></th>
                            <th><?php print CommonLanguage::getMessage('salesman') ?></th>
                            <th><?php print CommonLanguage::getMessage('user') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="route_schedule_modal" data-backdrop="static" tabindex="-1" role="dialog" 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 add-label h-labels hidden"><?php echo CommonLanguage::getMessage('add_route_schedule'); ?></h5>
                <h5 class="bold edit-label h-labels hidden"><?php echo CommonLanguage::getMessage('edit_route_schedule'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('route_schedule_alert'); ?></div>
                <form id="route_schedule_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='schedule_id' id='schedule_id'/>
                    <div class="row form-group">
                        <div class=" col-md-12  " >
                            <label class="labelText">
                                <?php print CommonLanguage::getMessage('routes'); ?>
                                <span class="red-star">*</span>
                            </label>
                            <input type="hidden" id="route_ids" name="route_ids" class="full-width " required />
                        </div>
                    </div> 
                    <div class="row form-group">
                        <div class=" col-md-12  " >
                            <label class="labelText">
                                <?php print CommonLanguage::getMessage('Date'); ?>
                                <span class="red-star">*</span>
                            </label>
                            <input type="text" id="schedule_date" name="schedule_date" class="form-control " required />
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class=" col-md-12  " >
                            <input type="checkbox" id="is_one_time" name="isonetime"/>
                            <label class="labelText" for="is_one_time">
                                <?php print CommonLanguage::getMessage('is_one_time'); ?>
                            </label>
                            <span class="fa fa-info-circle" title="<?php echo CommonLanguage::getMessage('one_time_hint');?>" style="cursor:pointer;"></span>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class=" col-md-12  " >
                            <label class="labelText">
                                <?php print CommonLanguage::getMessage('salesman'); ?>
                            </label>
                            <input type="hidden" id="employee_id" name="employee_id" class="full-width " />
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class=" col-md-12  " >
                            <label class="labelText">
                                <?php print CommonLanguage::getMessage('user'); ?>
                            </label>
                            <input type="hidden" id="user_id" name="user_id" class="full-width "  />
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-primary-darker white" id="submit_route_schedule"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('route_schedule_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#employee_id"), "xhr/employeeSearch");
        build_select2($("#user_id"), "xhr/CommonSearch?col_id=User_ID&col_name=User_Name&table=tbl_common_users");
        build_select2($("#route_ids"), "xhr/commonSearch?table=tbl_route&col_id=route_id&col_name=route_name",1);
        $('#schedule_date').datepicker({autoclose:true});
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'GeneralAjax/route_schedule_list');
        $('#tableWithSearch').on('click', '.delete', function () {
            var schedule_id = $(this).parents('div.Action').attr('data-id');
            IncludeDeleteModal(common_delete_modal, {schedule_id: schedule_id}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'GeneralAjax/route_schedule_list');
            }, "GeneralAjax/deleteRouteSchedule");
        });
        $(".new_route_schedule").on("click", function () {
            $("#schedule_id").val(0);
            $('#route_schedule_modal .add-label').removeClass('hidden');
            $('#schedule_date').datepicker('setDate',new Date());
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            var schedule_id = $(this).parents('div.Action').attr('data-id');
            $("#schedule_id").val(schedule_id);
            $('#route_schedule_modal .edit-label').removeClass('hidden');
            $("#route_name").val($(this).parents('tr').find('td:eq(0)').html());
            CommonViewById("GeneralAjax/getRouteSchedule",{schedule_id:schedule_id},'#route_schedule_loader',function(response){
                var main_response = response['main'];
                $('#schedule_date').datepicker('setDate',main_response['schedule_date']);
                $('#employee_id').val(main_response['employee_id']).attr('data-select-name',main_response['employee']).trigger('change');
                $('#user_id').val(main_response['user_id']).attr('data-select-name',main_response['User_Name']).trigger('change');
                $('#is_one_time').prop('checked',main_response['is_one_time']== 1 ? true :false);
                if('details' in response){
                    $('#route_ids').select2('val', []);
                    $('#route_ids').data().select2.updateSelection(response['details']);
                }

            })
        });
        $("#submit_route_schedule").on("click", function () {
            if (!$("#route_schedule_form").valid())
                return;
            var Data = new FormData(document.getElementById("route_schedule_form"));
            Data.append('is_one_time',$('#is_one_time').is(":checked") ? 1: 0);
            if(($('#employee_id').val() && $('#user_id').val()) || (!$('#employee_id').val() && !$('#user_id').val())){
                toastr.error('<?php echo CommonLanguage::getMessage('choose_either_user_salesman');?>');
                return;
            }
            $("#route_schedule_loader").removeClass('hidden');
            $.ajax({
                url: "GeneralAjax/submitRouteSchedule",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'GeneralAjax/route_schedule_list');
                            hideModalAlert('route_schedule_modal', 'route_schedule_alert');
                        }
                        AddAlert("#route_schedule_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#route_schedule_loader").addClass('hidden');
                }
            });
        });
    });

</script>