@extends('layouts.app')
@section('title', 'Running Text')
@section('content')

<!-- Content Header (Page header) -->
<section class="content-header">
  <h1>
    Running Text
    <small>Listing Teks Berjalan</small>
  </h1>
  <ol class="breadcrumb">
    <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
    <li class="active">Running Text</li>
  </ol>
</section>

<!-- Main content -->
<section class="content">
	<!-- Info boxes -->
	<div class="row">
		<div class="col-md-12">
			<div class="box">
				<div class="box-header with-border">
					<h3 class="box-title"><i class="fa fa-list"></i> Running Text</h3>
				</div>
				<div class="box-body">
					<a href="{{route('running.create')}}" class="btn btn-sm btn-success modal-show" title="Form Tambah Running Text"><i class="fa fa-plus"> Tambah</i></a>
          <hr>
          <div class="table-responsive">
  					<table id="datatables" class="table table-striped table-bordered table-hover">
  						<thead>
  							<tr>
                                <th>No</th>
                                <th>Action</th>
  								<th>Title</th>
                                <th>Text</th>
  								<th>Status</th>
  								<th>Tanggal Dibuat</th>
  								<th>Tanggal Update</th>
  							</tr>
  						</thead>
  						<tbody>
  						</tbody>
  					</table>
          </div>
				</div>
			</div>
		</div>
	</div>
</section>

@endsection

