<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
  @include('layout.header')
<style>

</style>
<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 min-vh-70">
                    <div class="card-body">
                        <h4 class="card-title"><i class="mdi mdi-dots-vertical menu-icon"></i> {{__('sample.form_add_title')}}</h4>

                        <form method="post" class="p-2" action="#" id="form-test-sample">
                            @csrf
                            <div class="row">
                                <div class="col-lg-12">
                                    <div class="row">
                                        <div class="col-lg-12">
                                            <div class="form-vertical mb-4">
                                                <div class="col-lg-12 pt-2 border-bottom">
                                                    <div class="row">
                                                        <div class="col-lg-6">
                                                            <div class="form-group mb-1">
                                                                <label for="lab-name-en">{{__('sample.form_patient')}} <span class="text-danger">*</span></label>
                                                                <div class="input-group" id="testItem">
                                                                    <div class="form-group" style="flex: 1 !important;">
                                                                        <select class="form-control select2 rounded-right-0" name="patient_id" id="patient-id" style=" width: 100% !important;">
                                                                            <option></option>
                                                                            @foreach($patients as $patient)
                                                                                <option {{$patient_id == $patient->id ? 'selected':''}} value="{{$patient->id}}">{{($patient->patient_uuid . ' - ' . $patient->name_en) . ((!empty($patient->phone_number) ? ' - '.$patient->phone_number: ''))}}</option>
                                                                            @endforeach
                                                                        </select>
                                                                    </div>
                                                                    <div class="input-group-append">
{{--                                                                        <button class="btn btn-sm btn-inverse-secondary px-3" id="searchPatientAddForm"  type="button"><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-inverse-success waves-effect rounded-25 px-3 ml-3" type="button" data-action="new" data-toggle="modal" data-target="#modal-patient" data-backdrop="static"><i class="typcn typcn-plus"></i>
                                                                            {{__('patient.form_add_title')}}</button>
                                                                        @endif
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="form-vertical sample-creation-form border p-4" style="{{!empty($patient_id) ? 'display: block' : 'display: none'}}">
                                                <div class="row">
                                                    <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_number')}} <span class="text-danger">{{$labSettings->sample_number == 1 ? '' : '*'}}</span></label>
                                                            <div class="input-group">
                                                                <input type="text" class="form-control rounded-0" {{$labSettings->sample_number == 1 ? ' disabled readonly ' : ''}} name="sample_number" id="sample-number" placeholder="Sample No">
                                                                <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('S','#sample-number')"><i class="mdi mdi-autorenew"></i>&nbsp;</button>--}}
                                                                </div>
                                                            </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->is_default==1? '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->is_default==1? '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')}}" 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')}}" 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')}}" 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" name="diagnosis" id="diagnosis" placeholder="Diagnosis">
                                                        </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')}} </label>
                                                            <div class="input-group">
                                                                <select class="form-control rounded-0" name="sample_condition" id="sample-condition">
                                                                    <option selected value="2">{{__('sample.form_sample_condition')}}</option>
                                                                    <option value="1">{{__('sample.condition_good')}}</option>
                                                                    <option value="0">{{__('sample.condition_reject')}}</option>
                                                                    <option 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 value="{{$rejectComment->id}}">{{$rejectComment->reject_comment}}</option>
                                                                        @endforeach
                                                                    </select>
                                                                </div>
                                                                <div class="input-group-append">
                                                                    <button class="btn btn-sm btn-white px-3" type="button" onclick="addRejectComment('reject-comment-id')"><i class="typcn typcn-plus"></i>&nbsp;</button>
                                                                </div>
                                                            </div>

                                                        </div>
                                                    </div>

                                                    <div class="col-lg-6 col-sm-12  m-0">
                                                        <div class="form-check pl-2">
                                                            <label class="form-check-label font-14">
                                                                <input type="checkbox" name="is_urgent" id="is-urgent" class="form-check-input">
                                                                {{__('sample.form_is_urgent')}}
                                                            </label>
                                                        </div>
                                                    </div>

                                                    <div class="col-lg-6 col-sm-12 pt-4 text-right">
                                                        <button type="button" data-toggle="modal" class="d-none" id="show-modal-assign-test" data-target="#modal-assign-test" data-backdrop="static"></button>
                                                        @if(\App\Http\Controllers\Helper\GlobalController::user_can(Auth::user()->role_id, ['create_sample']))
                                                        <button class="btn btn-info rounded-25" type="button" id="saveSample"><i class="mdi mdi-check-all"></i> {{__('sample.form_btn_save_assign_test')}}</button>
                                                        @endif
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                </div>
            </div>
          </div>
        </div>
      </div>

          <!-- Modal Patient -->
          <div class="modal fade modal-primary" id="modal-patient">
              <div class="modal-dialog show modal-xl">
                  <div class="modal-content">
                      <div class="modal-header">
                          <h6 class="modal-title">{{__('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-light" 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-light" 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-light" 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_patient']))
                          <button type="button" class="btn btn-info rounded-25" id="savePatient"><i class="fa fa-floppy-o"></i>&nbsp;&nbsp;<span class="save">{{__('lang.save')}}</span><span class="update" style="display: none">{{__('lang.update')}}</span></button>
                          @endif
                          <button type="button" class="btn btn-secondary rounded-25 mx-3" data-dismiss='modal'>{{__('lang.cancel')}}</button>
                      </div>
                  </div>
              </div>
          </div>

          @include('modal.assign_test')

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

        var sample_id = 0;

    </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>

    $('#patient-id').select2({
        tags: false,
        allowClear: true,
        ajax: {
            url: '{{url('patient/live-search')}}',
            dataType: 'json',
            delay: 10
        }
    });

    function redirectPrintLabel(){
        var suid = parseInt($('#modal-assign-test').find("input[name=sample_id]").val());
        if(suid > 0){
            $('#save-assign-test').click();
            setTimeout(function() {
                window.location = sample_base_url + '/edit/'+suid+'?print-label'
            }, 1000);
        }
    }

    function redirectMakeInvoice(){
        var suid = parseInt($('#modal-assign-test').find("input[name=sample_id]").val());
        if(suid > 0){
            $('#save-assign-test').click();
            setTimeout(function() {
                window.location = sample_base_url + '/edit/'+suid+'?add-invoice'
            }, 1000);

        }
    }


      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();
              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);
      });

      $('#sample-number').blur(function(){
          var inputId = $(this).val()
          generate_sample_number_plus_manul('S',inputId,'#sample-number')
        });

    function generate_sample_number_plus_manul (classPrefix, manualInputValue, targetDisplayElementId) {
        $.ajax({
            url: get_refs_base_url+"/uuidv2.generate/json/"+manualInputValue,
            type:"GET",
            dataType:"json",
            success:function(response){
                if(response.success) $(''+targetDisplayElementId).val(classPrefix+'-'+response.data.uuid);
                else alertify.error("Can't generate uuid.")
            }
        });
    }

  </script>

    <script src="{{url(env("APP_URL").'storage/assets/js/admin/sample.js?'.time())}}"></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/search_test.js')}}"></script>
</body>

</html>

