<!DOCTYPE html>

<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">

<head>

    @include('layout.header')

    <style>

        .btn-icon-prepend{

            float: left;

        }

        .ui-widget{
            z-index: 900000!important;
        }

        .ui-autocomplete {
            position: absolute;
            height: 400px;
            top: 100%;
            left: 0;
            z-index: 1000;
            float: left;
            display: none;
            min-width: 160px;
            padding: 4px 0;
            margin: 0 0 10px 25px;
            list-style: none;
            background-color: #ffffff;
            border-color: #ccc;
            border-color: rgba(0, 0, 0, 0.2);
            border-style: solid;
            border-width: 1px;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            border-radius: 5px;
            -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
            -moz-box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
            box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
            -webkit-background-clip: padding-box;
            -moz-background-clip: padding;
            background-clip: padding-box;
            *border-right-width: 2px;
            *border-bottom-width: 2px;
        }

        .ui-menu-item > a.ui-corner-all {
            display: block;
            padding: 3px 15px;
            clear: both;
            font-weight: normal;
            line-height: 18px;
            color: #555555;
            white-space: nowrap;
            text-decoration: none;
        }

        .ui-state-hover, .ui-state-active {
            color: #ffffff;
            text-decoration: none;
            background-color: #0088cc;
            border-radius: 0px;
            -webkit-border-radius: 0px;
            -moz-border-radius: 0px;
            background-image: none;
        }


        .input_container {
            border: 0px solid #e5e5e5;
        }

        input[type=file]::file-selector-button {
            background-color: #fff;
            color: #000;
            border: 0px;
            border-right: 0px solid #e5e5e5;
            padding: 10px 15px;
            margin-right: 20px;
            transition: .5s;
        }

        input[type=file]::file-selector-button:hover {
            background-color: #eee;
            border: 0px;
            border-right: 1px solid #e5e5e5;
        }


    </style>

    <link rel="stylesheet" href="{{url(env("APP_URL").'storage/assets/libs/lightgallery/lightgallery.css')}}" >

    <link rel="stylesheet" href="{{url(env("APP_URL").'storage/assets/libs/printjs/printjs.css')}}" >

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

    <link rel="stylesheet" href="{{url(env("APP_URL").'storage/assets/css/search_test.css')}}" >

</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: 83vh;">

                            <div class="card-body">

                                <form method="post" action="{{url('sample/update/'.$sample->id)}}" enctype="multipart/form-data" onkeydown="return event.key != 'Enter';">

                                    @csrf

                                    <div class="row font-14">

                                        <div class="col-lg-12 pl-4">

                                            <div class="row">

                                                <h6 class="pb-2 font-weight-medium card-title">{{$sample->sample_number}}</h6>

                                                <div class="col-lg-12">

                                                    <div class="form-vertical pt-0">

                                                        <div class="row font-weight-normal">
                                                            <div class="col-sm-3" style="height: 25px;">{{__('sample.form_patient_name')}}</div>
                                                            <div class="col-sm-9" style="height: 25px;"> : {{$sample->patient->name_en}}</div>
                                                            <div class="col-sm-3" style="height: 25px;">{{__('sample.form_patient_code')}}</div>
                                                            <div class="col-sm-9  text-info" style="height: 25px;"> : {{$sample->patient->patient_uuid}}</div>
                                                            <div class="col-sm-3" style="height: 25px;">{{__('sample.form_patient_age').' / '.__('sample.form_patient_gender')}}</div>
                                                            <div class="col-sm-9" style="height: 25px;"> : {{date('Y') - date('Y',strtotime($sample->patient->dob))}} y / {{$sample->patient->gender == 1 ? 'Male' : 'Female'}}</div>
                                                            <div class="col-sm-3" style="height: 25px;">{{__('sample.form_patient_mobile')}}</div>
                                                            <div class="col-sm-9" style="height: 25px;"> : {{$sample->patient->phone_number}}</div>
{{--                                                            <div class="col-sm-3" style="height: 25px;">{{__('sample.form_patient_address')}}</div>--}}
{{--                                                            <div class="col-sm-9" style="height: 25px;"> : {{\App\Helpers\Helpers::getPatientAddress($sample->patient)}}</div>--}}

                                                        </div>

                                                    </div>

                                                </div>

                                            </div>

                                        </div>

                                    </div>

                                    <hr>

                                    <div class="row">

                                        <div class="col-lg-12 pl-3">

                                            <div class="col-lg-12 border">

                                                <div class="row bg-gradient-light font-14 py-2">

                                                <span class="pl-3">{{__('sample.form_sample_number')}} : <b class="pl-1 sample-number text-info">{{$sample->sample_number}}</b></span>