@push('scripts')
<script type="text/javascript">
$(document).ready(function(){
	var dataTable = $('#datatables').DataTable({
        "stateSave": true,
        "processing" : true,
        "serverSide" : true,
        "searching" : true,
        "ajax" : {
        "url": "{{ route('running.json') }}",
        "dataType": "json",
        "type": "GET",
        "data": {
            _token: "{{csrf_token()}}"
        }
        },
        "columns": [
            { data: 'DT_RowIndex', name: 'DT_RowIndex' },
            { "data": "options" },
            { "data": "title" },
            { "data": "text" },
            { "data": "status" },
            { "data": "created_at" },
            { "data": "updated_at" }
        ],
        "columnDefs": [
            { "width": "2%", "targets": 0 },
            { "width": "15%", "targets": 1 },
            { "width": "50%", "targets": 2 },
        ],
    });

    $('body').on('click', '.modal-show', function (event) {
        event.preventDefault();
        var me = $(this),
            url = me.attr('href'),
            title = me.attr('title');

        $('#modal-title').text(title);
        $('#modal-btn-save').removeClass('hide').text(me.hasClass('edit') ? 'Ubah' : 'Simpan');

        $.ajax({
        url: url,
        dataType: 'html',
        success: function(response){
            $('#modal-body').html(response);
        }
        });

        $('#modal').modal('show');
    });

    $('body').on('click', '.btn-komentar', function (event) {
        event.preventDefault();
        var me = $(this),
            url = me.attr('href'),
            title = me.attr('title');

        $('#modal-title').text(title);
        $('#modal-btn-save').removeClass('hide').text('Simpan');

        $.ajax({
        url: url,
        dataType: 'html',
        success: function(response){
            $('#modal-body').html(response);
        }
        });

        $('#modal').modal('show');
    });

    $('body').on('click', '.btn-show', function (event) {
        event.preventDefault();
        var me = $(this),
            url = me.attr('href'),
            title = me.attr('title');

        $('#modal-title').text(title);
        $('#modal-btn-save').addClass('hide');

        $.ajax({
        url: url,
        dataType: 'html',
        success: function(response){
            $('#modal-body').html(response);
        }
        });

        $('#modal').modal('show');
    });

    $('body').on('click', '#modal-btn-save', function(e){
        e.preventDefault();
        $('.preloader').fadeIn();
        clearTimeout(timeout);

        var form = $('#modal-body form'),
            url = form.attr('action'),
            method = $('input[name=_method]').val() == undefined ? 'POST' : 'PUT';

        form.find('.help-block').remove();
        form.find('.form-group').removeClass('has-error');
        $.ajax({
        url: url,
        method: method,
        dataType: "JSON",
        data: form.serialize(),
        success: function(response){
            timeout = setTimeout(loaded, 1000);
            if(response.code === 200){
                form.trigger('reset');
                $('#modal').modal('hide');
                $('#datatables').DataTable().ajax.reload();

                swal({
                    type: 'success',
                    icon: 'success',
                    title: 'Berhasil!',
                    text: response.msg,
                });
            }else{
                swal({
                    type: 'error',
                    icon: 'error',
                    title: 'Gagal!',
                    text: response.msg,
                });
            }
        },
        error: function(xhr){
            timeout = setTimeout(loaded, 1000);
            var res = xhr.responseJSON;
            if($.isEmptyObject(res) == false) {
            $.each(res.errors, function (key, value) {
                $('#' + key)
                    .closest('.form-group')
                    .addClass('has-error')
                    .append('<span class="help-block"><strong>' + value + '</strong></span>');
            });
            }
        }
        });
    });

    $('body').on('change', '#status', function(e){
        e.preventDefault();
        var id = $(this).attr('data-id');

        swal({
            title: "Anda Yakin?",
            text: "Anda yakin untuk mengganti status running text ini ?",
            icon: "warning",
            buttons: [
                'Tidak, batalkan!',
                'Ya, ganti!'
            ],
            dangerMode: true,
        }).then(function(isConfirm) {
            if (isConfirm) {
                $('.preloader').fadeIn();
                clearTimeout(timeout);
                $.ajax({
                    type: "POST", // Method pengiriman data bisa dengan GET atau POST
                    url: "{{ route('running.update_status') }}", // Isi dengan url/path file php yang dituju
                    data: {
                        _token: "{{csrf_token()}}",
                        id: id,
                    }, // data yang akan dikirim ke file yang dituju
                    dataType: "json",
                    success: function(response){
                    timeout = setTimeout(loaded, 1000);
                        if(response.code === 200){
                            $('#datatables').DataTable().ajax.reload();
                            swal({
                                type: 'success',
                                icon: 'success',
                                title: 'Berhasil!',
                                text: response.msg,
                            });
                        }else{
                            swal({
                                type: 'error',
                                icon: 'error',
                                title: 'Gagal!',
                                text: response.msg,
                            });
                        }
                    },
                    error: function(xhr){
                    timeout = setTimeout(loaded, 1000);
                        swal({
                            type: 'error',
                            icon: 'error',
                            title: 'Gagal!',
                            text: xhr.statusText,
                        });
                    }
                });
            } else {
                swal("Dibatalkan", "Status running text batal diupdate! :)", "error");
            }
        });
    });

    $('body').on('click', '#destroy', function(e){
        e.preventDefault();
        var id = $(this).attr('data-id');
        swal({
        title: "Anda Yakin?",
        text: "Anda yakin untuk menghapus running text ini?",
        icon: "warning",
        buttons: [
            'Tidak, batalkan!',
            'Ya, hapus!'
        ],
        dangerMode: true,
        }).then(function(isConfirm) {
            if (isConfirm) {
                $('.preloader').fadeIn();
                clearTimeout(timeout);
                $.ajax({
                    type: "POST", // Method pengiriman data bisa dengan GET atau POST
                    url: "{{ route('running.remove') }}", // Isi dengan url/path file php yang dituju
                    data: {
                        _token: "{{csrf_token()}}",
                        id: id,
                    }, // data yang akan dikirim ke file yang dituju
                    dataType: "json",
                    success: function(response){
                        timeout = setTimeout(loaded, 1000);
                        if(response.code === 200){
                            $('#datatables').DataTable().ajax.reload();
                            swal({
                                type: 'success',
                                icon: 'success',
                                title: 'Berhasil!',
                                text: response.msg,
                            });
                        }else{
                            swal({
                                type: 'error',
                                icon: 'error',
                                title: 'Gagal!',
                                text: response.msg,
                            });
                        }
                    },
                    error: function(xhr){
                        timeout = setTimeout(loaded, 1000);
                        swal({
                            type: 'error',
                            icon: 'error',
                            title: 'Gagal!',
                            text: xhr.statusText,
                        });
                    }
                });
            } else {
                swal("Dibatalkan", "Running text batal dihapus! :)", "error");
            }
        });
    });
});
</script>
@endpush
