<x-layout>
    @section('title', $title.' - ')
    @section('page_title', $title)

    @include('admin/ar-css')
    @include('admin/ar-php-script')
    @include('admin/header_admin')

    <section id="dashboard" class="ar-dashboard-height">
        
        <div class="ar-dashboard-row" style="height:100%">
            <div id="ar-left-nav">
                @include('admin/left_nav')
            </div>

            <div id="ar-dashboard-content" style="flex-grow:1">
                <div style="margin:10px;text-align:center">

                    <div style="padding:40px 0">
                        <h3>Job Waiting List</h3>

                        <div class="container" style="text-align:left">
                            <div class="row">
                                <div class="col">
                                    <?
                                        $todayJob = '';
                                        $nextJob = '';
                                    
                                        if($bookings){
                                            foreach($bookings as $booking){
                                                $formattedDate = date('d-m-Y',strtotime($booking->appointment_date));
                                                
                                                if($booking->appointment_date == date('Y-m-d')){
                                                    $todayJob .= '<div style="background-color:lightgray;padding:10px;border-radius:10px;margin:5px;display:flex">
                                                                    <div style="display:table;margin-left:10px">
                                                                        <div style="display:table-row">
                                                                            <div style="display:table-cell;padding:0 10px">Tanggal</div>
                                                                            <div>:</div>
                                                                            <div style="display:table-cell;padding:0 10px">'. $formattedDate .'</div>
                                                                        </div>
                                                                        <div style="display:table-row">
                                                                            <div style="display:table-cell;padding:0 10px">Waktu Kunjungan</div>
                                                                                <div>:</div>
                                                                            <div style="display:table-cell;padding:0 10px">'. $booking->appointment_name .'</div>
                                                                        </div>
                                                                        <div style="display:table-row">
                                                                            <div style="display:table-cell;padding:0 10px">Customer</div>
                                                                                <div>:</div>
                                                                            <div style="display:table-cell;padding:0 10px">'. $booking->name .'</div>
                                                                        </div>
                                                                        <div style="display:table-row">
                                                                            <div style="display:table-cell;padding:0 10px">Telepon:</div>
                                                                                <div>:</div>
                                                                            <div style="display:table-cell;padding:0 10px">'. $booking->phone .'</div>
                                                                        </div>
                                                                        <div style="display:table-row">
                                                                            <div style="display:table-cell;padding:0 10px">Alamat</div>
                                                                                <div>:</div>
                                                                            <div style="display:table-cell;padding:0 10px">'. $booking->address .'</div>
                                                                        </div>
                                                                    </div>
                                                                    <!--
                                                                    <div class="dropdown" style="flex-grow:1;text-align:right">
                                                                        <button class="dropdown-ar1_exp" style="outline:none;border:none;padding:5px;border-radius:50%" data-toggle="dropdown">
                                                                            <i class="fa fa-ellipsis-v"></i>
                                                                        </button>
                                                                        <div class="dropdown-menu dropdown-menu-right dropdown-content1">
                                                                            <a href="/booking-billing?id=<?=$booking->id?>">Create Invoice</a>
                                                                        </div>
                                                                    </div>
                                                                    -->
                                                                 </div>';
                                                    
                                                }else{
                                                    $nextJob .= '<div style="background-color:lightgray;padding:10px;border-radius:5px">
                                                                    <div style="background-color:lightgray;padding:10px;border-radius:10px">
                                                                        <div style="display:table;margin-left:10px">
                                                                            <div style="display:table-row">
                                                                                <div style="display:table-cell;padding:0 10px">Tanggal</div>
                                                                                <div style="display:table-cell;padding:0 10px">'. $formattedDate .'</div>
                                                                            </div>
                                                                            <div style="display:table-row">
                                                                                <div style="display:table-cell;padding:0 10px">Waktu Kunjungan</div>
                                                                                <div style="display:table-cell;padding:0 10px">'. $booking->appointment_name .'</div>
                                                                            </div>
                                                                            <div style="display:table-row">
                                                                                <div style="display:table-cell;padding:0 10px">Customer</div>
                                                                                <div style="display:table-cell;padding:0 10px">'. $booking->name .'</div>
                                                                            </div>
                                                                            <div style="display:table-row">
                                                                                <div style="display:table-cell;padding:0 10px">Telepon</div>
                                                                                <div style="display:table-cell;padding:0 10px">'. $booking->phone .'</div>
                                                                            </div>
                                                                            <div style="display:table-row">
                                                                                <div style="display:table-cell;padding:0 10px">Alamat</div>
                                                                                <div style="display:table-cell;padding:0 10px">'. $booking->address .'</div>
                                                                            </div>
                                                                        </div>
                                                                    </div>
                                                                </div>';
                                                }
                                            }
                                        }
                                        
                                        if($todayJob != ''){
                                            echo '<a style="color:red">Daftar pekerjaan AR Clean hari ini:</a><br>'.$todayJob;
                                        }
                                        
                                        if($nextJob != ''){
                                            echo '<p></p>Pekerjaan hari berikutnya:<br>'.$nextJob;
                                        }
                                        
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div style="padding:40px 0">
                        <h3 style="color:red">Pending Invoices</h3>
                        <div class="container" style="text-align:left">
                            <div class="row">
                                <div class="col">
                                    <?
                                        if($billings){
                                            $output = '';
                                            foreach($billings as $billing){
                                                $jatuhTempo = '';
                                                if($billing->duedate_at){
                                                    $jatuhTempo = date("d-m-Y",strtotime($billing->duedate_at));
                                                }
                                                
                                                $output .=  '<div style="background-color:lightgray;padding:10px;border-radius:10px;display:flex;margin:5px">
                                                                <div style="display:table;margin-left:10px">
                                                                    <div style="display:table-row">
                                                                        <div style="display:table-cell;padding:0 10px">Customer</div>
                                                                        <div>:</div>
                                                                        <div style="display:table-cell;padding:0 10px">'. $billing->name .'</div>
                                                                    </div>
                                                                    <div style="display:table-row">
                                                                        <div style="display:table-cell;padding:0 10px">Billing ID</div>
                                                                        <div>:</div>
                                                                        <div style="display:table-cell;padding:0 10px">'. $billing->id .'</div>
                                                                    </div>
                                                                    <div style="display:table-row">
                                                                        <div style="display:table-cell;padding:0 10px">Tgl/Waktu Invoice</div>
                                                                        <div>:</div>
                                                                        <div style="display:table-cell;padding:0 10px">'. date("d-m-Y H:i:s",strtotime($billing->created_at)) .'</div>
                                                                    </div>
                                                                    <div style="display:table-row">
                                                                        <div style="display:table-cell;padding:0 10px">Termin (hari)</div>
                                                                        <div>:</div>
                                                                        <div style="display:table-cell;padding:0 10px">'. $billing->termin .'</div>
                                                                    </div>
                                                                    <div style="display:table-row">
                                                                        <div style="display:table-cell;padding:0 10px">Jatuh Tempo</div>
                                                                        <div>:</div>
                                                                        <div style="display:table-cell;padding:0 10px">'. $jatuhTempo .'</div>
                                                                    </div>
                                                                </div>
                                                                <div style="flex-grow:1;text-align:right">
                                                                    <button id="btn-update-status" data-id="'.$billing->id.'" style="outline:none;border:none;padding:5px;border-radius:50%">
                                                                        <i class="fa fa-pencil"></i>
                                                                    </button>
                                                                </div>
                                                            </div>';
                                                
                                            }
                                            echo $output;
                                        }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
            
        </div>
        
    </section>
    
    <x-payment_status_form/>
    
    <!-- Feather icons-->
    <script src="https://unpkg.com/feather-icons"></script>
    <!-- Feather Icons -->
    <script>
        feather.replace();
    </script>
    
    <script>
        $('body').on('click','#btn-update-status',function(e){
            $('#modal-update-status').modal('show');
            $("#id").val($(this).data('id'));
        });
        
        $('#btn-store').click(function(e){
            e.preventDefault();
            
            let formData = new FormData(document.querySelector('#form-billing-status'));
            let selectedStatus = formData.get('payment_status');
            if(selectedStatus == "null"){
                alert("Anda belum memilih status pembayaran");
                return;
            }

            $.ajax({
                headers: {
                    'X-CSRF-Token': "{{ csrf_token() }}" 
                },
                url: "billing-approve",
                method: "POST",
                cache: false,
                contentType: false,
                processData: false,
                data: formData,
                success: function(response)
                {
                    document.location.reload();
                }
            });

        });
         
    </script>
</x-layout>