@extends('backend.header')
@section('index-content')
<?php
function convertToUnicodeNumber($input) {

    $standard_numsets = array("0","1","2","3","4","5","6","7","8","9");
    $devanagari_numsets = array("०","१","२","३","४","५","६","७","८","९");
    
    return str_replace($standard_numsets, $devanagari_numsets, $input);
}
?>


<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" ></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css"  />
    <link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
    <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script> -->

<section role="main" class="content-body">
    <header class="page-header">
      <h2>Cluster Allot User Form</h2>
    </header>   
    <!-- allot user code here -->
    <div class="row" id="userallotform">
        <div class="col-md-12">
            <div class="py-2" style="font-size: 20px;">
                <label>Username :</label><span id="usernameid"> &nbsp;&nbsp;&nbsp;{{$user->name}}</span>
            </div>
            <form action="{{url('gramallotuser')}}" method="post" class="form-horizontal">
            @csrf
                <section class="panel">
                    <header class="panel-heading mb-3">
                        <h4 class="my-0">Cluster Alloted User Details</h4>
                        <div class="back-btn-div">
                            <button class="btn btn-primary back-btn" onclick="window.history.back()">Back</button>
                        </div>
                    </header>
                    <div class="row px-5">
                        <input type="hidden" class="userid" name="userid" value="{{$user->id}}">

                        <div class="col-sm-3 px-2">
                            <h4>clusternames</h4>
                            <select class="w-100 h40" id="cluster_name">
                             <option value="">clusternames</option>   
                              @foreach($clusternames as $cn)
                                <option value="{{$cn->id}}">{{$cn->cluster_name}}</option>
                                @endforeach
                            </select>
                        </div>
                           
                        <div class="col-sm-3 px-2">
                            <h4>Add</h4>
                            <button type="button" class="btn btn-primary mt-1" id="clusteraddallot"><i class="fa fa-plus" aria-hidden="true"></i></button>
                        </div>
                    </div>
                    <div class="row px-5">
                        <div class="col-12 px-2">
                            <table border="1" id="rowallot">
                                <tr>
                                    <th>S.No</th>
                                    <th>Cluster Name</th>
                                    <th>status</th>
                                    <th>Delete</th>
                                </tr>
                                <?php
                                $i=0;
                              
                                $count=0;
                                ?>
                                 @foreach($allotuser as $list)
                                <?php $count++; ?>
                                <tr>


                                       <td>{{$count}}</td>
                                       <td>{{$list->cluster->cluster_name??''}}</td>
                                      
                                       <?php 
                                            //dd($jkl);
                                            $s=$list->status;
                                            if($s=='0')
                                            {
                                            ?>
                                            <td><a class="btn btn-warning statuschk" id="statuschk" data-id="{{$list->id}}" data-mandal_id="{{$list->taluka}}">inactive</a></td>
                                            <?php
                                            }else{
                                            ?>
                                            <td><a  class="btn btn-success statuschk" id="statuschk" data-id="{{$list->id}}" data-mandal_id="{{$list->taluka}}">active</a></td>
                                            <?php
                                            }
                                            ?>
                       
                                       <td>

 
                                        <button type='button' onclick="return confirm('Are you sure you want to delete');" class='btn btn-danger gramallotdelete' value="{{$list->id}}">x</button>
                                   </tr>
                                
                                 @endforeach 
                                <?php
                                $i++;
                               
                                ?>
                               
                            </table>
                        </div>
                    </div>
                    
                </section>
            </form>
        </div>
    </div>

  
</section>


<script type="text/javascript">
 $(document).ready(function(){

     $("#clusteraddallot").on("click",function(){
            var cluster_name=$("#cluster_name").val();
            var userid=$(".userid").val();
           
            if(cluster_name=="")
            {
                alert('please select value');
            }
            else{
                var data={
                    cluster_name:cluster_name,
                    userid:userid
                    
                };
          
                $.ajax({
                    type:'get',
                    url:'{{url("/getclusternames")}}',
                    data:data,
                    success:function(data)
                    {

                        if(data=="Failed"){

                        alert("This user has already exist");
                       }else{
                        window.location.href= window.location.href;
                       }
                    },
                    error:function(data)
                    {
                        alert(data);
                    }
                });
            
           }

          

          });
     $("body").on("click",".statuschk",function(){
                         var id=$(this).data('id');
                         var mandal_id=0;
                         var path=$(this);
                          //alert(id); 
                        $.ajax({
                            url:'{{url("allotChangeStatus")}}/'+id+'?mandal_id='+mandal_id,
                            method:'get',
                             dataType:"json",
                            success:function(data)
                            {
                                if(data.status=="failed"){
                                    alert('This user already exist in another mandal');
                                }else{
                                   path.text(data.status);
                                   path.attr('class',data.class_name);
                               }
                                //alert(data.status);
                            },  
                            error:function(data)
                            {
                                 alert(data);
                            }
                        });
                    });
                    
                
  });

				</script>
			@stop

    