@php
    use App\Models\Mongo;
@endphp
<style>
    .dropdown-menu {
        padding: 0px;
    }

    .badge-dark {
        background: #000000;
        color: #ffffffc0;
        padding-left: 6px;
        padding-top: 4px;
        border: 1px solid #000000;
        border-radius: 4px;
        background-size: auto;
    }
</style>
<div class="card-ipad">
    <div class="row p-3 ">
        <div class="col-12 text-end">
            <h3 class=>Booking</h3>
        </div>
    </div>

    <div class="table-scroll">
        <table class="table table-nowrap booking">

            <thead>

                <tr>
                    <td width="11%"></td>
                    <td width="14%">HN</td>
                    <td width="20%">Name</td>
                    <td width="22%">Procedure</td>
                    <td width="22%">Endoscopist</td>
                    <td width="11%"></td>
                </tr>
            </thead>
            {{-- @dd($tb_booking) --}}




            <form action="{{ url('tablet/casemonitor') }}" method="POST">
                @csrf
                <tbody style="font-size: 13px;">
                    @foreach ($tb_booking as $case_booking)
                        {{-- @dd($case_booking) --}}
                        @php
                            $case_booking = (object) $case_booking;
                            $count = count($case_booking->procedure);
                        @endphp
                        <input type="hidden" id="case_m_hn" value="{{ $case_booking->hn }}">
                        <input type="hidden" name="event" value="createcaselist">
                        <input type="hidden" name="id" id="book_id" value="{{ $case_booking->_id }}">
                        <tr>
                            <td class="text-center">
                                <button type="submit" class="btn btn-success btn-sm">Check in </button>
                            </td>
                            <td>
                                <span class=" text-detail">{{ $case_booking->hn }}
                                </span> <br>
                                <span class="text-danger">{{ @$case_booking->description }}</span>
                            </td>
                            <td>
                                @php
                                    $ex = explode(' ', @$case_booking->patient_name);
                                    $firstname = @$ex[1];
                                    $lastname = @$ex[3] ? @$ex[3] : @$ex[2];
                                    // dd($ex);
                                @endphp
                                {{ @$firstname }} {{ @$lastname }}
                            </td>
                            <td class="text-dot" style="color: #bbbbbb;">
                                @foreach ($case_booking->procedure as $val)
                                    {{-- @dd($case_booking) --}}
                                    @php
                                        $tb_procedure = Mongo::table('tb_procedure')->where('code', $val)->first();
                                        $procedure[] = @$tb_procedure['name'];
                                    @endphp
                                    {{ @$tb_procedure['name'] }}
                                    @if (is_array($tb_procedure) && count($tb_procedure) > 1)
                                        {{ implode(', ', array_column($tb_procedure, 'name')) }}
                                    @endif
                                @endforeach
                            </td>
                            <td class="text-dot">
                                {{ @$case_booking->physician_name }}
                            </td>

                        </tr>
                    @endforeach

                    {{-- @dd($tb_casemonitor); --}}
                    @isset($tb_casemonitor['register'])
                        @forelse (isset($tb_casemonitor['register']) ? $tb_casemonitor['register'] : [] as $data)
                            @php
                                // dd($data);
                                $data = (object) $data;

                                $hn = @$data->hn . '';
                                $count = count($data->procedure);
                                $procedure = @$data->procedure;
                                $appoint = @$data->appointment ?? '';
                                if (@$data->description != '') {
                                    $desc = $data->description;
                                } else {
                                    $desc = 'No Description';
                                }
                                $ids = [];
                                foreach (isset($data->_id) ? $data->_id : [] as $key => $id) {
                                    $id = (array) $id;
                                    $ids[] = $id['oid'];
                                }
                            @endphp
                            {{-- <input type="hidden" id="muti_hide_caseid" value="{{ $data->caseuniq[0] }}"> --}}
                            <tr>
                                <td width="10%" class="text-center">
                                    <span class="badge badge-dark fw-normal"
                                        style="font-size: 13px; color:#ffffffc0; font-weight: 100;">{{ @$data->timevisit }}</span><br>
                                    <span> {{ @$data->location }}</span>
                                </td>
                                <td>
                                    <span class=" text-detail" style="font-size: 13px;">{{ $hn }}
                                    </span> <br>
                                    <span class="text-danger" class="description">{{ $desc }}</span>
                                </td>
                                <td>
                                    {{ @$data->patientname }}
                                </td>
                                <td class="text-dot" style="font-size: 13px;">
                                    @isset($data->procedure)
                                        @php
                                            $proc = (array) $data->procedure;
                                            // dd($proc)
                                        @endphp
                                        {{ implode(' , ', $proc) }}
                                    @endisset
                                </td>
                                <td class="text-dot" style="font-size: 13px;">
                                    {{ @$data->physician }}
                                </td>
                                {{-- @dd($data->caseuniq); --}}
                                <td class="text-end">
                                    <div class="btn-group-vertical" role="group" aria-label="Vertical button group">
                                        <div class="btn-group" role="group">
                                            <button id="btnGroupVerticalDrop1" type="button"
                                                class="btn btn-white-custom btn-sm dropdown-toggle"
                                                data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                Edit
                                            </button>
                                            <div class="dropdown-menu" aria-labelledby="btnGroupVerticalDrop1">
                                                <button type="button" class="dropdown-item btn-white-custom"
                                                    onclick="edit_location('{{ $data->caseuniq[0] }}')">Location</button>
                                                <button type="button"
                                                    class="dropdown-item btn-white-custom btn-description"
                                                    data-description="{{ addslashes(@$desc) }}"
                                                    onclick="create_description('{{ $data->caseuniq[0] }}')">
                                                    Description
                                                </button>
                                                <button type="button" class="dropdown-item btn-white-custom"
                                                    onclick="edit_room('{{ $data->caseuniq[0] }}')">Select Room</button>
                                                {{-- <button type="button" class="dropdown-item btn-white-custom" onclick="edit_case('{{ json_encode($data->caseuniq) }}')">Edit list</button> --}}
                                                <button type="button" class="dropdown-item btn-white-custom"
                                                    onclick="cancel_case('{{ json_encode($data->caseuniq) }}')">
                                                    Cancel
                                                </button>
                                            </div>
                                        </div>

                                    </div>

                                </td>
                            </tr>
                        @empty
                            <div class="col-12 text-center ">

                            </div>
                        @endforelse
                    @endisset
                </tbody>

            </form>

        </table>
    </div>


