@extends('backend.layout.app')

@section('content')
<div class="container">
    <div class="container-header">
        <h1>{{ $title }}</h1>
        <a href="{{ route('backend.products.index') }}" class="btn btn-outline">Back to products</a>
    </div>

    <div class="card mb-25">
        <div class="card-body">
            <form action="{{ route('backend.products.accounts.store', $product->id) }}" method="post" enctype="multipart/form-data">
                @csrf
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Import accounts (TXT)</label>
                        <input type="file" class="form-control" name="file" accept=".txt" required>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">&nbsp;</label>
                        <button type="submit" class="btn btn-primary">Import</button>
                    </div>
                </div>
                <small class="text-muted">One account per line (e.g. Username:user | Password:pass).</small>
            </form>
        </div>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="get" action="{{ route('backend.products.accounts', $product->id) }}" class="table-search-form">
                <div class="table-search">
                    <input type="text" name="search" class="search-input" placeholder="Search by account details..." value="{{ request('search') }}">
                    <i class="las la-search"></i>
                </div>
                <button type="submit" class="btn btn-secondary search-btn">Search</button>
                @if(request()->filled('search'))
                    <a href="{{ route('backend.products.accounts', $product->id) }}" class="btn btn-outline">Clear</a>
                @endif
            </form>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Details</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($accounts as $a)
                    <tr>
                        <td>{{ $accounts->firstItem() + $loop->index }}</td>
                        <td><code style="word-break:break-all;">{{ Str::limit($a->details, 80) }}</code></td>
                        <td>{{ $a->is_sold ? 'Sold' : 'Unsold' }}</td>
                        <td>
                            @if(!$a->is_sold)
                            <div class="table-actions">
                                <button type="button" class="table-btn edit-account" data-id="{{ $a->id }}" data-details="{{ e($a->details) }}" title="Edit"><i class="las la-pen"></i></button>
                                <form action="{{ route('backend.products.account.destroy', $a->id) }}" method="post" class="d-inline" onsubmit="return confirm('Delete this account?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="table-btn" title="Delete"><i class="las la-trash"></i></button>
                                </form>
                            </div>
                            @else
                            —
                            @endif
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="4" class="text-center">No accounts. Import a TXT file above.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="pagination-wrapper">
            {{ $accounts->links('vendor.pagination.default') }}
        </div>
    </div>
</div>

<div class="modal" id="edit-account-modal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">Edit account</h3>
            <button class="close-modal"><i class="las la-times"></i></button>
        </div>
        <form action="{{ route('backend.products.account.update') }}" method="post" id="edit-account-form">
            @csrf
            @method('PUT')
            <input type="hidden" name="id" id="edit-account-id">
            <div class="modal-body">
                <div class="form-group mb-25">
                    <label class="form-label">Details<span class="required">*</span></label>
                    <textarea class="form-control" name="details" id="edit-account-details" rows="3" required></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="submit">Update</button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('script')
<script>
document.querySelectorAll('.edit-account').forEach(function(btn) {
    btn.addEventListener('click', function() {
        document.getElementById('edit-account-id').value = this.getAttribute('data-id');
        document.getElementById('edit-account-details').value = this.getAttribute('data-details') || '';
        document.getElementById('edit-account-modal').classList.add('active');
    });
});
</script>
@endpush
