@extends('layouts.app')

@section('title', 'Product Details - Harshana Systems')
@section('page-title', 'Product Details')

@section('content')
    <div class="d-flex justify-content-between mb-3">
        <h1 class="h3 mb-0 page-title-theme">Product: {{ $product->product_name }}</h1>
        <div class="d-flex gap-2">
            @if(auth()->user() && auth()->user()->hasAccessTo('products.edit'))
                <a href="{{ route('products.edit', $product) }}" class="btn btn-warning">Edit</a>
            @endif
            <a href="{{ route('products.index') }}" class="btn btn-outline-secondary">Back to Products</a>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-md-12">
                    <h4>{{ $product->product_name }}</h4>
                    <p class="text-muted">Code: {{ $product->product_code }}</p>
                    <p class="text-muted">Barcode: {{ $product->product_barcode ?? '-' }}</p>
                </div>

                <div class="col-md-8">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="info-label">Category</label>
                            <div class="info-value">{{ $product->category->name ?? '-' }}</div>
                        </div>
                        <div class="col-md-6">
                            <label class="info-label">Qty Type</label>
                            <div class="info-value">{{ $product->qtyType->qty_type ?? '-' }}</div>
                        </div>
                        <div class="col-md-6">
                            <label class="info-label">Selling Price</label>
                            <div class="info-value">{{ number_format($product->selling_price, 2) }}</div>
                        </div>
                        <div class="col-md-6">
                            <label class="info-label">Warranty (Months)</label>
                            <div class="info-value">{{ $product->warranty_days ?? 0 }}</div>
                        </div>
                        <div class="col-md-6" style="display:none">
                            <label class="info-label">Last Selling Price</label>
                            <div class="info-value">{{ $product->market_price ? number_format($product->market_price, 2) : '-' }}</div>
                        </div>
                        <div class="col-12">
                            <label class="info-label">Branch Stock Information</label>
                            <div class="info-value">
                                <div class="table-responsive">
                                    <table class="table table-sm table-bordered mb-0">
                                        <thead>
                                            <tr>
                                                <th>Branch</th>
                                                <th>Current Stock (Qty)</th>
                                                <th>Total Cost</th>
                                                <th>Per Item Cost</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @if($product->stocks && $product->stocks->count())
                                                @foreach($product->stocks as $stock)
                                                    <tr>
                                                        <td>{{ $stock->branch->name ?? 'Unknown' }}</td>
                                                        <td>{{ $stock->qty }}</td>
                                                        <td>{{ number_format($stock->total_cost, 2) }}</td>
                                                        <td>{{ $stock->qty > 0 ? number_format($stock->total_cost / $stock->qty, 2) : '0.00' }}</td>
                                                    </tr>
                                                @endforeach
                                            @else
                                                <tr>
                                                    <td colspan="4" class="text-center text-muted">No stock records found</td>
                                                </tr>
                                            @endif
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-4" style="display:none">
                            <label class="info-label">Selling Price Can change</label>
                            <div class="info-value">{{ $product->is_active ? 'Yes' : 'No' }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
