<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    @include('layout.header')
    <link href="{{url(env("APP_URL").'storage/assets/libs/bootstrap-datepicker/css/bootstrap-datepicker.min.css')}}" rel="stylesheet" type="text/css" />
    <style>
        .datepicker{
            padding: 0.875rem 1.375rem !important;
        }

    </style>

</head>
<body>


<div class="container-scroller">
@include('layout.navbar')
<!-- partial -->
    @include('layout.breadscrum')
    <div class="container-fluid page-body-wrapper">

    @include('layout.sidebar')

    <!-- partial -->
        <div class="main-panel">
            <div class="content-wrapper">
                <div class="row">
                    <div class="col-md-12 grid-margin stretch-card">
                        <div class="card" style="min-height: 80vh;">
                            <div class="card-body">
                                <h4 class="card-title text-bold"><i class="mdi mdi-dots-vertical menu-icon"></i> {{__('patient.table_title')}}</h4>

                                <div class="row">
                                    <div class="col-sm-12">
                                        <form method="get" action="{{url('patient/search')}}">
                                            <div class="row">
                                                <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
                                                    <div class="input-group">
                                                        <input type="text" class="form-control rounded-top-left-25 rounded-bottom-left-25 rounded-right-0" value="{{isset($_GET['kword']) ? $_GET['kword'] : ''}}" name="kword" id="kword" placeholder="{{__('patient.search_placeholdolder')}}">
                                                        <div class="input-group-append">
                                                            <button class="btn btn-sm btn-inverse-secondary px-3 rounded-top-right-25 rounded-bottom-right-25" type="submit"><i class="fa fa-search"></i> {{__('general.btn_search')}}</button>
                                                            @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_patient']))
                                                                <button class="btn btn-sm btn-inverse-success waves-effect px-3 ml-2 rounded-25" type="button" data-action="new" data-toggle="modal" data-target="#modal-patient" data-backdrop="static"><i class="typcn typcn-plus"></i> {{__('patient.create_new')}}</button>
                                                            @endif
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </form> <hr class="my-3 d-none">

                                        <div class="col-sm-12 p-0 mb-1 mt-3" id="dynamic-patient-search-area" >
                                            <div class="table-responsive">
                                                <table class="table table-bordered table-hover" id="department_dt">
                                                    <thead>
                                                    <tr class="bg-gradient-light">
                                                        <th class="py-2 text-center" width="50px">{{__('patient.table_no')}}</th>
                                                        <th class="py-2 text-center" width="220px">{{__('patient.table_action')}}</th>
                                                        <th class="py-2 text-center" width="250px">{{__('patient.table_code')}}</th>
                                                        <th class="py-2">{{__('patient.table_name')}}</th>
                                                        <th class="py-2 text-right" width="120px">{{__('patient.form_age')}}</th>
                                                        <th class="py-2 text-center" width="120px">{{__('patient.table_sex')}}</th>
                                                        <th class="py-2" width="200px">{{__('patient.table_phon')}}</th>
                                                        <th class="py-2 text-center" width="100px">{{__('patient.sample')}}</th>

                                                        <!--<th class="py-2 text-center" width="100px">{{__('patient.table_status')}}</th>-->
                                                    </tr>
                                                    </thead>
                                                    <tbody id="dynamic-patient-search-area__" >
                                                    @foreach($patients as $k=>$patient)
                                                        <tr>
                                                            <td class="py-1 text-center">{{($k+1) + (($_GET['page'] ?? 1) * 20) - 20}}</td>
                                                            <td class="py-1 text-center">
                                                                <div class="btn-group" role="group" aria-label="Basic example" >
                                                                    <input type="text" class="d-none" id="ivt-{{$patient->id}}" value="{{\App\Helpers\Helpers::generatePatientBotLink('patient',$patient->lab_id, $patient->id)}}"><button type="button" onclick="copyTgBotInviteLink('ivt-{{$patient->id}}')" class="btn btn-sm d-none btn-inverse-warning mr-2 py-1"><i title="Copy {{\App\Helpers\Helpers::generatePatientBotLink('patient',$patient->lab_id, $patient->id)}} to clipboard" class="fa fa-telegram"></i></button>
                                                                @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['update_patient']))
                                                                    <button type="button" class="btn btn-sm btn-inverse-info mr-2 py-1" title="{{__('general.btn_edit')}}" data-action="edit" data-uid="{{$patient->id}}" data-toggle="modal" data-target="#modal-patient" data-backdrop="static">
                                                                        <i class="typcn typcn-edit"></i>
                                                                    </button>
                                                                    @endif
                                                                    @if($patient->record_status_id==0)
                                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_patient']))
                                                                            <button type="button" class="btn btn-sm btn-inverse-primary py-1" title="Restore"  onclick="restore_data({{$patient->id}})"><i class="typcn typcn-plus"></i></button>
                                                                        @endif
                                                                    @else
                                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_sample']))
                                                                        <a href="{{url('sample/patient/'.$patient->id.'/new')}}" class="btn btn-sm btn-inverse-success mr-2 py-1" title="{{__('sample.form_btn_new_sample')}}">
                                                                            <i class="mdi mdi-test-tube"></i> <i class="mdi mdi-plus" style="margin-left: -10px;"></i>
                                                                        </a>
                                                                        @endif
                                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['delete_patient']))
                                                                        <button type="button" class="btn btn-sm btn-inverse-danger py-1" title="{{__('general.btn_delete')}}" onclick="delete_data({{$patient->id}})"><i class="mdi mdi-close-circle"></i></button>
                                                                        @endif
                                                                    @endif
                                                                </div>
                                                            </td>
                                                            <td class="py-1 text-center">{{$patient->patient_uuid}}</td>
                                                            <td class="py-1">{{$patient->name_en}}</td>
                                                            <td class="py-2 text-right" width="100px">{{\App\Helpers\Helpers::getAge($patient->dob, date('Y-m-d'))}}</td>
                                                            <td class="py-1 text-center">{{$patient->gender == 1 ? 'M' : 'F'}}</td>
                                                            <td class="py-1 text-left">{{$patient->phone_number}}</td>
                                                            <td class="py-1 text-right">
                                                                <label class="badge badge-outline-info rounded-25 px-3">
                                                                    {{$patient->samples->count()}}
                                                                </label>
                                                            </td>
{{--                                                            <td class="py-1 text-center"><input type="text" class="d-none" id="ivt-{{$patient->id}}" value="{{\App\Helpers\Helpers::generatePatientBotLink('patient',$patient->lab_id, $patient->id)}}"><button type="button" onclick="copyTgBotInviteLink('ivt-{{$patient->id}}')" class="btn btn-sm btn-light"><i title="Copy {{\App\Helpers\Helpers::generatePatientBotLink('patient',$patient->lab_id, $patient->id)}} to clipboard" class="mdi mdi-content-copy"></i></button> </td>--}}

                                                        </tr>
                                                    @endforeach
                                                    </tbody>
                                                </table>
                                            </div>
                                            <div class="float-right mt-3">
                                                {!! $patients->appends($_GET)->links('pagination.custom') !!}
                                            </div>
                                        </div>



                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>

            <!-- Modal Laboratory -->
            <div class="modal fade modal-primary" id="modal-patient">
                <div class="modal-dialog modal-xl">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h6 class="modal-title card-title"><i class="mdi mdi-dots-vertical menu-icon"></i> {{__('patient.form_add_title')}}</h6>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true" class="mdi mdi-close"></span></button>
                        </div>

                        <div class="modal-body">
                            <form method="post" action="#" id="form" onkeydown="return event.key != 'Enter';">
                                @csrf
                                <input type="hidden" class="form-control" name="uid" id="data-id" value="0" />
                                <div class="form-vertical">

                                    <div class="row">
                                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-6 px-3">

                                            <div class="form-group mb-1">
                                                <label for="patient-code">{{__('patient.form_code')}} <span class="text-danger">*</span></label>
                                                <div class="input-group">
                                                    <input type="text" class="form-control" {{$labSettings->patient_code == 1 ? ' disabled readonly ' : ''}} name="patient_code" id="patient-code" placeholder="{{__('patient.form_code')}}">
                                                    <div class="input-group-append">
                                                        {{--<button class="btn btn-sm btn-info px-3 shadow-none rounded-0" title="Auto generate patient code" type="button" onclick="generate_uuid('P','#patient-code')"><i class="mdi mdi-autorenew"></i>&nbsp;</button>--}}
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_name')}} <span class="text-danger">*</span></label>
                                                <input type="text" class="form-control" name="patient_name" id="patient-name" placeholder="{{__('patient.form_name')}}">
                                            </div>
                                            <div class="form-group mb-1">
                                                <label for="lab-name-kh">{{__('patient.form_dob')}} <span class="text-danger">*</span></label>
                                                <div class="input-group">
                                                    <div class="input-group-append mr-3" style="flex: 5 !important;">
                                                        <input class="form-control datepicker" name="dob" type="text" id="dob" placeholder="{{__('general.date_placeholder')}}" required="" autocomplete="off">
                                                    </div>
                                                    <input class="form-control" name="year" onkeypress="return isNumber(event);" maxlength="2" type="text" id="year" autocomplete="off">
                                                    <div class="input-group-append" style="">
                                                        <button class="btn btn-sm btn-white" disabled type="button">{{__('patient_type.form_input_year')}}</button>
                                                    </div>

                                                    <input class="form-control" name="month" onkeypress="return isNumber(event);" maxlength="2" type="text" id="month" autocomplete="off">
                                                    <div class="input-group-append" style="">
                                                        <button class="btn btn-sm btn-white" disabled type="button">{{__('patient_type.form_input_month')}}</button>
                                                    </div>

                                                    <input class="form-control" name="month" onkeypress="return isNumber(event);" maxlength="2" type="text" id="day" autocomplete="off">
                                                    <div class="input-group-append" style="">
                                                        <button class="btn btn-sm btn-white" disabled type="button">{{__('patient_type.form_input_day')}}</button>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_gender')}} <span class="text-danger">*</span></label>
                                                <div class="form-group pl-3">
                                                    <div class="row">
                                                        <div class="form-check col-sm-12 col-md-12 col-lg-3" style="margin-bottom: 6px !important;">
                                                            <label class="form-check-label">
                                                                <input type="radio" class="form-check-input" name="gender" data-id="1" value="1">
                                                                {{__('patient.form_sex_male')}}
                                                            </label>
                                                        </div>
                                                        <div class="form-check col-sm-12 col-md-12 col-lg-3" style="margin-bottom: 6px !important;">
                                                            <label class="form-check-label">
                                                                <input type="radio" class="form-check-input" name="gender" data-id="2" value="2"> {{__('patient.form_sex_female')}}
                                                            </label>
                                                        </div>
                                                        <div class="form-check col-sm-12 col-md-12 col-lg-3" style="margin-bottom: 6px !important;">
                                                            <label class="form-check-label">
                                                                <input type="radio" class="form-check-input" name="gender" data-id="3" value="3"> {{__('patient.form_sex_other')}}
                                                            </label>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_phone')}}</label>
                                                <input type="text" class="form-control" name="phone_number" id="phone-number" placeholder="{{__('patient.form_phone')}}">
                                            </div>
											<div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_khid')}}</label>
                                                <input type="text" class="form-control" name="khid_number" id="khid-number" placeholder="{{__('patient.form_khid')}}">
                                            </div>
                                        </div>
                                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-6 px-3">
                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_house')}}</label>
                                                <input type="text" class="form-control" name="house_number" id="house-number" placeholder="{{__('patient.form_house')}}">
                                            </div>
                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_street')}}</label>
                                                <input type="text" class="form-control" name="street_number" id="street-number" placeholder="{{__('patient.form_street')}}">
                                            </div>

                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_province')}}</label>
                                                <select class="form-control select2" name="province_id" id="province-id" style="width: 100% !important;">
                                                    <option></option>
                                                    @foreach($provinces as $province)
                                                        <option value="{{$province->id}}">{{$province->name}}</option>
                                                    @endforeach
                                                </select>
                                            </div>
                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_district')}}</label>
                                                <select class="form-control select2" name="district_id" id="district-id" style="width: 100% !important;">
                                                </select>
                                            </div>
                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_commune')}}</label>
                                                <select class="form-control select2" name="commune_id" id="commune-id" style="width: 100% !important;">
                                                </select>
                                            </div>
                                            <div class="form-group mb-1">
                                                <label for="lab-name-en">{{__('patient.form_village')}}</label>
                                                <select class="form-control select2" name="village_id" id="village-id" style="width: 100% !important;">
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_sample']))
                            <button type="button" class="btn btn-inverse-info rounded-25  mx-3" action-id="1" id="btnSaveAndAddSample"><i class="mdi mdi-test-tube"></i> <i class="mdi mdi-plus" style="margin-left: -10px;"></i>&nbsp;&nbsp;<span class="save">{{__('general.btn_save_patient_add_new_sample')}}</span></button>
                            @endif
                            @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_patient', 'update_patient']))
                                <button type="button" class="btn btn-info rounded-25" id="btnSave" action-id="0"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;<span class="save">{{__('general.btn_save')}}</span></button>
                            @endif
                            <button type="button" class="btn btn-secondary rounded-25 mx-3" data-dismiss='modal'>{{__('general.btn_cancel')}}</button>
                        </div>
                    </div>
                </div>
            </div>

            @include('layout.footer')
        </div>
    </div>
