@extends('layouts.app')
@section('title', 'Spoofing Report')
@section('content')
<meta name="csrf-token" content="{{ csrf_token() }}">
<?php error_reporting(0); ?>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
    /* Custom style for pagination links */
    .pagination {
        margin: 0; /* Remove default margin */
        padding: 0; /* Remove default padding */
        list-style: none; /* Remove list bullet points */
        display: flex; /* Use flex layout for horizontal alignment */
    }

    .pagination li a {
        padding:6px;
    }

    .pagination a {
        display: inline-block;
        padding: 0; /* Adjust the padding as needed */
        text-decoration: none;
        background-color: #f0f0f0;
        border: 1px solid #ccc;
        color: #333;
        transition: background-color 0.3s ease, color 0.3s ease;
    }

    .pagination a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .pagination .active a {
        background-color: #007bff;
        color: #fff;
    }
</style>

<?php

use \App\Http\Controllers\InheritApiController;
$userdetails = InheritApiController::headerUserDetails();
$startDate = request()->input('start_date') ?? '';
$endDate = request()->input('end_date') ?? '';
    // if (!empty(request()->input('start_date')))
    // {
    //     $startDate = request()->input('start_date');
    // }
    // else
    // {
    //     $current_date = date("Y-m-d"); 
    //     $str_date = strtotime(date("Y-m-d", strtotime($current_date)) . " -15 day");
    //     $startDate = date('Y-m-d', $str_date);
    // }

    // if (!empty(request()->input('end_date')))
    // {
    //     $endDate = request()->input('end_date');
    // }
    // else
    // {
    //     $endDate = date('Y-m-d');
    // }

    $url_page = explode('?',str_replace('/','',$_SERVER['REQUEST_URI']));
    $url = $url_page[0];
