@extends('layouts.app')
@section('title', 'Delete all Listings')

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
    <li class="breadcrumb-item active">Delete all Listings</li>
@endsection

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title text-danger">Delete all Listings</h1>
        <p class="page-subtitle">Permanently remove all master data (accounts, customers, items, etc.) for this company</p>
    </div>
</div>

@if(session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
@endif
@if(session('error'))
<div class="alert alert-danger">{{ session('error') }}</div>
@endif

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card border-danger">
            <div class="card-header bg-danger bg-opacity-10 text-danger fw-semibold">
                <i class="bi bi-exclamation-triangle me-1"></i>Warning — irreversible action
            </div>
            <div class="card-body">
                <p>This will delete <strong>all master listings</strong> for <strong>{{ $company->name }}</strong>, including:</p>
                <ul class="mb-3">
                    <li>Chart of accounts, dimensions, tax codes, price levels</li>
                    <li>Customers, vendors, employees, and other names</li>
                    <li>Items, categories, warehouses, and units of measure</li>
                    <li>Exchange rates and accounting periods</li>
                </ul>
                <p class="mb-0 text-muted" style="font-size:13px">
                    <strong>Kept:</strong> company record, users, roles, settings, fiscal years, and currencies.
                </p>
            </div>
        </div>

        @if($hasTransactions)
        <div class="alert alert-warning mt-3">
            <strong>Transactions still exist.</strong>
            Delete all transactions first before removing listings.
            <a href="{{ route('admin.company-reset.transactions') }}" class="alert-link">Go to Delete All Transactions</a>
        </div>
        @endif

        @if($total > 0)
        <div class="card mt-3">
            <div class="card-header fw-semibold">Rows to be deleted ({{ number_format($total) }} total)</div>
            <div class="card-body p-0">
                <div class="table-responsive" style="max-height:280px;overflow-y:auto">
                    <table class="table table-sm table-striped mb-0">
                        <thead><tr><th>Table</th><th class="text-end">Rows</th></tr></thead>
                        <tbody>
                            @foreach($counts as $table => $count)
                            <tr>
                                <td><code>{{ $table }}</code></td>
                                <td class="text-end">{{ number_format($count) }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        @else
        <div class="alert alert-info mt-3 mb-0">No listing data found for this company.</div>
        @endif
    </div>

    <div class="col-lg-5">
        <div class="card">
            <div class="card-header fw-semibold">Confirm deletion</div>
            <div class="card-body">
                @php $canDelete = $total > 0 && !$hasTransactions; @endphp
                <form method="POST" action="{{ route('admin.company-reset.listings.destroy') }}"
                      onsubmit="return confirm('This cannot be undone. Delete all listings for {{ addslashes($company->name) }}?');">
                    @csrf
                    @method('DELETE')
                    <p class="text-muted" style="font-size:13px">
                        Type <strong>{{ $company->name }}</strong> below to confirm.
                    </p>
                    <div class="mb-3">
                        <input type="text" name="confirmation" class="form-control @error('confirmation') is-invalid @enderror"
                               placeholder="Company name" autocomplete="off" required
                               {{ $canDelete ? '' : 'disabled' }}>
                        @error('confirmation')<div class="invalid-feedback">{{ $message }}</div>@enderror
                    </div>
                    <button type="submit" class="btn btn-danger w-100" {{ $canDelete ? '' : 'disabled' }}>
                        <i class="bi bi-trash me-1"></i>Delete all Listings
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
