@extends('layouts.app')

@section('title', $supplier->name)

@section('actions')
    <a href="{{ route('suppliers.index') }}" class="btn btn-secondary">
        <i class="fas fa-arrow-left"></i> Back
    </a>
    <a href="{{ route('suppliers.edit', $supplier) }}" class="btn btn-warning">
        <i class="fas fa-edit"></i> Edit
    </a>
@endsection

@section('content')
<div class="container-fluid">
    <div class="row">
        <!-- Supplier Details -->
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">Supplier Information</h5>
                </div>
                <div class="card-body">
                    <div class="text-center mb-4">
                        <div class="avatar bg-primary text-white rounded-circle d-inline-flex align-items-center justify-content-center" 
                             style="width: 80px; height: 80px; font-size: 2rem;">
                            {{ substr($supplier->name, 0, 1) }}
                        </div>
                        <h4 class="mt-3">{{ $supplier->name }}</h4>
                        <span class="badge badge-{{ $supplier->status == 'active' ? 'success' : 'secondary' }}">
                            {{ ucfirst($supplier->status) }}
                        </span>
                    </div>
                    
                    <div class="supplier-details">
                        <div class="detail-item">
                            <strong><i class="fas fa-id-card"></i> Supplier Code:</strong>
                            <span>{{ $supplier->supplier_code }}</span>
                        </div>
                        
                        <div class="detail-item">
                            <strong><i class="fas fa-building"></i> Company:</strong>
                            <span>{{ $supplier->company_name ?? 'N/A' }}</span>
                        </div>
                        
                        <div class="detail-item">
                            <strong><i class="fas fa-phone"></i> Phone:</strong>
                            <span>{{ $supplier->phone }}</span>
                        </div>
                        
                        @if($supplier->alternate_phone)
                        <div class="detail-item">
                            <strong><i class="fas fa-phone-alt"></i> Alternate Phone:</strong>
                            <span>{{ $supplier->alternate_phone }}</span>
                        </div>
                        @endif
                        
                        @if($supplier->email)
                        <div class="detail-item">
                            <strong><i class="fas fa-envelope"></i> Email:</strong>
                            <a href="mailto:{{ $supplier->email }}">{{ $supplier->email }}</a>
                        </div>
                        @endif
                        
                        @if($supplier->tax_number)
                        <div class="detail-item">
                            <strong><i class="fas fa-file-invoice-dollar"></i> Tax Number:</strong>
                            <span>{{ $supplier->tax_number }}</span>
                        </div>
                        @endif
                        
                        <div class="detail-item">
                            <strong><i class="fas fa-map-marker-alt"></i> Address:</strong>
                            <span>{{ $supplier->address ?? 'N/A' }}</span>
                        </div>
                        
                        @if($supplier->contact_person)
                        <div class="detail-item">
                            <strong><i class="fas fa-user"></i> Contact Person:</strong>
                            <span>{{ $supplier->contact_person }}</span>
                        </div>
                        @endif
                        
                        @if($supplier->payment_terms)
                        <div class="detail-item">
                            <strong><i class="fas fa-file-contract"></i> Payment Terms:</strong>
                            <span>{{ $supplier->payment_terms }}</span>
                        </div>
                        @endif
                        
                        @if($supplier->notes)
                        <div class="detail-item">
                            <strong><i class="fas fa-sticky-note"></i> Notes:</strong>
                            <p class="mb-0">{{ $supplier->notes }}</p>
                        </div>
                        @endif
                    </div>
                </div>
            </div>
            
            <!-- Balance Card -->
            <div class="card mt-3">
                <div class="card-header">
                    <h5 class="mb-0">Balance Information</h5>
                </div>
                <div class="card-body">
                    <div class="text-center">
                        <h2 class="{{ $supplier->current_balance > 0 ? 'text-danger' : 'text-success' }}">
                            ৳{{ number_format($supplier->current_balance, 2) }}
                        </h2>
                        <p class="text-muted">Current Balance</p>
                        
                        <div class="row text-center">
                            <div class="col-6">
                                <h5 class="text-success">৳{{ number_format($summary['total_paid'] ?? 0, 2) }}</h5>
                                <small class="text-muted">Total Paid</small>
                            </div>
                            <div class="col-6">
                                <h5 class="text-danger">৳{{ number_format($summary['total_due'] ?? 0, 2) }}</h5>
                                <small class="text-muted">Total Due</small>
                            </div>
                        </div>
                        
                        <hr>
                        
                        <div class="text-left">
                            <p><strong>Credit Limit:</strong> ৳{{ number_format($supplier->credit_limit, 2) }}</p>
                            <p><strong>Opening Balance:</strong> ৳{{ number_format($supplier->opening_balance, 2) }}</p>
                        </div>
                        
                        <!-- Update Balance Form -->
                        <form method="POST" action="{{ route('suppliers.update-balance', $supplier) }}" class="mt-3">
                            @csrf
                            <div class="form-group">
                                <label>Update Balance</label>
                                <div class="input-group">
                                    <input type="number" name="amount" class="form-control" 
                                           placeholder="Amount" step="0.01" min="0" required>
                                    <select name="type" class="form-control" required>
                                        <option value="add">Add</option>
                                        <option value="subtract">Subtract</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <textarea name="note" class="form-control" 
                                          placeholder="Note (optional)" rows="2"></textarea>
                            </div>
                            <button type="submit" class="btn btn-primary btn-block">
                                Update Balance
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Purchases History -->
        <div class="col-md-8">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Purchase History</h5>
                    <a href="{{ route('purchases.create') }}?supplier_id={{ $supplier->id }}" 
                       class="btn btn-primary btn-sm">
                        <i class="fas fa-plus"></i> New Purchase
                    </a>
                </div>
                <div class="card-body">
                    <!-- Summary Stats -->
                    <div class="row mb-4">
                        <div class="col-md-3 col-6">
                            <div class="info-box">
                                <span class="info-box-icon bg-info">
                                    <i class="fas fa-shopping-cart"></i>
                                </span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Total Purchases</span>
                                    <span class="info-box-number">{{ $summary['total_purchases'] }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="info-box">
                                <span class="info-box-icon bg-success">
                                    <i class="fas fa-money-bill-wave"></i>
                                </span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Total Amount</span>
                                    <span class="info-box-number">৳{{ number_format($summary['total_amount'], 2) }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="info-box">
                                <span class="info-box-icon bg-warning">
                                    <i class="fas fa-clock"></i>
                                </span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Total Due</span>
                                    <span class="info-box-number">৳{{ number_format($summary['total_due'], 2) }}</span>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="info-box">
                                <span class="info-box-icon bg-danger">
                                    <i class="fas fa-exclamation-circle"></i>
                                </span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Last Purchase</span>
                                    <span class="info-box-number">
                                        @if($summary['last_purchase'])
                                            {{ \Carbon\Carbon::parse($summary['last_purchase']->purchase_date)->format('d M') }}
                                        @else
                                            N/A
                                        @endif
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Purchases Table -->
                    @if($supplier->purchases->count() > 0)
                    <div class="table-responsive">
                        <table class="table table-sm table-hover">
                            <thead>
                                <tr>
                                    <th>Invoice</th>
                                    <th>Date</th>
                                    <th>Amount</th>
                                    <th>Paid</th>
                                    <th>Due</th>
                                    <th>Status</th>
                                    <th>Payment</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($supplier->purchases as $purchase)
                                <tr>
                                    <td>
                                        <a href="{{ route('purchases.show', $purchase) }}" class="text-primary">
                                            {{ $purchase->purchase_code }}
                                        </a>
                                    </td>
                                    <td>{{ \Carbon\Carbon::parse($purchase->purchase_date)->format('d M, Y') }}</td>
                                    <td class="text-success">৳{{ number_format($purchase->grand_total, 2) }}</td>
                                    <td class="text-primary">৳{{ number_format($purchase->paid_amount, 2) }}</td>
                                    <td class="text-danger">৳{{ number_format($purchase->due_amount, 2) }}</td>
                                    <td>
                                        <span class="badge badge-{{ $purchase->status == 'received' ? 'success' : 'warning' }}">
                                            {{ ucfirst($purchase->status) }}
                                        </span>
                                    </td>
                                    <td>
                                        <span class="badge badge-{{ $purchase->payment_status == 'paid' ? 'success' : ($purchase->payment_status == 'partial' ? 'warning' : 'danger') }}">
                                            {{ ucfirst($purchase->payment_status) }}
                                        </span>
                                    </td>
                                    <td>
                                        <a href="{{ route('purchases.show', $purchase) }}" 
                                           class="btn btn-sm btn-info" title="View">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    
                    <div class="text-center mt-3">
                        <a href="{{ route('purchases.index', ['supplier_id' => $supplier->id]) }}" 
                           class="btn btn-outline-primary">
                            View All Purchases
                        </a>
                    </div>
                    @else
                    <div class="text-center py-5">
                        <i class="fas fa-shopping-cart fa-3x text-muted mb-3"></i>
                        <h5>No Purchase History</h5>
                        <p class="text-muted">This supplier has no purchase records yet.</p>
                        <a href="{{ route('purchases.create') }}?supplier_id={{ $supplier->id }}" 
                           class="btn btn-primary">
                            <i class="fas fa-plus"></i> Create First Purchase
                        </a>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
    .avatar {
        font-weight: bold;
    }
    
    .detail-item {
        margin-bottom: 10px;
        padding-bottom: 10px;
        border-bottom: 1px solid #eee;
    }
    
    .detail-item:last-child {
        border-bottom: none;
        margin-bottom: 0;
    }
    
    .detail-item strong {
        color: #555;
        display: block;
        margin-bottom: 3px;
    }
    
    .info-box {
        background: #f8f9fa;
        border-radius: 5px;
        padding: 15px;
        margin-bottom: 15px;
    }
    
    .info-box-icon {
        float: left;
        width: 50px;
        height: 50px;
        border-radius: 5px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.5rem;
        margin-right: 15px;
    }
    
    .info-box-content {
        overflow: hidden;
    }
    
    .info-box-text {
        display: block;
        font-size: 0.9rem;
        color: #6c757d;
    }
    
    .info-box-number {
        display: block;
        font-weight: 600;
        font-size: 1.2rem;
    }
</style>
@endpush