@extends('admin.layouts.app')

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/escalation-rules.create_title')"
        :subtitle="__('admin/escalation-rules.create_subtitle')"
        icon="fas fa-level-up-alt"
        :buttons="[
            ['label' => __('admin/escalation-rules.buttons_back'), 'route' => route('admin.escalation-rules.index'), 'icon' => 'fas fa-arrow-left', 'color' => 'outline-secondary'],
        ]"
    />

    <x-admin.alert />

    <form id="escalationRuleForm" method="POST" action="{{ route('admin.escalation-rules.store') }}">
        @csrf

        <div class="row">
            <div class="col-lg-8">
                <x-admin.card :title="__('admin/escalation-rules.fields_name')">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_name" class="form-label">{{ __('admin/escalation-rules.fields_name') }} <span class="text-danger">*</span></label>
                                <input type="text" class="form-control @error('name') is-invalid @enderror"
                                       id="create_name" name="name" value="{{ old('name') }}" required>
                                @error('name')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="form-text text-muted">{{ __('admin/escalation-rules.help_name') }}</small>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label for="create_sort_order" class="form-label">{{ __('admin/escalation-rules.fields_sort_order') }}</label>
                                <input type="number" class="form-control @error('sort_order') is-invalid @enderror"
                                       id="create_sort_order" name="sort_order" value="{{ old('sort_order', 0) }}" min="0">
                                @error('sort_order')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="form-text text-muted">{{ __('admin/escalation-rules.help_sort_order') }}</small>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="mb-3">
                                <label for="create_status" class="form-label">{{ __('admin/escalation-rules.fields_status') }}</label>
                                <select class="form-select @error('status') is-invalid @enderror" id="create_status" name="status">
                                    <option value="1" @selected(old('status', '1') == '1')>{{ __('admin/escalation-rules.status_active') }}</option>
                                    <option value="0" @selected(old('status') == '0')>{{ __('admin/escalation-rules.status_inactive') }}</option>
                                </select>
                                @error('status')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label for="create_description" class="form-label">{{ __('admin/escalation-rules.fields_description') }}</label>
                        <textarea class="form-control @error('description') is-invalid @enderror"
                                  id="create_description" name="description" rows="3">{{ old('description') }}</textarea>
                        @error('description')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                        <small class="form-text text-muted">{{ __('admin/escalation-rules.help_description') }}</small>
                    </div>
                </x-admin.card>

                <x-admin.card :title="__('admin/escalation-rules.fields_conditions')">
                    <p class="text-muted mb-3">{{ __('admin/escalation-rules.help_conditions') }}</p>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_condition_department_id" class="form-label">{{ __('admin/escalation-rules.fields_condition_department') }}</label>
                                <select class="form-select @error('condition_department_id') is-invalid @enderror"
                                        id="create_condition_department_id" name="condition_department_id">
                                    <option value="">{{ __('admin/escalation-rules.fields_condition_department_placeholder') }}</option>
                                    @foreach($ticketDepartments as $department)
                                        <option value="{{ $department->id }}" @selected(old('condition_department_id') == $department->id)>
                                            {{ $department->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('condition_department_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_condition_status_id" class="form-label">{{ __('admin/escalation-rules.fields_condition_status') }}</label>
                                <select class="form-select @error('condition_status_id') is-invalid @enderror"
                                        id="create_condition_status_id" name="condition_status_id">
                                    <option value="">{{ __('admin/escalation-rules.fields_condition_status_placeholder') }}</option>
                                    @foreach($ticketStatuses as $status)
                                        <option value="{{ $status->id }}" @selected(old('condition_status_id') == $status->id)>
                                            {{ $status->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('condition_status_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_condition_priority" class="form-label">{{ __('admin/escalation-rules.fields_condition_priority') }}</label>
                                <select class="form-select @error('condition_priority') is-invalid @enderror"
                                        id="create_condition_priority" name="condition_priority">
                                    <option value="">{{ __('admin/escalation-rules.fields_condition_priority_placeholder') }}</option>
                                    @foreach($priorities as $priority)
                                        <option value="{{ $priority['value'] }}" @selected(old('condition_priority') == $priority['value'])>
                                            {{ $priority['label'] }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('condition_priority')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_condition_time_elapsed" class="form-label">{{ __('admin/escalation-rules.fields_condition_time_elapsed') }}</label>
                                <input type="number" class="form-control @error('condition_time_elapsed') is-invalid @enderror"
                                       id="create_condition_time_elapsed" name="condition_time_elapsed"
                                       value="{{ old('condition_time_elapsed') }}" min="0" step="0.5">
                                @error('condition_time_elapsed')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="form-text text-muted">{{ __('admin/escalation-rules.help_time_elapsed') }}</small>
                            </div>
                        </div>
                    </div>
                </x-admin.card>

                <x-admin.card :title="__('admin/escalation-rules.fields_actions_title')">
                    <p class="text-muted mb-3">{{ __('admin/escalation-rules.help_actions') }}</p>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_action_department_id" class="form-label">{{ __('admin/escalation-rules.fields_action_department') }}</label>
                                <select class="form-select @error('action_department_id') is-invalid @enderror"
                                        id="create_action_department_id" name="action_department_id">
                                    <option value="">{{ __('admin/escalation-rules.fields_action_department_placeholder') }}</option>
                                    @foreach($ticketDepartments as $department)
                                        <option value="{{ $department->id }}" @selected(old('action_department_id') == $department->id)>
                                            {{ $department->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('action_department_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_action_status_id" class="form-label">{{ __('admin/escalation-rules.fields_action_status') }}</label>
                                <select class="form-select @error('action_status_id') is-invalid @enderror"
                                        id="create_action_status_id" name="action_status_id">
                                    <option value="">{{ __('admin/escalation-rules.fields_action_status_placeholder') }}</option>
                                    @foreach($ticketStatuses as $status)
                                        <option value="{{ $status->id }}" @selected(old('action_status_id') == $status->id)>
                                            {{ $status->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('action_status_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_action_priority" class="form-label">{{ __('admin/escalation-rules.fields_action_priority') }}</label>
                                <select class="form-select @error('action_priority') is-invalid @enderror"
                                        id="create_action_priority" name="action_priority">
                                    <option value="">{{ __('admin/escalation-rules.fields_action_priority_placeholder') }}</option>
                                    @foreach($priorities as $priority)
                                        <option value="{{ $priority['value'] }}" @selected(old('action_priority') == $priority['value'])>
                                            {{ $priority['label'] }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('action_priority')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_action_flag_to_admin_id" class="form-label">{{ __('admin/escalation-rules.fields_action_flag_to_admin') }}</label>
                                <select class="form-select @error('action_flag_to_admin_id') is-invalid @enderror"
                                        id="create_action_flag_to_admin_id" name="action_flag_to_admin_id">
                                    <option value="">{{ __('admin/escalation-rules.fields_action_flag_to_admin_placeholder') }}</option>
                                    @foreach($admins as $admin)
                                        <option value="{{ $admin->id }}" @selected(old('action_flag_to_admin_id') == $admin->id)>
                                            {{ $admin->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('action_flag_to_admin_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        {{-- PRC-794 — assign_to_agent action --}}
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="create_action_assign_to_admin_id" class="form-label">{{ __('admin/escalation-rules.fields_action_assign_to_admin') }}</label>
                                <select class="form-select @error('action_assign_to_admin_id') is-invalid @enderror"
                                        id="create_action_assign_to_admin_id" name="action_assign_to_admin_id">
                                    <option value="">{{ __('admin/escalation-rules.fields_action_assign_to_admin_placeholder') }}</option>
                                    @foreach($admins as $admin)
                                        <option value="{{ $admin->id }}" @selected(old('action_assign_to_admin_id') == $admin->id)>
                                            {{ $admin->name }}
                                        </option>
                                    @endforeach
                                </select>
                                @error('action_assign_to_admin_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <div class="form-check">
                                    <input type="hidden" name="action_notify_customer" value="0">
                                    <input class="form-check-input @error('action_notify_customer') is-invalid @enderror"
                                           type="checkbox" id="create_action_notify_customer" name="action_notify_customer"
                                           value="1" {{ old('action_notify_customer') ? 'checked' : '' }}>
                                    <label class="form-check-label" for="create_action_notify_customer">
                                        {{ __('admin/escalation-rules.fields_action_notify_customer') }}
                                    </label>
                                    @error('action_notify_customer')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <div class="form-check">
                                    <input type="hidden" name="action_notify_admin" value="0">
                                    <input class="form-check-input @error('action_notify_admin') is-invalid @enderror"
                                           type="checkbox" id="create_action_notify_admin" name="action_notify_admin"
                                           value="1" {{ old('action_notify_admin') ? 'checked' : '' }}>
                                    <label class="form-check-label" for="create_action_notify_admin">
                                        {{ __('admin/escalation-rules.fields_action_notify_admin') }}
                                    </label>
                                    @error('action_notify_admin')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="mb-3">
                                <label for="create_action_auto_reply_message" class="form-label">{{ __('admin/escalation-rules.fields_action_auto_reply_message') }}</label>
                                <textarea class="form-control @error('action_auto_reply_message') is-invalid @enderror"
                                          id="create_action_auto_reply_message" name="action_auto_reply_message"
                                          rows="4">{{ old('action_auto_reply_message') }}</textarea>
                                @error('action_auto_reply_message')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                                <small class="form-text text-muted">{{ __('admin/escalation-rules.help_auto_reply_message') }}</small>
                            </div>
                        </div>
                    </div>
                </x-admin.card>
            </div>

            <div class="col-lg-4">
                <x-admin.card :title="__('admin/escalation-rules.fields_actions')">
                    <div class="d-grid gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save me-2"></i>{{ __('admin/escalation-rules.buttons_save') }}
                        </button>
                        <a href="{{ route('admin.escalation-rules.index') }}" class="btn btn-outline-secondary">
                            <i class="fas fa-times me-2"></i>{{ __('admin/escalation-rules.buttons_cancel') }}
                        </a>
                    </div>
                </x-admin.card>
            </div>
        </div>
    </form>
</div>
@endsection

@push('scripts')
@vite('resources/js/admin/escalation-rules/create.js')
@endpush