@extends('layouts.app')

@section('title')
All Balloting Map Files 
@stop

@section('breadcrumb')
<ol class="breadcrumb pull-right">
        <li><a href="{{route('home')}}"><i class="fa fa-dashboard"></i> Home</a></li>

        <li class="active">All Balloting Map Files</li>
      </ol>
@stop
@section('css')
<style type="text/css">
    .bg-color{
        background-color: #1c3752;
    }
    .anchor-link
    {
        display: none;
    }
    body {
        font-weight: bold;
    }
</style>
<style type="text/css" media="print">
    
    @media print {
        .page-wrapper{
        font-size: 12px !important;
        font-family: "time-new-roman",Arial,sans-serif !important;
        color: black !important;
        }
        h3{
            line-height: 1px;
        }

          h3 strong{
        text-transform: uppercase;
        color: black !important
    }
    h4 strong{
        text-transform: uppercase;
        color: black !important
    }
    body {
        font-weight: bold;
    }
    .printFH11 {
    font-size: 13px;
    font-weight: 800;
    background-color: #000000;
    }
    .white-box .box-title {
    margin: 0px 0px 4px 0px;
    font-weight: 700;
    font-size: 14px;
    color: #1c3752;
    background-color: #000000 !important;
    }
    .white-box {
    padding: 0px 0px 2px 0px;
    margin-bottom: 0px;
    margin-bottom: 0;
    border-radius: inherit;
    }
    .table {
    width: 100% !important;
    margin-bottom: 5px;
    margin-top: -2px;
    }
    .table>tbody>tr>td, .table>tbody>tr>th, .table>tfoot>tr>td, .table>tfoot>tr>th {
    vertical-align: top;
    }
    .page-wrapper {
    padding-top: 0px;
    }
    }
</style>