?>
<style>
    .audiojs .scrubber { 
display: none; 
} 
</style>


    <section class="content">
			<div class="row">			  
				<div class="col-lg-12 col-12">
					  <div class="box">
						
						<!-- /.box-header -->
                        <form class="needs-validation" action="" method="post">
                            @csrf

                        <input type="hidden" class="form-control" name="show" value="10" id="show_value" required="">

                        <input type="hidden" class="form-control" name="campaign_id" value="{{$campaign_list[0]->id}}" id="campaign_name" required="">
							<div class="box-body">
								<h4 class="box-title text-info mb-0"><i class="fa fa-edit me-15"></i> Spoofing Report</h4>

								<hr class="my-15">
								<div class="row">
								  <div class="col-md-6">
                                  <div class="form-group">
									<label class="form-label">Phone Number</label>
									<div class="input-group mb-3">
										<span class="input-group-text"><i class="ti-user"></i></span>
                                        <input type="text" class="form-control" placeholder="Enter Phone Number" onkeypress="return isNumberKey(event)" type="" name="number" value="{{ request()->input('number') }}" id="mobile" maxlength="" >
									</div>
								</div>
								  </div>
                                  @if(Session::get('level') >= 5)
                                
                                <div class="col-md-6">
									<div class="form-group">
									  <label class="form-label">Extension List</label>
                                        <div class="input-group mb-3">
                                            <span class="input-group-text @error('caller_id') text-danger border-danger @enderror"><i class="ti-user"></i></span>

                                            <select class="form-select" name="extension" id="extension">
                                        <option value="">Select</option>
                                        @foreach($extension_list as $key => $extension)

                                        @if((request()->session()->get('level') > 9))
                                        @if(($extension->user_level <= 9) || ($extension->app_extension == request()->session()->get('extension')))
                                        <option @if($extension->app_extension == request()->input('extension'))  selected @endif value="{{$extension->app_extension}}">{{$extension->first_name}} {{$extension->last_name}} - {{$extension->app_extension}} @if($extension->is_deleted == 1) (Removed User) @endif</option>
                                        @endif

                                        @elseif(($extension->user_level < 9) || ($extension->app_extension == request()->session()->get('extension')))
                                        <option @if($extension->app_extension == request()->input('extension'))  selected @endif value="{{$extension->app_extension}}">{{$extension->first_name}} {{$extension->last_name}} - {{$extension->app_extension}} @if($extension->is_deleted == 1) (Removed User) @endif</option>
                                        @endif


                                        @endforeach
                                    </select>								
                                        </div>
								  </div>
								</div>
                              
                              @endif
                       
                          
                            <div class="col-md-6">
                                  <div class="form-group">
									<label class="form-label">Date range:</label>
									<div class="input-group mb-3">
                                            <div class="input-group-addon">
                                                <i class="fa fa-calendar"></i>
                                            </div>
                                            <input type="text" class="form-control pull-right cdr-range" id="reservation2">
                                            <input type="hidden" id="start_date" name="start_date" value="{{ $startDate }}">
                                            <input type="hidden" id="end_date" name="end_date" value="{{ $endDate }}">
                                    </div>
                                    </div>
                                    </div>
                       
                           
								 
                        
                    
                                </div>
                               
                            </div>
                        
                          
                      
                          
                           
							<!-- /.box-body -->
							<div class="box-footer text-end"style="margin-right:15px;">
                            <button type="submit" name="submit" id="form_submit"  value="Search" class="btn btn btn-primary waves-effect waves-light"><i class="fa fa-search" aria-hidden="true"></i> Search</button>
                            <!-- <button type="submit" name="submit_download" class="btn btn-danger waves-effect waves-light m-l-10" value="1"> <i class="fa fa-file-pdf-o"></i> PDF</button>                                     
                            <button type="submit" name="submit_download" class="btn btn-success waves-effect waves-light m-l-10" value="2"><i class="fa fa-file-excel-o" aria-hidden="true"></i> Excel</button> -->
							</div>  
						</form>
					  </div>
					  <!-- /.box -->			

                    </div>  

		
		  </div>
		  <!-- /.row -->

	

        <div class="row">

        <?php
        
                    if (!empty($report)) {
                   

                    if($page == 1)
                    {
                        $currentPage = 1;
                    }

                    else
                    {
                        $currentPage = isset($_GET['page']) ? (int) $_GET['page'] : 1;
                    }

                    $perPage = $show;
                    $paginator = new Illuminate\Pagination\LengthAwarePaginator($report, $record_count, $perPage, $currentPage, ['path' => url($url)]);
                ?>
        
        <div class="col-lg-12 col-12">
            <div class="box">
            
               <div class="box-body">  
                    <b>Total Rows :<?= $record_count ?></b>
                    <form method="GET" action="">
               
                    <label class="label-class" for="show">Show:</label>
                        <select class="form-select-sm"name="show" onchange="callButton(this.value)">
                            <option value="10" {{ request('show') == 10 ? 'selected' : '' }}selected>10</option>
                            <option value="25" {{ request('show') == 25 ? 'selected' : '' }}>25</option>
                            <option value="50" {{ request('show') == 50 ? 'selected' : '' }}>50</option>
                            <option value="100" {{ request('show') == 100 ? 'selected' : '' }}>100</option>
                        </select>
                        <label for="entries">entries</label>
                       
                    </form>  
                        <div class="table-responsive">
                        <table  class="table mt-0 table-hover no-wrap" data-page-size="10">
                            <thead>
                            <tr>
                                                        <th>#</th>
                                                        <th>Extension</th>
                                                        <th>Spoof CLI</th>                
                                                        <th >Number</th>
                                                        <th>Duration</th>
                                                        <th>Start Time</th>
                                                        <th>End Time</th>
                                                        <th>Recording</th>
                                                   


                                    </tr>
                            </thead>
                            <tbody>
                            <?php
                                                    $k = $lower_limit;
                                                    foreach ($paginator->items() as $key => $value) {

                                                                $timezone = $userdetails->data->timezone;
                                                                if(!empty($timezone))
                                                                {
                                                                    if(!empty($value->start_time))
                                                                    {
                                                                    $utc_start_time = $value->start_time;
                                                                    $dt_start_time = new DateTime($utc_start_time);
                                                                    $tz = new DateTimeZone($timezone); // or whatever zone you're after
                                                                    $dt_start_time->setTimezone($tz);
                                                                    $start_time = $dt_start_time->format('Y-m-d H:i:s');

                                                                    }
                                                                    else
                                                                    {
                                                                        $start_time='-';
                                                                    }

                                                                    if(!empty($value->end_time))
                                                                    {

                                                                    $utc_end_time = $value->end_time;
                                                                    $dt_end_time = new DateTime($utc_end_time);
                                                                    $tz = new DateTimeZone($timezone); // or whatever zone you're after
                                                                    $dt_end_time->setTimezone($tz);    
                                                                    $end_time = $dt_end_time->format('Y-m-d H:i:s');
                                                                }
                                                                else
                                                                {
                                                                        $end_time='-';

                                                                }

                                                                }
                                                                else
                                                                {
                                                                    if(!empty($value->start_time))
                                                                    {
                                                                    $start_time = $value->start_time;
                                                                }
                                                                else
                                                                {
                                                                    $start_time='-';
                                                                }

                                                            if(!empty($value->end_time))
                                                                    {
                                                                    $end_time = $value->end_time;
                                                                }
                                                                else
                                                                {
                                                                    $end_time='-';
                                                                }

                                                                }
                                                                
                                                        ?>                          
                                                        <tr>
                                            <th scope="row"><?php echo ++$k; ?></th>
                                            <td> <?php 
                                                            foreach($extension_list as $key => $extension)
                                                            {
                                                                if($extension->app_extension == $value->extension)
                                                                {
                                                                    $ext_name = $extension->first_name.' '.$extension->last_name.'-'.$value->extension;
                                                                }
                                                                else
                                                                if($extension->alt_extension == $value->extension)
                                                                {
                                                                    $ext_name = $extension->first_name.' '.$extension->last_name.'-'.$value->extension;
                                                                }

                                                                else
                                                                if($value->extension == NULL)
                                                                {
                                                                    $ext_name = 'N/A';
                                                                }
                                                                else
                                                                {
                                                                    $num = $value->extension;
                                                                    $numlength = strlen((string)$num);
                                                                    if($numlength > 9)
                                                                    {
                                                                        $ext_name = $value->extension;
                                                                    }
                                                                }

                                                            }
                                                            echo $ext_name;
                                                            ?></td>
                                                            
                                                            <td style="white-space: nowrap;">  
                                                                             
                                                            <?php
                                                            if(!empty($value->cli)) {
                                        // Assuming $fax->callerid contains the caller ID
                                        $callerID = $value->cli;

                                        // Remove any non-digit characters from the caller ID
                                        $callerID = preg_replace('/[^0-9]/', '', $callerID);

                                        // Check if the caller ID is at least 10 digits long
                                        if (strlen($callerID) >= 10) {
                                            // Extract the last 10 digits
                                            $lastTenDigits = substr($callerID, -10);

                                            // Format the last 10 digits as (999) 999-9999
                                            $formattedLastTenDigits = preg_replace('/(\d{3})(\d{3})(\d{4})/', '($1) $2-$3', $lastTenDigits);

                                            // Add a "+" sign in front of the original caller ID
                                            $formattedCallerID = '+' . substr($callerID, 0, -10) . $formattedLastTenDigits;

                                            // Output the formatted caller ID
                                            echo $formattedCallerID;
                                        } else {
                                            // Handle cases where the caller ID is too short
                                            echo "Invalid caller ID";
                                        }
                                    }
                                        else{
                                        echo 'N/A';
                                        } 
                                        ?>
                                    


                                                          </td>
                                                               
                                                              
                                                                <td style="white-space: nowrap;">
                                                            
                                                                <?php
                                        // Assuming $fax->callerid contains the caller ID
                                        $callerID = $value->number;

                                        // Remove any non-digit characters from the caller ID
                                        $callerID = preg_replace('/[^0-9]/', '', $callerID);

                                        // Check if the caller ID is at least 10 digits long
                                        if (strlen($callerID) >= 10) {
                                            // Extract the last 10 digits
                                            $lastTenDigits = substr($callerID, -10);

                                            // Format the last 10 digits as (999) 999-9999
                                            $formattedLastTenDigits = preg_replace('/(\d{3})(\d{3})(\d{4})/', '($1) $2-$3', $lastTenDigits);

                                            // Add a "+" sign in front of the original caller ID
                                            $formattedCallerID =  substr($callerID, 0, -10) . $formattedLastTenDigits;

                                            // Output the formatted caller ID
                                            echo $formattedCallerID;
                                        } else {
                                            // Handle cases where the caller ID is too short
                                            echo "Invalid caller ID";
                                        }
                                        ?>
                                                            
                                                            </td>
                                                                
                                                                <td><?php echo gmdate("H:i:s", $value->duration); ?></td>
                                                               
                                                                <td><?php echo $start_time; ?></td>
                                                            <td><?php echo $end_time;?></td>
                                                            <td>
                                                                <audio controls preload ='none'style="height:35px;width:220px;"><source src="<?php echo $value->call_recording; ?>" type='audio/wav'></audio>
                                                            </td>
                            
                                                                    
                                                                  

                                                                   
                                                           
                                            
                                    
                                        </tr>
                                        <?php } ?>


                                
                            </tbody>
                         
                        </table>
                        
                        <div style="float:right;">
                        {{$paginator->appends(Request::all())->links()}}



                                                            </div>
                                 <!-- Modal -->
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Call Matrix Report</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>

      <div class="modal-body" id="modalContent">
        <!-- <div class="text-center">Loading...</div> -->
      </div>
    </div>
  </div>
</div>

                        </div>
                </div>
                
            </div>
        </div>
        <?php } ?>

        </div>
