@extends('layouts.backend')

@section('extraCss')  
<link href="https://cdn.datatables.net/buttons/1.7.0/css/buttons.dataTables.min.css" rel="stylesheet">
<style type="text/css">
.modal {
text-align: center;
}

</style>
@endsection

@php 
$reqpos = request("warehouse_id");
@endphp
@section('mainContentArea')
<div class="card card-default">
  <div class="card-header">
    <h4>Discount Output Report</h4>
  </div>       
 
  <div class="alert alert-default" >
      <form action="{{ route('discount-output-report') }}" id="articlereport">
      <h4>Filter  </h4>
      <table class="table filterPosSo" >             
        <tr>       
         <td width="15%">Type</td>
        <td width="15%">Store</td>                         
        <td width="15%">Start Date</td>                         
        <td width="15%">End Date</td>                         
        <td colspan="2">ACTION</td>
        </tr>
        <tr>

          <td>
            <select name="storeType[]" id="storeType" class="input-md" multiple="multiple" > 
                                     
            @foreach($ALL_GROUP as $key => $type)
            <option value="{{ $type->id }}" {{ ( !empty(request("storeType")) && in_array($type->id , request("storeType")) ? "selected" : "")  }}
            {{ ( empty(request("storeType")) ? "selected" : "") }}>{{ $type->name }}</option>
            @endforeach
            </select>
        </td>

        <td>
            <select name="pos_id[]" id="posStore" class="input-md" multiple="multiple" style="width:150px;">
            </select>
        </td>

 
        
        <td>
          <input type="text" name="startdate" id="startdate" readonly="" class="form-control datepicker" placeholder="Enter Start date" value="{{ request('startdate') }}">
        </td>
        <td>
          <input type="text" name="enddate" id="enddate" readonly="" class="form-control datepicker" placeholder="Enter End date" value="{{ request('enddate') }}">
        </td>

        <td><button class="btn btn-block btn-success"><i class="fa fas fa-search" id="submit"></i></button></td>
        <td><button class="btn btn-block btn-warning" type="button" onclick="window.location.href='{{ route('discount-output-report') }}';"><i class="fa fas fa-refresh"></i></button></td>
        </tr>
        </table>
      </form>  
    </div>

  <div class="card-body table-responsive" >

    <table class="table table-striped table-bordered " id="articleTables">
      <thead>
        <tr>
          <th>#</th>
          <th>Invoice</th>
          <th>store</th>
          <th>Mrp</th>
          <th>Wallet Dis.</th>
          <th>Clearance Dis.</th>
          <th>Birthday Dis.</th>
          <th>Anniv. Dis.</th>
          <th>happyHour Dis.</th>
          <th>Voucher Dis.</th>
          <th>SPS Dis.</th>
          <th>other Dis.</th>
          <th>Qty </th>
          <th>Net Sale</th>       
          <th>Bill Date</th>       
        </tr>
      </thead>     
    <tbody>  <?php $total_mrp=$qty=$wallet=$netSale=$total_SPS=$Total_cler= $Total_bday_Dis= $Total_voucher_dis=$Total_annday_Dis=$Total_apt_Dis=$Total_dis=0 ?>
        @foreach($bills as $key =>$bill)
        <?php 
            //dd($bill);
            $walletAmt=0;
            $total_mrp +=$bill['total_mrp']; 
            $qty +=$bill['total_items']; 
            $netSale +=$bill['final_amt']; 
            $total_SPS +=$bill['total_sps_discount']; 
            $Total_bday_Dis +=$bill['total_bday_discount']; 
            $Total_annday_Dis +=$bill['total_ann_discount']; 
            $Total_apt_Dis +=$bill['total_apt_discount']; 
            $Total_voucher_dis +=$bill['total_voucher_discount']; 
            $Total_dis +=$bill['total_discount']; 

            $Total_cler +=$bill['total_clearance']; 
            foreach($bill->billPayments as $payment){
                if($payment->pmt > 0 && $payment->pmode=='Wallet' ){
               $walletAmt = $payment->pmt;
               $wallet += $payment->pmt;
                }
            }
        ?>
       
      <tr> 
             <td>{{ $key + 1 }}</td>
            <td>{{ $bill['bill_number'] }}</td>
            <td>{{-- $bill['store_name'] --}}
                @foreach($allPosWarehouse as $warehouse)
                @if($bill['pos_id']==$warehouse->id)
                {{  $warehouse->name }}
                @endif
                @endforeach
            </td>
            <td>{{ $bill['total_mrp'] }}</td>
            <td><!--@foreach($bill->billPayments as $payment)
            
                    @if($payment->pmt > 0 && $payment->pmode=='Wallet')
                      {{-- $payment->pmt --}}                      
                    @endif
                @endforeach-->
                {{ $walletAmt }}
            </td>
            <td>{{ $bill['total_clearance'] }}</td>
            <td>{{ $bill['total_bday_discount'] }}</td>
            <td>{{ $bill['total_ann_discount'] }}</td>
            <td>{{ $bill['total_apt_discount'] }}</td>
            <td>{{ $bill['total_voucher_discount'] }}</td>
            <td>{{ $bill['total_sps_discount'] }}</td>
            <td>{{ $bill['total_discount'] }}</td>
            <td>{{ $bill['total_items'] }}</td>
            <td>{{ $bill['final_amt'] }}</td>
            <td>{{ $bill['bill_date'] }}</td>
           
            
      </tr> 
      @endforeach

      </tbody>
    <tfoot>
        <tr>
            <th colspaN="3" style="text-align: center;">Total</th>            
            <th>{{$total_mrp  }}</th>
            <th>{{ $wallet }} </th>
            <th>{{ $Total_cler }} </th>
            <th>{{ $Total_bday_Dis }} </th>
            <th>{{ $Total_annday_Dis }} </th>
            <th>{{ $Total_apt_Dis }} </th>
            <th>{{ $Total_voucher_dis }} </th>
            <th>{{ $total_SPS }} </th>
            <th>{{ $Total_dis }} </th>
            <th>{{ $qty }} </th>
            <th>{{ $netSale }}</th>       
               
            <th></th>       
          </tr>
        <tr>
            <th>#</th>
            <th>Invoice</th>
            <th>store</th>
            <th>Mrp</th>
            <th>Wallet Dis </th>
            <th>Clearance Dis.</th>
            <th>Birthday Dis.</th>
            <th>Anniv. Dis.</th>
            <th>happyHour Dis.</th>
            <th>Voucher Dis.</th>
            <th>SPS Dis.</th>
            <th>Other Dis.</th>
            <th>Qty </th>
            <th>Net Sale</th>       
            <th>Bill Date</th>        
          </tr>
    </tfoot>
   </table>
  </div>
