<style>
    #tableWithSearch thead tr >th{
        background-color: #4c0870;
        color: #fff;
    }
</style>
<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title; ?></div>
            <?php if ($permissions->hasPermission('add_station')) {
    echo Forms::FloatingAddButton('4c0870', '#ticketing_station_modal', CommonLanguage::getMessage('add'), 'add_ticketing_station'); } ?>
            <div class="clearfix"></div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('station_name') ?></th>
                            <th><?php print CommonLanguage::getMessage('station_location') ?></th>
                            <th><?php print CommonLanguage::getMessage('Depot') ?></th>
                            <th><?php print CommonLanguage::getMessage('Action') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="ticketing_station_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="add-label h-labels text-left bold hidden"> <?php echo CommonLanguage::getMessage('add_ticketing_station'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"> <?php echo CommonLanguage::getMessage('edit_ticketing_station'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="ticketing_station_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='ticketing_station_id' id='ticketing_station_id'/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('ticketing_station_alert'); ?>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('station_name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('station_name'); ?>" id="ticketing_station_name" name="ticketing_station_name" class="form-control " autocomplete="off" required>
                        </div>
                    </div> 
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('station_location'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text"  placeholder="<?php print CommonLanguage::getMessage('station_location'); ?>" id="ticketing_station_location" name="ticketing_station_location" class="form-control " autocomplete="off" required>
                        </div>
                    </div>  
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('Depot'); ?>
                                <span class="red-star">*</span>
                                <?php if($permissions->hasPermission('ListDepot')){?>
                                <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Stock/ListDepot?is_pop=1&is_modal_shown=1" style="color:#4c0870;cursor:pointer"></span>
                                <?php }?>
                            </label>
                            <input type="hidden" id="depot_id" name="depot_id" class="full-width" autocomplete="off" required>
                        </div>
                    </div> 
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button class="btn btn-complete" id="submit_ticketing_station"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('ticketing_station_loader'); ?>
        </div>
    </div>
</div>
<script>
    $(function () {
        build_select2($("#depot_id"), "xhr/depotSearch");
        RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'TicketingAjax/ticketing_station_list');
        $(".add_ticketing_station").on("click", function () {
            $('#ticketing_station_modal .add-label').removeClass('hidden')
            $("#ticketing_station_id").val(0);
        });
        $('#tableWithSearch').on('click', '.delete', function () {
            var entry_value = $(this).parents('div.Action').attr('data-id')
            IncludeDeleteModal(common_delete_modal, {ticketing_station_id: entry_value}, function () {
                RebuildDataTable($('.panel-heading .panel-title').html(), [0], 'TicketingAjax/ticketing_station_list');
            }, "TicketingAjax/delete_ticketing_station");
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            $('#ticketing_station_modal .edit-label').removeClass('hidden')
            var ticketing_station_id = $(this).parents('div.Action').attr('data-id');
            $("#ticketing_station_id").val(ticketing_station_id);
            CommonViewById('common_ajax/ViewByEntryId', {table: 'ticketing_station_list',entry_id:'ticketing_station_id',entry_value:ticketing_station_id},
                           '#ticketing_station_loader', function (response) {
                $('#ticketing_station_name').val(response[0]['ticketing_station_name']);
                $('#ticketing_station_location').val(response[0]['ticketing_station_location']);
                $('#depot_id').val(response[0]['depot_id']).attr('data-select-name',response[0]['Depot_Name']).trigger('change');
            });
        });
        $("#submit_ticketing_station").on("click", function () {
            if (!$("#ticketing_station_form").valid())
                return;
            var Data = new FormData(document.getElementById("ticketing_station_form"));
            $("#ticketing_station_loader").removeClass('hidden');
            $.ajax({
                url: "TicketingAjax/submit_ticketing_station",
                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], 'TicketingAjax/ticketing_station_list');
                            hideModalAlert('ticketing_station_modal', 'ticketing_station_alert');
                        }
                        AddAlert("#ticketing_station_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#ticketing_station_loader").addClass('hidden');
                }
            });
        });
    });
</script>