@php
    use App\Models\Patient;
@endphp
<style>
    /* #table_casetoday tr:nth-child(odd){
        background: red;
    }
    #table_casetoday tr:nth-child(even){
        background: blue;
    } */
     .fs-12 {
        font-size: 12px;
     }
     .bold{
        font-weight: bold;
     }
     .fs-11 {
        font-size: 11px !important;
     }

</style>
@php

    $i = 0;
    $admin = getCONFIG('admin');
@endphp

@foreach ($alltoday_case as $in => $data)
    @if (isset($data['case_hn']) || isset($data['hn']))
        @php

            $data = (object) $data;
            $hn = isset($data->case_hn) ? $data->case_hn : $data->hn;
            $patientname = isset($data->patientname) ? $data->patientname : $data->firstname . ' ' . $data->lastname;
            $physician = isset($data->doctorname) ? $data->doctorname : $data->physician;
            $procedure = isset($data->procedurename) ? $data->procedurename : "";
            $statuspacs = isset($data->statuspacs) ? $data->statuspacs : [];
            $statusvna = isset($data->vna) ? $data->vna : [];
            // $statusjob = isset($data->statusjob) ? $data->statusjob : "";
    // dd($data->statusjob);
            $_id = $data->_id;
            // dd($room);


            $appointment = isset($data->appointment) ? explode(' ', $data->appointment) : null;
            $date = isset($appointment) ? format_date($appointment[0], 'd M Y') : '';
            $desc = @$data->description . '';
            // if (in_array('operation', $statusjob)) {
            //     $statusjob = ['operation'];
            // } else {
            //     if (in_array('holding', $statusjob) && find_duplicate($statusjob, 'holding')) {
            //         $statusjob = ['holding'];
            //     } elseif (in_array('recovery', $statusjob) && !find_duplicate($statusjob, 'discharged')) {
            //         $statusjob = ['recovery'];
            //     } elseif (in_array('discharged', $statusjob) && find_duplicate($statusjob, 'discharged')) {
            //         $statusjob = ['discharged'];
            //     }
            // }
            $case_pdfversion =
                isset($data->case_pdfversion) && @$data->case_pdfversion != '' ? $data->case_pdfversion : [];
            $count_pdf = is_array($case_pdfversion) ? count($case_pdfversion) : 0;
            $patient_tel = get_patient_data($hn, 'phone');



                $urease_other = $data->rapid_other??'';

            // $urease_other = isset($data->rapid_other) ? implode('', $data->rapid_other) : '';
            // dd( $urease  ,$urease_other );
        @endphp
        <tr>
            <td data-text="{{ Patient::show_hn($hn)}}" class="index" style="white-space: nowrap;">
                {{-- @if (count($procedure) > 1)
                    <a href="javascript:;"
                        onclick="open_same_hn('{{ $data->hn }}', '{{ $data->_id }}', '{{ @$more_cases }}', 'capture/procedure')">
                        <span style="color: #245788;">{{ $hn }}</span>
                    </a>
                @else --}}
                <a href="{{ url("procedure/$_id") }}" class="">
                    <span style="color: #245788;">&ensp;&ensp;{{ Patient::show_hn($hn) }}</span>
                </a>
                {{-- @endif --}}
            </td>

            <td data-text="{{ $patientname }}">
                {{-- @if (count($procedure) > 1)
                    <a href="javascript:;"
                        onclick="open_same_hn('{{ $data->hn }}', '{{ $data->_id }}', '{{ @$more_cases }}', 'capture/procedure')">
                        <span style="color: #245788;">{{ @$patientname }}</span>
                    </a>
                @else --}}
                <a href="{{ url("procedure/$_id") }}" class="">
                    <span style="color: #245788;">{{ @$patientname }}</span>
                </a>
                {{-- @endif --}}
            </td>


            <td>
                @php
                    $statusjob = $data->statusjob;
                @endphp

                @if ($statusjob == 'discharged' || $statusjob == 'Discharged')
                <div class="">
                    <span class="badge-soft-success p-1 px-1 fs-11 bold">DISCHARGED</span> <br>
                </div>
            @endif

            @if ($statusjob == 'recovery')
                <div class="">
                    <span class="badge-soft-secondary p-1 fs-11 bold px-1">RECOVERY</span> <br>

                </div>
            @endif

            @if ($statusjob == 'holding')
                <div class="">
                    <span class="badge-soft-warning p-1 px-1 fs-11 bold">HOLDING</span> <br>
                </div>
            @endif

            @if ($statusjob == 'operation')
                <div class="">
                    <span class="badge-soft-danger p-1 px-1 fs-11 bold">OPERATION</span> <br>
                </div>
            @endif
                {{-- @foreach ($statusjob as $data2)
                    @if ($data2 == 'discharged' || $data2 == 'Discharged')
                        <div class="">
                            <span class="badge-soft-success p-1 px-1 fs-12 bold">DISCHARGED</span> <br>
                        </div>
                    @endif

                    @if ($data2 == 'recovery')
                        <div class="">
                            <span class="badge-soft-secondary p-1 fs-12 bold px-1">RECOVERY</span> <br>

                        </div>
                    @endif

                    @if ($data2 == 'holding')
                        <div class="">
                            <span class="p-1 badge-soft-warning px-1 fs-12 bold">HOLDING</span> <br>
                        </div>
                    @endif

                    @if ($data2 == 'operation')
                        <div class="">
                            <span class="badge-soft-danger p-1 px-1 fs-12 bold">OPERATION</span> <br>
                        </div>
                    @endif
                @endforeach --}}

            </td>

            <td>
                @php
                    $numpacs = 0;
                @endphp
                {{$procedure}}
            </td>
            <td>
                @isset($physician)
                    {{ $physician }}
                @else
                    -
                @endisset
            </td>
            <td>
                @if(isset($data->room))
                    Station {{ @$data->room }}
                @else
                    Screening
                @endif
            </td>
            <td>
                {{-- @dd($urease_other); --}}

                {{-- @isset($urease_other)
                    @if($urease_other == 'Positive (+)')
                    <span class="badge rounded-pill badge-soft-danger">Positive (+)</span>
                    @elseif($urease_other == 'Negative (-)')
                    <span class="badge rounded-pill badge-soft-success">Negative (-)</span>
                    @elseif($urease_other == 'Pending')
                    <span class="badge rounded-pill badge-soft-warning">Pending</span>
                @endif
                @endisset --}}
                {{-- @dd($procedure) --}}
                @isset($urease_other)
                    @if (strlen($urease_other) > 15 || $urease_other == 'Pending')
                        <button class="btn modal_ureasetest btn-soft-warning btn-sm fs-14" sub-name="pending"
                            cid="{{ @$_id }}" hn="{{ @$hn }}" patientname="{{ @$patientname }}"
                            contact="{{ @$patient_tel }}" select="{{ @$urease }}" other="{{ @$urease_other }}"
                          >Pending</button>
                    @endif
                    @if ($urease_other == 'Positive (+)')
                        <button class="btn modal_ureasetest btn-soft-danger btn-sm fs-14" sub-name="positive"
                            cid="{{ @$_id }}" hn="{{ @$hn }}" patientname="{{ @$patientname }}"
                            contact="{{ @$patient_tel }}" select="{{ @$urease }}" other="{{ @$urease_other }}"
                           >Positive(+)</button>
                    @endif
                    @if ($urease_other == 'Negative (-)')
                        <button class="btn modal_ureasetest btn-soft-success btn-sm fs-14" sub-name="negative"
                            cid="{{ @$_id }}" hn="{{ @$hn }}" patientname="{{ @$patientname }}"
                            contact="{{ @$patient_tel }}" select="{{ @$urease }}" other="{{ @$urease_other }}"
                          >Negative (-)</button>
                    @endif
                @else
                    -
                @endisset
            </td>
            <td>
                @if (isset($data->prediagnosis_other) && @$data->prediagnosis_other . '' != '')
                    {{ $data->prediagnosis_other }}
                @else
                   -
                @endif
            </td>
            <td class="index" style="white-space: nowrap;">


                @php
                    $cid = $data->_id;
                    $hn = $data->case_hn;
                    $patientname = $data->patientname;
                    $procedurecode = @$data->case_procedurecode;
                    $procedure = $data->procedurename;
                    $date = $data->appointment_date;
                    $physician = $data->doctorname;
                    $statusjob = $data->statusjob;

                    //##########################//
                    $temp['cid'] = (string)$cid;
                    $temp['hn'] = @$data->case_hn;
                    $temp['procedurecode'] = @$data->case_procedurecode;
                    $temp['procedure'] = @$data->procedurename;
                    $temp['date'] = @$data->appointment_date;
                    $temp['physician'] = @$data->doctorname;
                    $temp['patientname'] = @$data->patientname;
                    $temp['gender'] = @$data->gender;
                    $temp['age'] = @$data->age;
                    $temp['room_name'] = @$room_name ?? "";



                    // dd($temp);
                    $json = json_encode($temp);
                    $bin = bin2hex($json);
                @endphp
    {{-- @dd(1) --}}
                @if(uget('user_type') == 'endo')

                <button type="button" class="btn btn-icon btn-loadicon btn-danger btn_operation" data-cid="{{ $cid }}" data-casedata="{{ $bin }}">
                    <i class="ri-camera-fill ri-lg"></i>
                </button>


                {{-- <a id="update_operation" href="http://localhost/screening/operation?casedata={{ $bin }}" class="btn btn-icon btn-loadicon btn-danger"><i
                            class="ri-camera-fill ri-lg"></i>
                        </a> --}}

                @endif
                    <a href="{{ url("procedure/$_id") }}" class="btn btn-loadicon btn-icon btn-primary"><i
                            class="ri-folder-open-fill ri-lg"></i></a>
                    <a href="{{ url("report/$_id") }}" class="btn btn-loadicon btn-icon btn-blue" id="btn_dis"
                        @if ($statusjob == "operation" || $statusjob == "Operation" || $statusjob == "holding" || $statusjob == "Holding")
                          style="pointer-events: none;
                          background-color:#F3F6F9;
                          color: #707070 " @endif>
                        <i class="ri-file-text-fill  ri-lg "></i>
                    </a>

            </td>
        </tr>

        @php
            $i = $i + 1;
        @endphp
    @endif
