<?php $__env->startSection('filter'); ?>
    <div id="datatable-filter" class="fixed new_filter" data-current-user="Art Ramadani" data-order-by-status="1" data-max-chat-history="25">
        <div class="filter-inner">
            <h2 class="filter-header">
                <a href="#" class="filter-close" data-animate="1"><i class="entypo-cancel"></i></a>
                <i class="fa fa-filter"></i>
                Filter
            </h2>
            <form novalidate class="form-horizontal form-groups-bordered validate" method="get" id="ratetable_filter">
                <div class="form-group">
                    <label for="Name" class="control-label">Name</label>
                    <input class="form-control" name="Name" id="Name"  type="text" >
                </div>
                <div class="form-group">
                    <label for="Name" class="control-label">Contact Owner</label>
                    <select name="Owner" class="select2" data-allow-clear="true" data-placeholder="Select Account Owner...">
                        <option></option>
                        <optgroup label="Leads">
                            <?php if( count($lead_owners)): ?>
                            <?php foreach($lead_owners as $lead_owner): ?>
                            <?php if(!empty($lead_owner->AccountName) && $lead_owner->Status == 1): ?>
                            <option value="<?php echo $lead_owner->AccountID; ?>">
                            <?php echo $lead_owner->AccountName; ?>

                            </option>
                            <?php endif; ?>
                            <?php endforeach; ?>
                            <?php endif; ?>
                        </optgroup>
                        <optgroup label="Accounts">
                            <?php if( count($account_owners)): ?>
                            <?php foreach($account_owners as $account_owner): ?>
                            <?php if(!empty($account_owner->AccountName) && $account_owner->Status == 1): ?>
                            <option value="<?php echo $account_owner->AccountID; ?>" >
                            <?php echo $account_owner->AccountName; ?>

                            </option>
                            <?php endif; ?>
                            <?php endforeach; ?>
                            <?php endif; ?>
                        </optgroup>
                    </select>
                </div>
                <div class="form-group">
                    <label for="Phone" class="control-label">Phone</label>
                    <input class="form-control" name="Phone" id="Phone"  type="text" >
                </div>
                <div class="form-group">
                    <label for="Search" class="control-label">Email</label>
                    <input class="form-control" name="Search" id="Search"  type="text" >
                </div>
               
                <div class="form-group">
                    <br/>
                    <button type="submit" class="btn btn-primary btn-md btn-icon icon-left">
                        <i class="entypo-search"></i>
                        Search
                    </button>
                </div>
            </form>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>


<ol class="breadcrumb bc-3">
    <li>
        <a href="<?php echo URL::to('dashboard'); ?>"><i class="entypo-home"></i>Home</a>
    </li>
    <li class="active">
        <strong>Contacts</strong>
    </li>
</ol>
<h3>Contacts</h3>

