
@{section header}

<title>لیست کاربران</title>
<style>
    #frmPass #oldpass , #frmPass label[for="oldpass"]{display:none;}
</style>
@{end}




<div class="Hrow Hpadd-2per">
    @{if repository.user.role=="administrator"}
    <a class="Hfloat-L Hbtn Hbtn-lightGreen HmarB-10 trans0_25 HpaddT-7 HpaddB-7"   href="/user/insert" >ثبت کاربر</a>
    @{else}
    <div class="Hrow HpaddT-40"></div>
    @{fi}
    <form id="frm" class="Hrow HmarB-3per" name="frm" action="" method="post" enctype="multipart/form-data" >
      
      <div class="Hcol-xlg-5 Hcol-lg-5 Hcol-xmd-5 Hcol-md-5 Hcol-xsm-5 Hcol-sm-5 Hcol-xs-12 Hcol-xss-12 ">
        <select name="query" id="query" class="Htxt-V1">
          <option value="fam">نام خانوادگی</option>
          <option value="phone">شماره(تلفن - همراه - فکس ...)</option>
          <option value="idcode">کد ملی</option>
          <option value="username">نام کاربری</option>
        </select>
        
      </div> 
      <div class="Hcol-xlg-1 Hcol-lg-1 Hcol-xmd-1 Hcol-md-1 Hcol-xsm-1 Hcol-sm-1"></div>
      <div class="Hcol-xlg-6 Hcol-lg-6 Hcol-xmd-6 Hcol-md-6 Hcol-xsm-6 Hcol-sm-6 Hcol-xs-12 Hcol-xss-12 ">
        <input type="text" class="Htxt-V1" id="userSearch" name="text" placeholder=""/>
      </div>
      <div class="Hrow"></div>  
      <div class="Hcol-xlg-5 Hcol-lg-5 Hcol-xmd-5 Hcol-md-5 Hcol-xsm-5 Hcol-sm-5 Hcol-xs-12 Hcol-xss-12 SubmitionBox HpaddT-10">
        <input class="trans0_25 Hbtn-fullfix Hbtn-yellow2 Hpadd-7"  type="button" id="filterUsers" data-action="/user/list/filter"  value="جستجو کاربر"/>
        <a href="" id="filter-remove" class="HmarT-10 Hbtn-mini Hbg-digired" data-color="#fff"  style="padding:5px 15px;display:none;">حذف فیلتر</a>
      </div>
    </form>
   
   @{if repository.user.role=="administrator"}
    <div class="Hrow Hpadd-10 Hbg-white">
            <label class="checkBox Hfloat-R Hcheck-group" data-function="" group-class="chlist" style="position:relative;top:10px;"><input type="checkbox" value="" style="display:none;"/><span class="checkmark" data-txt="✔" style=""></span></label>
            <button id="multidel" data-color="#fff" class="Hbtn Hbg-digired Hfloat-L"> حذف کاربران انتخاب شده </button>
    </div>
    @{fi}
    <div id="list" class="listShow-N2">
      @{foreach m in model.data}
      @{if (repository.user._id+"")!= (m._id+"")}
      <div class="Hrow Hpadd-10" id="i@{!m._id}" >
        @{if repository.user.role=="administrator"}<label class="checkBox Hfloat-R chlist" value="@{!m._id}"><input type="checkbox" value="" style="display:none;"/><span class="checkmark" style=""></span></label>@{fi}
        <b style="float:right;padding-left:10px;" class="username">@{!m.username}</b> 
        <span class="nameFam">@{!m.name} @{!m.fam}</span>  
        <span class="phone" style="display:none">@{foreach x in m.phone}<span class="Hmar-5 Hpadd-5 HpaddR-10 HpaddL-10 Hbg-white Hround-40" style="display:inline-block">@{!x.k} : @{!x.v}</span>@{end}</span>
        <span class="email" style="display:none">@{!m.email}</span>
        <span class="idcode" style="display:none">@{!m.idcode}</span>
        <span class="Hfloat-L Htxt-left">
          <b class="far fa-eye  user-info Hbtn-mini Hbg-skyblue"  data-color="#fff" ></b> 
          @{if repository.user.role=="administrator"}<b class="fas fa-user-lock Hbtn-mini Hbg-lightorange" data-color="#fff"  ></b> @{fi}
          <a href="/user/update/@{!m._id}" class="fas fa-pencil-alt Hbtn-mini Hbg-yellow"></a>
          @{if repository.user.role=="administrator"}<b data-id="@{!m._id}" class="fas fa-trash-alt remove-user Hbtn-mini Hbg-digired" data-color="#fff"></b>@{fi}
        </span>
      </div>
      @{fi}
      @{end}
   </div>
   
   <div class="Hrow" id="paging-user">
   @{view('~partial/pagination',model)}
   </div>
	
