@extends('layouts.app')

@section('title', 'Product Details')
@section('header', 'Product Details')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <h3>{{ $product->name }}</h3>
                        <div>
                            <a href="{{ route('products.index') }}" class="btn btn-secondary">
                                <i class="fas fa-arrow-left"></i> Back
                            </a>
                            <a href="{{ route('products.edit', $product) }}" class="btn btn-primary">
                                <i class="fas fa-edit"></i> Edit
                            </a>
                            <a href="{{ route('products.print-barcode', $product) }}" class="btn btn-info" target="_blank">
                                <i class="fas fa-print"></i> Print Barcode
                            </a>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-4 text-center">
                            @if($product->image && Storage::disk('public')->exists($product->image))
                                <img src="{{ Storage::url($product->image) }}" alt="{{ $product->name }}" class="img-fluid rounded" style="max-height: 250px;">
                            @else
                                <img src="{{ asset('images/no-image.png') }}" alt="No image" class="img-fluid rounded" style="max-height: 250px;">
                            @endif
                        </div>
                        <div class="col-md-8">
                            <table class="table table-bordered">
                                <tr>
                                    <th width="30%">Product Name:</th>
                                    <td><strong>{{ $product->name }}</strong></td>
                                </tr>
                                <tr>
                                    <th>SKU:</th>
                                    <td>{{ $product->sku }}</td>
                                </tr>
                                <tr>
                                    <th>Barcode:</th>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            <code class="mr-2" style="font-size: 18px;">{{ $product->barcode }}</code>
                                            <button class="btn btn-sm btn-secondary copy-barcode" data-barcode="{{ $product->barcode }}">
                                                <i class="fas fa-copy"></i> Copy
                                            </button>
                                        </div>
                                        <div class="mt-2">
                                            {!! DNS1D::getBarcodeHTML($product->barcode, 'C128', 2, 50) !!}
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <th>Category:</th>
                                    <td>{{ $product->category->name ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <th>Purchase Price:</th>
                                    <td>${{ number_format($product->purchase_price, 2) }}</td>
                                </tr>
                                <tr>
                                    <th>Selling Price:</th>
                                    <td>${{ number_format($product->selling_price, 2) }}</td>
                                </tr>
                                <tr>
                                    <th>Current Stock:</th>
                                    <td>
                                        @if($product->stock <= $product->min_stock)
                                            <span class="badge bg-danger">{{ $product->stock }}</span>
                                        @else
                                            <span class="badge bg-success">{{ $product->stock }}</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>Min Stock Alert:</th>
                                    <td>{{ $product->min_stock }}</td>
                                </tr>
                                <tr>
                                    <th>Status:</th>
                                    <td>
                                        @if($product->is_active)
                                            <span class="badge bg-success">Active</span>
                                        @else
                                            <span class="badge bg-danger">Inactive</span>
                                        @endif
                                    </td>
                                </tr>
                                <tr>
                                    <th>Description:</th>
                                    <td>{{ $product->description ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <th>Created At:</th>
                                    <td>{{ $product->created_at->format('F d, Y H:i:s') }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    $('.copy-barcode').click(function() {
        const barcode = $(this).data('barcode');
        navigator.clipboard.writeText(barcode);
        Swal.fire({
            icon: 'success',
            title: 'Copied!',
            text: 'Barcode copied to clipboard',
            timer: 1500,
            showConfirmButton: false
        });
    });
</script>
@endpush
@endsection