</div>

@include('layout.common_script')

<script src="{{url(env("APP_URL").'storage/assets/libs/bootstrap-datepicker/js/bootstrap-datepicker.min.js')}}"></script>

<script>

    let modal_add_title = "{{__('patient.form_add_title')}}";
    let modal_edit_title = "{{__('patient.form_edit_title')}}";


</script>
<script src="{{url(env("APP_URL").'storage/assets/js/admin/patient.js')}}"></script>

<script src="{{url(env("APP_URL").'storage/assets/libs/moment/moment.js')}}"></script>
<script src="{{url(env("APP_URL").'storage/assets/libs/moment/moment-precise-range.js')}}"></script>

<script>



    $('#kword').keyup(function(){
        var kword = $(this).val()
        if(kword){
            $.ajax({
                url: '{{url('patient/live-filter')}}?kword='+kword,
                type:"GET",
                dataType:"Text",
                //delay: 10,
                success:function(res){
                    $('#dynamic-patient-search-area').html(res)
                }
            });

        }

    })


    function copyTgBotInviteLink(tgLinkSelectorId) {
        // Get the text field
        var copyText = document.getElementById(tgLinkSelectorId);
        copyText.select();
        copyText.setSelectionRange(0, 99999); // For mobile devices
        navigator.clipboard.writeText(copyText.value);
        alertify.warning("Telegram Bot invitation link already copied!")
    }

    function calculateAge(dob, current_date) {
        dob          = moment(dob);
        current_date = current_date ? moment(current_date) : moment(new Date('{{date('m/d/Y')}}'));
        return moment.preciseDiff(dob, current_date, true)
    }

    var $patient_info_form		= $("#modal-patient");
    $patient_info_form.find("#dob").on("change", function() {
        var dob_split = $(this).val().split("-")
        var dob = new Date(dob_split[1]+'/'+dob_split[0]+'/'+dob_split[2]);

        if (dob) {
            //current_date =  moment('{{date('Y-m-d H:i:s')}}');
            var age = calculateAge(dob);
            $patient_info_form.find("#year").val(age.years);
            $patient_info_form.find("#month").val(age.months);
            var days = age.days === 0 && age.months === 0 && age.years === 0 ? 1 : age.days;
            $patient_info_form.find("#day").val(days);
        }
    });

    $patient_info_form.on("keyup change", "#year, #month, #day", function (evt) {
        var years  = $patient_info_form.find("#year").val() || 0;
        var months = $patient_info_form.find("#month").val() || 0;
        var days   = $patient_info_form.find("#day").val() || 0;
        var dob = moment(new Date('{{date('m/d/Y')}}'))
        dob.subtract(days, 'days');
        dob.subtract(months, 'months');
        dob.subtract(years, 'years');
        var dob_split = dob.toDate().toLocaleDateString().split("/")
        var dob = dob_split[1].padStart(2,"0")+'-'+dob_split[0].padStart(2,"0")+'-'+dob_split[2];
        $patient_info_form.find("#dob").val(dob);
    });
</script>

</body>

</html>

