@extends('layouts.admin')

@section('content')
<div class="row">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header">
                <div class="col">
                    <h5 class="mb-0 h6">Send WhatsApp For</h5>
                </div>
            </div>      
            <form action="{{ route('admin.update-whatsapptemplate') }}" method="POST">
                @csrf
                <div class="card-body">
                    <table class="table aiz-table mb-0">
                        <thead>
                            <tr>
                                <th style="width: 50%;">Template Name</th>
                                <th style="width: 25%;">Template Type</th>
                                <th style="width: 25%;">Active</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($templateData as $key => $template)
                                @php
                                    $isUtility = in_array($template->identifier, config('whatsapp.utility_templates', []), true);
                                    $creditsPerMessage = $isUtility
                                        ? (int) config('whatsapp.credit_costs.utility', 1)
                                        : (int) config('whatsapp.credit_costs.marketing', 4);
                                @endphp
                                <tr>
                                    <td>{{ $template->label ?? ucfirst(str_replace('_', ' ', $template->identifier)) }}</td>
                                    <td>
                                        @if($isUtility)
                                            <span class="badge bg-success">Utility ({{ $creditsPerMessage }} credit)</span>
                                        @else
                                            <span class="badge bg-danger">Marketing ({{ $creditsPerMessage }} credits)</span>
                                        @endif
                                    </td>
                                    <td>
                                        <label class="aiz-switch aiz-switch-success mb-0">
                                        <input type="checkbox" name="is_active[]" value="{{ $template['identifier'] }}" {{ $template['is_active'] == 1 ? 'checked' : '' }}>
                                            <span></span>
                                        </label>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>

                    <hr class="my-4">
                    <h6 class="mb-2">POS Owner Copy</h6>
                    <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
                        <div>
                            <div class="fw-semibold">Send `pos_order_placement` copy to Site Owner</div>
                            <small class="text-muted">
                                Applies only for wholesale customers. Site owner number is taken from General Settings (`site_phone`).
                            </small>
                        </div>
                        <label class="aiz-switch aiz-switch-success mb-0">
                            <input type="checkbox" name="whatsapp_pos_owner_copy_enabled" value="1" {{ ($posOwnerCopyEnabled ?? false) ? 'checked' : '' }}>
                            <span></span>
                        </label>
                    </div>
                </div>
                <div class="form-group m-3 text-right">
                    <button type="submit" class="btn btn-sm btn-primary mr-4">Save Settings</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