<!-- /.row -->


</section>
@push('styles')
<link rel="stylesheet" type="text/css"
    href="{{ asset('assets/plugins/dashboard_date/bootstrap-datepicker.min.css') }}">
    <link rel="stylesheet" type="text/css"
    href="{{ asset('assets/plugins/dashboard_date/bootstrap-datepicker.css') }}">
@endpush
   
@push('scripts')
	<!-- Vendor JS -->
	
    <script src="{{asset('assets/plugins/dashboard_date/moment.min.js') }}"></script>
    <script src="{{asset('assets/plugins/dashboard_date/bootstrap-datepicker.min.js') }}"></script>
    <script src="{{ asset('assets/plugins/dashboard_date/daterangepicker.js') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/imask"></script>
<script>
    var phoneInput = document.getElementById('mobile');
    var phoneMask = new IMask(phoneInput, {
        mask: '(000) 000-0000'
    }); 
    </script>


    <script>
    function callButton(value)
    {
        $("#show_value").val(value);
        $('#form_submit').trigger('click'); 
    }
</script>
 

<script>
    var myAudio = document.getElementById("myAudio");
    var isPlaying = false;

function togglePlay(valued) {
    $("#pause_"+valued).show();
    $("#play_"+valued).hide();

  isPlaying ? myAudio.pause() : myAudio.play();
};

