<?php 



class nav{
    static function alert($message = ""){
        app::apiRep("alert",$message);
    }
    static function log($message = ""){
        app::apiRep("log",$message);
    }
    static function js($message = ""){
        if(is_callable($message)){
            ob_start();
            $message();
            $message= Route::rmvTag("script",ob_get_clean());
        }
        app::apiRep("eval",$message);
    }
    static function modalMsg($message,$titre = null,$time = 0){
        if (is_null($titre)) {
            $titre = $_SERVER["HTTP_HOST"];
        }
        if(is_callable($message)){
            ob_start();
            $message();
            $message= ob_get_clean();
        }
        ob_start();
        ?>
<dialog titre="<?= $titre ?>" ng-model="dialogmsg" btn-fermer="true">
    <?= $message ?>
</dialog>
        <?php
        $message=ob_get_clean();
        self::js(function() use ($message,$time){
?>
<script>
    $("body").append(window.compileHtml(<?= json_encode($message) ?>));
    setTimeout(function(){
        window.$root.dialogmsg.show();
        <?php if($time>0) { ?>
         setTimeout(function(){
            window.$root.dialogmsg.hide();
         },<?= $time ?>);
        <?php } ?>
    },100);
</script>
<?php
        });
    }
}




function avs(&$data){
    return new avsIns($data);
}

class avsIns{
    var $data = [];
    function __construct(&$data){
        $this->data = $data;
    }
    function isEmail($prop){
        return new avsProp($this,$prop,"email");
    }
    function isString($prop){
        return new avsProp($this,$prop,"string");
    }
    function isNum($prop){
        return new avsProp($this,$prop,"num");
    }
    function ok(){
        return avs::$bool;
    }
}

class avsProp{
    var $parent = null;
    var $name = null;
    var $type = null;
    var $etat = 0;
    var $optional = false;
    function __construct($parent,$name,$type){
        $this->parent = $parent;
        $this->name = $name;
        $this->type = $type;
    }
    function option(){
        $this->optional = true;
        return $this;
    }
    var $lengthdata = 1;
    function length($nbr){
        $this->lengthdata = $nbr;
        return $this;
    }
    var $minval = null;
    function min($nbr){
        $this->minval = $nbr;
        return $this;
    }
    var $maxval = null;
    function max($nbr){
        $this->maxval = $nbr;
        return $this;
    }
    function else($fn="",$emptydata = null){
        if ($this->optional && empty($this->parent->data[$this->name])) {
           $this->etat = 0;
        }else{
            if (empty($this->parent->data[$this->name])) {
                $this->etat = -2;
            }else{
                $val = $this->parent->data[$this->name];
                switch ($this->type) {
                    case 'email':
                        $this->etat = -1;
                        if (filter_var($val, FILTER_VALIDATE_EMAIL)) {
                            $this->etat = 1;
                        }
                    break;
                    case "num":
                        $this->etat = -1;
                        if (is_numeric($val)) {
                            $this->etat = 1;
                            $this->parent->data[$this->name] = floatval($val);
                            if (!is_null($this->minval)) {
                                if ($this->parent->data[$this->name]<$this->minval) {
                                     $this->etat = -1;
                                }
                            }
                            if(!is_null($this->maxval)){
                                if ($this->parent->data[$this->name]>$this->maxval) {
                                     $this->etat = -1;
                                }
                            }
                        }
                    break;
                    case "string":
                        $this->etat = 1;
                        if (empty($val) || strlen($val)<$this->lengthdata) {
                            $this->etat = -1;
                        }
                    break;
                }
            }
        }
        if ($this->etat == -1) {
            if (is_callable($fn)) {
                ob_start();
                $fn();
                $fn=ob_get_clean();
            }
            avs::err($this->name,$fn);
        }
        if ($this->etat == 1) {
            if (is_callable($fn)) {
                ob_start();
                $fn();
                $fn=ob_get_clean();
            }
            avs::ok($this->name);
        }
        if ($this->etat == -2) {
            if (!is_null($emptydata)) {
                $fn = $emptydata;
                if (is_callable($fn)) {
                    ob_start();
                    $fn();
                    $fn=ob_get_clean();
                }
            }else{
                if (is_callable($fn)) {
                    ob_start();
                    $fn();
                    $fn=ob_get_clean();
                }
            }
            avs::err($this->name,$fn);
        }
        return $this;
    }
}




