@php
    use App\Models\Mongo;
    use App\Models\Patient;
@endphp
<style>
    .heightoftr {
        padding: 3px 5px !important;
    }
</style>
<div class="card-ipad">
    <div class="row p-3 ">
        <div class="col-12 text-end">
            <h3 class=>{{ $view['room_tap']['room_name'] }}</h3>
        </div>
        <div class="row">
            <div class="col-2">
                <h5 class=>HN</h5>
            </div>
            <div class="col-4">
                <h5 class=>Name</h5>
            </div>
            <div class="col-3">
                <h5 class=>Procedure</h5>
            </div>
        </div>
        <div class="row">
            {{-- @dd($view['tb_casemonitor']); --}}
            {{-- @dd($view['tb_casemonitor']['operation']) --}}
            @isset($view['tb_casemonitor']['operation'])
                @forelse ($view['tb_casemonitor']['operation'] as $hn=>$data3)
                    @php
                        if (
                            $data3['room'] != $view['room_tap']['room_name'] &&
                            $data3['room'] != $view['room_tap']['room_id']
                        ) {
                            continue;
                        }

                        $data3 = (object) $data3;

                        $count = count($data3->procedure);
                        $hn3 = @$data3->hn . '';
                        $procs = @$data3->procedure;
                        $app = @$data3->appointment . '';
                        $desc = @$data3->description . ' No Description';
                        $caseuniq = isset($data3->caseuniq) ? $data3->caseuniq : '';

                        $patientname = Patient::fullname_patient($hn3);
                        $ids = [];
                        foreach (isset($data3->_id) ? $data3->_id : [] as $key => $id) {
                            $id = (array) $id;
                            $ids[] = $id['oid'];
                        }
                        // dd();
                    @endphp

                    <div class="col-2">
                        <h5 class="text-detail">{{ $hn3 }}</h5>
                    </div>
                    <div class="col-4">
                        <h5 class="text-detail">{{ $patientname }}</h5>
                    </div>
                    <div class="col-3">
                        <span class=" text-detail">
                            @isset($data3->procedure)
                                @php
                                    $proc = (array) $data3->procedure;
                                    // dd($proc)
                                @endphp
                                {{ implode(' , ', $proc) }}
                            @endisset
                        </span>


                    </div>
                    <div class="col-3">
                        {{-- @dd($data3); --}}
                        <span class="badge badge-soft-danger fw-normal mt-1">{{ $data3->statusjob[0] }}</span>
                        {{-- <span class="badge badge-soft-success fw-normal mt-1">REPORTING</span> --}}
                    </div>
                @empty
                    <div class="col-12 text-center">
                        {{-- <h3>No Data</h3> --}}
                    </div>
                @endforelse
            @endisset
        </div>
        <div class="border-bottom m-0"></div>
    </div>
    <div class="table-scroll">
        <table class="table table-nowrap booking">
            {{-- @dd(1); --}}
            <tbody>

                {{-- @dd($view['tb_casemonitor']) --}}
                @isset($view['tb_casemonitor']['holding'])
                    @forelse ($view['tb_casemonitor']['holding'] as $hn=>$data2)
                        {{-- @dd($view) --}}
                        @php
                            if (
                                $data2['room'] != $view['room_tap']['room_name'] &&
                                $data2['room'] != $view['room_tap']['room_id']
                            ) {
                                continue;
                            }

                            $data2 = (object) $data2;

                            $count = count($data2->procedure);
                            $hn = @$data2->hn . '';
                            $patientname = Patient::fullname_patient($hn);
                            $procs = @$data2->procedure;
                            $app = @$data2->appointment . '';
                            if (@$data2->description != '') {
                                $desc = $data2->description;
                            } else {
                                $desc = 'No Description';
                            }
                            $caseuniq = isset($data2->caseuniq) ? $data2->caseuniq : '';
                            $ids = [];
                            foreach (isset($data2->_id) ? $data2->_id : [] as $key => $id) {
                                $id = (array) $id;
                                $ids[] = $id['oid'];
                            }
                            // dd();
                        @endphp

                        <tr style="background: #2B2F34; ">
                            <td width="10%" class="text-center">
                                <span class="badge badge-dark fw-normal">{{ @$data2->timevisit }}</span><br>
                                <span class="" style="font-size: 13px; color:#ffffffc0; font-weight: 300;">
                                    {{ @$data2->location }}
                                </span>
                            </td>
                            <td width="10%" class="">
                                <span class=" text-detail">{{ $hn }}
                                </span> <br>
                                <span class="text-danger text-detail">{{ $desc }}</span>
                                {{-- <span class="text-danger text-detail"></span> --}}

                            </td>
                            <td width="25%">
                                <span class=" text-detail">{{ $patientname }}</span>
                            </td>
                            <td class="text-dot ">
                                <span class=" text-detail">
                                    @isset($data2->procedure)
                                        {{-- @foreach ($data2->procedure as $p) --}}
                                        {{-- {{@trim($p)}}
                                        @if ($count > 1)
                                            ,
                                        @endif --}}
                                        @php
                                            $proc = (array) $data2->procedure;
                                            // dd($proc)
                                        @endphp
                                        {{ implode(',', $proc) }}
                                        {{-- @endforeach --}}
                                    @endisset
                                </span>
                            </td>
                            <td class="text-dot ">
                                <span class=" text-detail">{{ $data2->physician }}</span>
                            </td>
                            {{-- @dd($data2->caseuniq)[0] --}}
                            <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('{{ $data2->caseuniq[0] }}')">Location</button>
                                            <button type="button" class="dropdown-item btn-white-custom btn-description"
                                                onclick="create_description('{{ $data2->caseuniq[0] }}')">Description</button>
                                            <button type="button" class="dropdown-item btn-white-custom"
                                                onclick="edit_room('{{ $data2->caseuniq[0] }}')">Select Room</button>
                                            <button type="button" class="dropdown-item btn-white-custom"

                                            onclick="edit_case('{{ json_encode(@$data2->caseuniq) }}')">Edit list</button>
                                            {{-- <button type="button" class="dropdown-item btn-white-custom"
                                                onclick="cancel_case('{{ $data2->caseuniq[0] }}')">Cancel</button> --}}
                                                <button type="button" class="dropdown-item btn-white-custom"
                                                    onclick="cancel_case('{{ json_encode(@$data2->caseuniq) }}')">
                                                    Cancel
                                                </button>
                                        </div>
                                    </div>

                                </div>

                            </td>
                        </tr>

                        <tr>
                            <td class="heightoftr" colspan="6" style="background: #222529; height: px;"></td>
                        </tr>

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

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


    <div class="row footer-casemonitor px-2 w-100   ">
        {{-- @dd($view) --}}
        <div class="col-6">
            <b>Physician</b>

            <div class="col-12">
                @foreach ($view['room_doctor'] ?? [] as $d)

                    @php

                        $doctor = Mongo::table("users")
                        ->where('id', intval($d))
                        ->where("user_type" , "doctor")
                        ->first();

                    @endphp

                    {{fullname($doctor)}}
                @endforeach

            </div>
        </div>
        <div class="col-6">
            <b>Assistant</b>
            <div class="col-12">
                @foreach ($view['room_nurse'] ?? [] as $n)
                @php

                    $nurse = Mongo::table("users")
                    ->where('id', intval($n))
                    ->where("user_type" ,"!=" , "doctor")
                    ->first();

                @endphp

                {{ implode('|', [fullname($nurse)]) }}
            @endforeach
            </div>
        </div>
    </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);
    });
});
</script>