{{--                                                <span class="pl-3">{{__('sample.form_collected_date')}} : <b class="pl-1 sample-source text-info">{{date('d-m-Y h:i A',strtotime($sample->collected_date))}}</b></span>--}}

{{--                                                <span class="pl-3">{{__('sample.form_received_date')}} : <b class="pl-1 sample-source text-info">{{date('d-m-Y h:i A',strtotime($sample->received_date))}}</b></span>--}}

                                            </div>

                                                <div class="row" id="sample-forms">

                                                    <div class="col-lg-12 pt-2">

                                                        <div class="form-vertical sample-creation-form p-2">

                                                            <div class="row p-0">

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <input type="hidden" name="id" value="{{$sample->id}}">

                                                                        <input type="hidden" name="patient_id" value="{{$sample->patient_id}}">

                                                                        <label for="lab-name-en">{{__('sample.form_sample_number')}} <span class="text-danger">*</span></label>

                                                                        <div class="input-group">

                                                                            <input type="text" readonly disabled class="form-control rounded-0" value="{{$sample->sample_number}}" name="sample_number" id="sample-number" placeholder="{{__('sample.form_sample_number')}}">

                                                                        </div>

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <label for="lab-name-en">{{__('sample.form_sample_source')}} <span class="text-danger">*</span></label>

                                                                        <select class="form-control select2 rounded-0" name="sample_source_id" id="sample-source-id" style="width: 100%">

                                                                            <option></option>

                                                                            @foreach($sampleSources as $sampleSource)

                                                                                <option {{$sampleSource->id == $sample->sample_source_id ? 'selected': ''}} value="{{$sampleSource->id}}">{{$sampleSource->name_en}}</option>

                                                                            @endforeach

                                                                        </select>

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <label for="lab-name-en">{{__('sample.form_requester')}} <span class="text-danger">*</span></label>

                                                                        <select class="form-control select2 rounded-0" name="physician_id" id="physician-id" style="width: 100%">

                                                                            <option></option>

                                                                            @foreach($physicians as $physician)

                                                                                <option {{$physician->id == $sample->physician_id ? 'selected': ''}} value="{{$physician->id}}">{{$physician->name_en}}</option>

                                                                            @endforeach

                                                                        </select>

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <label for="lab-name-en">{{__('sample.form_collected_date')}} <span class="text-danger">*</span></label>

                                                                        <input type="datetime-local" value="{{date('Y-m-d\TH:i:s',strtotime($sample->collected_date))}}" class="form-control" name="collected_date" id="collected-date">

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <label for="lab-name-en">{{__('sample.form_received_date')}} <span class="text-danger">*</span></label>

                                                                        <input type="datetime-local" value="{{date('Y-m-d\TH:i:s',strtotime($sample->received_date))}}" class="form-control" name="received_date" id="received-date">

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <label for="lab-name-en">{{__('sample.form_admission_date')}} <span class="text-danger">*</span></label>

                                                                        <input type="datetime-local" value="{{date('Y-m-d\TH:i:s', strtotime($sample->admission_date))}}" class="form-control" name="admission_date" id="admission-date">

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <label for="lab-name-en">{{__('sample.form_diagnisis')}}</label>

                                                                        <input type="text" class="form-control rounded-0" value="{{$sample->diagnosis}}" name="diagnosis" id="diagnosis" placeholder="{{__('sample.form_diagnisis')}}">

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1">

                                                                        <label for="lab-name-en">{{__('sample.form_sample_condition')}} <span class="text-danger"></span></label>

                                                                        <div class="input-group">

                                                                            <div class="input-group-append">

                                                                                <div class="form-check pr-4">

                                                                                    <label class="form-check-label">

                                                                                        <input {{$sample->is_urgent == 1 ? 'checked' : ''}} value="{{$sample->is_urgent}}" type="checkbox" name="is_urgent" id="is-urgent" class="form-check-input">

                                                                                        {{__('sample.form_is_urgent')}}

                                                                                    </label>

                                                                                </div>

                                                                            </div>

                                                                            <select class="form-control rounded-0" name="sample_condition" id="sample-condition">
                                                                                <option selected value="2">{{__('sample.form_sample_condition')}}</option>

                                                                                <option {{$sample->sample_condition == 1 ? 'selected' : ''}} value="1">{{__('sample.condition_good')}}</option>

                                                                                <option {{$sample->sample_condition == 0 ? 'selected' : ''}} value="0">{{__('sample.condition_reject')}}</option>

                                                                                <option {{$sample->sample_condition == 2 ? 'selected' : ''}} value="2">{{__('sample.condition_acceptable')}}</option>

                                                                            </select>

                                                                        </div>

                                                                    </div>

                                                                </div>

                                                                <div class="col-sm-12 col-md-6 col-lg-4">

                                                                    <div class="form-group mb-1 " style="flex: 1 !important;">

                                                                        <label for="lab-name-en">{{__('sample.form_reject_reason')}} <span class="text-danger text-small"></span></label>

                                                                        <div class="input-group">

                                                                            <div class="form-group" style="flex: 1 !important;">

                                                                                <select class="form-control select2 rounded-right-0" style="width: 100%" disabled name="reject_comment_id" id="reject-comment-id">

                                                                                    <option></option>

                                                                                    @foreach($rejectComments as $rejectComment)

                                                                                        <option {{$rejectComment->id == $sample->reject_comment_id ? 'selected':''}} value="{{$rejectComment->id}}">{{$rejectComment->reject_comment}}</option>

                                                                                    @endforeach

                                                                                </select>

                                                                            </div>

                                                                            <div class="input-group-append">
                                                                                <button class="btn btn-sm btn-white rounded-left-0  px-3" type="button" onclick="addRejectComment('reject-comment-id')"><i class="typcn typcn-plus"></i>&nbsp;</button>
                                                                            </div>
                                                                        </div>
                                                                    </div>

                                                                </div>

                                                            </div>


                                                        </div>

                                                    </div>

                                                    <div class="col-lg-12 py-3">

                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id,['delete_sample']))
                                                            <button type="button" class="btn btn-md btn-inverse-danger float-right rounded-25 mb-3 mx-2 px-4" onclick="deleteSample({{$sample->id}})"><i class="mdi mdi-delete-forever mr-2"></i>
                                                                {{__('sample.from_btn_remove')}}</button>
                                                        @endif

                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_sample']))
                                                            <a href="{{url('sample/patient/'. $sample->patient_id.'/new')}}" class="btn btn-md btn-inverse-success float-right rounded-25 mb-3 mx-2"><i class="typcn typcn-plus mr-2"></i> {{__('sample.form_btn_new_sample')}}</a>
                                                        @endif

                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['view_result', 'print_result']))
                                                            <a href="javascript:void(0)" class="btn btn-md btn-inverse-warning float-right rounded-25 mb-3 mx-2" id="previewResult" data-toggle="modal" data-target="#print_preview_modal" data-backdrop="static"><i class="mdi mdi-printer mr-2"></i> {{__('sample.form_btn_preview_result')}}</a>
                                                        @endif

                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['add_result','update_result']))
                                                            <button type="button" class="btn btn-md btn-inverse-info float-right rounded-25 mb-3 mx-2 px-4" id="btnAddTestResult" data-toggle="modal" data-target="#modal-add-test-result" data-backdrop="static"><i class="mdi mdi-table-plus mr-2"></i> {{__('sample.form_btn_add_result')}}</button>
                                                        @endif

                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['update_sample']))
                                                            <button type="button" class="btn btn-md btn-inverse-info float-right  rounded-25 mb-3 mx-2 px-4 btnShowTestModal" data-toggle="modal" data-target="#modal-assign-test" data-backdrop="static"><i class="mdi mdi-format-list-bulleted-type mr-2"></i> {{__('sample.form_btn_assign_test')}}</button>
                                                            <button type="submit" class="btn btn-md btn-inverse-info float-right mb-3 mx-2 rounded-25 px-5"><i class="mdi mdi-content-save mr-2"></i>
                                                            {{__('sample.form_btn_update')}}</button>
                                                        @endif

                                                        <button type="button" class="btn btn-md btn-danger d-none rounded-25 mb-2">Reject Sample</button>

                                                    </div>

                                                </div>

                                            </div>



                                        </div>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>



            @include('layout.footer')



        </div>



        @include('modal.assign_test')


        @include('modal.organism_result')

        @include('modal.invoice')
	    @include('modal.sample_label_print')


        @include('modal.enter_result_forms.sample_result_'.$enter_form_id)

        @include('modal.preview_result_forms.preview_result_'.$preview_form_id)


    </div>

    @include('layout.common_script')

    <script src="https://code.jquery.com/ui/1.14.1/jquery-ui.js"></script>

    <script>

        $('#form-add-test-result').delegate('.auto-complete-1', 'click', function() {
            $(".auto-complete-1").autocomplete({source: [<?php echo  "'" . implode ( "', '", config('labis.auto_complete_items') ) . "'" ?>]}).click(function(){
                $(this).autocomplete('search', 't')
            });
        });

        var file_path = "{{ url(env("APP_URL").'storage/images/attachment_lab_result') }}";



        var is_auto_generate_sample_number = 0;

        let sample_id = {{\Route::current()->parameter('id')}};

        let first_prev_sample_id = {{$firstPrvSampleId}};

        let previous_id = "<?php echo isset($_GET['previous_sample']) ? $_GET['previous_sample'] : 0?>";

        let invoice_id = "{{($sample->invoice) ? $sample->invoice->id : 0}}";

        let invoice_code = "{{($sample->invoice) ? $sample->invoice->invoice_code : 0}}";

        let currency = {{$labSettings->currency}};
        let exchange_rate = {{$labSettings->exchange_rate}};

        let testSamples = [];

        let previous_sample_date = "{{$firstPrvSampleDate}}";
        let selected_previous_sample_date = "{{$selected_pre_sample_date}}";


    </script>


    <script src="{{url(env("APP_URL").'storage/assets/js/admin/sample.js')}}"></script>
    <script src="{{url(env("APP_URL").'storage/assets/js/admin/calculation_results/underscore-min.js')}}"></script>

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

    <script src="{{url(env("APP_URL").'storage/assets/js/admin/calculation_results/math-expression-evaluator.js')}}"></script>

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

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

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

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

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

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

    <script src="{{url(env("APP_URL").'storage/assets/js/admin/enter_result_forms/test_result_'.$enter_form_id.'.js?time='.time())}}"></script>

    <script src="{{url(env("APP_URL").'storage/assets/js/admin/preview_result_forms/preview_result_'.$preview_form_id.'.js?time='.time())}}"></script>

    <script>



        var is_add_result = "{{isset($_GET['add-result'])? true : false}}";

        var is_preview_result = "{{isset($_GET['print-result'])? true : false}}";

        var is_edit_test = "{{isset($_GET['edit-test'])? true : false}}";

        if(is_edit_test) $('.btnShowTestModal').trigger('click');


        var is_print_label = "{{isset($_GET['print-label'])? true : false}}";
        if(is_print_label) $('#print-label').trigger('click');

        var is_add_invoice = "{{isset($_GET['add-invoice'])? true : false}}";
        if(is_add_invoice){
            $('.btnShowTestModal').trigger('click');
            //
            setTimeout(function() {
                $('#btnShowInvoice').trigger('click');
            }, 1000);

        }



        if(is_preview_result) {

            var $loading = $('#loadingDiv').hide();

            $(document).ajaxStart(function () {$loading.fadeIn();})

            setTimeout(function () {

                $('#previewResult').trigger('click');

                $(document).ajaxStop(function () {$loading.fadeOut();})

            }, 500)

        }

        if(is_add_result) $('#btnAddTestResult').trigger('click');



        $('#btnShowInvoice').click(function () {

           generate_invoice()

        })


        $('#result_neonetal_template').change(function(){
            if(this.checked) {
                setResultTemplate(4);
            }
            else{
                setResultTemplate(0);
            }
        })

        function setResultTemplate(result_template_id) {
            $.ajax({
                url: sample_base_url.toString()+'/set-result-template',
                type:"POST",
                dataType:"JSON",
                data :{
                    "_token": csrf_token,
                    uid: sample_id,
                    result_template_id : result_template_id
                },
                success:function(res) {
                    if (res.success) window.location.reload()
                }

            });

        }



        function printRecorder() {

            $.ajax({

                url: sample_base_url.toString()+'/print-record',

                type:"POST",

                dataType:"JSON",

                data :{

                    "_token": csrf_token,

                    uid: sample_id

                },

                success:function(res) {



                }

            });

        }



        function approveSample(sampleId) {

            $.ajax({

                url: sample_base_url.toString()+'/approve',
                type:"POST",
                dataType:"JSON",
                data :{
                    "_token": csrf_token,
                    uid: sampleId
                },
                success:function(res) {
                    handleMessage(res.success, res.message)
                    $('.approve').hide();
                }
            });

        }


        $('#modal-organism-result #search-organism').keypress(_.debounce(function () {
            getOrganisms($(this).val())
        }, 500));

        function getOrganisms(name = '') {
            $.ajax({
                url: get_refs_base_url+ '/test-sample-organism-search',
                type:"POST",
                data:{
                    "_token": csrf_token,
                    'sample_id' : sample_id,
                    'test_sample_id' : $("#modal-organism-result").attr('data-test-sample-id'),
                    'name' : name
                },
                dataType:"json",
                success:function(response){
                    if (response.success) {
                        $('#modal-organism-result-organism #orgn-result-area').empty();
                        let fieldType = $("#modal-organism-result").attr('data-field-type');
                        response.data.map(item =>{
                            renderOrganismResultItemRow(item.test_sample_id, fieldType, item, response.qualities, response.selected_org);
                            //console.log('qty ', response.selected_org)
                        })
                    }

                },
                error: function(response) {
                },
            });
        }


        $('#send_lab_result_patient, #send_lab_result_clinician').click(function(){
            let target_receiver = $(this).attr('target_receiver')
            $.ajax({
            url: sample_base_url + '/result/tg-webhook/{{$sample->patient->id}}/{{$sample->id}}',
            type:"POST",
            data :{
                "_token": csrf_token,
                "target_receiver": target_receiver
            },
            success:function(res){
                handleMessage(res.success, res.message)
            },
            error:function(res){
                 handleMessage(true, 'Laboroatory results have been sent!')
            }
        });
    })




    </script>



</body>



</html>



