

$('#patient-id').change(function () {
    var patient_id = $(this).val()
    if(patient_id>0) $('.sample-creation-form').fadeIn()
    else $('.sample-creation-form').fadeOut()
})

function searchPatientToForm(patientElementId){
    Swal.fire({
        html:"<p style='text-align: left;'>Search the patient with their name, phone, Khmer ID, or patient ID</p>",
        input: "text",
        showCancelButton: !0,
        confirmButtonText: "<i class='fa fa-search mr-2'></i>Search",
        cancelButtonText: "Close",
        position: 'top',
        showLoaderOnConfirm: !1,
        confirmButtonColor: "#4fb51a",
        preConfirm: function (n) {
            return new Promise(function (t) {
                $.ajax({
                    url: patient_base_url + "/async",
                    method:'get',
                    dataType:'json',
                    data:{kword: n},
                    success:function(response)
                    {
                        if(response.data.length<1 || !response.success){
                            Swal.showValidationMessage('No patient found!')
                            Swal.enableButtons()
                        }
                        else {
                            t(response.data)
                        }
                    }
                })
            })
        },
        allowOutsideClick:!1,
    }).then(function (t) {
        if(!t.dismiss){
            $('#'+patientElementId).html('');
            t.value.map(item =>{
                var patientOption = document.createElement('OPTION');
                patientOption.setAttribute('value',item.id);
                patientOption.text = item.patient_uuid + ' - ' + item.name_en;
                $('#'+patientElementId).append(patientOption);
            })
        }
    })
}

$("#searchPatientAddForm").click(function () {
    searchPatientToForm('patient-id')
});

$('#province-id').change(function () {
    getDistrictsByProvinceId('district-id', $(this).val())
})

$('#district-id').change(function () {
    getCommunesByDistrictId('commune-id', $(this).val())
})

$('#commune-id').change(function () {
    getVillagesByCommuneId('village-id', $(this).val())
})


$("#savePatient").on('click',function(e){
    e.preventDefault();
    $.ajax({
        url: patient_base_url.toString()+'.save',
        type:"POST",
        dataType:"JSON",
        data :{
            "_token": csrf_token,
            name_en: $('#patient-name').val(),
            patient_uuid: $('#patient-code').val(),
            dob: $('#dob').val(),
            gender: $("input[name='gender']:checked").attr('data-id'),
            marital_status: $("input[name='marital_status']:checked").attr('data-id'),
            phone_number: $('#phone-number').val(),
            province_id: $('#province-id').val(),
            district_id : $('#district-id').val(),
            commune_id: $('#commune-id').val(),
            village_id: $('#village-id').val(),
            khid_number: $('#khid-number').val(),
            house_number: $('#house-number').val(),
            street_number: $('#street-number').val()
        },
        success:function(res){
            if(res.success){
                var patient = res.data
                var patientOption = document.createElement('OPTION');
                patientOption.setAttribute('value',patient.id);
                patientOption.setAttribute('selected','selected');
                patientOption.text = patient.patient_uuid + ' - ' + patient.name_en;
                $('#patient-id').append(patientOption);

                $('#modal-patient .close').click();
                $('#patient-id').trigger('change')

            }else{
                alertify.error("Can't create patient.")
            }
        }
    });
});

$("#saveSample").on('click',function(e){
    $('#form-start-opd-visit').submit();
});
