@extends('layout')

@section('title', __('store.cancellation_title'))

@section('content')
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <x-clientarea.page-header
                icon="fas fa-times-circle"
                :title="__('store.cancellation_title')"
                :subtitle="$service->product->name ?? __('store.service_unknown_product')"
            >
                <a href="{{ route('store.auth.services.details', $service->id) }}" class="btn btn-outline-secondary">
                    <i class="fas fa-arrow-left me-2"></i>{{ __('store.back_to_services') }}
                </a>
            </x-clientarea.page-header>

            
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-clipboard-list me-2"></i>{{ __('store.cancellation_service_summary') }}
                    </h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <dl class="row mb-0">
                                <dt class="col-sm-5">{{ __('store.service_product_name') }}:</dt>
                                <dd class="col-sm-7">{{ $service->product->name ?? __('store.service_unknown_product') }}</dd>

                                @if($service->domain)
                                    <dt class="col-sm-5">{{ __('store.service_name') }}:</dt>
                                    <dd class="col-sm-7"><strong>{{ is_array($service->domain) ? implode(', ', $service->domain) : $service->domain }}</strong></dd>
                                @endif

                                <dt class="col-sm-5">{{ __('store.service_status') }}:</dt>
                                <dd class="col-sm-7">
                                    <span class="badge bg-{{ $service->status === 'active' ? 'success' : ($service->status === 'pending' ? 'warning' : ($service->status === 'suspended' ? 'danger' : 'secondary')) }}">
                                        {{ ucfirst($service->status) }}
                                    </span>
                                </dd>
                            </dl>
                        </div>
                        <div class="col-md-6">
                            <dl class="row mb-0">
                                <dt class="col-sm-5">{{ __('store.cancellation_billing_cycle') }}:</dt>
                                <dd class="col-sm-7">
                                    {{ __('store.cancellation_billing_' . ($billingInfo['billing_cycle'] ?? 'monthly')) }}
                                </dd>

                                @if($billingInfo['recurring_amount'] > 0)
                                    <dt class="col-sm-5">{{ __('store.cancellation_recurring_amount') }}:</dt>
                                    <dd class="col-sm-7">
                                        <strong>{{ number_format($billingInfo['recurring_amount'], 2) }}</strong>
                                    </dd>
                                @endif

                                @if($billingInfo['next_due_date'])
                                    <dt class="col-sm-5">{{ __('store.cancellation_next_due') }}:</dt>
                                    <dd class="col-sm-7">{{ \Carbon\Carbon::parse($billingInfo['next_due_date'])->format($dateFormat) }}</dd>
                                @endif

                                <dt class="col-sm-5">{{ __('store.cancellation_days_remaining') }}:</dt>
                                <dd class="col-sm-7">
                                    <span class="badge bg-info">
                                        {{ __('store.cancellation_days_remaining_text', ['days' => $billingInfo['days_remaining']]) }}
                                    </span>
                                </dd>
                            </dl>
                        </div>
                    </div>
                </div>
            </div>

            
            @if(session('success'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <i class="fas fa-check-circle me-2"></i>{{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @if(session('error'))
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <i class="fas fa-exclamation-circle me-2"></i>{{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            @if($service->pending_cancellation)
                
                <div class="card border-0 shadow-sm mb-4 border-start border-4 border-warning">
                    <div class="card-body text-center py-4">
                        <i class="fas fa-calendar-times fa-3x text-warning mb-3"></i>
                        <h4>{{ __('store.cancellation_scheduled_status') }}</h4>
                        <p class="text-muted mb-3">{{ __('store.cancellation_scheduled_description') }}</p>
                        @if($service->cancellation_date)
                            <div class="mb-3">
                                <span class="badge bg-warning fs-6">
                                    <i class="fas fa-calendar-check me-1"></i>{{ __('store.cancellation_scheduled_on', ['date' => \Carbon\Carbon::parse($service->cancellation_date)->format($dateFormat)]) }}
                                </span>
                            </div>
                        @endif
                        @if($scheduledCancellation)
                            <p class="text-muted small mb-1">
                                <strong>{{ __('store.cancellation_type') }}:</strong> {{ $scheduledCancellation->refund_type_text }}
                            </p>
                        @endif
                    </div>
                </div>
            @elseif($pendingRequest)
                
                <div class="card border-0 shadow-sm mb-4">
                    <div class="card-body text-center py-4">
                        <i class="fas fa-clock fa-3x text-warning mb-3"></i>
                        <h4>{{ __('store.cancellation_pending_status') }}</h4>
                        <p class="text-muted mb-3">{{ __('store.cancellation_pending_description') }}</p>

                        <div class="mb-3">
                            <span class="badge bg-warning fs-6">
                                <i class="fas fa-hourglass-half me-1"></i>{{ $pendingRequest->status_text }}
                            </span>
                        </div>

                        <p class="text-muted small mb-1">
                            <strong>{{ __('store.cancellation_reason') }}:</strong> {{ $pendingRequest->reason_text }}
                        </p>
                        <p class="text-muted small mb-1">
                            <strong>{{ __('store.cancellation_type') }}:</strong> {{ $pendingRequest->refund_type_text }}
                        </p>
                        @if($pendingRequest->description)
                            <p class="text-muted small mb-1">
                                <strong>{{ __('store.cancellation_details') }}:</strong> {{ $pendingRequest->description }}
                            </p>
                        @endif
                        <p class="text-muted small mb-3">
                            <i class="fas fa-calendar me-1"></i>
                            {{ $pendingRequest->created_at->format($dateTimeFormat) }}
                        </p>

                        <form action="{{ route('store.auth.services.cancel.withdraw', $pendingRequest->id) }}" method="POST">
                            @csrf
                            <button type="submit" class="btn btn-outline-secondary">
                                <i class="fas fa-times me-1"></i>{{ __('store.cancellation_withdraw') }}
                            </button>
                        </form>
                    </div>
                </div>
            @else
                
                <div class="card border-0 shadow-sm mb-4 border-start border-4 border-danger">
                    <div class="card-body">
                        <h5 class="text-danger mb-2">
                            <i class="fas fa-exclamation-triangle me-2"></i>{{ __('store.cancellation_warning') }}
                        </h5>
                        <p class="text-muted mb-0">{{ __('store.cancellation_warning_immediate') }}</p>
                    </div>
                </div>

                
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <form action="{{ route('store.auth.services.cancel.store', $service->id) }}" method="POST">
                            @csrf

                            
                            <div class="mb-4">
                                <label class="form-label fw-bold">{{ __('store.cancellation_type') }}</label>
                                <div class="row">
                                    @foreach($refundTypes as $refundType)
                                        <div class="col-md-6 mb-2">
                                            <div class="form-check border rounded p-3">
                                                <input class="form-check-input" type="radio" name="refund_type" id="refund_type_{{ $refundType }}" value="{{ $refundType }}" {{ old('refund_type', $defaultRefundType ?? 'end_of_period') === $refundType ? 'checked' : '' }}>
                                                <label class="form-check-label w-100" for="refund_type_{{ $refundType }}">
                                                    @if($refundType === 'immediate_refund')
                                                        <strong><i class="fas fa-bolt text-danger me-1"></i>{{ __('store.cancellation_type_immediate') }}</strong>
                                                        <br><small class="text-muted">{{ __('store.cancellation_type_immediate_desc') }}</small>
                                                    @elseif($refundType === 'end_of_period')
                                                        <strong><i class="fas fa-calendar-check text-info me-1"></i>{{ __('store.cancellation_type_end_of_billing') }}</strong>
                                                        <br><small class="text-muted">
                                                            @if($billingInfo['next_due_date'])
                                                                {{ __('store.cancellation_type_end_period_desc', ['date' => \Carbon\Carbon::parse($billingInfo['next_due_date'])->format($dateFormat)]) }}
                                                            @else
                                                                {{ __('store.cancellation_type_end_of_billing_desc') }}
                                                            @endif
                                                        </small>
                                                    @else
                                                        <strong>{{ __('models.cancellation_request_refund_type_' . $refundType) }}</strong>
                                                    @endif
                                                </label>
                                            </div>
                                        </div>
                                    @endforeach
                                </div>
                                @error('refund_type')
                                    <div class="text-danger small mt-1">{{ $message }}</div>
                                @enderror
                            </div>

                            
                            <div class="mb-4">
                                <label for="reason" class="form-label fw-bold">{{ __('store.cancellation_reason') }} @if($reasonRequired ?? true)<span class="text-danger">*</span>@endif</label>
                                <select class="form-select @error('reason') is-invalid @enderror" id="reason" name="reason" @if($reasonRequired ?? true) required @endif>
                                    <option value="">{{ __('store.cancellation_reason_select') }}</option>
                                    @foreach($reasons as $reason)
                                        <option value="{{ $reason }}" {{ old('reason') === $reason ? 'selected' : '' }}>
                                            {{ __('models.cancellation_request_reason_' . $reason) }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('reason')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            
                            <div class="mb-4">
                                <label for="description" class="form-label fw-bold">{{ __('store.cancellation_details') }}</label>
                                <textarea
                                    class="form-control @error('description') is-invalid @enderror"
                                    id="description"
                                    name="description"
                                    rows="4"
                                    placeholder="{{ __('store.cancellation_details_placeholder') }}"
                                >{{ old('description') }}</textarea>
                                @error('description')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            
                            <div class="d-grid">
                                <button type="submit" class="btn btn-danger">
                                    <i class="fas fa-paper-plane me-1"></i>{{ __('store.cancellation_submit') }}
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
