@include('header')
@php
$UserId = Session::get('UserId');
$UserId = Session::get('UserId');
if(file_exists('json_files/json-file.json')){
$json = file_get_contents('json_files/json-file.json');
//Decode JSON
$json_data = json_decode($json,true);
$results = array();
}

if(file_exists('json_files/timetables_date.json')){
$json2 = file_get_contents('json_files/timetables_date.json');
//Decode JSON
$json_data2 = json_decode($json2,true);
$results2 = array();
}

@endphp

<div class="container-fluid flex-grow-1 container-p-y">
    <h4 class="font-weight-bold py-3 mb-0">Pre-Operative Prescription Treatments </h4>
    <div class="card mb-4">
        <div class="card-body">
        <form id="validation-form" novalidate="novalidate" method="POST" enctype="multipart/form-data" autocomplete="off"
        action="">       
        <input type="hidden" id="id" name="id" value="{{$id}}">
        <input type="hidden" id="catstatus" value="{{$catstatus}}">
        <input type="hidden" id="SaveStatus" value="{{$data3[0]->SaveStatus}}">
        @csrf
            <div class="row">
                <div class="col-md-12">
                    @include('ipd/incprevopt-treatments')
                </div>
                <div class="col-md-12"><br>
                                <fieldset>
                                    <button class="btn btn-primary" type="submit" name="submit"
                                        id="submit">Save</button>
                                    <button class="btn btn-success" onclick="preOptNotes()" id="printbtn" type="button"
                                        >Print</button>
                                       
                                </fieldset>
                            </div>
            </div>
</form>

        </div>
    </div>




</div>

@include('footerscript')
@include('excelscript')

<script>
    function preOptNotes() {
    var id = $('#id').val();
   
        setTimeout(function() {
            window.open(
                '/{{projectUrl()}}preOptNotes/' + id, 'popUpWindow33',
                'height=1000,width=800,left=0,top=0,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no'
            );
        }, 1);

       
    
}
function getTreatmentList() {
    var query = $('#Treatment').val();
    var len = query.length;
    if (len > 0) {
        if (query != '') {
            $.ajax({
                url: "{{ url('fetchData') }}",
                method: "POST",
                data: {
                    query: query,
                    _token: '{{csrf_token()}}'
                },
                success: function(data) {
                    console.log(data);
                    $('#TreatmentList').fadeIn();
                    $('#TreatmentList').html(data);
                }
            });
        }
    } else {
        $('#TreatmentList').fadeOut();
        getRecords(query);
    }

}

function getRecords(val) {
    $.ajax({
        url: "{{ url('getTreatments') }}",
        method: "POST",
        data: {
            _token: '{{csrf_token()}}',
            Treatment: val
        },
        success: function(data) {
            //console.log(data);
            $('#showTreatments').html(data);
            $('#Treatment').val(val);
            $('#TreatmentList').fadeOut();
        }
    });
}

function procedure_lists() {
    $.ajax({
        url: "{{ url('viewDrugList') }}",
        method: "POST",
        data: {
            _token: '{{csrf_token()}}'
        },
        success: function(data) {
            //console.log(data);
            $('#drugcustresult').html(data);
        }
    });
}

function drugList() {
    $.ajax({
        url: "{{ url('saveDrugList') }}",
        method: "POST",
        data: {
            _token: '{{csrf_token()}}'
        },
        success: function(data) {
            $('.treatments').html(data);

        }
    });
}

function getTreatNameDetails(name, dose, freq) {
    var treatname = $('#' + name).val();
    $.ajax({
        url: "{{ url('getTreatNameDetails') }}",
        method: "POST",
        data: {
            _token: '{{csrf_token()}}',
            name: treatname
        },
        dataType: "json",
        success: function(data) {
            //console.log(data);
            //$('#' + dose).val(data.Dose);
            //$('#' + freq).val(data.Freq);
        }
    });
}

function get_drug_name(srno) {
    var query = $('#TreatName' + srno).val();
    var len = query.length;
    if (len > 0) {
        if (query != '') {
            $.ajax({
                url: "{{ url('fetchData2') }}",
                method: "POST",
                data: {
                    query: query,
                    _token: '{{csrf_token()}}',
                    srno: srno
                },
                success: function(data) {
                    console.log(data);
                    $('#DrugLists' + srno).fadeIn();
                    $('#DrugLists' + srno).html(data);
                }
            });
        }
    } else {
        $('#DrugLists' + srno).fadeOut();
    }

}

function selectliValue(Name, srno) {
    $('#TreatName' + srno).val(Name);
    $('#DrugLists' + srno).fadeOut();
}

$(document).ready(function() {
    var catstatus = $('#catstatus').val();
    var SaveStatus = $('#SaveStatus').val();
    if(SaveStatus == 0){
    if(catstatus == 1){
    getRecords('Pre op cat');
    }
}
$('#validation-form').on('submit', function(e) {
        e.preventDefault();
        if ($('#validation-form').valid()) {
            $.ajax({
                url: "{{ url('savePreOptNotesTreatments') }}",
                method: "POST",
                data: new FormData(this),
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $('#submit').attr('disabled', 'disabled');
                    $('#submit').text('Please Wait...');
                },
                success: function(data) {
                    $('#actionvalue').val('Edit');
                    $('#saveid').val(data);
                    $('#printbtn').attr("disabled", false);
                    $('#submit').attr('disabled', false);
                    $('#submit').text('Submit');
                    $('#billbtn').attr("disabled", false);
                $('#receiptbtn').attr("disabled", false);
                }
            });
        }

    });
});
</script>