import moment from 'moment';

$(document).ready(function(){
    $.fn.getDays = function(){
        var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
        return days;
    }
    /**
     * 
     * @returns 
     */
    $.fn.validations = function(){
        return [
            {
            "name":"noEmpty",
            "message":window.translations["create-appointment"].fieldRequired,
            "run":function (field){
                if((field.key === "date") || (field.key==="dateAppointment")) 
                    return true;
                return ((field.value != undefined) && (field.value.trim() !== "")); 
            },
            },
            {
                "name":"noEmptySelect",
                "message":window.translations["create-appointment"].fieldRequired,
                "run":function (field){
                    return (field.value !== "SELECT"); 
                },
            }
        ];
    }
    $("#appointment").on("submit",function(e){
        let form    = document.querySelector("#appointment");
        let data    = $.fn.getDataForm.call(this,form);
        let valid   = $.fn.runValidations(data,$.fn.validations());
        let date,hour;
        let appointmentDate = (window.appointmentDate == undefined) ? $("#date").val() : window.appointmentDate;
        if(!valid.valid)
            e.preventDefault();
        else if(appointmentDate == undefined || appointmentDate == null || appointmentDate === ""){
            alert(window.translations["create-appointment"].emptyDate);
            e.preventDefault();
        }else{
            date = new Date(appointmentDate);
            hour = Number($("#hour").val());
            if (($("#schedule").val() === "PM") && (hour < 12)) 
                hour += 12;
            else if (($("#schedule").val() === "AM") && hour == 12)
                hour -= 12;
    
            date.setHours(hour,$("#minute").val(),"00");
            let currentDateTime = moment(date).format('DD/MM/YYYY hh:mm:ss A');
            $("#date").val(currentDateTime);
        }
    });

    $("#RegisterPatient").on("click",function(){
        let form    = document.querySelector("#appointment");
        let data    = $.fn.getDataForm.call(this,form);
        let dataSend = {};

        if((window.appointmentDate != undefined) && (window.appointmentDate !== ""))
            dataSend.date = window.appointmentDate.toString();
        if(data.dateAppointment !== "")
            dataSend.dateAppointment = data.dateAppointment;
        if(data.especiality !== "")
            dataSend.especiality = data.especiality;
        if(data.hour !== "")
            dataSend.hour = data.hour;
        if(data.minute !== "")
            dataSend.minute = data.minute;
        if(data.schedule !== "")
            dataSend.schedule = data.schedule; 
        if(data.summary !== "")
            dataSend.summary = data.summary; 
        if(data.doctorname !== "")
            dataSend.doctorname = data.doctorname;

        let params = new URLSearchParams(dataSend).toString();
        let url = "create-patient-external";
        
        window.location.href=url;
        //console.log(JSON.stringify(data));
    });
})