@extends('layouts.app')

@section('content')
<style>
    .profit-badge {
        font-size: 13px;
        padding: 6px 10px;
        border-radius: 6px;
        display: inline-block;
        min-width: 90px;
    }
</style>

<div class="container-xxl flex-grow-1 container-p-y">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5>Animals</h5>
            <a href="{{ route('animals.create') }}" class="btn btn-primary">Add Animal</a>
        </div>

        <div class="card-body">
            <div class="table-responsive">
                <table @if($animals->count() > 0) id="bookings-table" @endif
                       class="table table-bordered align-middle text-center">

                <thead>
                    <tr>
                        <th>#</th>
                        <th>Image</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Per Animal Expense</th>
                        <th>Buy Price</th>
                        <th>Total Expense</th>
                        <th>Sale Price</th>
                        <th>Faida / Profit</th>
                        <th>Tag Number</th>
                        <th>Action</th>
                    </tr>
                </thead>

                <tbody>
                    @foreach($animals as $animal)
                        @php
                            $lastImage = $animal->images->last();

                            $salePrice = $animal->sale_price;

                            $profitLoss = $animal->profit_loss;

                            if (!is_null($profitLoss)) {
                                if ($profitLoss > 0) {
                                    $profitText = '+' . number_format($profitLoss, 2);
                                    $profitClass = 'bg-success';
                                } elseif ($profitLoss < 0) {
                                    $profitText = number_format($profitLoss, 2);
                                    $profitClass = 'bg-danger';
                                } else {
                                    $profitText = number_format($profitLoss, 2);
                                    $profitClass = 'bg-secondary';
                                }
                            }
                        @endphp

                        <tr>
                            <td>{{ $loop->iteration }}</td>

                            <td>
                                @if($lastImage)
                                    <img src="{{ asset($lastImage->image) }}"
                                         width="70"
                                         height="70"
                                         style="object-fit:cover;border-radius:8px;">
                                @else
                                    N/A
                                @endif
                            </td>

                            <td>{{ $animal->animal_name }}</td>

                            <td>{{ $animal->category->title ?? 'N/A' }}</td>

                            <td>
                                {{ number_format($animal->expense_per_animal ?? 0, 2) }}
                            </td>

                            <td>
                                {{ number_format($animal->buy_price ?? 0, 2) }}
                            </td>

                            <td>
                                {{ number_format($animal->total_expense ?? 0, 2) }}
                            </td>

                            <td>
                                @if(!is_null($salePrice) && $salePrice !== '')
                                    {{ number_format($salePrice, 2) }}
                                @else
                                    N/A
                                @endif
                            </td>

                            <td>
                                @if(is_null($profitLoss))
                                    N/A
                                @else
                                    <span class="badge {{ $profitClass }} profit-badge">
                                        {{ $profitText }}
                                    </span>
                                @endif
                            </td>

                            <td>{{ $animal->tag_number ?? 'N/A' }}</td>

                            <td>
                                <div class="d-inline-flex align-items-center action-icons">

                                    <a href="{{ route('animals.show', $animal->id) }}"
                                       class="action-icon text-info"
                                       title="View">
                                        <i class="bx bx-show"></i>
                                    </a>

                                    <a href="{{ route('animals.edit', $animal->id) }}"
                                       class="action-icon text-warning"
                                       title="Edit">
                                        <i class="bx bx-edit-alt"></i>
                                    </a>

                                    <form action="{{ route('animals.destroy', $animal->id) }}"
                                          method="POST"
                                          class="d-inline"
                                          onsubmit="return confirm('Are you sure you want to delete this animal? Images will also be deleted.')">
                                        @csrf
                                        @method('DELETE')

                                        <button type="submit"
                                                class="action-icon text-danger border-0 bg-transparent p-0"
                                                title="Delete">
                                            <i class="bx bx-trash"></i>
                                        </button>
                                    </form>

                                </div>
                            </td>
                        </tr>
                    @endforeach
                </tbody>

                </table>
            </div>
        </div>
    </div>
</div>
@endsection

@push('script')
<script>
$(document).ready(function () {
    if ($('#bookings-table').length) {
        $('#bookings-table').DataTable({
            responsive: false,
            scrollX: true,
            scrollCollapse: true,
            autoWidth: false,
            pageLength: 10,
            lengthMenu: [5, 10, 25, 50, 100],
            order: [[0, 'asc']],
            columnDefs: [
                { orderable: false, targets: [1, -1] }
            ]
        });
    }
});
</script>
@endpush
