@include('header')
<div class="container-fluid flex-grow-1 container-p-y">
    <h4 class="font-weight-bold py-3 mb-0">Add New Appointment</h4>

    <form id="validation-form" method="POST" action="">

        <input type="hidden" name="action" id="action" value="Save">
        <input type="hidden" name="saveid" id="saveid" value="">
        @csrf
        <div class="card mb-12 mb-4">
            <div class="card-body">
                <div class="row">


                    <div class="col-sm-2">
                        <div class="form-group">
                            <label>Mrd. No <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" name="MrdNo" id="MrdNo" placeholder="" value="">
                        </div>
                    </div>
                    <div class="col-sm-4">
                        <div class="form-group">
                            <label class="floating-label" for="Name">Patient Name <span
                                    class="text-danger">*</span></label>
                            <input type="text" class="form-control" name="PatientName" id="PatientName" placeholder=""
                                value="">
                        </div>
                    </div>
                    <div class="col-sm-2">
                        <div class="form-group">
                            <label class="floating-label" for="Name">Phone <span
                                    class="text-danger">*</span></label>
                            <input type="text" class="form-control" name="Phone" id="Phone" placeholder=""
                                value="">
                        </div>
                    </div>




                    <div class="col-sm-2">
                        <div class="form-group">
                            <label class="floating-label" for="Phone">Date <span class="text-danger">*</span></label>
                            <input type="date" class="form-control" id="AppDate" Name="AppDate"
                                value="{{date('Y-m-d')}}" placeholder="" required>
                        </div>
                    </div>

                    <div class="col-sm-2">
                        <div class="form-group">
                            <label class="floating-label" for="Phone">Time <span class="text-danger">*</span></label>
                            <input type="text" class="form-control" id="AppTime" Name="AppTime" value=""
                                placeholder="" required>
                        </div>
                    </div>


                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="floating-label" for="Phone">Appointment For <span
                                    class="text-danger">*</span></label>
                            <div class="input-group">

                                <select class="form-control"
                                    style="width: 100%;margin-top: 3px !important;height: 36px;" data-allow-clear="true"
                                    name="ReferFor" id="ClinicName" onchange="getSpecConsultant(this.value)">
                                    <option value="" selected="">...</option>
                                    @foreach($clinicnameList as $result)
                                    <option value="{{$result->ClinicName}}"
                                        >
                                        {{$result->ClinicName}}
                                    </option>
                                    @endforeach


                                </select>
                                <select class="form-control"
                                    style="width: 100%;margin-top: 3px !important;height: 36px;" data-allow-clear="true"
                                    name="Consultant" id="SpecDocName">
                                    <option value="" selected="">...</option>
                              


                                </select>
                                <span class="input-group-append">
                                    <button class="btn btn-secondary" type="button" data-toggle="modal"
                                        data-target="#modal-report-spec" id="add-spec-clinic"
                                        onclick="addNewSpClinic()">+</button>
                                </span>

                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6" style="padding-top:20px;">
                        <button class="btn btn-primary" type="submit" name="submit" id="submit">Save</button>
                        <a href="{{projectUrl()}}addNewAppointment" class="btn btn-info">New Appointment</a>
                        <a href="{{projectUrl()}}Appointment" class="btn btn-info">View Appointment</a>
                    </div><br>
                    <!-- <div class="col-sm-12" id="custresult2" style="padding-top: 25px;">

                        </div> -->

                    <input type="hidden" id="CurrDate" value="{{date('Y-m-d')}}">

                </div>
            </div>



    </form>


</div>

<script src="{{url('assets/js/jquery-3.6.0.min.js')}}"></script>
@include('masters/incspecclinicmodal')
@include('footerscript')
<script>
    function addNewSpClinic() {
    $('#Spaction').val('Save');
    $('#Spid').val('');
    $('#SpClinicName').val('');
    $('#SpName').val('');
    $('#drugbtn').text("Save");
    $('#specclinictitle').html(
        "Add <span class='font-weight-light'>Specility Clinic</span>");
    spec_clinic_lists();

}

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

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



function saveSpecialityClinic() {
    var SpClinicName = $('#SpClinicName').val();
    var SpName = $('#SpName').val();
    var action = $('#Spaction').val();
    var id = $('#Spid').val();
    $.ajax({
        url: "{{ url('saveSpecialityClinic') }}",
        method: "POST",
        data: {
            _token: '{{csrf_token()}}',
            ClinicName: SpClinicName,
            Name: SpName,
            action: action,
            id: id
        },
        beforeSend: function() {
            $('#specclinicbtn').attr('disabled', 'disabled');
            $('#specclinicbtn').text('Please Wait...');
        },
        success: function(data) {
            cliniclists();
            $('#ClinicName').attr("selected", true).val('');
            $('#SpecDocName').attr("selected", true).val('');
            $('#modal-report-spec').modal('hide');
            $('#Spaction').val('Save');
            $('#Spid').val('');
            $('#specclinicbtn').attr('disabled', false);
            $('#specclinicbtn').text('Submit');


        }
    });
}