<?php echo $__env->make('includes.errors', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<?php echo $__env->make('includes.success', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

<p style="text-align: right;">
<?php if(User::checkCategoryPermission('Contacts','Add')): ?>
    <a href="<?php echo URL::to('contacts/create'); ?>" class="btn btn-primary ">
        <i class="entypo-plus"></i>
        Add New
    </a>
<?php endif; ?>
</p>

<table class="table table-bordered datatable" id="table-4">
    <thead>
    <tr>
        <th>Contact Name</th>
        <th>Contact Owner</th>
        <th>Phone</th>
        <th>Email</th>
        <th>Actions</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<script type="text/javascript">
    jQuery(document).ready(function ($) {
        // Filter code start here 
        $('#filter-button-toggle').show();
        var $searchFilter = {};
        var update_new_url;
        $searchFilter.Name = $("#ratetable_filter [name='Name']").val();
        $searchFilter.Owner = $("#ratetable_filter [name='Owner']").val();
        $searchFilter.Phone = $("#ratetable_filter [name='Phone']").val();
        $searchFilter.Search = $('#ratetable_filter [name="Search"]').val();
        // End here
         data_table = $("#table-4").dataTable({

            "bProcessing":true,
            "bServerSide":true,
            "sAjaxSource": baseurl + "/contacts/ajax_datagrid",
            "iDisplayLength": parseInt('<?php echo CompanyConfiguration::get('PAGE_SIZE'); ?>'),
            "sDom": "<'row'<'col-xs-6 col-left'l><'col-xs-6 col-right'<'export-data'T>f>r>t<'row'<'col-xs-6 col-left'i><'col-xs-6 col-right'p>>",
            "sPaginationType": "bootstrap",
            "oTableTools": {},
            "aaSorting"   : [[4, 'desc']],
            // Filter
            "fnServerParams": function(aoData) {
                aoData.push({"name":"Name","value":$searchFilter.Name},{"name":"Owner","value":$searchFilter.Owner},{"name":"Phone","value":$searchFilter.Phone},{"name":"Search","value":$searchFilter.Search});
                data_table_extra_params.length = 0;
                data_table_extra_params.push({"name":"Name","value":$searchFilter.Name},{"name":"Owner","value":$searchFilter.Owner},{"name":"Phone","value":$searchFilter.Phone},{"name":"Search","value":$searchFilter.Search});
            },
            "fnRowCallback": function(nRow, aData) {
                $(nRow).attr("id", "host_row_" + aData[2]);
            },
            // End
            "aoColumns":
             [
                { "bSortable": true }, //0 Fill name
                { "bSortable": true }, //1 Owner
                { "bSortable": true }, //2 Phone
                { "bSortable": true }, //3 Email
                {  // 4 Contact ID
                   "bSortable": true,
                    mRender: function ( id, type, full ) {
                        var action , edit_ , show_ ;
                        edit_ = "<?php echo URL::to('contacts/{id}/edit'); ?>";
                        show_ = "<?php echo URL::to('contacts/{id}/show'); ?>";
                        delete_ = "<?php echo URL::to('contacts/{id}/delete'); ?>";

                        edit_ = edit_.replace( '{id}', id );
                        show_ = show_.replace( '{id}', id );
                        delete_  = delete_ .replace( '{id}', id );
                        action = '';
                        <?php if(User::checkCategoryPermission('Contacts','Edit') ){ ?>
                        action += ' <a href="'+edit_+'" title="Edit" class="btn btn-default btn-sm"><i class="entypo-pencil"></i></a>';
                        <?php } ?>
                        action += ' <a href="'+show_+'" Title="View" class="btn btn-default btn-sm"><i class="fa fa-eye"></i></a>';
                        <?php if(User::checkCategoryPermission('Contacts','Delete') ){ ?>
                        action += ' <a href="'+delete_+'" title="Delete"  class="btn btn-danger btn-default btn-sm"><i class="entypo-trash"></i></a>';
                        <?php } ?>
                        return action;
                      }
                  },
            ],
            "oTableTools": {
                "aButtons": [
                    {
                        "sExtends": "download",
                        "sButtonText": "EXCEL",
                        "sUrl": baseurl + "/contacts/exports/xlsx", //baseurl + "/generate_xlsx.php",
                        sButtonClass: "save-collection btn-sm"
                    },
                    {
                        "sExtends": "download",
                        "sButtonText": "CSV",
                        "sUrl": baseurl + "/contacts/exports/csv", //baseurl + "/generate_csv.php",
                        sButtonClass: "save-collection btn-sm"
                    }
                ]
            }
        });

        $(".dataTables_wrapper select").select2({
            minimumResultsForSearch: -1
        });

        // Highlighted rows
        $("#table-2 tbody input[type=checkbox]").each(function (i, el) {
            var $this = $(el),
                $p = $this.closest('tr');

            $(el).on('change', function () {
                var is_checked = $this.is(':checked');

                $p[is_checked ? 'addClass' : 'removeClass']('highlight');
            });
        });

        // Replace Checboxes
        $(".pagination a").click(function (ev) {
            replaceCheckboxes();
        });
    
    $('body').on('click', 'a[title="Delete"]', function (e) {
                e.preventDefault();
                var response = confirm('Are you sure?');
                if (response) {
                    $.ajax({
                        url: $(this).attr("href"),
                        type: 'POST',
                        dataType: 'json',
                        success: function (response) {
                            $(".btn.delete").button('reset');
                            if (response.status == 'success') {
                                data_table.fnFilter('', 0);
                            } else {
                                toastr.error(response.message, "Error", toastr_opts);
                            }
                        },
                        // Form data
                        //data: {},
                        cache: false,
                        contentType: false,
                        processData: false
                    });


                }
                return false;

            });
            $("#ratetable_filter").submit(function(e) {
            e.preventDefault();
            $searchFilter.Name = $("#ratetable_filter [name='Name']").val();
            $searchFilter.Owner = $("#ratetable_filter [name='Owner']").val();
            $searchFilter.Phone = $("#ratetable_filter [name='Phone']").val();
            $searchFilter.Search = $('#ratetable_filter [name="Search"]').val();
            data_table.fnFilter('', 0);
            return false;
         });
});
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.main', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>