@extends('layouts.master')
  @section('page_title')
      @if(isset($page_title))
          {{ $page_title }}
      @endif
  @endsection
@section('css')
@endsection
@section('content')
  <div class="">
      <div class="page-title">
        <div class="title_left">
          <div class="title_left">
            <ol class="breadcrumb">
              <li><a href="{!! url('dashboard') !!}">Dashboard</a></li>
              <li class="active">Clients</li>
            </ol>
          </div>
        </div>

        <div class="title_right">
          <div class="row">
            <form method="GET" action="">
              <div class="col-md-5 col-sm-5 col-xs-12 form-group pull-right top_search" style="margin-top: -2px;">
                <div class="input-group">
                  <input type="text" name="keyword" class="form-control" id="search" placeholder="Keyword here...">
                  <span class="input-group-btn">
                    <button type="button" class="btn btn-primary" style="color: #fff;" type="button">Go!</button>
                  </span>
                </div>
              </div>
            </form>
          </div>
          <div class="row">
              <div class="col-sm-12">
                <div class="pull-right"></div>
              </div>
          </div>
        </div>
      </div>

      <div class="clearfix"></div>
      <div class="row">
                    @include('commons.notifications')
        <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
          <div class="x_title">
            <h2> Clients </h2>
            <div class="pull-right">
                <a class="btn btn-primary btn-sm" href="{!! url('clients/create') !!}"><i class="fa fa-plus-circle"></i> Add Client
                </a>
            </div>
            <div class="clearfix"></div>
          </div>

          <div class="x_content">
            <div class="table-responsive">
              <table class="table table-striped jambo_table bulk_action table-condensed" style="width: 100%;">
                <thead>
                  <tr class="headings">
                    <th class="column-title">#</th>
                    <th class="column-title">Client</th>
                    <th class="column-title">Account Manager</th>
                    <th class="column-title">Status</th>
                    <th class="column-title">Country</th>
                    <th class="column-title">Last Modified By</th>
                    <th class="column-title no-link last"><span class="nobr">Action</span>
                    </th>
                    
                  </tr>
                </thead>

                <tbody></tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
</div>

@endsection

@section('javascript')
  <script type="text/javascript">
  $(document).ready(function(){
    
    console.log('heere at the wall');
    $('#search').on('keyup', function(){
      console.log('clicked');
      search();
    });
  
    search();

    function search(){
       var keyword = $('#search').val();
       console.log(keyword);
       $.post('{{ url("clients/raw") }}',
        {
           _token: $('meta[name="csrf-token"]').attr('content'),
           keyword:keyword
         },
         function(data){
            // console.log(data.data);
            // return false;
            table_post_row(data);
            console.log(data);
         });
    }
    // table row with ajax
    function table_post_row(result){
      let htmlView = '';
      if(result.data.length <= 0){
        htmlView += `
         <tr>
            <td colspan="4">No data.</td>
        </tr>`;
      }
      for(let i = 0; i < result.data.length; i++){
        htmlView += `
            <tr>
                  <td>`+ (i+1) +`</td>
                  <td><a href="` + base_url + `/clients/`+result.data[i].id+`" class="btn btn-link" >` + result.data[i].name +`</a> </td>
                  <td>`+result.data[i].accountMgr+`</td>
                  <td>`+result.data[i].status+`</td>
                  <td>`+result.data[i].country+`</td>
                  <td>`+result.data[i].modifiedBy+`</td>
                  <td>
                    <a href="` + base_url + `/clients/`+result.data[i].id+`" class="btn btn-link" ><i class="fa fa-eye"></i></a> 
                    <a href="` + base_url + `/clients/`+result.data[i].id+`/edit" class="btn btn-link" ><i class="fa fa-edit"></i></a> 
                  </td>
                  
            </tr>`;
      }
      $('tbody').html(htmlView);
    }

  });
  </script>
@endsection
