{{-- @extends('layouts.layouts_index.main') --}}
@extends('layouts.layouts_index.layoutv6')

@section('title', 'EndoINDEX')
@section('style')
<style>
    .table-w tr td:nth-child(1){width: 20%;}
    .table-w tr td:nth-child(2){width: 15%;}
    .table-w tr td:nth-child(3){width: 20%;}
    .table-w tr td:nth-child(4){width: 15%;}
    .table-w tr td:nth-child(5){width: 10%;}
    .table-w tr td:nth-child(6){width: 10%;}

    ::-webkit-scrollbar {
        width: 3px !important;
        height: 3px !important;
    }
    ::-webkit-scrollbar-track {
        background: #f1f1f1 !important;
    }
    ::-webkit-scrollbar-thumb {
        background: #888 !important;
    }
    ::-webkit-scrollbar-thumb:hover {
        background: #555 !important;
    }
</style>


@endsection
{{-- @section('title-left')
    <h4 class="mb-sm-0">Hospital Screening</h4>
@endsection
@section('title-right')
    <ol class="breadcrumb m-0">
        <li class="breadcrumb-item"><a href="javascript: void(0);">Hospital Screening</a></li>
        <li class="breadcrumb-item active">Setting</li>
    </ol>
@endsection --}}

@section('content')

<div class="card">
    <div class="card-body">

        <div class="row m-0">
            <div class="col-3">
                <div>
                    <input type="text" class="form-control bg-light" id="search_name" placeholder="Search hospital name..." oninput="function_search()">
                </div>
            </div>

            <div class="col-2">

                <select class="form-select mb-3 js-example-basic-single" id="search_province" aria-label="Default select example" onchange="function_search()">
                    <option value="">All provinces</option>

                        @foreach ($provinces as $key => $province)
                            <option value="{{$key}}">{{$province}}</option>
                        @endforeach

                </select>
            </div>
            <div class="col-auto " style="margin-left: auto;">
                <a href="{{url("admin/hospitalscreening/create")}}" type="button" class="btn btn-secondary waves-effect waves-light"> <i class="ri ri-add-line "></i> Add</a>
            </div>
        </div>
    </div>
</div>
<div class="card">
    <div style="overflow-y: scroll; max-height: 590px;">
        <div class="table-responsive table-card table-w m-1">
            <table class="table table-nowrap mb-0" id="table-search">
                <thead class="table-light">
                    <tr>
                        <th scope="col">Name</th>
                        <th scope="col">Code</th>
                        <th scope="col">Province</th>
                        <th scope="col">Status</th>
                        <th scope="col">Action</th>
                    </tr>
                </thead>
                <tbody id="tbody">
                    @isset($hospitals)
                        @foreach($hospitals as $hospital)
                            @isset($hospital['_id'])
                                <tr>
                                    <td><a class="fw-semibold">{{@$hospital['department_name']}}</a></td>
                                    <td>{{@$hospital['department_code']}}</td>
                                    <td>{{@$hospital['department_province_name']}}</td>
                                    <td>{{@$hospital['department_status']}}</td>
                                    <td>
                                        <a href="{{url("admin/hospitalscreening/".@$hospital['_id']."/edit")}}" class="btn btn-sm btn-success">edit</a>
                                    </td>
                                </tr>
                            @endisset
                        @endforeach
                    @endisset
                </tbody>
            </table>
        </div>
    </div>
</div>

@endsection

@section('script')
<script>
    function function_search() {
        let name = $('#search_name').val().toLowerCase();
        let provinceCode = $('#search_province').val();
        let provinceName = $('#search_province option:selected').text();

        $('#tbody tr').each(function() {
            let row = $(this);
            let hospitalName = row.find('td:eq(0)').text().toLowerCase();
            let hospitalProvince = row.find('td:eq(1)').text();

            let nameMatch = hospitalName.includes(name);
            let provinceMatch = !provinceCode || hospitalProvince === provinceName;

            if (nameMatch && provinceMatch) {
                row.show();
            } else {
                row.hide();
            }
        });
    }

    $(document).ready(function() {
        $('.js-example-basic-single').select2({
            width: '100%'
        });
    });
</script>
@endsection