@endforeach



<script>
        $(".btn_operation").click(function() {
        let cid = $(this).data('cid')
        let casedata = $(this).data('casedata')
        socket.emit('chat message', 'refresh_dashboard')
        window.location.href = "http://localhost/screening/operation?casedata=" + casedata
    });




    $(".modal_ureasetest").click(function() {
        // alert(1);
        $('#span_patientname').html('')
        $('#span_hn').html('')
        $('#span_contact').html('')
        let subname = $(this).attr('sub-name')
        let count = $(this).attr('count')
        let hn = $(this).attr('hn')
        let phone = $(this).attr('contact')
        if (count > 1) {
            $.post('{{ url('api') }}/home', {
                event: "get_only_egd",
                hn: hn,
            }, function(data, status) {
                let parse = JSON.parse(data)
                console.log(hn);
                let id = parse._id
                id = id['$oid']
                console.log(parse);
                $(`#urease_${subname}`).prop('checked', true)
                $("#ureasetest").modal('show');
                $('#span_ureahn').html(parse.case_hn)

                $('#span_patientname').html(parse.patientname)
                $('#span_contact').html(phone)
                $('#urease_text').val(parse.rapid_other)
                $('#case_id').val(id)
            })
        } else {
            // alert(3)
            // console.log(data);
            $(`#urease_${subname}`).prop('checked', true)
            $("#ureasetest").modal('show');
            $('#span_ureahn').html(hn)
            $('#span_patientname').html($(this).attr('patientname'))
            $('#span_contact').html($(this).attr('contact'))
            $('#urease_text').val($(this).attr('other'))
            $('#case_id').val($(this).attr('cid'))
        }

    });


    function change_urease_text(type) {
        let text = ''
        if (type.includes('positive')) {
            text = 'Positive (+)'
        } else if (type.includes('negative')) {
            text = 'Negative (-)'
        } else if (type.includes('pending')) {
            text = ' Positive [   ]         Negative [   ]'
        }
        $('#urease_text').val(text)
    }
</script>