</div>



@{if repository.user.role=="administrator"}
<form id="multidelform" style="display:none;" class="Hrow HmarB-3per" name="multidelform" action="" method="post" enctype="multipart/form-data" ><input type="hidden" name="users" id="users" value=""/></form>
@{fi}
<div class="Hmodal" id="mod1"> 
  <div class="Hmodal-lg Hcards " style="border-radius:5px;">
    <div class="Hcards_header">
      <div class="close" style="background:#ff3333;border-radius:4px;"></div>
    </div>
    <div class="Hcards-content Hpadd-2per">
      <h2 id="name"></h2>
      <p class="HmarB-5">نام کاربری : <span id="username"></span></p>
      <p class="HmarB-5">ایمیل : <span id="email"></span></p>
      <p class="HmarB-5"> کد ملی : <span id="idcode"></span></p>
      <p class="HmarB-5"> شماره تماس : <span id="phone"></span></p>
      
    </div>
    <div class="Hcards-footer">
      <a id="goview" class="Hbtn Hbtn-yellow2 Hfloat-L" href="">ویرایش این کاربر</a>
    </div>
  </div>
</div>

<button id="md1" data-selector="#mod1" data-target="Hmodal" style="display: none;"></button>

<button id="md2" data-selector="#mod2" data-target="Hmodal" style="display: none;"></button>

<div class="Hmodal" id="mod2"> 
  <div class="Hmodal-sm Hcards " style="border-radius:5px;">
    <div class="Hcards-header">
      <div class="close" style="background:#ff3333;border-radius:4px;"></div>
    </div>
    <div class="Hcards-content">
      @{view('~../modules/https_modules/users/views/partial/updPassword')}
    </div>
    
  </div>
</div>
<p>&nbsp;</p>
@{section footer2}


<script src="../../../../../js/Hvalidation.js"></script>
<script src="../../../../../js/heramvalidate.js"></script>

    <script type="text/javascript">


