@extends('layouts.app')

@section('title', __('whatsapp::lang.whatsapp') . ' — ' . __('whatsapp::lang.channels'))

@section('content')
<section class="content-header">
    <h1><i class="fab fa-whatsapp"></i> {{ __('whatsapp::lang.whatsapp') }}
        <small>{{ __('whatsapp::lang.channels') }}</small></h1>
</section>

<section class="content">
    @include('whatsapp::partials._nav')

    <div class="row">
        <div class="col-md-5">
            <div class="box box-primary">
                <div class="box-header with-border"><h3 class="box-title">{{ __('whatsapp::lang.add_channel') }}</h3></div>
                <form method="POST" action="{{ route('whatsapp.channels.store') }}">
                    @csrf
                    @include('whatsapp::channels._form', ['channel' => null, 'locations' => $locations])
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i> {{ __('whatsapp::lang.add_channel') }}</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="col-md-7">
            <div class="box box-default">
                <div class="box-header with-border"><h3 class="box-title">{{ __('whatsapp::lang.channels') }}</h3></div>
                <div class="box-body table-responsive">
                    <table class="table table-striped">
                        <thead><tr>
                            <th>{{ __('whatsapp::lang.channel_label') }}</th>
                            <th>{{ __('whatsapp::lang.driver') }}</th>
                            <th>{{ __('whatsapp::lang.status') }}</th>
                            <th>{{ __('whatsapp::lang.st_pending') }}</th>
                            <th></th>
                        </tr></thead>
                        <tbody>
                        @forelse($channels as $channel)
                            <tr>
                                <td>
                                    <strong>{{ $channel->label }}</strong>
                                    @if($channel->is_default)<span class="label label-info">default</span>@endif<br>
                                    <small class="text-muted">{{ $channel->display_number ?: '—' }}
                                        @if($channel->location_id) · {{ $locations[$channel->location_id] ?? ('loc#'.$channel->location_id) }}@endif
                                    </small>
                                </td>
                                <td>{{ $channel->driver === 'meta' ? 'Meta' : 'Web/QR' }}</td>
                                <td>
                                    @php $cls = ['active'=>'success','scanning'=>'warning','connecting'=>'warning','disconnected'=>'danger','disabled'=>'default'][$channel->status] ?? 'default'; @endphp
                                    <span class="label label-{{ $cls }}">{{ $channel->status_label }}</span>
                                </td>
                                <td>{{ $pending[$channel->id] ?? 0 }}</td>
                                <td class="text-right">
                                    @if($channel->driver === 'web')
                                        <a href="{{ route('whatsapp.channels.connect', $channel->id) }}" class="btn btn-xs btn-warning" title="{{ __('whatsapp::lang.connect') }}"><i class="fa fa-qrcode"></i></a>
                                    @endif
                                    <button class="btn btn-xs btn-success" data-toggle="modal" data-target="#test-{{ $channel->id }}" title="{{ __('whatsapp::lang.send_test') }}"><i class="fa fa-paper-plane"></i></button>
                                    <button class="btn btn-xs btn-default" data-toggle="modal" data-target="#edit-{{ $channel->id }}" title="Edit"><i class="fa fa-edit"></i></button>
                                    @if($channel->driver === 'web')
                                        <form method="POST" action="{{ route('whatsapp.channels.token', $channel->id) }}" style="display:inline;" onsubmit="return confirm('Regenerate bridge token? The Node bridge must be reconfigured.')">
                                            @csrf <button class="btn btn-xs btn-default" title="{{ __('whatsapp::lang.regenerate_token') }}"><i class="fa fa-key"></i></button>
                                        </form>
                                    @endif
                                    <form method="POST" action="{{ route('whatsapp.channels.destroy', $channel->id) }}" style="display:inline;" onsubmit="return confirm('Delete this channel?')">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-xs btn-danger"><i class="fa fa-trash"></i></button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-muted">No channels configured.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    {{-- Edit + Test modals --}}
    @foreach($channels as $channel)
        <div class="modal fade" id="edit-{{ $channel->id }}" tabindex="-1" role="dialog">
            <div class="modal-dialog"><div class="modal-content">
                <form method="POST" action="{{ route('whatsapp.channels.update', $channel->id) }}">
                    @csrf @method('PUT')
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">{{ __('whatsapp::lang.channel') }}: {{ $channel->label }}</h4>
                    </div>
                    <div class="modal-body">
                        @include('whatsapp::channels._form', ['channel' => $channel, 'locations' => $locations])
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">{{ __('whatsapp::lang.saved') }}</button>
                    </div>
                </form>
            </div></div>
        </div>

        <div class="modal fade" id="test-{{ $channel->id }}" tabindex="-1" role="dialog">
            <div class="modal-dialog"><div class="modal-content">
                <form method="POST" action="{{ route('whatsapp.channels.test', $channel->id) }}">
                    @csrf
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">{{ __('whatsapp::lang.send_test') }} — {{ $channel->label }}</h4>
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label>{{ __('whatsapp::lang.to') }}</label>
                            <input type="text" name="to" class="form-control" placeholder="+20100..." required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-success"><i class="fa fa-paper-plane"></i> {{ __('whatsapp::lang.send_test') }}</button>
                    </div>
                </form>
            </div></div>
        </div>
    @endforeach
</section>
@endsection
