
FUNC.testo=function(z){return "kkkkkk";};
FUNC.testo=function(a,b){return "eeeeeeeeeeeee";};
FUNC.province = async function(conn,mongo,dbase){
      //var user=await mongo.join2merge(con.data,dbase,"user","_id","user-info","_id",{username:user,password:pass},project);
      let query={ _idprovince: 0  }
      let sort={ name: 1 };
      let pro={_idprovince:0};
      var s=await mongo.join2(conn,dbase,"cityprovince","_id","cityprovince","_idprovince",{$expr:{$gt:[{$size:"$joinlist"},0]}});
       return s;
}







FUNC.listview=async function(conn,mongo,dbase,order,limit,listnum,limiIDs){
    var result=[],query={};
    result.push({"ok":true});
    query[order]=true;
    result.push(query);
    query={$and:result};
    result=[];
    var r=await mongo.join2merge(conn,dbase,"melk","_idcity","cityprovince","_id",query,{_idprovince: 0,_registerarid:0,_userid: 0,_idcity:0,position:0,datein:0,structuer:0,axc:0,rank: 0,offertitr:0,listdesc:0,_confirmid:0,ok:0,old:0,deed:0,baft:0,barter:0},{_id:-1},limit);
    var len=r.data.length;
    if(len>0){
        if(len<=listnum){
            result=r.data;
        }else{
            var s=[],rand;
            for(i=0;i<listnum;i++){
                rand=Math.floor(Math.random()*len);
               // console.log(limiIDs.findIndex(function(x){return x == r.data[rand]._id}));
                if((s.findIndex(function(x){return x == rand})<0) && (limiIDs.findIndex(function(x){return x == r.data[rand]._id})<0)){
                    s.push(rand);result.push(r.data[rand])
                    
                }else{
                    i--;
                    
                }
                
            }
//            for(i=0;i<listnum;i++){result.push(r.data[s[i]]);}
        }
    }
    return result;
}
FUNC.paging3=async function(total,numpage,limit,url,model,urix){
      
    //   console.log();
    urix2=urix.replace('/{0}/',"");
      // console.log("urix2");
      var instance=new Pagination(total, numpage, limit,urix);
      var big="",small="<select id='selsmal'>";
      if(instance.count<6){
        for(var i=1;i<=instance.count;i++){
          if(i==numpage){
            big+='<span class="active">'+i+'</span>';
          }else{
            big+='<a href="'+decodeURI(urix2)+"/"+i+'" >'+i+'</a>';
          }
        }
        small=big;
      }else{
        for(var i=1;i<=instance.count;i++){
          if(i==numpage){
            small+='<option selected>'+i+'</option>';
          }else{
            small+='<option><a href="'+decodeURI(urix2)+"/"+i+'" >'+i+'</a></option>';
          }
        }
        small+="</select>";
   
        if(numpage<=5){
          for(var i=1;i<=9;i++){
            if(i==numpage){
              big+='<span class="active">'+i+'</span>';
            }else{
              big+='<a href="'+decodeURI(urix2)+"/"+i+'" >'+i+'</a>';
            }
          }
          big+='<span>...</span>';

        }else{
          if(instance.count-5<=numpage){
            big+='<span>...</span>';
            for(var i=instance.count-9;i<=instance.count;i++){
              if(i==numpage){
                big+='<span class="active">'+i+'</span>';
              }else{
                big+='<a href="'+decodeURI(urix2)+"/"+i+'" >'+i+'</a>';
              }
            }
          }else{
            big+='<span>...</span>';
            for(var i=(numpage*1)-4;i<=(numpage*1)+4;i++){
              if(i==numpage){
                big+='<span class="active">'+i+'</span>';
              }else{
                big+='<a href="'+decodeURI(urix2)+"/"+i+'" >'+i+'</a>';
              }
            }
            big+='<span>...</span>';
          }
        }
      }

      // if(instance.page<5){
      //   big='<a href=\"'+decodeURI(url)+'\" >'+numpage+'</a>';
        
      // }
      model['pagingbig']=big;
      model['pagingsmall']=small;
      model['paging']=instance.html();
      // console.log("urix2");

    //   model['pagingdata']={"page":numpage,"limit":limit};
    //   self.repository.css="";
      model['pagingset']={"first":"","prev":"","next":"","last":"","visible":""};
      if(!instance.isFirst){model['pagingset'].first='<a href="'+urix.replace("{0}",1)+'" >|<</a>';}else{model['pagingset'].first='<span class="disabled">|<</span>';}
      if(instance.isPrev){model['pagingset'].prev='<a href="'+urix.replace("{0}",(numpage-1))+'" ><<</a>';}else{model['pagingset'].prev='<span class="disabled"><<</span>';}
      if(instance.isNext){model['pagingset'].next='<a href="'+urix.replace("{0}",((numpage*1)+1))+'" >>></a>';}else{model['pagingset'].next='<span class="disabled">>></span>';}
      if(!instance.isLast){model['pagingset'].last='<a href="'+urix.replace("{0}",instance.lastPage)+'" >>|</a>';}else{model['pagingset'].last='<span class="disabled">>|</span>';}
      model['pagingset'].visible=instance.visible;
      
      var x='<a href=\"'+decodeURI(url)+'\" class=\"selected\">'+numpage+'</a>';
      var y='<span class="active">'+numpage+'</span>';
      model.paging=model.paging.replace(x,y);
      return model;

}
FUNC.paging=async function(total,numpage,limit,url,model,urix){
      
    //console.log("vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv");
    // console.log(urix);
    // console.log(total);
    // console.log(numpage);
    // console.log(limit);
    // console.log(url);
    // console.log(model);
    //console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
      var instance=new Pagination(total, numpage, limit,urix);
              var jlimit=9,ldot="<span>...</span>";

      var big="",small='<div class="smallpaging" ><div class="sp_head headdown">'+numpage+'</div><div class="sp_ops Hscroll_v5">';
      if(instance.count<6){
        for(var i=1;i<=instance.count;i++){
          if(i==numpage){
            big+='<span class="active">'+i+'</span>';
          }else{
            big+='<a href="'+decodeURI(urix).replace("{0}",i)+'" >'+i+'</a>';
          }
        }
        small=big;
      }else{
        for(var i=1;i<=instance.count;i++){
          if(i==numpage){
            small+='<span selected>'+i+'</span>';
          }else{
//            small+='<span class="cmbspan2"><a href="'+decodeURI(urix).replace("{0}",i)+'" >'+i+'</a></span>';
            small+='<a href="'+decodeURI(urix).replace("{0}",i)+'" ><span class="cmbspan2">'+i+'</span></a>';
          }
        }
        small+="</div></div>";
        jlimit=9,ldot="<span>...</span>";
        if(instance.count<9){jlimit=instance.count;ldot="";}
   
        if(numpage<=5){
          for(var i=1;i<=jlimit;i++){
            if(i==numpage){
              big+='<span class="active">'+i+'</span>';
            }else{
              big+='<a href="'+decodeURI(urix).replace("{0}",i)+'" >'+i+'</a>';
            }
          }
          big+=ldot;

        }else{
          if(instance.count-5<=numpage){
            big+=ldot;
            for(var i=instance.count-jlimit;i<=instance.count;i++){
              if(i==numpage){
                big+='<span class="active">'+i+'</span>';
              }else{
                big+='<a href="'+decodeURI(urix).replace("{0}",i)+'" >'+i+'</a>';
              }
            }
          }else{
            big+=ldot;
            for(var i=(numpage*1)-4;i<=(numpage*1)+4;i++){
              if(i==numpage){
                big+='<span class="active">'+i+'</span>';
              }else{
                big+='<a href="'+decodeURI(urix).replace("{0}",i)+'" >'+i+'</a>';
              }
            }
            big+=ldot;
          }
        }
      }

      // if(instance.page<5){
      //   big='<a href=\"'+decodeURI(url)+'\" >'+numpage+'</a>';
        
      // }
      model['pagingbig']=big;
      model['pagingsmall']=small;
      model['paging']=instance.html();
   //    console.log("urix2");

    //   model['pagingdata']={"page":numpage,"limit":limit};
    //   self.repository.css="";
      model['pagingset']={"first":"","prev":"","next":"","last":"","visible":""};
      if(!instance.isFirst){model['pagingset'].first='<a href="'+urix.replace("{0}",1)+'" >|<</a>';}else{model['pagingset'].first='<span class="disabled">|<</span>';}
      if(instance.isPrev){model['pagingset'].prev='<a href="'+urix.replace("{0}",(numpage-1))+'" ><<</a>';}else{model['pagingset'].prev='<span class="disabled"><<</span>';}
      if(instance.isNext){model['pagingset'].next='<a href="'+urix.replace("{0}",((numpage*1)+1))+'" >>></a>';}else{model['pagingset'].next='<span class="disabled">>></span>';}
      if(!instance.isLast){model['pagingset'].last='<a href="'+urix.replace("{0}",instance.lastPage)+'" >>|</a>';}else{model['pagingset'].last='<span class="disabled">>|</span>';}
      model['pagingset'].visible=instance.visible;
      
      var x='<a href=\"'+decodeURI(url)+'\" class=\"selected\">'+numpage+'</a>';
      var y='<span class="active">'+numpage+'</span>';
      model.paging=model.paging.replace(x,y);
            //console.log("------------------------------------------------------------------------------");
      //console.log(small);

      return model;

}