@stop
@section('content')


            <div class="container-fluid">
                <!-- /row -->
                <div class="row">

                 
                        <div class="col-md-12">
                            @if ($errors->any())
                                <div class="white-box">
                                    <h2>Errors</h2>
                                    <ul>
                                        {!! implode('', $errors->all('<li><span class="help-block"><strong>:message</strong></span></li>')) !!}
                                    </ul>
                                </div>
                            @endif
                            @php
                       $total_val=     count($allStatements);
                            @endphp
                            <div class="white-box">
                                
                                      
                                        <div class="row">
                                            <div class="col-sm-3 col-md-6"  >
                                                <h3 class="box-title m-b-0"><i class="icon-layers fa-fw"></i> Pre-Condtion Result</h3>
                                                <p class="text-muted  font-18">All Eligible Inventories</p>
                                                <p class="text-muted  font-13"> Total Searched Inventories are : <span class="label label-success" id='total'>{{$total_val}}</span></p>
                                            </div>
                                            <div class="col-sm-9 col-md-6"  >
                                           
                                                <p style="font-size: 18px; font-weight: 700; color: black;"> Inventories Size Found</p>
                                                
                                                  
                                                <div class="row">
                                                    @foreach($unit_size as $us)
                                                    @php
                                                  $specificUnitSizeId=  $us->id;
                                                   $unitSizeCount =  $unitSizeIds->filter(function ($value) use ($specificUnitSizeId) {
                                                          return $value == $specificUnitSizeId;
                                                                })->count();

                                                    @endphp
                                                    <div class="col-sm-3">
                                                         
                                                        <p>{{$us->name}} Marla: <strong style="color: #f80c0c">{{$unitSizeCount}}</strong></p>
                                                        
                                                      </div>
                                                    @endforeach
                                                   
                                                   

                                                     
                                            </div>
                                          </div>
                                    </div>
                                     
                               
                                <hr>
        
                            </div>
                        </div>
                    
                </div>
                <div class="row" id="update-table">

                    <div class="col-lg-12">
                        <div class="white-box1">
                            <h3 class="box-title m-b-0"><i class="icon-layers fa-fw"></i> Founded Inventories</h3>
                            <button onclick="addNewRow()" style=' float: right; margin-top: -33px;'  class="btn  btn-primary" type='button'>
                                <i class="fa fa-plus"></i>&nbsp; Add
                            </button>
                            <div class="table-responsive"   style=" clear: both; overflow-y:auto;height:381px; margin-top: 10px;">
                                <form id='myForm' method="POST" action="{{ route('balloting.store.pre.condition') }}">
                                    @csrf
                                    <input  type='hidden' id='multipro_id' name='multipro_id' value="{{$multipro_id}}">
                                <table  class="table table-bordered" id='state_table'>
                                    <thead class="bg-color">
                                        <tr style="text-align: center">
                                            <th style="color: #fff;font-size: 13px;">#</th>
                                            <th style="color: #fff;font-size: 13px;">Unit #</th>
                                            <th style="color: #fff;font-size: 13px;">Size</th>
                                            <th style="color: #fff;font-size: 13px;">Customer</th>
                                            <th class="no-print" style="color: #fff;font-size: 13px;">Net Amount</th>
                                            <th style="color: #fff;font-size: 13px;">Booking Date</th>
                                            <!-- <th class="no-print">Detail</th> -->
                                          
                                            <th style="color: #fff;font-size: 13px;">Action</th>
                                            <!-- <th>Ref#</th> -->
                                           
                                            <!-- <th>Net Amt</th> -->
                                        </tr>
                                    </thead>
                                    <tbody> 
                                      
                                        <?php $count =1;?>  
                                       @foreach($allStatements as $statement)
                                        <tr id='account_tr_{{$count}}' data-id="{{$count}}">
                                             <td>{{$count++}}</td>
                                           <input type='hidden' name='inventory_id[]' value='{{$statement->inventory->id}}'>
                                           <input type='hidden' name='unit_size_id[]'  value='{{$statement->inventory->unit_size_id}}' >
                                           <td>{{$statement->inventory ? $statement->inventory->unit_number : '' }}</td>
                                              <td>@if($statement->inventory)
                                                @if($statement->inventory->size)
                                                
                                                {{$statement->inventory->size->name}}
                                                
            
                                                {{ \App\ProjectType::society_size}}
                                               
                                                @endif
                                                @endif
                                            </td>
                                            <td>{{$statement->customer ? $statement->customer->name.'---'.$statement->customer->cnic: ''}}<br><b></td>

                                            <td><b>{{number_format($statement->net_pay)}}</b></td>


                                            
                                           

                                            

                                            <td>{{date('d-m-Y', strtotime($statement->booking_date)) }}</td>



                                            {{-- @if($statement->status == \App\CustomerProperty::pending)
                                            <td><span class="label label-primary" style="background-color:#cd703d;">Pending</span></td>

                                            @elseif($statement->status == \App\CustomerProperty::approved)
                                            <td><span class="label label-success">Approved</span></td>
                                            @elseif($statement->status == \App\CustomerProperty::cancel)
                                            <td><span class="label label-danger">Canceled</span></td>
                                            @elseif($statement->status == \App\CustomerProperty::block)
                                            <td><span class="label label-danger"  style="background-color:#f80c0c;">Block</span></td>
                                            @else
                                            <td></td>
                                            @endif --}}


                                            <td class="pt-3-half name">

                                                <button type="button" class="btn btn-sm btn-danger btn-sm" onclick="removeParentDiv(this)"><i class="fa fa-times"></i></button>
                                            </td>
                                            

                                        </tr>

                                        @endforeach
                                        <tr id="custom_account_tr_0" class="tr_">

                                        </tr>

                                      
                                    </tbody>
                                </table>
                            </form>
                              
                            </div>
                            <button onclick="confirmSubmit()"  class="btn  btn-primary" type='button'>
                                <i class="fa fa-lock"></i>&nbsp; Lock Inventories
                            </button> 
                          
                        </div>
                        <br>
                    
                    </div>
                </div>
                <!-- /.row -->
                    
              </div>
            <!-- /.container-fluid -->

@stop

