@extends('backend.header')
@section('index-content')
<style>
.justify-content-end{
justify-content: end;
}
</style>
<div class="row px-3">


  <section role="main" class="content-body">
    <header class="page-header">
      <h2>List Of Gram Completion</h2>
    </header>
    <div class="row">
      <div class="col-xs-12">
        <div class="right-wrapper">
          <ol class="breadcrumbs">
            <li>
              <a href="{{url('index')}}">
                <i class="fa fa-home"></i>
              </a>
            </li>
            <li><a href="{{url('approved_districtwise_report')}}"><span class="active">{{$district->area_name??"District"}}</span></a></li>
            <li><span class="active">Gram</span></li>
          </ol>
        </div>
      </div>
    </div>
    <!-- start: page -->
    <section class="panel mt-3">
      <header class="panel-heading">
        <h4 class="my-0">List Of Gram Completion</h4>
        <div class="back-btn-div">
          <button class="btn btn-primary back-btn" onclick="window.history.back()">Back</button>
        </div>


      </header>
      
 
          <table border="1">
       <thead>
         <tr>
          <th><input type="checkbox" name="" id="selectAll"></th>
          <th>S.NO</th>
           <th>Mandal Name</th>
           <th>Gram Name</th> 
           <th>18&nbsp;W</th>
           <th>35&nbsp;W</th>
           <th>70&nbsp;W</th>
           <th>110&nbsp;W</th>
           <th>190&nbsp;W</th>
           <th>Total</th>

         </tr>
       </thead>
       <tbody>
        
        <?php $tw1=0;$tw2=0;$tw3=0;$tw4=0;$tw5=0; $sum=0; $i=0;$phaseids=[];?>
        @foreach($gram_lists as $report)
    @if(isset($report->phases))
    <?php $phasecount = count($report->phases);  $z=1?>
        @foreach($report->phases as $phase)
        <?php $list=[]; $i++;  $arms=["18 W","35 W","70 W","110 W","190 W"];
        $query="";
        if(Request::get('selected_date')){
          $query = " and DATE(updated_at) >='".date('Y-m-d',strtotime(Request::get('selected_date')))."' and DATE(updated_at) <='".date('Y-m-d',strtotime(Request::get('to_date')))."'";
        }?>
                        @foreach($arms as $value) 
                        <?php $counts= DB::select("SELECT LED_wattage,    
                            ROUND (
                (
                  LENGTH(LED_wattage)
                  - LENGTH( REPLACE ( LED_wattage, '".$value."', '') ) 
                ) / LENGTH('".$value."')        
                            ) AS count    
                            FROM survey_infos where approve_status=1 and phase_no=".$phase->phase_no." ".$query." and gram_id=".$report->id);                        
                         $count=array_sum(array_column($counts,'count'));
                         $list[]=$count;
                        ?>  
                        @endforeach
        <tr>
      <td>
        <input type="checkbox" name="checked_phases[]" value="{{$phase->id}}" class="gram" >
      </td>
      <td>{{$i}}</td>
      <td>{{ $mandals[$report->parent_id] }}</td>
      <td>{{$report->area_name}} (Phase-{{$phase->phase_no}})</td>         
      <td><?php $w1=isset($list[0])?$list[0]:0?>{{$w1}}</td>
      <td><?php $w2=isset($list[1])?$list[1]:0?>{{$w2}}</td>
      <td><?php $w3=isset($list[2])?$list[2]:0?>{{$w3}}</td>
      <td><?php $w4=isset($list[3])?$list[3]:0?>{{$w4}}</td>
      <td><?php $w5=isset($list[4])?$list[4]:0?>{{$w5}}</td>
      <td><?php $counts=$w1+$w2+$w3+$w4+$w5;?>{{$counts}}</td>
                         
       </tr>
       <?php 
       $tw1=$w1+$tw1;
       $tw2=$w2+$tw2;
       $tw3=$w3+$tw3;
       $tw4=$w4+$tw4;
       $tw5=$w5+$tw5;

         $sum=$sum+$counts;  $z++;?>
        
            @endforeach     
             @endif               
       @endforeach
       <tr>
         <td></td> 
         <td></td>
         <td></td>
         <td><b>Total</b></td>
          <td><b>{{$tw1}}</b></td>
          <td><b>{{$tw2}}</b></td>
          <td><b>{{$tw3}}</b></td>
          <td><b>{{$tw4}}</b></td>
          <td><b>{{$tw5}}</b></td>
          <td><b>{{$sum}}</b></td>
          
          
       </tr>
      </tbody>


    </table>
     
<div class="d-flex justify-content-end">
   <button class="btn btn-primary " id="submit">Submit</button>
</div>   
   <a id="download" class="hide" href="{{url('districtwise_completion')}}"></a>

 </section>
</section>
<script type="text/javascript">
$(document).ready(function(){
$(".btn_upload").on("click",function(){
   var id=$(this).data('id');
    $('#mandal_id').val(id);

 });
  $('.mandal_wise_report').change(function(){
    var id=$(this).data('mandal_id');
    var phase=$(this).data('phase');
    var status=0;
    if($(this).is(":checked")){
       status=1;
    }
        $.ajax({
        url: "{{url('mandal_wise_report_gramse')}}",
        type: "get",
        data: {id : id,phase:phase,status:status},
        dataType: "html",
        success: function(html){
          
        }
      });
  });

   $('#testing').on('change', function () {
        var url = "{{ url('/approved_mandalwise_report')}}?with=makername&id={{Request::get('id')}}&selected_date={{Request::get('selected_date')}}&to_date={{Request::get('to_date')";

        if (url) {
            window.location = url;
        }

        return false;
    });

    $('#selectAll').click(function (e) {
    $(this).closest('table').find('td input:checkbox').prop('checked', this.checked);
        check();
    });

    $('#singcheck').click(function (e) {
        //$(this).closest('table').find('td input:checkbox').prop('checked', this.checked);
      check();
    });

    function check(){
    if($("#singcheck:checked").length  > 0) {
      $('#selectalls').removeAttr('disabled');
    }else{
      $('#selectalls').attr('disabled','disabled');
    } 
    }

    $('#submit').click(function(){
					  
	  var id = $("#selected-jillaid").val();

      var checked_mandals = [];
      $('.mandal:checked').each(function(i){
        checked_mandals[i] = $(this).val();
      });

      //$("#gram-close").click(); 
      let searchParams = new URLSearchParams(window.location.search);


	  var id = searchParams.get('id');

	  var checked_phases = [];
      $('.gram:checked').each(function(i){
        checked_phases[i] = $(this).val();
      });

	  var url = $('#download').attr('href')+"?id="+id;

	  $.each( checked_phases, function( i, l ){
          url += "&phase%5B%5D="+l;
	  });

	  window.open(url, '_blank');

    });


});
</script>

@stop