function togglePause(valued) {
    $("#pause_"+valued).hide();
    $("#play_"+valued).show();

 myAudio.pause();
};

myAudio.onplaying = function() {
  isPlaying = true;
};
myAudio.onpause = function() {
  isPlaying = false;
};
</script>
<script type="text/javascript">
    $(function () {
        var startDate = "{{ $startDate }}";
        var endDate = "{{ $endDate }}";
        $('#reservation2').daterangepicker({
            locale: { format: 'YYYY-MM-DD' },
            autoUpdateInput: false, // Prevent auto-filling the input field
            opens: 'right'
        }, function (start, end) {
            $('#start_date').val(start.format('YYYY-MM-DD'));
            $('#end_date').val(end.format('YYYY-MM-DD'));
            $('#reservation2').val(start.format('YYYY-MM-DD') + ' - ' + end.format('YYYY-MM-DD')); // Update the input field with the selected date range
        });

        // If both start and end dates are provided, set them in the input field
        if (startDate && endDate) {
            $('#reservation2').data('daterangepicker').setStartDate(startDate);
            $('#reservation2').data('daterangepicker').setEndDate(endDate);
            $('#reservation2').val(startDate + ' - ' + endDate);
        } else {
            $('#reservation2').val(''); // Ensure the input field is empty initially
        }
    });
</script>


@endpush

@endsection



