/**
 * Created by Gabi on 26/10/2017.
 */

function load_ajax(filter_key,filter_value,ajax_url,target_id,model_class,data_key,data_title){
    var req = $.ajax({
        url: ajax_url,
        type: "GET",
        data: {filter_key: filter_key, filter_value : filter_value,
            model_class : model_class, data_key : data_key, data_title : data_title
        },
        dataType: "html"
    });

    req.done(function (response, textStatus, jqXHR) {
        $('#'+target_id).html(response);
    });
    req.fail(function (jqXHR, textStatus) {
        swal("Erreur!", "Erreur lors de l'opération.", "warning");
    });
}


function refresh_table_ajax(filter_key,filter_value,ajax_url,target_id,model_class,data_key){
    var req = $.ajax({
        url: ajax_url,
        type: "GET",
        data: {
            filter_key: filter_key, filter_value : filter_value,
            model_class : model_class, data_key : data_key
        },
        dataType: "html"
    });

    req.done(function (response, textStatus, jqXHR) {
        $('#'+target_id).html(response);
    });
    req.fail(function (jqXHR, textStatus) {
        swal("Erreur!", "Erreur lors de l'opération.", "warning");
    });
}

function formaDate(date, delimiter) {
    if(date==null){
        return '';
    }
    if(delimiter==undefined)
        delimiter = '/';
    var datefin = new Date(date);
    var month2 = datefin.getMonth() + 1;
    return datefin.getDate() + delimiter + (month2 >= 10 ? month2 : "0" + month2) + delimiter + datefin.getFullYear();

}

function formaDatetime(p_date) {
    add = 0;
    var date = new Date(p_date);
    var month2 = date.getMonth() + 1;
    var h = date.getHours() + add;
    var min = date.getMinutes();
    return (date.getDate() >= 10 ? date.getDate() : "0" + date.getDate()) + "/" + (month2 >= 10 ? month2 : "0" + month2) + "/" + date.getFullYear() + ' ' + (h >= 10 ? h : "0" + h) + ':' + (min >= 10 ? min : "0" + min);
}

function selectSlide(select) {
    var src_img = select.find(":selected").attr('data-thumbnail');
    if(src_img!=undefined)
        $('#img-slide').attr('src',src_img);
    else
        $('#img-slide').attr('src','');
}

function formatMoney(num, fixedDecimalLength, localize) {
    num = num + "";
    var str = num;
    var reg = new RegExp(/(\D*)(\d*(?:[\.|,]\d*)*)(\D*)/g);
    if (reg.test(num)) {
        var pref = RegExp.$1;
        var suf = RegExp.$3;
        var part = RegExp.$2;
        if (fixedDecimalLength / 1) part = (part / 1).toFixed(fixedDecimalLength / 1);
        if (localize) part = (part / 1).toLocaleString();
        str = pref + part.match(/(\d{1,3}(?:[\.|,]\d*)?)(?=(\d{3}(?:[\.|,]\d*)?)*$)/g).join(' ') + suf;
    }
    return str;
}

function activeIputImage() {
    $(".filer_input_gabi").filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png'],
        changeInput: '<div class="jFiler-input-dragDrop"><div class="jFiler-input-inner"><a class="jFiler-input-choose-btn btn btn-custom waves-effect waves-light">Choisir  une image</a></div></div>',
        showThumbs: true,
        theme: "dragdropbox",
        templates: {
            box: '<ul class="jFiler-items-list jFiler-items-grid"></ul>',
            item: '<li class="jFiler-item">\
                        <div class="jFiler-item-container">\
                            <div class="jFiler-item-inner">\
                                <div class="jFiler-item-thumb">\
                                    <div class="jFiler-item-status"></div>\
                                    <div class="jFiler-item-info">\
                                        <span class="jFiler-item-title"><b title="{{fi-name}}">{{fi-name | limitTo: 25}}</b></span>\
                                        <span class="jFiler-item-others">{{fi-size2}}</span>\
                                    </div>\
                                    {{fi-image}}\
                                </div>\
                                <div class="jFiler-item-assets jFiler-row">\
                                    <ul class="list-inline pull-left">\
                                        <li>{{fi-progressBar}}</li>\
                                    </ul>\
                                    <ul class="list-inline pull-right">\
                                        <li><a class="icon-jfi-trash jFiler-item-trash-action"></a></li>\
                                    </ul>\
                                </div>\
                            </div>\
                        </div>\
                    </li>',
            itemAppend: '',
            progressBar: '<div class="bar"></div>',
            itemAppendToEnd: false,
            removeConfirmation: true,
            _selectors: {
                list: '.jFiler-items-list',
                item: '.jFiler-item',
                progressBar: '.bar',
                remove: '.jFiler-item-trash-action'
            }
        },
        dragDrop: {
            dragEnter: null,
            dragLeave: null,
            drop: null,
        },
        uploadFile: {

        },
        files: [

        ],
        addMore: false,
        clipBoardPaste: true,
        excludeName: null,
        beforeRender: null,
        afterRender: null,
        beforeShow: null,
        beforeSelect: null,
        onSelect: null,
        afterShow: null,
        onRemove: function(itemEl, file, id, listEl, boxEl, newInputEl, inputEl){
            var file = file.name;

        },
        onEmpty: null,
        options: null,
        captions: {
            button: "Choisir une image",
            feedback: "Choisir une image",
            feedback2: "Fichier choisis",
            drop: "Déposez votre image ici",
            removeConfirmation: "Vous êtes sur de supprimer cette image?",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Veuiller choisir un fichier correcte.",
                filesSize: "{{fi-name}} est trop grand. Choisir une image de taille inférieur {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
}


function calcul_age(date_naiss, datenow) {
    var d=date_naiss;
    var m=datenow;
    var age=""; var age_a=0;var age_m=0;
    if (d!=null) {
        if (d.getTime()>m.getTime()) {
            age="La date de naissance est incorrecte";
            //document.formage.dt_naissance.focus();
        }else{
            age_a = m.getFullYear()-d.getFullYear();
            m.setYear(d.getYear());
            if ((d.getTime()>m.getTime())&&(d.getMonth()-m.getMonth()!=0)) {age_a--;}
            if (d.getMonth() >= m.getMonth()) {
                age_m = 12 - (d.getMonth()-m.getMonth())
            } else {
                age_m = (m.getMonth()-d.getMonth())
            }
            if (age_m==12) {age_m=0;}
            if (age_a==1) { age=age_a+" an"}
            if (age_a>1) { age=age_a+" ans"}
            //if ((age_a>0)&&(age_m>0)) {age+=" et "}
            //if (age_m>0) {age+=age_m+" mois"}
            //if (age=="") { age=" moins de 1 mois"}
        }
    } else {
        age="La date de naissance est incorrecte";
    }
    return age;
}


