
$(".datepicker").datepicker({
    format:'dd-mm-yyyy'
});

$("#btnSave").on('click',function(e){
   // console.log($('#dob').val().replaceAll('-','/'))
    var dob_ = $('#dob').val().split("-");
    var _dob = dob_[0]+'-'+dob_[1]+'-'+dob_[2];
    e.preventDefault();
    data_id = $('#data-id').val();
    let url = patient_base_url.toString() + (parseInt(data_id) === 0 ? '.save' : '.update').toString();
    $.ajax({
        url: url,
        type:"POST",
        data :{
            "_token": csrf_token,
            uid : data_id,
            name_en: $('#patient-name').val(),
            patient_uuid: $('#patient-code').val(),
            dob: _dob,
            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){
            handleMessage(res.success, res.message)
            setTimeout(function () {location.reload()}, messageDuration)
        }
    });
});

function delete_data(uid){
    deleteEntityRecord(uid, patient_base_url.toString() + '.delete')
}

function restore_data(uid){
    restoreEntityRecord(uid,patient_base_url.toString() + '.restore')
}


$('#modal-patient').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var action = button.data('action') // Extract info from data-* attributes
    var modal = $(this);
    if(action === "new"){
        // Clear inputs
        modal.find('.modal-body input').val("");
        modal.find('.modal-body input[name="uid"]').val(0);
        modal.find('.modal-title').html(modal_add_title);
        modal.find('.modal-body select[name="province_id"]').val(0).trigger('change');
    }else if(action === "edit"){
        uid = button.data('uid');
        modal.find('.modal-body input[name="uid"]').val(uid);
        modal.find('.modal-title').html(modal_edit_title);

        $.get(patient_base_url+'.get?uid='+uid, function(res, status){
            if(res.success) {
                let data = res.data;
                let dob = new Date(data.dob)
                var dob_split = dob.toLocaleDateString().split("/")

                modal.find('.modal-body input[name="patient_code"]').val(data.patient_uuid);
                modal.find('.modal-body input[name="patient_name"]').val(data.name_en);
                //modal.find('.modal-body input[name="dob"]').val(data.dob);
                $("#dob").val(dob_split[1].padStart(2,"0")+'-'+dob_split[0].padStart(2,"0")+'-'+dob_split[2]);
                $("#dob").change();
                //$('#age-unit').change();
                modal.find('.modal-body input:radio[name="gender"]').filter("[data-id=" + data.gender + "]").prop('checked', true);
                modal.find('.modal-body input:radio[name="marital_status"]').filter("[data-id=" + data.marital_status + "]").prop('checked', true);
                modal.find('.modal-body input[name="phone_number"]').val(data.phone_number);
                modal.find('.modal-body input[name="khid_number"]').val(data.khid_number);
                modal.find('.modal-body input[name="house_number"]').val(data.house_number);
                modal.find('.modal-body input[name="street_number"]').val(data.street_number);
                getProvinces('province-id', data.province_id)
                getDistrictsByProvinceId('district-id', data.province_id, data.district_id)
                getCommunesByDistrictId('commune-id', data.district_id, data.commune_id)
                getVillagesByCommuneId('village-id', data.commune_id, data.village_id)
            }
            else {
                handleMessage(res.success, res.message)
            }
        });
    }
})


$('#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())
})

