@extends('main')
@section('css')
<link href="{{ asset('/css/bootstrapValidator.min.css')}}" rel="stylesheet">
@endsection
@section('content')
<section class="main_content">
    <div class="container">
    <ol class="breadcrumb cust-breadcrumb">
              <li><a href="{{url('/')}}">Home</a></li>
              <li><a href="{{url('/search')}}">Search</a></li>
              <li class="active">Search Result</li>
            </ol>
      <div class="row talent_home">
        @include("left_menu")
        <form name="hiddenSearchForm" id="hiddenSearchForm">
          @foreach($search_data as $key => $value)
            <input type="hidden" name="{{$key}}" value="{{$value}}">
          @endforeach
            <input type="hidden" id="limit" name="limit" value="{{Config::get('constants.PROFILE_SEARCH_LIMIT')}}">
            <input type="hidden" id="offset" name="offset" value="{{Config::get('constants.PROFILE_SEARCH_LIMIT')}}">
        </form>
        <div class="col-md-9 col-sm-9"> 
             
             
            <div id="connectionmsg" >
                
            </div>
             
             <div class="col-xs-6">
          <h1 class="heading mtop-0 search-result-head">Search</h1>
          </div>
          <div class="col-xs-6">
            @if(count($search_result))
              <button type="button" class="btn profile_search_save-btn pull-right" id="profile_search_save">Save Search</button>
            @endif
            </div>
          <div class="clearfix"></div>
        <form name="searchProfileForm" id="searchProfileForm" method="post">
         <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class="row talent_data search_result search-result-right-br" id="search_result_data">
            @if(count($search_result))
            <?php $i=1;?>
              @foreach($search_result as $pro)
             <?php if($i%2==0){?> <div class="row search-page-br"><?php } ?>
              <div class="col-md-6">
                <div class="row">
                  <div class="emty-div-row">
                   <div class="row profile-search-top-actions">
                        <div class="col-xs-4 col-md-4"><div class="search_input"><input type="checkbox" id="profileList" name="profileList[]" value="{{$pro->id}}"><label></label></div></div>
						<div class="col-md-8 col-xs-8">
                      <a href="{{url('profiles/'.$pro->id)}}">
                       <h4>{{$pro->name}}</h4>
                      </a>
                    </div>
					</div>
					<div class="row profile-search-middle-actions ">
					<div class="col-xs-4 col-md-4 searchpg-profile-pic">
                       <a href="{{url('profiles/'.$pro->id)}}">
  					         @if($pro->profile_image && is_file('uploads/profile_images/'.$pro->profile_image))
                        <img class="img-responsive img-circle" src="{{url('uploads/profile_images/'.$pro->profile_image)}}" alt=""/>
                      @else                    
                        <img class="img-responsive img-circle" src="{{url('uploads/profile_images/default-profile.png')}}" alt=""/>
                      @endif
                      </a>
                   </div>
				    <div class="col-xs-4 col-md-4">
                   @if(count($res1)>0 && count($res2)>0 && in_array($pro->id,$res1['likes_id']) && in_array($user_id,$res2['like_user_id']))
                    <div class="search-like-div">   
                      <a href="javascript:void(0)" class="updt_pro like{{$pro->id}} unlike_col" data-id="{{$pro->id}}" ><span class="glyphicon glyphicon-thumbs-down"></span> (Unlike)</a> 
                    </div>
                     @else
                    <div class="search-like-div">   
                      <a href="javascript:void(0)" class="updt_pro unlike{{$pro->id}}" data-id="{{$pro->id}}" ><span class="glyphicon glyphicon-thumbs-up"></span> (Like)</a>                 
                    </div>
                   @endif
				   </div>
                    <div class="col-xs-4 col-md-4">
                    <div class="profile-addpic-search"><a href="{{url('profiles/'.$pro->id)}}"><img class="img-responsive" src="../images/add_pic.png" alt=""/></a></div></div>
                  
                  </div>
                  </div>
                    
                </div>
                   <div class="clearfix">     <div id="LoadingImage" style="display: none;">
                 <img class="img-responsive" src="../images/spin.gif" width="120" height="120" alt=""/>
        </div></div>
              </div>
             <?php if($i%2==0){?></div><?php }?>
              <?php $i++;?>
              @endforeach
            @else
              <div class="col-md-6 txt-red">
                Required profiles not found
              </div>  
            @endif  
            </div>          
          </form>
          <div class="clearfix"></div>
          <!--<input type="hidden" name="_token" value="{{{ Session::getToken() }}}">-->

