@extends('layouts.admin-simple')

@section('title', 'Asset Depreciation')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item active">Depreciation</li>
@endsection

@section('page-title')
    <div class="d-flex justify-content-between align-items-center w-100">
        <div class="page-title-left">
            <h3 class="mb-1">Depreciation Dashboard</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Overview of fixed asset depreciation</p>
        </div>
        <div class="page-actions-right d-flex">
            @can('warehouse.fixed-asset-depreciation.calculate')
            <button type="button" class="btn btn-sm btn-primary" onclick="showRunDepreciationModal()">
                <i class="material-icons mr-1" style="font-size: 16px;">calculate</i> Run Depreciation
            </button>
            @endcan
        </div>
    </div>
@endsection

@section('content')
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show"><span>{{ session('success') }}</span><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show"><span>{{ session('error') }}</span><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>
    @endif

    <!-- Period Selector -->
    <div class="card mb-3">
        <div class="card-body py-2">
            <form method="GET" class="row align-items-center">
                <div class="col-md-2">
                    <select name="year" class="form-control form-control-sm">
                        @for($y = date('Y'); $y >= date('Y') - 5; $y--)
                            <option value="{{ $y }}" {{ $year == $y ? 'selected' : '' }}>{{ $y }}</option>
                        @endfor
                    </select>
                </div>
                <div class="col-md-2">
                    <select name="month" class="form-control form-control-sm">
                        @for($m = 1; $m <= 12; $m++)
                            <option value="{{ $m }}" {{ $month == $m ? 'selected' : '' }}>{{ date('F', mktime(0, 0, 0, $m, 1)) }}</option>
                        @endfor
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="category_id" class="form-control form-control-sm">
                        <option value="">All Categories</option>
                        @foreach($categories as $cat)
                            <option value="{{ $cat->id }}" {{ request('category_id') == $cat->id ? 'selected' : '' }}>{{ $cat->category_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-sm btn-primary">Filter</button>
                    <a href="{{ route('warehouse.fixed-assets.depreciation.index') }}" class="btn btn-sm btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <!-- Summary Cards -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-primary" style="font-size: 36px;">business_center</i>
                    <h4 class="mt-2 mb-0">{{ $summary['total_assets'] ?? 0 }}</h4>
                    <small class="text-muted">Total Assets</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-success" style="font-size: 36px;">attach_money</i>
                    <h4 class="mt-2 mb-0">AED {{ number_format($summary['total_purchase_value'] ?? 0, 2) }}</h4>
                    <small class="text-muted">Total Purchase Value</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-danger" style="font-size: 36px;">trending_down</i>
                    <h4 class="mt-2 mb-0">AED {{ number_format($summary['total_accumulated_depreciation'] ?? 0, 2) }}</h4>
                    <small class="text-muted">Accumulated Depreciation</small>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-3">
                    <i class="material-icons text-info" style="font-size: 36px;">account_balance</i>
                    <h4 class="mt-2 mb-0">AED {{ number_format($summary['total_book_value'] ?? 0, 2) }}</h4>
                    <small class="text-muted">Current Book Value</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Depreciation Records Table -->
    <div class="card">
        <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">trending_down</i>Asset Depreciation Records</h6></div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="thead-light">
                        <tr><th>#</th><th>Asset Code</th><th>Asset Name</th><th>Category</th><th class="text-right">Purchase Price</th><th class="text-right">Accum. Depr.</th><th class="text-right">Book Value</th><th>Method</th><th>Useful Life</th><th>Actions</th></tr>
                    </thead>
                    <tbody>
                        @forelse($records as $i => $rec)
                        <tr>
                            <td>{{ $records->firstItem() + $i }}</td>
                            <td><code>{{ $rec->asset_code }}</code></td>
                            <td>{{ $rec->asset_name }}</td>
                            <td><span class="badge badge-secondary">{{ $rec->category ? $rec->category->category_name : '-' }}</span></td>
                            <td class="text-right">AED {{ number_format($rec->purchase_price ?? 0, 2) }}</td>
                            <td class="text-right text-danger">AED {{ number_format($rec->accumulated_depreciation ?? 0, 2) }}</td>
                            <td class="text-right font-weight-bold">AED {{ number_format($rec->current_book_value ?? 0, 2) }}</td>
                            <td>{{ ucfirst(str_replace('_',' ', $rec->depreciation_method ?? 'straight_line')) }}</td>
                            <td class="text-center">{{ ($rec->useful_life_years ?? 0) * 12 }} mo</td>
                            <td><a href="{{ route('warehouse.fixed-assets.depreciation.show', $rec->id) }}" class="btn btn-sm btn-info" title="View Schedule"><i class="material-icons" style="font-size: 14px;">visibility</i></a></td>
                        </tr>
                        @empty
                        <tr><td colspan="10" class="text-center text-muted py-4">No depreciation records found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($records->hasPages())
        <div class="card-footer">{{ $records->links() }}</div>
        @endif
    </div>

    <!-- Run Depreciation Modal -->
    <div class="modal fade" id="runDepreciationModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <form method="POST" action="{{ route('warehouse.fixed-assets.depreciation.calculate') }}">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title"><i class="material-icons mr-2 align-middle">calculate</i> Run Depreciation</h5>
                        <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                    </div>
                    <div class="modal-body">
                        <div class="alert alert-info mb-3">
                            <i class="material-icons mr-1" style="font-size: 16px;">info</i>
                            This will calculate depreciation for all active assets for the selected period.
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Year <span class="text-danger">*</span></label>
                                    <select name="year" class="form-control" required>
                                        @for($y = date('Y'); $y >= date('Y') - 5; $y--)
                                            <option value="{{ $y }}" {{ $y == date('Y') ? 'selected' : '' }}>{{ $y }}</option>
                                        @endfor
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Month <span class="text-danger">*</span></label>
                                    <select name="month" class="form-control" required>
                                        @for($m = 1; $m <= 12; $m++)
                                            <option value="{{ $m }}" {{ $m == date('n') ? 'selected' : '' }}>{{ date('F', mktime(0, 0, 0, $m, 1)) }}</option>
                                        @endfor
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-primary" onclick="return confirm('Run depreciation calculation?')"><i class="material-icons mr-1" style="font-size: 16px;">calculate</i> Run Calculation</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
<script>
function showRunDepreciationModal() { $('#runDepreciationModal').modal('show'); }
</script>
@endpush
