@extends('layout.app')

@section('content')
    <div class="card">
        <div class="card-body">


                <div class="col-md-3 d-flex-justify-content-center">
                    Client Name : {{$client->clientsinfo->clients_name ?? '' }}
                </div>

                <table class="table responsibe">
                    <thead>
                        <th>Date</th>
                        <th>Edit By</th>
                        <th>Old Data</th>
                        <th>New Data</th>
                    </thead>

                    <tbody>
                        @foreach($client->clientEditLog as $log)

                        @php
                            $old = json_decode($log->old_data);
                            $new = json_decode($log->new_data);

                            $old_client_info = $old->clientsinfo;
                            $new_client_info = $new->clientsinfo;

                            $change_old =[];
                            $change_new = [];
                            foreach ($old as $key => $value ) {
                                if($old->$key != $new->$key && $key != "clientsinfo" && $key != "updated_at"){
                                // if( $key != "clientsinfo" && $key != "updated_at"){
                                    $change_old[$key] = $old->$key;
                                    $change_new[$key] = $new->$key;
                                }

                            }

                            $change_old_clientinfo = [];
                            $change_new_clientinfo = [];
                            foreach ($old_client_info as $key => $value ) {
                                if($old_client_info->$key != $new_client_info->$key && $key != "updated_at"){
                                // if( $key != "updated_at"){
                                    $change_old_clientinfo[$key] = $old_client_info->$key;
                                    $change_new_clientinfo[$key] = $new_client_info->$key;
                                }

                            }
                        @endphp
                            <tr>
                                <td>{{$log->created_at}}</td>
                                <td>{{$log->user->name ?? '' }}</td>
                                <td>
                                    @if($change_old)
                                        @foreach($change_old as $key => $value)
                                            <span class="badge badge-primary">{{$key}}</span><span> : {{$value}}</span><br>
                                        @endforeach
                                    @endif
                                    @if($change_old_clientinfo)
                                        @foreach($change_old_clientinfo as $key => $value)
                                            <span class="badge badge-primary">{{$key}}</span><span> : {{$value}}</span><br>
                                        @endforeach
                                    @endif

                                </td>
                                <td>
                                    @if($change_new)
                                        @foreach($change_new as $key => $value)
                                            <span class="badge badge-warning">{{$key}}</span><span> : {{$value}}</span><br>
                                        @endforeach
                                    @endif
                                    @if($change_new_clientinfo)
                                        @foreach($change_new_clientinfo as $key => $value)
                                            <span class="badge badge-warning">{{$key}}</span><span> : {{$value}}</span><br>
                                        @endforeach
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>

        </div>
    </div>


@endsection

@section('ajax')

@endsection
