@extends('backend.layouts.master')
@section('section-title', 'Report')
@section('page-title', 'Purchase Report')

@push('css')
<style>
    @media print{
        table,table th,table td {
            color:black !important;
        }

        #h-hide {
            display: none;
        }
    }
</style>
@endpush

@section('content')
    <div class="row">
        <div class="col-lg-12">
            <div class="card card-body mb-2" id="h-hide">
                <form action="{{ route('report.purchase') }}">
                  <div class="form-row">
                    <div class="form-group col-md-4">
                        <select class="select2" name="product_id">
                            <option selected value="">Select Product</option>
                            @foreach ($products as $product)
                                <option value="{{ $product->id }}" @isset($oneProduct) @if($product->id == $oneProduct->id) selected @endif @endisset>{{ $product->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group col-md-4">
                      <input type="date" name="start_date" class="form-control" value="{{ (isset($sdate))?date('Y-m-d', strtotime($sdate)):''; }}">
                    </div>
                    <div class="form-group col-md-4">
                      <input type="date" name="end_date" class="form-control" value="{{ (isset($edate))?date('Y-m-d', strtotime($edate)):''; }}">
                    </div>
                  </div>
                  <div class="form-row mt-2">
                    <div class="form-group col-12">
                        <button class="btn btn-primary" type="submit">
                                <i class="fa fa-sliders"></i> Filter
                        </button>
                        <a href="{{ route('report.purchase') }}" class="btn btn-info">Reset</a>
                        <a href="" class="btn btn-primary float-right" onclick="window.print()">Print</a>
                    </div>
                  </div>
                </form>
            </div>
            <div class="card m-b-30 print_area">
                <div class="card-header">
                    <h4 style="text-align: center; font-weight:bold; margin-top:25px;">Purchase Report ({{ (isset($sdate))?date('m-d-Y', strtotime($sdate)):''; }} - {{ (isset($edate))?date('m-d-Y', strtotime($edate)):''; }})</h4>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        @if (isset($purchaseItem))
                        <table class="table table-bordered">
                        <thead>
                            <tr class="bg-primary">
                                <th>SL.</th>
                                <th>Date</th>
                                <th>Purchase No</th>
                                <th>Product</th>
                                <th>Quantity</th>
                                <th>Unit Price</th>
                                <th>Sub Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            @php
                                $sub_total = 0;
                            @endphp
                            @forelse($purchaseItem as $key => $data)
                            @php
                                if ($data->product->unit->related_unit  == null) {
                                    $main_qty = $data->main_qty;
                                    $qty = $main_qty.' '.$data->product->unit->name;
                                } else {
                                    //purchase
                                    $main_qty = $data->main_qty;
                                    $pur_total_main = (float)($main_qty*$data->product->unit->related_value);
                                    $sub_qty = $data->sub_qty;
                                    $pur_total_qty = (float)($pur_total_main+$sub_qty);
                                    $total_qty = $pur_total_qty;
                                    $check = $total_qty/$data->product->unit->related_value;
                                    if (is_integer($check)) {
                                        $data['stock_qty'] = $check.' '.$data->product->unit->name.' 0 '.$data->product->unit->related_unit->name;
                                    } else {
                                        $main_value = floor($check)*$data->product->unit->related_value;
                                        $sub_value = $total_qty-$main_value;
                                        $qty = floor($check).' '.$data->product->unit->name.' '.$sub_value.' '.$data->product->unit->related_unit->name;
                                    }
                                }
                                $sub_total += $data->subtotal;
                                // dd($sub_total);
                            @endphp
                            <tr>
                                <td>{{ $key+1 }}</td>
                                <td>{{ date('m-d-Y', strtotime($data->purchase->date)) }}</td>
                                <td>{{ $data->purchase->purchase_no }}</td>
                                <td>{{ $data->product->name }}</td>
                                <td>{{ $qty }}</td>
                                <td>{{ number_format($data->rate, 2) }} TK</td>
                                <td>{{ number_format($data->subtotal, 2) }} TK</td>
                            </tr>
                            @empty
                                <tr>
                                    <td colspan="10" class="text-center">No Data Found</td>
                                </tr>
                            @endforelse
                        </tbody>
                        <tfooter>
                            <tr style="background: #3393f2; font-size: 20px; font-width: 700; font-family:sans-serif">
                                <td colspan="5"></td>
                                <td> <strong> Total Price: </strong></td>
                                <td colspan="2"><strong> {{number_format($sub_total,2)}} TK </strong></td>
                            </tr>
                        </tfooter>
                        </table>
                        @else
                        <div class="col-md-12" style="padding-bottom: 30px;">
                            <div class="alert alert-danger text-center" role="alert"> Please Select Start and End Month</div>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
