/home/mip/mip/app/Modules/Client/Views/posts/index.blade.php
@extends('Client::layouts')
@section('page-body')
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            <i class="fa fa-{{$pageIcon ? $pageIcon : 'angle-double-right'}}" aria-hidden="true"></i> {{$pageTitle}}
            <br>
            <small>{{$pageDescription}}</small>
        </h1>
        <ol class="breadcrumb">
            <li class="active"><i class="fa fa-{{$pageIcon ? $pageIcon : 'angle-double-right'}}"></i> {{$pageTitle}}</li>           
        </ol>
    </section>
    <!-- Main content -->
    <section class="content"> 
        @include('Client::message')
        <div class="box box-default">
            <div class="box-header with-border">
                <div class="pull-left">
                    <form action="javascript:void(0)" id="post-filter" method="POST" class="form-inline" role="form">
                        <b>Filter: </b>
                        <div class="form-group">
                            <label class="sr-only" for="">Search Posts</label>
                            {!! Form::select('posts_category_id', $category, null, ['class' => 'form-control', 'placeholder'=>'All']) !!}
                        </div>
                        <div class="form-group">
                            {!! Form::text('title', null, ['placeholder' => 'Post Title', 'class' => 'form-control', 'autocomplete' => 'off']) !!}
                        </div>
                        <button type="submit" class="btn btn-primary">Filter</button>
                        <button href="javascript:void(0)" class="btn btn-default" id="reset">Reset</button>
                    </form>
                </div>
                @can('create', $permissions)
                    <div class="pull-right">
                        <a href="/client/posts/create" class="btn btn-primary btn-flat"><i class="fa fa-plus-circle"></i> Add {{$pageModuleName}}</a>
                    </div>
                @endcan
            </div>
            <div class="box-body">
                <table class="table table-bordered table-striped table-hover" id="post-table" width="100%">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Title</th>
                            <th>Category</th>
                            <th>Author</th>
                            <th>Views</th>
                            <th>Date</th>
                            <th>Action</th>
                        </tr>
                    </thead>
               </table>
            </div>                
        </div>
        @include('Client::logs')
    </section>
@stop
@section('page-js')
<script type="text/javascript">
    $(document).ready(function() {
        var postTable = $('#post-table').DataTable({
            "processing": true,
            "serverSide": true,
            "pagingType": "input",
            "bFilter": false,
            ajax: {
                url:'posts/get-post-data', 
                type: 'POST',
                "data": function(d){
                    d.title = $('#post-filter [name="title"]').val();
                    d.posts_category_id = $('#post-filter [name="posts_category_id"]').val();
                }
            },
            "dom": "<'row'<'col-sm-6'i><'col-sm-6 text-right'l>>" +
                "<'row'<'col-sm-12'tr>>" +
                "<'row'<'col-sm-5'><'col-sm-7'p>>",
            columns: [
                {
                    width:'10px', searchable: false, orderable: false,
                    render: function (data, type, row, meta) {
                        return meta.row + meta.settings._iDisplayStart + 1;
                    }
                },
                { data: 'title', name: 'title' },
                { data: 'category', name: 'category', sortable: false, searchable: false},
                { data: 'author', name: 'author', sortable: false, searchable: false},
                { data: 'pageviews', name: 'pageviews', sortable: true, searchable: false},
                { data: 'date', name: 'date', sortable: true, searchable: false, width:'90px'},
                { data: 'action', name: 'action', orderable: false, searchable: false, width:'60px', className:'text-center'},
            ],
            fnDrawCallback: function ( oSettings ) {
                $('[data-toggle="tooltip"]').tooltip();
                $("#post-table td").on("click", '.deleted', function() {
                    var action = $(this).data('action');
                    swal({
                        title: 'Are you sure you want to delete?',
                        text: "This will be permanently deleted",
                        type: 'warning',
                        showCancelButton: true,
                        confirmButtonColor: '#3085d6',
                        cancelButtonColor: '#d33',
                        confirmButtonText: 'Yes'
                        }).then(function () {                    
                        $.ajax({
                            type: "GET",
                            url: action,
                            dataType: 'json',
                            success: function(data) {
                                if(data.type == "success") {
                                    postTable.draw();
                                    swal(data.message, '', 'success')
                               }else{
                                    swal(data.message, '', 'error')
                               }
                            },
                            error :function( jqXhr ) {
                                swal('Unable to delete.', 'Please try again.', 'error')
                            }
                        });
                    })              
                });            
            },
            "order": [[5, "desc"]],
        });
        $('#post-filter').on('submit', function(){
            postTable.draw();
        });
        $('#reset').on('click', function(event) {
            event.preventDefault();
            $('#post-filter [name="title"]').val('');
            $('#post-filter [name="posts_category_id"]').val('');
            postTable.draw();
        });
    });
</script>
@include('Client::posts.includes.js')
@include('Client::notify')
@stop