@extends('backend.layouts.master')
@section('section-title', 'Purchase')
@section('page-title', 'Purchase List')
@if (check_permission('purchase.create'))
    @section('action-button')
        <a href="{{ route('purchase.create') }}" class="btn btn-primary-rgba">
            <i class="mr-2 feather icon-plus"></i>
            Add Purchase
        </a>
    @endsection
@endif
@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 m-b-30">
                <div class="card-body">
                    <form action="{{ route('purchase.index') }}" method="GET">
                        @php
                            $suppliers = App\Models\Supplier::get();
                            $products = App\Models\Product::get();
                        @endphp
                        <div class="row h-hide">
                            <div class="col-md-3 mt-2">
                                <input type="date" class="form-control" name="startDate" value="{{ $startDate }}" />
                            </div>
                            <div class="col-md-3 mt-2">
                                <input type="date" class="form-control" name="endDate" value="{{ $endDate }}" />
                            </div>
                            <div class="col-md-3 mt-2">
                                <input type="text" placeholder="Enter Purchase No" value="{{ $purchase_no }}"
                                    name="purchase_no" class="form-control">
                            </div>
                            <div class="col-md-3 col-12 mt-2">
                                <select name="product_id" id="" class="select2">
                                    <option value="">Select Product</option>
                                    @foreach ($products as $item)
                                        <option
                                            value="{{ $item->id }}"{{ $product_id == $item->id ? 'selected' : '' }}>
                                            {{ $item->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-3 mt-2">
                                <select name="supplier_id" id="" class="select2">
                                    <option value="">Select Supplier</option>
                                    @foreach ($suppliers as $item)
                                        <option
                                            value="{{ $item->id }}"{{ $supplier_id == $item->id ? 'selected' : '' }}>
                                            {{ $item->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="row mt-3 h-hide d-flex justify-content-between">
                            <div class="col-md-12 col-12">
                                <button type="submit" class="btn btn-primary">Filter</button>
                                <a href="{{ route('purchase.index') }}" class="btn btn-primary">Reset</a>
                                <a href="" class="btn btn-primary float-right" onclick="window.print()">Print</a>
                            </div>
                        </div>
                    </form>
                    <div class="table-responsive mt-3">
                        <table id="datatable-buttons" class="table table-striped table-bordered">
                            <thead class="table-primary">
                                <tr class="text-center">
                                    <th> Purchase Date </th>
                                    <th> Purchase No </th>
                                    <th> Product </th>
                                    <th> Supplier </th>
                                    <th> Creator </th>
                                    <th> Total Amount </th>
                                    <th> Total Paid </th>
                                    <th> Total Due </th>
                                    <th> Status </th>
                                    <th> Action </th>
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                    $total_paid = 0;
                                    $total_amt = 0;
                                    $total_due = 0;
                                @endphp
                                @forelse($purchases as $data)
                                    @php
                                        $total_paid += $data->total_paid;
                                        $total_amt += $data->total_amount;
                                        $total_due += $data->total_due;
                                        $purchase_item = App\Models\PurchaseItem::where(
                                            'purchase_id',
                                            $data->id,
                                        )->get();
                                    @endphp
                                    <tr class="text-center">
                                        <td>{{ $data->date }}</td>
                                        <td>{{ $data->purchase_no }}</td>
                                        {{-- <td>{{ $purchase_item?->product?->name }}</td> --}}
                                        <td>
                                            @foreach ($purchase_item as $item)
                                                <ul>
                                                    <li>
                                                        {{ $item->product->name }}
                                                    </li>
                                                </ul>
                                            @endforeach
                                        </td>
                                        <td>{{ $data->supplier->name }}</td>
                                        <td>{{ $data->user->name }}</td>
                                        <td>{{ $data->total_amount }}</td>
                                        <td>{{ $data->total_paid }}</td>
                                        <td>{{ $data->total_due }}</td>
                                        <td>
                                            @if ($data->status == 0)
                                                <span class="badge badge-warning">Due</span>
                                            @elseif($data->status == 1)
                                                <span class="badge badge-success">Paid</span>
                                            @elseif($data->status == 2)
                                                <span class="badge badge-danger">Returned</span>
                                            @endif
                                        </td>
                                        <td>
                                            <div class="dropdown">
                                                <button class="btn btn-dark btn-sm dropdown-toggle" type="button"
                                                    id="dropdownMenuButton" data-toggle="dropdown">
                                                    Action
                                                </button>
                                                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                                    {{-- pay button  --}}
                                                    @if (check_permission('purchase.pay'))
                                                        @if ($data->total_due > 0)
                                                            <a class="dropdown-item text-success" href="{{ url('purchase/pay/' . $data->id) }}">
                                                                <i class="feather icon-dollar-sign"></i> Due
                                                            </a>
                                                        @endif
                                                    @endif
                                                    @if (check_permission('rtnPurchase.edit'))
                                                        <a class="dropdown-item text-warning" href="{{ route('rtnPurchase.edit',$data->id) }}">
                                                            <i class="fa fa-undo"></i> Return
                                                        </a>
                                                    @endif
                                                    @if (check_permission('purchase.edit'))
                                                        <a class="dropdown-item text-info" href="{{ url('purchase/edit/' . $data->id) }}">
                                                            <i class="fa fa-edit"></i> Edit
                                                        </a>
                                                    @endif
                                                    {{-- print --}}
                                                    <a class="dropdown-item text-success" href="{{ route('purchase.print', $data->id) }}">
                                                        <i class="feather icon-printer"></i> Print
                                                    </a>

                                                    {{-- delete --}}
                                                    @if (check_permission('purchase.destroy'))
                                                        <a class="dropdown-item text-danger" href="#" data-toggle="modal"
                                                            data-target="#deleteModal-{{ $data->id }}">
                                                            <i class="feather icon-trash"></i> Delete
                                                        </a>
                                                    @endif
                                                </div>
                                            </div>
                                        </td>
                                    </tr>

                                    {{-- delete modal --}}
                                    <form action="{{ route('purchase.destroy', $data->id) }}" method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <x-delete-modal title="Purchase" id="{{ $data->id }}" />
                                    </form>
                                @empty
                                    <tr>
                                        <td colspan="12" class="text-center">No Purchase Found</td>
                                    </tr>
                                @endforelse
                            </tbody>
                            <tfoot class="text-dark">
                                <tr class="text-dark text-right">
                                    <td colspan="5"><strong>Total: </strong></td>
                                    <td> <strong>{{ number_format($total_amt,2) }} TK</strong></td>
                                    <td> <strong>{{ number_format($total_paid,2) }} TK</strong></td>
                                    <td> <strong>{{ number_format($total_due,2) }} TK</strong></td>
                                    <td colspan="3"></td>
                                </tr>
                            </tfoot>
                        </table>
                        {{ $purchases->onEachSide(1)->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
