<?php
 include('dashboard.php');
require 'vendor/autoload.php';

use Twilio\Rest\Client;

$account_sid = 'AC8bac449b32a1fe6070f736d142cbac1f';
$auth_token = '8e489af7dfa15b610298953e184de89d';

$client = new Client($account_sid, $auth_token);
$number = '+13322308524';

// Fetch calls where number is the caller
$fromCalls = $client->calls->read(['from' => $number]);

// Fetch calls where number is the callee
$toCalls = $client->calls->read(['to' => $number]);


// Merge both arrays
$allCalls = array_merge($fromCalls, $toCalls);

?>

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<style>
.dataTables_wrapper {
	padding: 25px;
}

main {
    width: 78%;
    top: 94px;
    left: unset;
    right:1%;
    height: 88%;
}

main h1 {
	position: absolute;
	top: -9px;
	left: 36%;
	font-size: 29px;
	text-decoration: underline;
	font-weight: 600;
	color: #2d54c6;
}
.main-content-inner{
    margin-top:50px;
}

@media(max-width:1024px){
    main{
        width:77%;
    }
}
@media(max-width:912px){
    main{
        width:76%;
    }
}
@media(max-width:767px) {
	main h1 {
		font-size: 22px;
	}
	main {
    width: 96%;
    top: 94px;
    left: unset;
    right: 2%;
    border-radius: 4px;
    height:86% !important;
}

	.dataTables_wrapper {
		min-width: 830px;
	}

	.dataTables_wrapper .dataTables_length {
		float: left;
		margin-top: 10px;
		margin-left: 10px;
	}

	.dataTables_wrapper .dataTables_filter {
		margin-left: 20px !important;
	}
}

@media (max-width: 450px) {
    main{
        transform:unset;
    }
}
</style>
<main>
    <h1>CALL DETAILS</h1>
    <div class="main-content-inner">

        <div class="row">
            <div class=" col-ml-12">

            </div>
        </div>

        <table id="example" class="display" style="width:100%">
            <thead>
                <tr>
                    <th>Sr.No</th>
                    <th>Direction</th>
                    <th>From</th>
                    <th>To</th>
                    <th>Status</th>
                    <th>Recording</th>
                    <th>Date and Time</th>
                    <th>Redial</th>

                </tr>
            </thead>
            <tbody>

                <?php
                $i = 1;

                    foreach ($allCalls as $call) {
                        $date_time_obj = (array)$call->dateCreated;
                          // Use parent SID if available, else current call SID
                            $callSidToFetch = $call->parentCallSid ?: $call->sid;
                        
                            $recordings = $client->calls($callSidToFetch)->recordings->read();
                                                 
                        $recordingUrl = '';
                    
                        if (!empty($recordings)) {
                            $recording = $recordings[0]; // Assuming only one recording per call
                            $recordingUrl = "https://api.twilio.com" . str_replace(".json", ".mp3", $recording->uri);
                        }
                ?>
                        <tr>
                            <td><?php echo $i++; ?></td>
                            <td><?php echo $call->direction; ?></td>
                            <td><?php echo $call->from; ?></td>
                            <td><?php echo $call->to; ?></td>
                            <td><?php echo $call->status; ?></td>
                            <td>
                                <?php if ($recordingUrl): ?>
                                    <a href='<?php echo $recordingUrl; ?>' target='_blank'>Recording</a>
                                <?php else: ?>
                                    No Recording
                                <?php endif; ?>
                             </td>      
                             <td><?php echo  $date_time_obj['date'];  ?></td>
                             <td>
                                <form method="POST" action="redial.php">
                                    <input type="hidden" name="to" value="<?php echo $call->from == $number ? $call->to : $call->from; ?>">
                                    <button type="submit" class="btn btn-primary btn-sm">Redial</button>
                                </form>
                            </td>
                        </tr>
                <?php
                    }
                
                ?>
            </tbody>
            <tfoot>
                <tr>
                    <th>Sr.No</th>
                    <th>Direction</th>
                    <th>From</th>
                    <th>To</th>
                    <th>Status</th>
                    <th>Recording</th>
                    <th>Date and Time</th>
                    <th>Redial</th>
                </tr>
            </tfoot>
        </table>
    </div>
</main>
<?php
include('footer.php');

?>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script>
    $(document).ready(function() {
        $('#example').DataTable();
    });
</script>