@section('js')
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<script>
    $(function() {
        $('#myTable').DataTable();

        var table = $('#example').DataTable({
            "columnDefs": [{
                "visible": false,
                "targets": 2
            }],
            "order": [
                [2, 'asc']
            ],
            "displayLength": 25,
            "drawCallback": function(settings) {
                var api = this.api();
                var rows = api.rows({
                    page: 'current'
                }).nodes();
                var last = null;
                api.column(2, {
                    page: 'current'
                }).data().each(function(group, i) {
                    if (last !== group) {
                        $(rows).eq(i).before('<tr class="group"><td colspan="5">' + group + '</td></tr>');
                        last = group;
                    }
                });
            }
        });
        // Order by the grouping
        $('#example tbody').on('click', 'tr.group', function() {
            var currentOrder = table.order()[0];
            if (currentOrder[0] === 2 && currentOrder[1] === 'asc') {
                table.order([2, 'desc']).draw();
            } else {
                table.order([2, 'asc']).draw();
            }
        });
    });
    $('#example23').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]
    });
    function removeParentDiv(self) {
            var id = $(self).closest('tr').data("id");

            // console.log(id);
            $('#account_tr_' + id).remove();
           count= $('#total').text();
           $('#total').text(parseInt(count) - 1);

           
        }

        var count ={{$total_val}};
        function addNewRow() {
           
            count = parseInt(count) + 1;
            var account_row= `<tr height="2px" id="custom_account_tr_`+count+`"
                                            data-id="`+count+`" >
                                            <input type='hidden' name='inventory_id[]' id='inventory_id`+count+`' >
                                            <input type='hidden' name='unit_size_id[]' id='unit_size_id`+count+`' >
                                           
                                            <td id='count`+count+`'>

                                              
                                                
                                            </td>
                                            <td  >
                                                <select class='js-example-basic-single' name="customer_property" id='customer_property`+count+`'
                                                class="form-control "
                                                 onchange='getInformation(this.value,{data_id:`+count+`})'>
                                                 <option value='' select=''> None</option>
                                                
                                                     
                                               
                                            </select> 
                                            </td>
                                            <td id='plot_size`+count+`'></td>
                                            <td id='customer_name`+count+`'></td>



                                               <td id='net_pay`+count+`'></td>


                                              




                               <td id='booking_data`+count+`'></td>

                                          

                                            <td class="pt-3-half name">

                                                <button type="button" class="btn btn-sm btn-danger btn-sm"
                                                    onclick="removeCustomParentDiv(this)"><i
                                                        class="fa fa-times"></i></button>
                                            </td>

                                        </tr>
                                             `;
                                             
// var tr_id =count -1;
var lastrow=$('#state_table').find('tr').last();
$(lastrow).after(account_row);
$('#count'+count).text(count);
        //    $('#custom_account_tr_'+0).after(account_row);
         
        selectCall();
        }
        function removeCustomParentDiv(self){
            var id = $(self).closest('tr').data("id");

 
$('#custom_account_tr_' + id).remove();
count= $('#total').text();
selectCall();
        }
function getInformation(value,data ){
//     console.log(value); // logs the selected value of the select element
//   console.log(data.data_id);
  $.ajax({
    url: '{{route("balloting.get.inventory-info")}}',
    data: { id: value },
    dataType: 'json',
    success: function (response) {
        // Get the input array using jQuery
var inputArray = $('input[name="inventory_id[]"]');
var match_count = 0;
// Loop through the array to compare each value with item.id
inputArray.each(function() {
  var inputValue = $(this).val(); // Get the value of the current input
   // The ID to match against
 
  // Check if the input value matches the item ID
  if (inputValue == response.id) {
    // Trigger a SweetAlert alert
    Swal.fire("Match found!", "This Unit number  all ready selected.", "error");
   match_count = match_count+1;
  }
  
});

if(match_count > 0){
    $('#customer_property'+data.data_id).val(null).trigger('change');
    return false;

}
        // val_t= $('#total').text();
        //    $('#total').text(parseInt(val_t) + 1);
        
        $('#inventory_id'+data.data_id).val(response.id);
        $('#unit_size_id'+data.data_id).val(response.unit_size_id);
        
        $('#plot_size'+data.data_id).text(response.size+' Marla');   
        $('#customer_name'+data.data_id).text(response.name);
        $('#net_pay'+data.data_id).text(response.netpay);
        $('#booking_data'+data.data_id).text(response.data);  
    },
    error: function () {
      // Handle errors
    }
  });
}
       function selectCall(){
       var multi_value= $('#multipro_id').val();

        $('.js-example-basic-single').select2({
                            width: '250px',
                            placeholder: 'Search Unit Number...',
                            ajax: {
 url: '{{route("balloting.get.inventory-unit")}}',
   
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term, // search term
        multipro_id: multi_value
      };
    },
    processResults: function (data) {
    return {
        results: $.map(data, function (item) {
            
            return {
                id: item.id,
                text: item.text
            }
        })
    };
},
cache: true

  }
                        });
       }

       function confirmSubmit() {
        Swal.fire({
    title: "Are you sure?",
    text: "Do you want to Lock these inventories for balloting?",
    icon: "warning",
    buttons: ["No", "Yes"]
  }).then((confirm) => {
    if (confirm.isConfirmed) {
      // User clicked "Yes", submit the form
      document.getElementById("myForm").submit();
    } else {
      // User clicked "No", do nothing
      
    }
  });
}

    </script>
@stop