<div class="btn-group search-select-ac-btn-block">
  <button type="button" class="btn btn-success ">Select Action</button>
  <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
      <span class="caret"></span>
    <span class="sr-only">Toggle Dropdown</span>
  </button>
  <ul class="dropdown-menu" role="menu">
                <li><a href="javascript:void(0)" id="connect_request">Connection Request</a></li>
                <?php if(empty($flag)){?>
               <?php
                if(Session::get('auth_selected_profile_type')!='O')
                   {
                ?>  
			  <li><a  href="javascript:void(0)" id="mentor_request">
Mentor Request</a></li>
<?php
                  }
                
                ?>
                <li><a href="javascript:void(0)" id="share_profile">Share</a></li>       
                 <li><a href="javascript:void(0)" id="inquiries">Inquiries</a></li> 
                <?php
                if(Session::get('auth_selected_profile_type')=='O')
                   {
                ?>                
                <li><a href="javascript:void(0)" id="jobspec">Send Job Spec</a></li> 
                 <?php
                  }
                }
                ?> 
              </ul> 
</div>
 <div class="modal" id="mentor_model" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
&times;</button>
<h4 class="modal-title" align="center">Mentor Subject Information</h4>
</div>
    <div class="modal-body">
        <form role="form" action="mentor" method="POST"id="mentor_subject">
        <div class="form-group">
             <input type="hidden" name="_token" value="{{ csrf_token() }}">
             <input type="hidden" name="checbox_val" id="checkbox_val" value="">
           <label for="name">Title<span class="reqired-val">*</span>:</label>
           <input type="text" class="form-control" id="mentor_sub" name="mentor_sub" 
              placeholder="Enter subject">
        </div> 
    <div class="modal-footer">
<button type="submit" class="btn btn-primary">Submit</button>

</div>          
        </form>
    </div>

</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>         
 
          <div class="modal" id="connection_model" data-backdrop="static">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
&times;</button>
<h4 class="modal-title" align="center">Connection Subject Information</h4>
</div>
    <div class="modal-body">
        <form role="form" action="{{url('send_connection')}}" method="GET" id="connection_subject" name="connection_subject">
        
             <input type="hidden" name="_token" value="{{ csrf_token() }}">
             <input type="hidden" name="ids" id="ids">
             <input type="hidden" name="checbox_val" id="checkbox_val" value="">
        <div class="form-group">
           <label for="name">Title :</label>
           <input type="text" class="form-control" id="conneciton_sub" name="conneciton_sub" 
                  placeholder="Enter subject" value="I would like to Connect" readonly="true">
        </div>
        <div class="form-group">
            <label for="name">Message :</label>
            <textarea id="connection_message" name="connection_message" class="form-control" ></textarea>
        </div> 
    <div class="modal-footer">
<!--<button type="button" class="btn btn-primary" id="connection_ajax">Submit</button>-->
<button type="submit" class="btn btn-primary"> Submit</button>

</div>          
        </form>
    </div>

</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div> 
          
          
         
          @if($total_records>$default_limit)
            <div class="search_load_more text-center">
              <button type="button" class="btn btn-success load-m-profile-btn" id="load_more_profiles">  Load More Profiles</button>
            </div>
          @endif
      </div>
    </div>
    <div id="profile_search_save_modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Save Search</h4>
        </div>
        <div class="modal-body">
            <form class="form-horizontal" method="post" action="" id="saveSearchform" name="saveSearchform" enctype="multipart/form-data" onsubmit="return check_save_search()">
            @foreach($search_data as $key => $value)
              <input type="hidden" name="{{$key}}" value="{{$value}}">
            @endforeach
           <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div id="save_search_error" class="txt-red"></div>
            <div class="form-group">
              <label class="col-sm-4 control-label">Save with existing</label>
              <div class="col-sm-8">
                <select name="save_id" id="save_id" class="form-control">
                  <option value="">Select</option>
                  @if(count($search_history)>0)
                    @foreach($search_history as $sh)
                      <option value="{{$sh['id']}}">{{$sh['name']}}</option>
                    @endforeach
                  @endif  
                </select>
              </div>
            </div>            
            <div class="form-group">
                <div class="col-md-6 pull-right">OR</div>
            </div>            
            <div class="form-group">
              <label class="col-sm-4 control-label">New Name</label>
              <div class="col-sm-8">
              <input type="text" class="form-control" id="save_name" name="save_name" placeholder="New Name">
              </div>
            </div>            
            <div class="form-group">
                <div class="col-sm-12">
                    <button type="submit" name="search" value="search_save" class="btn btn-primary pull-right">Save Search</button>
                </div>
            </div>
          </form>          
        </div><!-- End of Modal body -->
        </div><!-- End of Modal content -->
        </div><!-- End of Modal dialog -->
    </div><!-- End of Profile Image Modal -->


  </section> 