function getSpecConsultant(val) {
    $.ajax({
        url: "{{ url('getSpecConsultant') }}",
        method: "POST",
        data: {
            _token: '{{csrf_token()}}',
            val: val
        },
        success: function(data) {
            //console.log(data);
            $('#SpecDocName').html(data);
        }
    });
}
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function getValue(val, idval) {
    $('#ModalValue').val(val);
    $('#ValueId').val(idval);
    $('#OldValue').val('');
    $('#search_text77').val('');
    $('#Ref_By').focus();
    desc_lists(val);
}
function success_toast() {
    var isRtl = $('body').attr('dir') === 'rtl' || $('html').attr('dir') === 'rtl';
    $.growl.notice({
        title: 'Success',
        message: 'New Appointment Created Successfully!',
        location: isRtl ? 'tl' : 'tr'
    });
}

function error_toast() {
    var isRtl = $('body').attr('dir') === 'rtl' || $('html').attr('dir') === 'rtl';
    $.growl.error({
        title: 'Error',
        message: 'Record Not Saved',
        location: isRtl ? 'tl' : 'tr'
    });
}

$(document).ready(function() {
    $('#validation-form').on('submit', function(e) {
        e.preventDefault();
        if ($('#validation-form').valid()) {
            $.ajax({
                url: "{{ url('saveNewAppointmnet') }}",
                method: "POST",
                data: new FormData(this),
                contentType: false,
                processData: false,
                beforeSend: function() {
                    $('#submit').attr('disabled', 'disabled');
                    $('#submit').text('Please Wait...');
                },
                success: function(data) {
                    var res = JSON.parse(data);
                    $('#saveid').val(res.id);
                    $('#action').val('Edit');
                    if (res.Status == 1) {
                        success_toast();
                        window.location.href = "{{ url('Appointment') }}";
                    } else {
                        error_toast();
                    }
                    $('#submit').attr('disabled', false);
                    $('#submit').text('Save');
                }
            });
        }
    });


$(document).on("input", "#MrdNo", function(event) {
    var val = this.value;
    $.ajax({
        url: "{{ url('getPatientDetails') }}",
        method: "POST",
        data: {
            _token: '{{csrf_token()}}',
            id: val
        },
        dataType: "json",
        success: function(data) {
            //console.log(data);
            $('#MrdNo').val(data.MrdNo);
            $('#PatientName').val(data.PatientName);
            $('#Phone').val(data.Phone);

        }
    });

});

$(document).on("click", ".updatespclinic", function(event) {
        event.preventDefault();
        event.stopPropagation();
        var id = $(this).attr("data-id");
        var action = "fetch_record";
        $.ajax({
            url: "{{ url('editSpecialityClinic') }}",
            method: "POST",
            data: {
                _token: '{{csrf_token()}}',
                id: id
            },
            dataType: "json",
            success: function(data) {
                $('#SpClinicName').val(data.ClinicName);
                $('#SpName').val(data.Name);
                $('#Spaction').val('Edit');

                $('#Spid').val(id);
                $('#specclinicbtn').text("Update");
                $('#modal-report-spec').modal('show');
                $('#specclinictitle').html(
                    "Update <span class='font-weight-light'>Specility Clinic</span>");


            }
        });
    });



    $(document).on("click", ".deletespclinic", function(event) {
        event.preventDefault();
        var id = $(this).attr("data-id");
        var action = "delete";
        //alert(id);
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this Record!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Yes, delete",
            cancelButtonText: "No, cancel",
            closeOnConfirm: false,
            closeOnCancel: false
        }, function(isConfirm) {
            if (isConfirm) {
                $.ajax({
                    url: "{{ url('deleteSpecialityClinic') }}",
                    method: "POST",
                    data: {
                        _token: '{{csrf_token()}}',
                        id: id
                    },

                    success: function(data) {
                        swal("Deleted!", "Speciality Clinic has been deleted.",
                            "success");

                        spec_clinic_lists();


                    }
                });

            } else {
                swal("Cancelled", "Speciality Clinic is safe :)", "error");
            }
        });


    });
});
</script>