DEF.helpers.xxx = function(x) {
  function sss(z,p){
    let q="<ul>";
      z.forEach((t, i) => {
        q=q+'<li><a href="'+t.url+'">'+t.name+'/'+p+'</a>';
        if(t.subs!=undefined && t.subs.length>0)
        {
          console.log(t.subs);
          p+='-'+t._id;
          q=q+sss(t.subs,p);
        }
        q=q+'</li>';

      });
      q=q+'</ul>';
    return q;
  }
  let q="<ul>";
  x.forEach((t, i) => {
    q=q+'<li><a href="'+t.url+'">'+t.name+'</a>';
    if(t.subs!=undefined)
     {
       q=q+sss(t.subs,t._id);
     }
    q=q+'</li>';

  });
  q=q+'</ul>';

  return q;

}






DEF.helpers.adminCategory = function(x) {
  function sss(z,p){
    let q="<ul>";
      z.forEach((t, i) => {
        q=q+'<li><a  topid="'+p+'" mid="'+t._id+'">'+t.name+'<span class="Hfloat-L"><b class="fas fa-plus additem"></b><b class="fas fa-pencil-alt upditem"></b><b class="far fa-trash-alt delitem"></b></span></a>';
        if(t.subs!=undefined && t.subs.length>0)
        {
          console.log(t.subs);
          p+='-'+t._id;
          q=q+sss(t.subs,p);
        }
        q=q+'</li>';

      });
      q=q+'</ul>';
    return q;
  }
  let q="<ul>";
  x.forEach((t, i) => {
    q=q+'<li><a mid="'+t._id+'">'+t.name+'<span class="Hfloat-L"><b class="fas fa-plus additem"></b><b class="fas fa-pencil-alt upditem"></b><b class="far fa-trash-alt delitem"></b></span></a>';
    if(t.subs!=undefined)
     {
       q=q+sss(t.subs,t._id);
     }
    q=q+'</li>';

  });
  q=q+'<div class="Hbtn Hbtn-fullfix Hbtn-skyblue Hpadd-5 fas fa-plus additemMAIN"></div></ul>';

  return q;

}


