@extends('layouts.admin-simple')

@section('title', 'New Asset Disposal')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.fixed-assets.disposal.index') }}">Disposal</a></li>
    <li class="breadcrumb-item active">New Disposal</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">New Asset Disposal</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Dispose of a fixed asset</p>
        </div>
        <div class="page-actions-right">
            <a href="{{ route('warehouse.fixed-assets.disposal.index') }}" class="btn btn-sm btn-outline-secondary"><i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i> Back</a>
        </div>
    </div>
@endsection

@section('content')
    @if($errors->any())
        <div class="alert alert-danger alert-dismissible fade show">
            <ul class="mb-0">@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul>
            <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
        </div>
    @endif

    <form method="POST" action="{{ route('warehouse.fixed-assets.disposal.store') }}">
        @csrf
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0"><i class="material-icons mr-2 align-middle">delete_forever</i>Disposal Details</h6></div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Asset <span class="text-danger">*</span></label>
                            <select name="fixed_asset_id" id="fixed_asset_id" class="form-control select2-dropdown" required>
                                <option value="">Select Asset</option>
                                @foreach($assets as $asset)
                                    <option value="{{ $asset->id }}"
                                        data-book-value="{{ $asset->current_book_value ?? 0 }}"
                                        {{ old('fixed_asset_id') == $asset->id ? 'selected' : '' }}>
                                        {{ $asset->asset_code }} - {{ $asset->asset_name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Disposal Type <span class="text-danger">*</span></label>
                            <select name="disposal_type" class="form-control" required>
                                <option value="">Select Type</option>
                                @foreach(['sold','scrapped','donated','traded_in','stolen','lost','retired'] as $t)
                                    <option value="{{ $t }}" {{ old('disposal_type') == $t ? 'selected' : '' }}>{{ ucfirst(str_replace('_',' ',$t)) }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Disposal Date <span class="text-danger">*</span></label>
                            <input type="date" name="disposal_date" class="form-control" value="{{ old('disposal_date', date('Y-m-d')) }}" required>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Book Value at Disposal</label>
                            <input type="text" id="book_value_display" class="form-control" readonly value="AED 0.00">
                            <input type="hidden" name="book_value_at_disposal" id="book_value_at_disposal" value="{{ old('book_value_at_disposal', 0) }}">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Disposal Amount (AED)</label>
                            <input type="number" name="disposal_amount" id="disposal_amount" class="form-control" step="0.01" min="0" value="{{ old('disposal_amount', 0) }}" onchange="calcGainLoss()" onkeyup="calcGainLoss()">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Buyer Name</label>
                            <input type="text" name="buyer_name" class="form-control" value="{{ old('buyer_name') }}" placeholder="Name of buyer">
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="form-group">
                            <label>Buyer Contact</label>
                            <input type="text" name="buyer_contact" class="form-control" value="{{ old('buyer_contact') }}" placeholder="Contact info">
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Disposal Reason <span class="text-danger">*</span></label>
                            <textarea name="disposal_reason" class="form-control" rows="3" required placeholder="Reason for disposing this asset">{{ old('disposal_reason') }}</textarea>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Notes</label>
                            <textarea name="notes" class="form-control" rows="3" placeholder="Additional notes">{{ old('notes') }}</textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-body text-center py-3">
                <button type="submit" class="btn btn-success mr-3"><i class="material-icons mr-1" style="font-size: 16px;">save</i> Create Disposal</button>
                <a href="{{ route('warehouse.fixed-assets.disposal.index') }}" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </div>
    </form>
@endsection

@push('styles')
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
    <style>
        .select2-container { width: 100% !important; }
        .select2-container--default .select2-selection--single { height: 38px; border: 1px solid #ced4da; }
        .select2-container--default .select2-selection--single .select2-selection__rendered { line-height: 36px; }
        .select2-container--default .select2-selection--single .select2-selection__arrow { height: 36px; }
    </style>
@endpush

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#fixed_asset_id').select2({ placeholder: 'Search and select asset...', allowClear: true }).on('change', function() {
                var selected = $(this).find(':selected');
                var bookValue = parseFloat(selected.data('book-value')) || 0;
                $('#book_value_display').val('AED ' + bookValue.toFixed(2));
                $('#book_value_at_disposal').val(bookValue);
                calcGainLoss();
            });
            @if(old('fixed_asset_id'))
                $('#fixed_asset_id').trigger('change');
            @endif
        });

        function calcGainLoss() {
            var bookValue = parseFloat($('#book_value_at_disposal').val()) || 0;
            var disposalAmt = parseFloat($('#disposal_amount').val()) || 0;
            var gainLoss = disposalAmt - bookValue;
            if (gainLoss > 0) {
                $('#book_value_display').closest('.row').find('#disposal_amount').css('border-color', '#28a745');
            } else if (gainLoss < 0) {
                $('#book_value_display').closest('.row').find('#disposal_amount').css('border-color', '#dc3545');
            }
        }
    </script>
@endpush