@endsection
@section('js')
<script src="{{ asset('js/bootstrapValidator.min.js')}}"></script>
<script src="{{ asset('js/maskedinput/jquery.maskedinput.min.js')}}"></script>
<script src="{{ asset('js/validation.js')}}"></script>
<script src="{{ asset('js/bootbox.js')}}"></script>
<script type="text/javascript">
  /*var search_data = JSON.stringify('<?php echo json_encode($search_data)?>');
  alert(search_data);*/
  var checked_values = '';  
  function check_save_search()
  {
    if($("#save_id").val()=='' && $("#save_name").val()=='')
      $("#save_search_error").html('Please Select Save with existing or Enter new name');
    else
    {
      $("#save_search_error").html('');
      return true;
    }
    return false;
  }
  var k=1;
 if($("#load_more_profiles").length) 
  {
    var total_profiles = {{$total_records}};
    $(document).on('click','#load_more_profiles',function(){
     
      $(this).html('<i class="fa fa-spinner fa-pulse"></i> Loading Profiles');

      next = parseInt($("#offset").val())+{{Config::get('constants.PROFILE_SEARCH_LIMIT')}};

      $.ajax({
       type: "GET",
       data: $("#hiddenSearchForm").serialize(),
       dataType: "json",
       url: "{{url('search_ajax')}}",
       success: function(ajax_data){
        total_rec = ajax_data.total;
        prof_data = ajax_data.data;
        likes_id = ajax_data.res1;
        like_user_id = ajax_data.res2;
        user_id = ajax_data.user_id;
        
        append_data = '';
        default_image = '{{url('uploads/profile_images/default-profile.png')}}';
        //$.each(ajax_data,function(k,v))
        $.each(prof_data,function(i,v){
          if(prof_data[i].profile_image)
            image_path = "{{url('uploads/profile_images')}}/default-profile.png";
          else
            image_path = "{{url('uploads/profile_images/default-profile.png')}}";
           vid_likes='';
          if($.inArray(prof_data[i].id,likes_id.likes_id)!=-1 && $.inArray(user_id,like_user_id.like_user_id)!=-1)
          {
            vid_likes=vid_likes+'<div class="col-xs-4 col-md-4"><div class="search-like-div"><a href="javascript:void(0)" class="updt_pro like'+prof_data[i].id+' unlike_col" data-id="'+prof_data[i].id+'"><span class="glyphicon glyphicon-thumbs-down"></span> (Unlike)</a></div></div>';
          }                        
          else
          {
            vid_likes=vid_likes+'<div class="col-xs-4 col-md-4"><div class="search-like-div"><a href="javascript:void(0)" class="updt_pro unlike'+prof_data[i].id+'" data-id="'+prof_data[i].id+'" ><span class="glyphicon glyphicon-thumbs-up"></span> (Like)</a></div></div>';
          }
          var dd="";var dd1="";
          if(k%2==0) dd='<div class="row search-page-br">';
          if(k%2==0) dd1='</div>';
          append_data = append_data+dd+'<div class="col-md-6"><div class="row"><div class="emty-div-row"><div class="row profile-search-top-actions"><div class="col-xs-4 col-md-4"><div class="search_input"><input type="checkbox" id="profileList" name="profileList[]" value="'+prof_data[i].id+'"><label></label></div></div><div class="col-md-8 col-xs-8"><a href="{{url("profiles")}}/'+prof_data[i].id+'"><h4>'+prof_data[i].name+'</h4></a></div></div><div class="row profile-search-middle-actions "><div class="col-xs-4 col-md-4 searchpg-profile-pic"><a href="{{url("profiles")}}/'+prof_data[i].id+'"><img class="img-responsive img-circle" src="'+image_path+'" alt=""/></a></div>'+vid_likes+'<div class="col-xs-4 col-md-4"><div class="profile-addpic-search"><a href="{{url("profiles")}}/'+prof_data[i].id+'"><img class="img-responsive" src="../images/add_pic.png" alt=""/></a></div></div></div></div></div></div></div></div>'+dd1;
         /* alert(prof_data[i].id);
          alert(prof_data[i].profile_image);
          alert(prof_data[i].name);*/
       k++; });
          $("#search_result_data").append(append_data);
          $("#offset").val(next);
          if(next>total_profiles)
              $("#load_more_profiles").hide();
          else
              $("#load_more_profiles").show();

          $("#load_more_profiles").html('Load More Profiles');
       }
      });
    });
  }
   $(document).ready(function(){
   $('#connect_request').click(function(e){
        var checkValues = $('input[name="profileList[]"]:checked').map(function()
            {
                return $(this).val();
            }).get();
         var checkValues_len = $('input[name="profileList[]"]:checked').length;  
        if(checkValues_len>10)
        {
          bootbox.alert("Up to 10 profile's to connection request once");
        }
      else if(checkValues_len)
      {
        var dataString = 'ids='+ checkValues +  '&_token=' + $("input[name=_token]").val();
           if(checkValues){
               checked_values = checkValues;
               $('#ids').val(checked_values);
               e.preventDefault();    
               $("#connection_model").modal('show');
             /* // ajax_data = $("#connection_subject").serialize();
               $("#connectionmsg").empty();
                $.ajax({
                    url: "{{url('send_connection')}}",
                    type: 'get',
                    data: { 'ids':checkValues, '_token': $('input[name=_token]').val() },
                    //data: ajax_data,
                    success:function(data){
                      $("#connectionmsg").append(data);
                    // alert(data);
                    }
                });*/
        }
      }
      else
      {
        bootbox.alert("Select atleast one profile");
      }                      
 
    });
    });
    $(document).on("click","#connection_ajax",function(){
        //$("#connectionmsg").empty();
        $.ajax({
            url: "{{url('send_connection')}}",
            type: 'get',
            data: $("#connection_subject").serialize(),
            //data: ajax_data,
            success:function(data){
                $("#connectionmsg").empty();
              $("#connectionmsg").append(data);
             
              $("#connection_message").val('');
              $("#connection_model").modal('hide');
            // alert(data);
            }
        });        
    });
  $(document).on("click","#share_profile",function(){
    var checkValues = $('input[name="profileList[]"]:checked').length;
      if(checkValues>10)
      {
        bootbox.alert("Up to 10 profile's share once");
      }
      else if(checkValues)
      {
        $("#searchProfileForm").attr('action','{{url("share/request")}}');
        $("#searchProfileForm").submit();
      }
      else
      {
        bootbox.alert("Select atleast one profile");
      }
  });
    $(document).on("click","#inquiries",function(){
    var checkValues = $('input[name="profileList[]"]:checked').length;
      if(checkValues>10)
      {
        bootbox.alert("Up to 10 profile's inquirie once");
      }
      else if(checkValues)
      {
        $("#searchProfileForm").attr('action','{{url("share/inquiries")}}');
        $("#searchProfileForm").submit();
      }
      else
      {
        bootbox.alert("Select atleast one profile");
      }
  });
  
      $(document).on("click","#jobspec",function(){
    var checkValues = $('input[name="profileList[]"]:checked').length;
      if(checkValues>10)
      {
        bootbox.alert("Up to 10 profile's jobspec once");
      }
      else if(checkValues)
      {
        $("#searchProfileForm").attr('action','{{url("sendjobspec")}}');
        $("#searchProfileForm").submit();
      }
      else
      {
        bootbox.alert("Select atleast one profile");
      }
  });
  
  $(document).on("click","#mentor_request",function(){
    var checkValues = $('input[name="profileList[]"]:checked').length;
    var checkValues_list = $('input[name="profileList[]"]:checked').map(function()
            {
                return $(this).val();
            }).get();
      if(checkValues>10)
      {
        bootbox.alert("Up to 10 profile's mentor request once");
      }
      else if(checkValues)
      { 
        $("#checkbox_val").val(checkValues_list);
        $("#mentor_model").modal('show');        
      }
      else
      {
        bootbox.alert("Select atleast one profile");
      }
  });

  $("#connection_subject").bootstrapValidator({        
      message: 'This value is not valid',
      feedbackIcons: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
      },
      fields: {
        'connection_message': {
              validators: {
                  notEmpty: {
                      message: 'The Connection Message should not be empty'
                  },
              }
          },
      }
  });

  $("#mentor_subject").bootstrapValidator({        
      message: 'This value is not valid',
      feedbackIcons: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
      },
      fields: {
        'mentor_sub': {
              validators: {
                  notEmpty: {
                      message: 'The Title should not be empty'
                  },
              }
          },
      }
  });
$(function(){  
    $(document).on('click','.updt_pro',function(){
       var data_id = $(this).attr("data-id");  
        var dataString = 'like_id='+ data_id +  '&_token=' + $("input[name=_token]").val();
        $("#LoadingImage").show();
          $.ajax({
                    type: "GET",
                    url : "/profile/likes",
                    data : dataString,
                    success : function(data){
                         $("#LoadingImage").hide();
                     var Unlike='<a href="javascript:void(0)" class="updt_pro like'+data_id+' unlike_col" data-id="'+data_id+'" ><span class="glyphicon glyphicon-thumbs-down"></span> (Unlike)</a>';
                      var Like='<a href="javascript:void(0)" class="updt_pro unlike'+data_id+'" data-id="'+data_id+'" ><span class="glyphicon glyphicon-thumbs-up"></span> (Like)</a>';
                       if(data=='Like'){ $('.like'+data_id).replaceWith(Like);}
                       if(data=='Unlike'){ $('.unlike'+data_id).replaceWith(Unlike);}
                
    }
                },"json");
    });
    
    });
</script>
@endsection
