//Get Form in Every Page-Check(After Billing)
var flag = ''
function CheckFormRights(patient_id, form_id, consultingType, ApplicableTo, DoctorId, master_service_id) {
    debugger;
    var DoctorData = {
        dml_indicator: 'UC',
        Patient_Id: patient_id,
        FormId: form_id,
        RoleId:$('#hiddenRole_id').val(),
        UserId:  $('#hiddenUser_id').val(),
        ConsultingType: consultingType,
        ApplicableTo: ApplicableTo,
        Visit_Report_Id: master_service_id
    }
    return $.ajax({
        async: true,
        type: 'POST',
        url: '/Form_builder/GetForms',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                debugger;
                if (data2.data.Table[0].StringMsg == "Rights") {
                    var patientid = data2.data.Table[0].patientid
                    if (form_id!=1133) {
                        loadForm(patient_id, form_id, consultingType, ApplicableTo, DoctorId, master_service_id, 'SF', 1)
                        $('#iframeFormpopup').modal("toggle");
                    }
                    $('#formShow').removeAttr('src');
                    $('#formShow_header').html('')
                  //  $('#formShow_header').html(data2.data.Table[0].Form_Name);
                    $('#hBuyId').val(data2.data.Table[0].BuyId);
                    $('#hIsBill').val(data2.data.Table[0].IsBill);
                 //   $('#hFormFillId').val(data2.data.Table[0].FormFillId)
                    $('#hConsultingType').val(consultingType)
                   // if (data2.data.Table[0].IsUpload == 1) {
                        $('.fileuploadid').show()
                    //}
                    //else {
                    //    $('.fileuploadid').hide()
                    //}
                   // $('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + data2.data.Table[0].Formid + '?patientid=' + btoa(data2.data.Table[0].patientid) + '')
                }
                
            }
        }
    });
}
//Form Check For Pre Consultation and Post Consultation
function CheckConsulationRights(patient_id, form_id, consultingType, DoctorId, doctor_appoinment_solt_id, vist_reportid, branch_id) {
    debugger;
    var DoctorData = {
        dml_indicator: 'UCC',
        UserId: DoctorId,
        ConsultingType: consultingType,
        Patient_Id: patient_id,
      
    }
    return $.ajax({
        async: true,
        type: 'POST',
        url: '/Form_builder/GetForms',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                debugger;
                if (data2.data.Table[0].StringMsg == "Rights") {
                    var patientid = data2.data.Table[0].patientid
                    loadForm(patientid, form_id, consultingType, 0, DoctorId, 0, 'SFC', 2)
                    $('#hndbranch_id').val(branch_id)
                    $('#hdoctor_appoinment_solt_id').val(doctor_appoinment_solt_id)
                    $('#hdoctorId').val(DoctorId)
                    $('#hndvisit_reportid').val(vist_reportid)
                    $('#iframeFormpopup').modal("toggle");
                   // $('#formShow_header').html(data2.data.Table[0].Form_Name);
                   // $('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + data2.data.Table[0].Formid + '?patientid=' + btoa(data2.data.Table[0].patientid) + '&visit_reportid=' + btoa(vist_reportid) + '')
                }

            }
        }
    });
}

function imageupload() {
    debugger;
    var fileUpload = $("#fileToUploaded").get(0);
    var files = fileUpload.files;
    var filePath = fileUpload.value;

    // Allowing file type
    var allowedExtensions =
               /(\.jpg|\.jpeg|\.png|\.pdf)$/i;

    if (!allowedExtensions.exec(filePath)) {
        toastr.warning('Invalid File Type, It should be PDF');
        return false;
    }
    if (window.FormData !== undefined) {
        var fileData = new FormData();

        for (var i = 0; i < files.length; i++) {
            fileData.append(files[i].name, files[i]);
        }
        fileData.append('username', 'Manas');
        $.ajax({
            url: '/Form_builder/UploadPhotoFiles',
            type: "POST",
            contentType: false,
            processData: false,
            data: fileData,
            success: function (result) {
                $.each(result, function (i, emp) {
                    var d, n, drloimage;
                    d = new Date();
                    n = d.getTime();
                    drloimage = $('#hdnCDfilepath').val() + $('#HospitalId').val() + '/FormMappingFileUpload/' + emp + '?' + n;
                    $('#photolog').empty();
                    $('#photolog').append('<a href="' + drloimage + '" target="_blank" class="another-eye">View</a>');
                    $('#Hdfileupload').val(emp);
                    toastr.success('Uploaded Successfully');
                });
            }
        });
    }
}