</div>

<div class="modal fade " id="ImageModal" tabindex="-1" role="dialog" aria-hidden="true" >
    <div class="modal-dialog" role="document">
    <div class="modal-content">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
    <span aria-hidden="true">&times;</span>
    </button>
    </div>
    <div class="modal-body">
    <div id="carouselExample" class="carousel slide" data-ride="carousel">

    <div class="carousel-inner">
    <div class="carousel-item active " >

    <img id='articlepop'  src="" alt="First slide" style="max-height:300px;">

    </div>

    </div>

    </div>
    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-secondary"  data-dismiss="modal">Close</button>
    </div>
    </div>
    </div>
</div>
@endsection

@section('extraJs')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js" ></script>
<script src="https://cdn.datatables.net/buttons/1.7.0/js/buttons.html5.min.js" ></script>
<script type="text/javascript">  


$("#submit").click(function () {    

  if (($('#warehouse').val() == '') && ($('#posStore').val() == '')) {

    alert("Please Select Warehouse / Store");
    return false;
  }

  

});

$(document).ready(function() {

  $('#articleTables').DataTable( {
  "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],         
  dom: 'Bfrtip',
  buttons: [ 
  'colvis',
  {
    extend: 'excelHtml5',
    autoFilter: true,
    sheetName: 'Exported data',
    exportOptions: {
    columns: ':visible'
    },
    footer: true
  }                     
  ],                   
  colReorder: true,  
  paging: false 
  } );       
} );


$(function () {
  var type = $('#storeType').val();
  callstores(type);

  $("#storeType").on('change', function () { 

  var type = $('#storeType').val();
  callstores(type);

});

});




function callstores(type)
{
  var _token   = $('meta[name="csrf-token"]').attr('content');   

  $.ajax({
  url:"{{ route('type-store-list') }}",
  method:"get",
  data:{type: type, _token:_token},
  success:function(res)
  { 

  if(res){ console.log(res);
  $("#posStore").empty();

  $.each(res,function(key,value){ //console.log(key,value);
  $("#posStore").append('<option value="'+key+'" >'+value+'</option>');
  });

  console.log( <?php echo json_encode($reqpos); ?>);
  var values = <?php echo json_encode($reqpos); ?>;
  $.each(values, function(i,e){ console.log(i,e);
  $("#posStore option[value='" + e + "']").prop("selected", true);
  });

  $('#posStore').multiselect({            
  enableFiltering: true,
  allSelectedText: 'All',
  maxHeight: 200,
  includeSelectAllOption: true,
  enableCaseInsensitiveFiltering: true  
  });        

  $('#posStore').multiselect('rebuild');

  }else{
  $("#posStore").empty();
  }
  }

  })
}

</script>

@endsection