class avs{
    static $data = [];
    static $bool = true;
    static function error($prop,$message=null){
        self::$bool = false;
        self::$data[$prop] = ["etat"=>false,"msg"=>$message];
    }
    static function success($prop){
        self::$data[$prop] = ["etat"=>true];
    }
    static function run(){
        if (!empty(self::$data)) {
            app::apiRep("avs",self::$data);
        }
    }
    static function err($prop,$message=null){
        self::$bool = false;
        return self::error($prop,$message);
    }
    static function no($prop,$message=null){
        self::$bool = false;
        return self::error($prop,$message);
    }
    static function ok($prop,$message=null){
        return self::success($prop,$message);
    }
    static function yes($prop,$message=null){
        return self::success($prop,$message);
    }
    static function show($msg_of_fn,$hidefrm = true){
        if (is_callable($msg_of_fn)) {
            ob_start();
            $msg_of_fn();
            $msg_of_fn = ob_get_clean();
        }
        app::apiRep("avsShow",["html"=>$msg_of_fn,"hide"=>$hidefrm]);
    }
    static function match($name,$data,$pattren,$msg = null){
        if (preg_match($pattren, $data)) {
            self::ok($name);
            return true;
        }else{
            self::err($name,$msg);
        }
        return false;
    }
}




Route::isCss(function($data){
$successpng = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAAB3RJTUUH4AMBBhgidnoJmAAAA+VJREFUSMfF10toXFUYB/Df3Jk0WougVRTBhRILEjEpKm2Y1I27KNYi9YEixgcKaikiLnwtKq4UFHFRaEUt1SaKlAqpIKILW2Npq7EoARvQnQttuonWmkzi4nzHuTPMpFgUD1zOPa///37P892KUqtPjJSHFRRoxLgXg7gOV+HCmJ/FcRzFFE7HfBWLWMqAB2/e3wLeibSIfhFX4CFsDsLl2nF8gJ34sQ2nhbzSgbQaUp6HF7AF58TaDA5gGr/G3EW4GsPoi7k/8Dq24bcS5t/klS6k/RiPfh7v4w0cwUIXaWu4Ho/jDvTge9wZfQt50YF0GF8E6RTquBeHYr0aJOUnnz0Ue+txtj+whktnW4izE12LfbggJB7C4QAvwlEaIXX5acRaEXsPx9nxwNoX2I3MWQs7L2IVdkneOoa7Y1O1Xb1l72zzkazKGv4MjEqofldIPodKUZL6eQyEHR8s4bY4RTtppw/RDKMljAbmAJ7LGs4q7sMTMbkVv2vGYTfwThJnaRexEV/iUjwaa1uCq5GlHcW5oY6DcbhxFqQ9YZbNYa6hwD6K94JjVKi5F5vi4I7ol86SdD7suidwP8crsb49+k3ordQnRtZjEj/gmjgsVL3UTd1dSO/DWyHQZ7hVSiB5z3dYg6ECa2Phqzhc1QyvHC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gox5XLa3717l1SrC8+5oc5I5dzOr+01ajiAiE2x2K6mF164zb7mGj//9NMUDg5Ky/bttYP21VcD1ap4X37JMKbAzc0r9NDQ59C6rSFzOsvx9CEzPcAQIjaUOqRaW+8vrl/vSxBw9KabDKfTQJ06xHt7ERw5AvF9A2ZFlnVAtbYuI9c9BhG7/gEQnqPPc7MOIWIgYlM0uk+1ty8jxxkQEYZtm2pfHzLr1p0Bdrq74SxaBPE8mPFxbcplZc+fv9/u6lpEicRWWJaCiIIIQcTUgzizamAEEQuWRZzJvBW5/fbFMzdtOmmKRTaFgjHFItwlS6ZRdNrGHnsM4fHjgDGgZJLDwUGYXM5Eb731RlMoPCWed7eEYQYiOD3X66OQlBoj1/2YU6nfBYcP75MwVKq5WcTzDJSCamnBJVu2nB8YAMZfegn+nj01OuJx9gcGKJwxQ0diMThXXTUjPHz4evG8haZQSIAZnEwWyHX32wsWfK2z2eLkyAh4aEg5c+caqVZFjEF81SrEenrwrebv24f8s89CpKaUkQceUP9dvFgVnn8ekx9+iP7LLjt9tn/OHPwDgIggu2SJGl21ig8D2AMg/9xzF8SgiwUw+dln8PfsQWrtWphsFoUNG0iPjLApl0kmJwlE4GhUKJEQ1dJiml59VfYTYfaGDUivWXPR5P4HiXErTtfyZs8AAAAldEVYdGRhdGU6Y3JlYXRlADIwMTYtMDMtMDFUMDY6NTU6MTEtMDU6MDC5d7KpAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE2LTAzLTAxVDA2OjU1OjExLTA1OjAwyCoKFQAAAABJRU5ErkJggg==')";
?>
<style>
    .avs{
        -webkit-transition: all 200ms ease-in-out;
        -moz-transition: all 200ms ease-in-out;
        -ms-transition: all 200ms ease-in-out;
        -o-transition: all 200ms ease-in-out;
        transition: all 200ms ease-in-out;
    }
    .avs-success{
        background: rgba(46,204,113,0.1) <?= $successpng ?> 99% 50% no-repeat !important;
        border: 1px solid rgba(46,204,113,1) !important;
        padding-right: 30px !important;
        background-size: 20px 20px !important;
    }
    .avs-failed{
        background: rgba(231,76,60,0.1) <?= $failedpng ?> 99% 50% no-repeat !important;
        border: 1px solid rgba(231,76,60,1) !important;
        padding-right: 30px !important;
        background-size: 20px 20px !important;
    }
    .avs-parent-failed{

    }
    .avs-msg{
        color: rgba(231,76,60,1) !important;
        font-size: 80% !important;
        padding: 2px 0 2px 2px !important;
        background-size: 20px 20px !important;
    }
    .avs-msg1{
        
        color: rgba(231,76,60,1) !important;
        padding: 2px 0 2px 2px !important;
        background-size: 20px 20px !important;
    }
    .avs-msg1-icon{
        background: <?= $failedpng ?> 0% 50% no-repeat !important;
        display: inline-block;
        height: 20px;
        width: 20px;
        margin: 0px 5px;
        background-size: 20px 20px !important;
        vertical-align: top;
    }
    textarea.avs-success,textarea.avs-failed{
        background-position-y: 3px !important;
    }
</style>
<?php
});



