@extends('layouts.app')
@section('content')
<div class="main-header">
    <div class="admin-header">
        <div class="uk-grid">
            <div class="uk-width-1-2@m uk-text-center uk-text-left@l">
                <h1 class="uk-margin-small-top main-heading">{{$title}}</h1>
            </div>
            <div class="uk-width-1-2@m uk-text-center uk-text-right@l">
                <a class="uk-button uk-button-primary" href="{{route('admin.realtor-years.create')}}"> Add New Realtor Year</a>
            </div>
        </div>
    </div>
</div>
<div class="uk-overflow-auto">
    @include('partials.messages')
    <table class="uk-table uk-table-divider" id="realtor_years">
        <thead>
            <tr>  
                <th>Actions</th>
                <th>Ordering</th>
                <th>Name</th>
                <th>Created At</th>
            </tr>
        </thead>
    </table>
</div>    
@endsection
@section('footer-script')
<script>
    $('#realtor_years').DataTable({
        processing: true,
        pageLength: 100,
        responsive: true,
        serverSide: true,
        ordering: true,
        bPaginate: true,
        bLengthChange: false,
        bFilter: true,
        bInfo: false,
        bAutoWidth: false,
        language: {
            search: '',
            searchPlaceholder: 'Search'
        },
        // aaSorting: [[1, 'desc']],
        ajax: window.location.href, 
        columns: [
            { data: 'actions',orderable: false, name: 'actions',searchable: false},
            { data: 'ordering',orderable: true, name: 'ordering',searchable: true },
            { data: 'name',orderable: true, name: 'name',searchable: true },
            { data: 'created_at',orderable: false, name: 'created_at',"render": function (v,j,y) {
                return moment(v).format('MMM D, YYYY');
            },searchable: true },
        ],
        order: [[ 1, "asc" ]]
    });
    
    $(document).on('click','.delete-realtor-year',function(){
        var id   = $(this).data('id');
        var clst = $(this).closest('tr');
        UIkit.modal.confirm('Are you sure you want to delete this term realtor year?').then(function() {
            $.ajax(
            {
                url: url + "/admin/realtor-years/"+id,
                type: 'DELETE',
                dataType: "JSON",
                success: function (res)
                {
                    if(res.success == 1){
                        clst.remove();
                        UIkit.modal.alert('Realtor year successfully deleted.');
                    }
                }
            });
        }, function () {
        });
    });
    
    $(document).on('click','.active',function(){
        var id   = $(this).data('id');
        var clst = $(this).closest('td');
        UIkit.modal.confirm('Are you sure you want to inactive this realtor year?').then(function() {
            $.ajax(
            {
                url: url +  "/admin/realtor-years-status",
                type: 'POST',
                dataType: "JSON",
                data : {
                    type : 'inactive',
                    'id':id
                },
                success: function (res)
                {
                    if(res.success == 1){
                        clst.html("<a href='javascript:void(0)' data-id='"+id+"' class='uk-label uk-label-danger inactive'>Inactive</a>");
                    }
                }
            });
        }, function () {
        });
    });
    
    $(document).on('click','.inactive',function(){
        var id   = $(this).data('id');
        var clst = $(this).closest('td');
        UIkit.modal.confirm('Are you sure you want to active this realtor year?').then(function() {
            $.ajax(
            {
                url: url +  "/admin/realtor-years-status",
                type: 'POST',
                dataType: "JSON",
                data : {
                    type : 'active',
                    'id':id
                },
                success: function (res)
                {
                    if(res.success == 1){
                        clst.html("<a href='javascript:void(0)' data-id='"+id+"' class='uk-label uk-label-success active'>Active</a>");
                    }
                }
            });
        }, function () {
        });
    });
</script>
@endsection