//////////////////////

        

        
        
        
        
        
      var whatsrole=`@{repository.user.role}`;
        function show_filteruser(result){ // for filter users
            switch(result.status){
                case 400:
                    Hnotification({status:"info",Ttext:"",Ctext:result.data});
                    break;
                case 200:
                    $("#list").html("");
                    var x="";
                    $("#paging-user").html("");
                    result.data.forEach(m=>{
                        var numbers="";
                        m.phone.forEach(x=>{numbers+='<span class="Hmar-5 Hpadd-5 HpaddR-10 HpaddL-10 Hbg-white Hround-40"> '+x.k+' : '+x.v+'</span>'});
                        x=($("#multidelform").length)?'<label class="checkBox Hfloat-R chlist" value="'+m._id+'"><input type="checkbox" value="" style="display:none;"/><span class="checkmark" style=""></span></label>':"";
                        if(whatsrole!="administrator"){
                          $("#list").html($("#list").html()+'<div class="Hrow Hpadd-10" id="i'+m._id+'" >'+x+'<b class="username" style="float:right;padding-left:10px;">'+m.username+'</b><span class="nameFam">'+m.name+' '+m.fam+'</span><span class="phone" style="display:none;">'+numbers+'</span><span class="email" style="display:none;">'+m.email+'</span><span class="idcode" style="display:none;">'+m.idcode+'</span><span style="float:left;"><b class="far fa-eye user-info Hbtn-mini Hbg-skyblue"  data-selector="#mod1" data-target="Hmodal" style="color:#fff"></b><a href="/user/update/'+m._id+'" class="fas fa-pencil-alt Hbtn-mini Hbg-yellow"></a></span></div>');
                        }else{
                          $("#list").html($("#list").html()+'<div class="Hrow Hpadd-10" id="i'+m._id+'" >'+x+'<b class="username" style="float:right;padding-left:10px;">'+m.username+'</b><span class="nameFam">'+m.name+' '+m.fam+'</span><span class="phone" style="display:none;">'+numbers+'</span><span class="email" style="display:none;">'+m.email+'</span><span class="idcode" style="display:none;">'+m.idcode+'</span><span style="float:left;"><b class="far fa-eye user-info Hbtn-mini Hbg-skyblue"  data-selector="#mod1" data-target="Hmodal" style="color:#fff"></b> <b class="fas fa-user-lock  Hbtn-mini Hbg-lightorange"  data-selector="#mod2" data-target="Hmodal" style="color:#fff"></b><a href="/user/update/'+m._id+'" class="fas fa-pencil-alt Hbtn-mini Hbg-yellow"></a><b data-id="'+m._id+'" class="fas fa-trash-alt remove-user Hbtn-mini Hbg-digired" style="color:#fff"></b></span></div>');
                        }  
                    });
                    $("#filter-remove").css("display","inline-block");
                    break;
                default:
                    Hnotification({status:"warning",Ttext:"خطای ارسال داده - ",Ctext:result.data});
            }
        }


////////////////////////////////////
    function checkPaging(){
      var users=$("#list").children().length;
      var url=window.location.href;
      url=decodeURI(url);
      if(users>1){
        window.location.href=url;
      }else{
        var page=url.split('/');
        var numpage=page[page.length-1]*1;
        if(page[page.length-1]==""){numpage=page[page.length-2]*1;}   
        if(numpage==1){window.location.href=url;
        }else{
          if(page[page.length-1]==""){
            page[page.length-2]=numpage-1;
          }else{
            page[page.length-1]=numpage-1;
          }
          url=page.join("/");window.location.href=url;
          
        }
      }
    }
    
//////////////////////
    async function show_res(result){        /// for remove user
        switch(result.status){
            case 103:
                Hnotification({status:"danger2",Ttext:"",Ctext:result.data});    
            break;
            case 206:
                Hnotification({status:"success",Ttext:"",Ctext:result.data});    
                checkPaging();
            break;
            case 309: 
                let x=await Hconfirm({status:"warning",icon:"warning",Ttext:"اخطار بسیار مهم از سوی سامانه",Ctext:result.data});    
                if(x){
                    var f=result.id+"";
                    mydelete(f,"/user/delete_force/");
               }
            break;
            default:
                Hnotification({status:"warning",Ttext:"",Ctext:result.data});    
        }
    }
    
    async function show_res4(result){        /// for remove users
        switch(result.status){

            case 200:
                Hnotification({status:"success",Ttext:"",Ctext:result.data});    
                location.reload(); 
            break;
            default:
                Hnotification({status:"warning",Ttext:"",Ctext:result.data});    
        }
    }
    
    
        function show_res3(result){        /// for upd pass
        $("#mod2 .close").trigger("click");
        switch(result.status){
            case 205: 
                $("#newpass").val("");
                $("#newpass2").val("");
                Hnotification({status:"success",Ttext:"",Ctext:result.data});    
            break;
            default:
                Hnotification({status:"danger2",Ttext:"",Ctext:result.data});    
        }
        
    }