Route::isJs(function($data){
?>
<script>
    window.avsShow = function(data,dom){
        var form = dom?$(dom):$(document);
        var idelm = form.data("idelmshowdiv");
        if(idelm){
            var elm = $("#"+idelm).html(data.html);
            elm.slideUp("fast");
        }else{
            var idelm = uniqid();
            form.data("idelmshowdiv",idelm);
            var elm = $("<div style='display:none' id='"+idelm+"'>"+data.html+"</div>");
            elm.insertBefore(form);
        }
        if (data.hide) {
            form.slideUp();
        }
        elm.slideDown();
        $('html, body').animate({scrollTop: elm.offset().top-200 }, 300);
        $("form",elm).each(function(){
            $(this).data("idelmshowdiv",idelm);
        });
    }
    window.avs = function(data,dom){
        $(".avs-msg").remove();
        domm = dom?$(dom):$(document);
        $(".avs-msg1",domm).each(function(){
            $(this).parents(".avs").removeClass("avs-success").removeClass("avs-error").removeClass("has-success").removeClass("has-error");
            $(this).parent().html("");
        });
        $(".avs-failed,.avs-success",domm).each(function(){
            $(this).removeClass("avs-failed").removeClass("avs-success").removeClass("has-success").removeClass("has-error");
            $(this).parents(".avs").removeClass("avs-success").removeClass("avs-error").removeClass("has-success").removeClass("has-error");
        });
        for(var p in data){
          var a = $("input[name="+p+"],.form-control[name="+p+"],.avs[name="+p+"],*[avs="+p+"]",domm);
          var b = $(".avs-"+p+"-msg",domm);
          if (!a.length && b.length) {
            a = b.parents(".avs");
          }
          if(a.length && p !=="api" ){
            resetavisdom(a);
            if (data[p].etat) {
                a.addClass("avs-success");
                a.parent(".avs").addClass("has-success");
            }else{
                a.addClass("avs-failed");
                a.parent(".avs").addClass("has-error");
                if (data[p].msg) {
                    if (b.length) {
                        var id = uniqid();
                        a.data("avsmsgid",id);
                        b.html('<div class="avs-msg1"><span id="'+id+'" class="avs-msg1-icon"></span>'+data[p].msg+'</div>');
                    }else{
                        var id = uniqid();
                        a.data("avsmsgid",id);
                        var pp = $(".input-group-addon,.input-group-btn",parent).get(0);
                        if (a.parents && a.parents(".input-group").length && pp) {
                            var parent = a.parents(".input-group");
                            var width = pp.offsetWidth;
                                parent.after('<div class="avs-msg" id="'+id+'" style="padding-left:'+width+'px !important">'+data[p].msg+'</div>');
                        }else{
                            a.after('<div class="avs-msg" id="'+id+'">'+data[p].msg+'</div>');
                        }
                    }
                }
            }
          }else if(b.length){
          b.html('<div class="avs-msg1"><span class="avs-msg1-icon"></span>'+data[p].msg+'</div>');
          }else{
            if (!data[p].etat && p !=="api") {
                if (data[p].msg) {
                    alert(data[p].msg);
                }else{
                    alert(p+" invalid !");
                }
            }
          }
        }
    }
    window.resetavisdom = function(dom){
        dom.removeClass("avs-success").removeClass("avs-failed");
        dom.parent(".avs").removeClass("has-success").removeClass("has-error");
        var id = dom.data("avsmsgid");
        if(id)$("#"+id).remove();
        if(!dom.hasClass("avs")) dom.addClass("avs");
    }
    window.resetallavs = function(){
        $(".avs-success,.avs-failed").each(function(){
            window.resetavisdom($(this));
        });
    }
    $(function(){
        $(document).on("change",".avs-success,.avs-failed",function(){
            resetavisdom($(this));
        });
        $(document).on("keypress",".avs-success,.avs-failed",function(){
            resetavisdom($(this));
        });
    });




</script>
<?
});