//Edit Form (after form filling completed)
function EditForm(form_data_id, Form_name, patient_id, IsUpload) {
    debugger;
  //  if (IsUpload == 1) {
        $('.fileuploadid').show()
    //}
    //else {
    //    $('.fileuploadid').hide()
    //}
    $("#hFormCloseCheck").val(1)
    $('#iframeFormpopup').modal("toggle");
    $('#formShow_header').html(Form_name); 
    $('#formShow').attr('src', $("#Hdformurl").val() + 'edit-form/' + form_data_id + '?patientid=' + btoa(patient_id) + '')
    $('#div_selectform').hide()

}
//load Select Form
function loadForm(patient_id, form_id, consultingType, ApplicableTo, DoctorId, master_service_id, dml, Type) {
    debugger;  
    var data = {
        Dml_Indicator: dml,
        Patient_Id: patient_id,
        FormId: form_id,
        RoleId: $('#hiddenRole_id').val(),
        UserId: $('#hiddenUser_id').val(),
        ConsultingType: consultingType,
        ApplicableTo: ApplicableTo,
        Visit_Report_Id: master_service_id,
        doctor_id: DoctorId
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Form_builder/GetForms',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(data),
        success: function (data) {
            debugger;

            var data2 = JSON.parse(data);
            console.log(data2);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    $('#selectform').empty();
                    var option = "";
                    var data1 = data2.data.Table;
                    var default_option = "<option value='0'>--Select--</option>";
                    $.each(data1, function (i, emp) {
                        if (dml=="SF") {
                            option = option + "<option value='" + emp.Formid + "' data-formfillid='" + emp.FormFillId + "'>" + emp.Form_Name + "</option>"
                        }
                        else{
                         option = option + "<option value=" + emp.Formid + " >" + emp.Form_Name + "</option>"
                         }

                    })
                    // $('#servicetype_filter').append(default_option + option);
                    $('#selectform').append(default_option + option);
                    $('#hndpatientid').val(patient_id)
                    flag = Type
                    
                }
            }
        }
    })
}

$('#selectform').change(function () {
    debugger;
    $('#formSubmit').attr('disabled', false);
    if ($('#Hdtipopflag').length > 0) {
        if ($('#Hdtipopflag').val() !== undefined || $('#Hdtipopflag').val() != 0 || $('#Hdtipopflag').val() != '') {      //added by blessie
            //set Patient_id
            if ($('#hnppatientid').val() != '') { //get hnppatient_id from dashboard op Info-icon forms
                $('#hndpatientid').val($('#hnppatientid').val())
            }
          
            //set vistreport_id
            if ($('#hnpvist_reportid').val() != '') { //get hnpvist_report_id from dashboard op Info-icon forms
                $('#hndvisit_reportid').val($('#hnpvist_reportid').val())
            }
        }
    }

    var selectedOption = $(this).find('option:selected');

    var FormFillId = selectedOption.data('formfillid');

    $('#hFormFillId').val(FormFillId);


    var selectedOptionText = $(this).find('option:selected').text();
    $('#formShow_header').html(selectedOptionText);
    var formid = $('#selectform').val()
    $('#formShow').empty()
    if (flag == 1) {
        $('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + formid + '?patientid=' + btoa($('#hndpatientid').val()) + '')
    }
    else {
        $('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + formid + '?patientid=' + btoa($('#hndpatientid').val()) + '&visit_reportid=' + btoa('') + '')
    }
})
    