DEF.helpers.category_list = function(x) {
  function sss(z,p){
    let q="<ul>";
      z.forEach((t, i) => {
        
        if(t.subs!=undefined && t.subs.length>0)
        {
          q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a><b class="fas fa-angle-down trans0_25"></b></span>';
          p+='-'+t._id;
          q=q+sss(t.subs,p);
        }else{
            q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a></span>';
        }
        q=q+'</li>';

      });
      q=q+'</ul>';
    return q;
  }
  let q="<ul class='topCat'>";
  x.forEach((t, i) => {
    
    if(t.subs!=undefined)
     {
       q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a><b class="fas fa-angle-down trans0_25"></b></span>';
       q=q+sss(t.subs,t._id);
     }else{
         q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a></span>';
     }
    q=q+'</li>';

  });
  q=q+'</ul>';

  return q;

}



DEF.helpers.category_menu = function(x) {
  function sss(z,p){
    let q="<ul>";
      z.forEach((t, i) => {
        
        if(t.subs!=undefined && t.subs.length>0)
        {
          q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a><b class="fas fa-angle-down HcategoryOpenMenu trans0_25"></b></span>';
          p+='-'+t._id;
          q=q+sss(t.subs,p);
        }else{
            q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a></span>';
        }
        q=q+'</li>';

      });
      q=q+'</ul>';
    return q;
  }
  let q="<ul class='topCat'>";
  x.forEach((t, i) => {
    
    if(t.subs!=undefined)
     {
       q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a><b class="fas fa-angle-down HcategoryOpenMenu trans0_25"></b></span>';
       q=q+sss(t.subs,t._id);
     }else{
         q=q+'<li ><span class="trans0_25"><a>'+t.name+'</a></span>';
     }
    q=q+'</li>';

  });
  q=q+'</ul>';

  return q;

}


FUNC.catmenuUL=async function(x,y){
  let arrs=[];
  let arr=[];
  for(let i=0;i<x.length;i++) {
    for(let j=0;j<y.length;j++) {
      if((y[j]._topid+"")==(x[i]._id+"")){arrs.push(y[j]);}
    }
    if(arrs.length>0){
      let c=x[i];
      arr=await FUNC.catmenuUL(arrs,y);
      c['subs']=arrs;
      x[i]=c;
      arrs=[];
    }
  }
return x;
}