</div>

<script>
    $(document).ready(function() {
        $(".btn-description").on('click', function() {
            // Retrieve the description safely
            var des = $(this).data('description'); // ใช้ data attribute แทน PHP
            console.log(des);

            // Set the value of the modal field
            $(".modal_description_value").val(des);
        });
    });

    //  $('#confirm_hide_btn').on('click', function() {
    //         let caseuniq = $('#hide_caseid').val()
    //         // alert(caseuniq);
    //         $.post('{{ url('tablet/casemonitor') }}', {
    //             event: 'hide_case',
    //             caseuniq: caseuniq
    //         }, function(data, status) {
    //             socket.emit('chat message', 'casemonitor');
    //         });
    //     })




    //  function cancel_case(id_json) {

    //         if (id_json != '' && id_json != undefined) {
    //                 let ids = JSON.parse(id_json)

    //                 ids = ids != undefined && ids != '' && Array.isArray(ids) ? ids : []


    //                 if (ids.length == 1) {

    //                     $.post('{{ url('api/casemonitor') }}', {
    //                         event: 'get_2_procedure',
    //                         caseuniq: id_json

    //                     }, function(data, status) {
    //                        let casemonitor_patient = JSON.parse(data)
    //                             console.log(casemonitor_patient);
    //                             $('#modal_cancelcase_hn').html(casemonitor_patient.monitor_hn);
    //                             $('#modal_cancelcase_fullname').html(casemonitor_patient.monitor_patientname)
    //                             $('#modal_cancelcase_procedure').html(casemonitor_patient.monitor_procedure)
    //                             $('#modal_cancelcase_doctor').html(casemonitor_patient.monitor_doctorname)
    //                     });



    //                     $('#confirm_hide_modal').modal('show')
    //                 } else {


    //                     get_casemonitor_detail(id_json, 'hide')
    //                 }
    //                 $('#hide_caseid').val(id_json)


    //             }

    //     }
</script>