//////////////////////

    async function mydelete(id,url){
        var query="id="+id;
        var resdel=await jqajax(url,query,"get","loader");
        show_res(resdel); 
    }
$(document).ready(function(){
    ///////////////////////
    $("#filterUsers").click(async function(){

       var formdata=new FormData(frm);
        var sel=$("#query").val();
        var item=$("#userSearch");
        var value=item.val();
        if(ISstring(value,1)){

      let ok=0;
          switch (sel){
            case "idcode":
            if(await ISidcode(value)){
              ok=1;
            }else{item.css("border-color","red");}
            break;
            case "phone":
            if(value!=""){
              ok=1;
            }else{item.css("border-color","red");}
            break;
            case "username":
            case "fam":
            if(await ISstring(value,3)){
              ok=1;
            }else{item.css("border-color","red");}
                
            break;
          }
        
          if(ok){
            item.css("border-color","#ccc");
            var result=await jqajax("/user/list/filter",formdata,"post","loader");
       

            show_filteruser(result);
          }
        }else{
          item.css("border-color","red");
             Hnotification({status:"warning",Ttext:"خطای ارسال داده - ",Ctext:"لطفا کادر را با مقادیر مناسب پر کنید"});

        }
    });

///////////////////////////////
    $("#updpass").click(async function(){
        var newpass=$("#newpass").val();
        var newpass2=$("#newpass2").val();
        if(ISstring(newpass,6) && ISstring(newpass2,6)){
            if(newpass==newpass2){
                var formdata=new FormData(frmPass);
                var result=await jqajax("/user/update/password",formdata,"post","loader");  
                show_res3(result);
            }else{
                Hnotification({status:"info",Cstyle:[{color:"#ffffff"}],Ctext:"پسورد و تکرار آن یکسان نیستند"});
            }
        }else{
            Hnotification({status:"info",Cstyle:[{color:"#ffffff"}],Ctext:"پسورد بصورت صحیح وارد نشده است"});
        }
    });
////////////////////////////////
    $(document).on("click", ".fa-user-lock" ,async function(){
    
      $("#userid").val($(this).parent().parent().attr("id").replace("i",""));
      $("#md2").trigger('click');
    });
////////////////////////////////
    $(document).on("click", "b.user-info" ,async function(){
        $("#name").html($(this).parent().siblings(".nameFam").html());
        $("#username").html($(this).parent().siblings(".username").html());
        $("#email").html($(this).parent().siblings(".email").html());
        $("#idcode").html($(this).parent().siblings(".idcode").html());
        $("#phone").html($(this).parent().siblings(".phone").html());
        $("#goview").attr("href",$(this).next().next().attr("href"));
        $("#md1").trigger('click');
    });
////////////////////////////////
    $(document).on("click", "b.remove-user" ,async function(){
        let x=await Hconfirm({Ctext:"آیا اطمینان داردید که میخواهید این کاربر را حذف کنید ؟ \n درصورت تایید ، امکان بازیابی وجود نخواهد داشت"});
        if(x){
            mydelete($(this).attr("data-id"),"/user/delete/");
        }
    });
    
    
    $(document).on("click", "#multidel" ,async function(){
        let x=await Hconfirm({Ctext:"آیا اطمینان داردید که میخواهید این کاربران را حذف کنید ؟ \n درصورت تایید ، امکان بازیابی وجود نخواهد داشت"});
        
        if(x){
            x=await Hconfirm({Ctext:"هشدار حذف! \n در صورت هرگونه مشکل امکان بازیابی دیتا وجود ندارد"});
            if(x){
                var ilist=[];
                $(".chlist input[type=checkbox]:checked").each(function(){
                    ilist.push($(this).parent().attr("value"));
                });
                $("#users").val(ilist);
                var formdata=new FormData(multidelform);
                var result=await jqajax("/user/multidelete/",formdata,"post","loader");  
                show_res4(result);
            }
                
        }
    });


 

});





</script>


@{end}
