@extends('admin.layouts.app')

@section('title', __('admin/ticket-departments.edit_title'))

@section('content')
<div class="container-fluid px-4">
    <x-admin.page-header
        :title="__('admin/ticket-departments.edit_title')"
        icon="fas fa-building"
        :buttons="[
            ['label' => __('admin/ticket-departments.buttons_cancel'), 'route' => route('admin.ticket-departments.index'), 'icon' => 'fas fa-arrow-left', 'color' => 'secondary'],
        ]"
    />

    <x-admin.alert />

    <x-admin.card>
        <form id="editTicketDepartmentForm" action="{{ route('admin.ticket-departments.update', $ticketDepartment) }}" method="POST">
            @csrf
            @method('PUT')

            <!-- Tab Navigation -->
            <ul class="nav nav-tabs" id="ticketDepartmentTabs" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="details-tab" data-bs-toggle="tab" data-bs-target="#details" type="button" role="tab">
                        <i class="fas fa-info-circle me-2"></i>{{ __('admin/ticket-departments.tabs_details') }}
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="custom-fields-tab" data-bs-toggle="tab" data-bs-target="#custom-fields" type="button" role="tab">
                        <i class="fas fa-list me-2"></i>{{ __('admin/ticket-departments.tabs_custom_fields') }}
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="mail-import-tab" data-bs-toggle="tab" data-bs-target="#mail-import" type="button" role="tab">
                        <i class="fas fa-envelope me-2"></i>{{ __('admin/ticket-departments.tabs_mail_import') }}
                    </button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="integrations-tab" data-bs-toggle="tab" data-bs-target="#integrations" type="button" role="tab">
                        <i class="fas fa-plug me-2"></i>{{ __('admin/ticket-departments.tabs_integrations') }}
                    </button>
                </li>
            </ul>

            <!-- Tab Content -->
            <div class="tab-content mt-4" id="ticketDepartmentTabsContent">
                <!-- Details Tab -->
                <div class="tab-pane fade show active" id="details" role="tabpanel">
                    <div class="row">
                        <div class="col-md-8">
                            <!-- Name Field -->
                            <x-translation-tabbed-group
                                :label="__('admin/ticket-departments.fields_name')"
                                field="name"
                                model="ticket_department"
                                :modelId="$ticketDepartment->id"
                                fieldName="name"
                                target="name"
                                name="name"
                                :value="old('name', $ticketDepartment->name)"
                                :required="true"
                                :placeholder="__('admin/ticket-departments.placeholders_name')" />

                            <!-- Description Field -->
                            <x-translation-tabbed-group
                                :label="__('admin/ticket-departments.fields_description')"
                                field="description"
                                model="ticket_department"
                                :modelId="$ticketDepartment->id"
                                fieldName="description"
                                target="description"
                                type="textarea"
                                name="description"
                                :value="old('description', $ticketDepartment->description)"
                                :rows="4"
                                :placeholder="__('admin/ticket-departments.placeholders_description')" />

                            <!-- Client Only Field -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_client_only') }}</label>
                                <div class="col-md-6">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="edit_client_only" name="client_only" value="1" {{ old('client_only', $ticketDepartment->client_only) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="edit_client_only">
                                            {{ __('admin/ticket-departments.fields_enable_client_only') }}
                                        </label>
                                    </div>
                                    <small class="form-text text-muted">{{ __('admin/ticket-departments.help_client_only') }}</small>
                                </div>
                            </div>

                            <!-- Accept Unregistered Emails -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_accept_unregistered_emails') }}</label>
                                <div class="col-md-6">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="edit_accept_unregistered_emails" name="accept_unregistered_emails" value="1" {{ old('accept_unregistered_emails', $ticketDepartment->accept_unregistered_emails) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="edit_accept_unregistered_emails">
                                            {{ __('admin/ticket-departments.fields_enable_accept_unregistered_emails') }}
                                        </label>
                                    </div>
                                    <small class="form-text text-muted">{{ __('admin/ticket-departments.help_accept_unregistered_emails') }}</small>
                                </div>
                            </div>

                            <!-- Hidden Field -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_hidden') }}</label>
                                <div class="col-md-6">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="edit_hidden" name="hidden" value="1" {{ old('hidden', $ticketDepartment->hidden) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="edit_hidden">
                                            {{ __('admin/ticket-departments.fields_enable_hidden') }}
                                        </label>
                                    </div>
                                    <small class="form-text text-muted">{{ __('admin/ticket-departments.help_hidden') }}</small>
                                </div>
                            </div>

                            <!-- SLA Plan -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/sla.plans_title') }}</label>
                                <div class="col-md-6">
                                    <select class="form-select" name="sla_plan_id">
                                        <option value="">-- {{ __('admin/common.none') }} --</option>
                                        @foreach(\App\Models\SlaPlan::active()->get() as $slaPlan)
                                            <option value="{{ $slaPlan->id }}" {{ old('sla_plan_id', $ticketDepartment->sla_plan_id) == $slaPlan->id ? 'selected' : '' }}>
                                                {{ $slaPlan->name }} ({{ $slaPlan->first_response_time }}m / {{ $slaPlan->resolution_time }}m)
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>

                            <!-- Status Field -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_status') }}</label>
                                <div class="col-md-6">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="edit_status" name="status" value="1" {{ old('status', $ticketDepartment->status) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="edit_status">
                                            {{ __('admin/ticket-departments.fields_active') }}
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <!-- Outgoing Email TO Address -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_outgoing_email_to') }}</label>
                                <div class="col-md-6">
                                    <input type="email" class="form-control" id="edit_outgoing_email_to" name="outgoing_email_to"
                                           value="{{ old('outgoing_email_to', $ticketDepartment->outgoing_email_to) }}"
                                           placeholder="{{ __('admin/ticket-departments.placeholders_outgoing_email_to') }}">
                                </div>
                            </div>

                            <!-- Outgoing Email TO Display Name -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_outgoing_email_name') }}</label>
                                <div class="col-md-6">
                                    <input type="text" class="form-control" id="edit_outgoing_email_name" name="outgoing_email_name"
                                           value="{{ old('outgoing_email_name', $ticketDepartment->outgoing_email_name) }}"
                                           placeholder="{{ __('admin/ticket-departments.placeholders_outgoing_email_name') }}">
                                </div>
                            </div>

                            <!-- No Auto Responder -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_no_auto_responder') }}</label>
                                <div class="col-md-6">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="edit_no_auto_responder" name="no_auto_responder" value="1"
                                               {{ old('no_auto_responder', $ticketDepartment->no_auto_responder) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="edit_no_auto_responder">
                                            {{ __('admin/ticket-departments.fields_disable_auto_responder') }}
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <!-- Have Feedback Request -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_have_feedback_request') }}</label>
                                <div class="col-md-6">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="edit_have_feedback_request" name="have_feedback_request" value="1"
                                               {{ old('have_feedback_request', $ticketDepartment->have_feedback_request) ? 'checked' : '' }}>
                                        <label class="form-check-label" for="edit_have_feedback_request">
                                            {{ __('admin/ticket-departments.fields_enable_feedback_request') }}
                                        </label>
                                    </div>
                                </div>
                            </div>

                            <!-- Feedback Request After Hours -->
                            <div class="row mb-3" id="feedback_after_hours_container" style="display: {{ old('have_feedback_request', $ticketDepartment->have_feedback_request) ? 'flex' : 'none' }};">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_feedback_after_hours') }}</label>
                                <div class="col-md-6">
                                    <div class="input-group">
                                        <input type="number" class="form-control" id="edit_feedback_after_hours" name="feedback_after_hours"
                                               value="{{ old('feedback_after_hours', $ticketDepartment->feedback_after_hours ?? 24) }}"
                                               min="1" max="720">
                                        <span class="input-group-text">{{ __('admin/ticket-departments.fields_hours') }}</span>
                                    </div>
                                    <small class="form-text text-muted">{{ __('admin/ticket-departments.help_feedback_after_hours') }}</small>
                                </div>
                            </div>

                            <!-- Assigned Admins -->
                            <div class="row mb-3">
                                <label class="col-md-3 col-form-label text-md-end">{{ __('admin/ticket-departments.fields_assigned_admins') }}</label>
                                <div class="col-md-6">
                                    <div class="border rounded p-3" style="max-height: 200px; overflow-y: auto;">
                                        @foreach($allAdmins as $admin)
                                            <div class="form-check mb-2">
                                                <input class="form-check-input" type="checkbox"
                                                       id="edit_admin_{{ $admin->id }}"
                                                       name="assigned_admins[]"
                                                       value="{{ $admin->id }}"
                                                       {{ in_array($admin->id, old('assigned_admins', $assignedAdminIds)) ? 'checked' : '' }}>
                                                <label class="form-check-label d-flex justify-content-between align-items-center w-100" for="edit_admin_{{ $admin->id }}">
                                                    <span>{{ $admin->name }} ({{ $admin->email }})</span>
                                                    <span class="badge bg-{{ $admin->status === 'active' ? 'success' : 'danger' }} ms-2">
                                                        {{ $admin->status === 'active' ? __('admin/common.status_active') : __('admin/common.status_inactive') }}
                                                    </span>
                                                </label>
                                            </div>
                                        @endforeach
                                    </div>
                                    <small class="form-text text-muted">{{ __('admin/ticket-departments.help_assigned_admins') }}</small>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Custom Fields Tab -->
                <div class="tab-pane fade" id="custom-fields" role="tabpanel">
                    <div class="row">
                        <div class="col-12">
                            <div id="customFieldsContainer">
                                <!-- Custom fields will be loaded here -->
                            </div>
                            <button type="button" class="btn btn-outline-primary" id="addCustomField">
                                <i class="fas fa-plus me-2"></i>{{ __('admin/ticket-departments.buttons_add_custom_field') }}
                            </button>
                        </div>
                    </div>
                </div>

                <!-- Mail Import Tab -->
                <div class="tab-pane fade" id="mail-import" role="tabpanel">
                    <div class="table-responsive">
                        <table class="table table-sm table-hover align-middle mb-0" id="mailboxesTable">
                            <thead>
                                <tr class="small text-uppercase text-muted">
                                    <th>{{ __('admin/ticket-departments.fields_label') }}</th>
                                    <th style="width: 80px;">{{ __('admin/ticket-departments.fields_mail_protocol') }}</th>
                                    <th>{{ __('admin/ticket-departments.fields_mail_email') }}</th>
                                    <th style="width: 50px;" class="text-center">SSL</th>
                                    <th style="width: 70px;" class="text-center">{{ __('admin/ticket-departments.fields_enabled') }}</th>
                                    <th style="width: 110px;">{{ __('admin/ticket-departments.fields_post_action') }}</th>
                                    <th style="width: 110px;" class="text-center">{{ __('admin/ticket-departments.fields_actions') }}</th>
                                </tr>
                            </thead>
                            <tbody id="mailboxesContainer">
                                @forelse($mailboxes as $i => $mb)
                                <tr class="mailbox-card" data-index="{{ $i }}">
                                    <td>
                                        <input type="hidden" name="mailboxes[{{ $i }}][id]" value="{{ $mb->id }}" class="mbx-id">
                                        <input type="hidden" name="mailboxes[{{ $i }}][label]" value="{{ $mb->label }}" class="mbx-label">
                                        <input type="hidden" name="mailboxes[{{ $i }}][protocol]" value="{{ $mb->protocol }}" class="mbx-protocol">
                                        <input type="hidden" name="mailboxes[{{ $i }}][hostname]" value="{{ $mb->hostname }}" class="mbx-hostname">
                                        <input type="hidden" name="mailboxes[{{ $i }}][port]" value="{{ $mb->port }}" class="mbx-port">
                                        <input type="hidden" name="mailboxes[{{ $i }}][email]" value="{{ $mb->email }}" class="mbx-email">
                                        <input type="hidden" name="mailboxes[{{ $i }}][password]" value="" class="mbx-password">
                                        <input type="hidden" name="mailboxes[{{ $i }}][post_action]" value="{{ $mb->post_action ?? 'mark_read' }}" class="mbx-postaction">
                                        <input type="checkbox" name="mailboxes[{{ $i }}][ssl]" class="mbx-ssl d-none" {{ $mb->ssl ? 'checked' : '' }}>
                                        <input type="checkbox" name="mailboxes[{{ $i }}][enabled]" class="mbx-enabled d-none" {{ $mb->enabled ? 'checked' : '' }}>
                                        <span class="fw-semibold mbx-label-text">{{ $mb->label }}</span>
                                    </td>
                                    <td><span class="badge bg-secondary mbx-protocol-text">{{ strtoupper($mb->protocol) }}</span></td>
                                    <td class="small mbx-email-text">{{ $mb->email }}</td>
                                    <td class="text-center mbx-ssl-text">@if($mb->ssl)<i class="fas fa-check text-success"></i>@else<i class="fas fa-minus text-muted"></i>@endif</td>
                                    <td class="text-center mbx-enabled-text">@if($mb->enabled)<span class="badge bg-success">{{ __('admin/ticket-departments.fields_enabled') }}</span>@else<span class="badge bg-secondary">—</span>@endif</td>
                                    <td><span class="badge bg-info mbx-postaction-text">{{ __('admin/ticket-departments.post_action_'.($mb->post_action ?? 'mark_read')) }}</span></td>
                                    <td class="text-center">
                                        <div class="btn-group btn-group-sm">
                                            <button type="button" class="btn btn-outline-primary edit-mailbox" title="{{ __('admin/ticket-departments.buttons_edit_mailbox') }}"><i class="fas fa-edit"></i></button>
                                            <button type="button" class="btn btn-outline-info test-mailbox-btn" title="{{ __('admin/ticket-departments.buttons_test_connection') }}"><i class="fas fa-plug"></i></button>
                                            <button type="button" class="btn btn-outline-danger remove-mailbox" title="{{ __('admin/ticket-departments.fields_actions') }}"><i class="fas fa-trash"></i></button>
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr id="noMailboxMessage"><td colspan="7" class="text-muted text-center py-3">{{ __('admin/ticket-departments.no_mailboxes') }}</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-2">
                        <button type="button" class="btn btn-outline-primary btn-sm" id="addMailboxBtn"
                                {{ ($mailboxes->count() ?? 0) >= 3 ? 'disabled' : '' }}>
                            <i class="fas fa-plus me-1"></i>{{ __('admin/ticket-departments.buttons_add_mailbox') }}
                            <small class="text-muted ms-1">({{ $mailboxes->count() ?? 0 }}/3)</small>
                        </button>
                    </div>
                </div>

                <!-- Integrations Tab -->
                <div class="tab-pane fade" id="integrations" role="tabpanel">
                    <p class="text-muted small mb-4">{{ __('admin/ticket-departments.integrations_intro') }}</p>

                    <div class="row g-3">
                        {{-- Slack --}}
                        <div class="col-md-6">
                            <div class="border rounded p-3 h-100">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <h6 class="fw-semibold mb-0"><i class="fab fa-slack text-primary me-2"></i>Slack</h6>
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" id="slack_enabled" name="slack_enabled" value="1" {{ old('slack_enabled', $ticketDepartment->slack_enabled) ? 'checked' : '' }}>
                                    </div>
                                </div>
                                <p class="text-muted small mb-2">{{ __('admin/ticket-departments.integrations_slack_desc') }}</p>
                                <label class="form-label small fw-semibold">{{ __('admin/ticket-departments.integrations_slack_prefix') }}</label>
                                <input type="text" class="form-control form-control-sm" name="slack_channel_prefix" value="{{ old('slack_channel_prefix', $ticketDepartment->slack_channel_prefix) }}" placeholder="ticket-">
                                <small class="text-muted">{{ __('admin/ticket-departments.integrations_slack_hint') }}</small>
                            </div>
                        </div>

                        {{-- Telegram --}}
                        <div class="col-md-6">
                            <div class="border rounded p-3 h-100">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <h6 class="fw-semibold mb-0"><i class="fab fa-telegram text-primary me-2"></i>Telegram</h6>
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" id="telegram_enabled" name="telegram_enabled" value="1" {{ old('telegram_enabled', $ticketDepartment->telegram_enabled) ? 'checked' : '' }}>
                                    </div>
                                </div>
                                <p class="text-muted small mb-2">{{ __('admin/ticket-departments.integrations_telegram_desc') }}</p>
                                <label class="form-label small fw-semibold">{{ __('admin/ticket-departments.integrations_telegram_chat_id') }}</label>
                                <input type="text" class="form-control form-control-sm" name="telegram_chat_id" value="{{ old('telegram_chat_id', $ticketDepartment->telegram_chat_id) }}" placeholder="-1001234567890">
                                <small class="text-muted">{{ __('admin/ticket-departments.integrations_telegram_hint') }}</small>
                            </div>
                        </div>

                        {{-- Discord --}}
                        <div class="col-md-6">
                            <div class="border rounded p-3 h-100">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <h6 class="fw-semibold mb-0"><i class="fab fa-discord text-primary me-2"></i>Discord</h6>
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" id="discord_enabled" name="discord_enabled" value="1" {{ old('discord_enabled', $ticketDepartment->discord_enabled) ? 'checked' : '' }}>
                                    </div>
                                </div>
                                <p class="text-muted small mb-2">{{ __('admin/ticket-departments.integrations_discord_desc') }}</p>
                                <label class="form-label small fw-semibold">{{ __('admin/ticket-departments.integrations_discord_webhook') }}</label>
                                <input type="url" class="form-control form-control-sm" name="discord_webhook_url" value="{{ old('discord_webhook_url', $ticketDepartment->discord_webhook_url) }}" placeholder="https://discord.com/api/webhooks/...">
                                <div class="form-check form-switch mt-2">
                                    <input class="form-check-input" type="checkbox" id="discord_use_forum_threads" name="discord_use_forum_threads" value="1" {{ old('discord_use_forum_threads', $ticketDepartment->discord_use_forum_threads) ? 'checked' : '' }}>
                                    <label class="form-check-label small" for="discord_use_forum_threads">
                                        {{ __('admin/ticket-departments.integrations_discord_forum_threads') }}
                                    </label>
                                </div>
                                <small class="text-muted d-block mt-1">{{ __('admin/ticket-departments.integrations_discord_hint') }}</small>
                            </div>
                        </div>

                        {{-- Microsoft Teams (PRC-782) --}}
                        <div class="col-md-6">
                            <div class="border rounded p-3 h-100">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <h6 class="fw-semibold mb-0"><i class="fab fa-microsoft text-primary me-2"></i>Microsoft Teams</h6>
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" id="teams_enabled" name="teams_enabled" value="1" {{ old('teams_enabled', $ticketDepartment->teams_enabled) ? 'checked' : '' }}>
                                    </div>
                                </div>
                                <p class="text-muted small mb-2">{{ __('admin/ticket-departments.integrations_teams_desc') }}</p>
                                <label class="form-label small fw-semibold">{{ __('admin/ticket-departments.integrations_teams_webhook') }}</label>
                                <input type="url" class="form-control form-control-sm" name="teams_webhook_url" value="{{ old('teams_webhook_url', $ticketDepartment->teams_webhook_url) }}" placeholder="https://prod-XX.westeurope.logic.azure.com:443/workflows/...">
                                <small class="text-muted d-block mt-1">{{ __('admin/ticket-departments.integrations_teams_hint') }}</small>
                            </div>
                        </div>

                        {{-- Pushover --}}
                        <div class="col-md-6">
                            <div class="border rounded p-3 h-100">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <h6 class="fw-semibold mb-0"><i class="fas fa-bell text-primary me-2"></i>Pushover</h6>
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" id="pushover_enabled" name="pushover_enabled" value="1" {{ old('pushover_enabled', $ticketDepartment->pushover_enabled) ? 'checked' : '' }}>
                                    </div>
                                </div>
                                <p class="text-muted small mb-2">{{ __('admin/ticket-departments.integrations_pushover_desc') }}</p>
                                <label class="form-label small fw-semibold">{{ __('admin/ticket-departments.integrations_pushover_key') }}</label>
                                <input type="text" class="form-control form-control-sm" name="pushover_user_key" value="{{ old('pushover_user_key', $ticketDepartment->pushover_user_key) }}" placeholder="u... or g...">
                                <small class="text-muted">{{ __('admin/ticket-departments.integrations_pushover_hint') }}</small>
                            </div>
                        </div>

                        {{-- WhatsApp --}}
                        <div class="col-md-6">
                            <div class="border rounded p-3 h-100">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <h6 class="fw-semibold mb-0"><i class="fab fa-whatsapp text-primary me-2"></i>WhatsApp</h6>
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" id="whatsapp_enabled" name="whatsapp_enabled" value="1" {{ old('whatsapp_enabled', $ticketDepartment->whatsapp_enabled) ? 'checked' : '' }}>
                                    </div>
                                </div>
                                <p class="text-muted small mb-2">{{ __('admin/ticket-departments.integrations_whatsapp_desc') }}</p>
                                <label class="form-label small fw-semibold">{{ __('admin/ticket-departments.integrations_whatsapp_phone') }}</label>
                                <input type="text" class="form-control form-control-sm" name="whatsapp_phone" value="{{ old('whatsapp_phone', $ticketDepartment->whatsapp_phone) }}" placeholder="+905551234567">
                                <small class="text-muted">{{ __('admin/ticket-departments.integrations_whatsapp_hint') }}</small>
                            </div>
                        </div>

                        {{-- SMS --}}
                        <div class="col-md-6">
                            <div class="border rounded p-3 h-100">
                                <div class="d-flex align-items-center justify-content-between mb-2">
                                    <h6 class="fw-semibold mb-0"><i class="fas fa-sms text-primary me-2"></i>SMS</h6>
                                    <div class="form-check form-switch mb-0">
                                        <input class="form-check-input" type="checkbox" id="sms_enabled" name="sms_enabled" value="1" {{ old('sms_enabled', $ticketDepartment->sms_enabled) ? 'checked' : '' }}>
                                    </div>
                                </div>
                                <p class="text-muted small mb-2">{{ __('admin/ticket-departments.integrations_sms_desc') }}</p>
                                <label class="form-label small fw-semibold">{{ __('admin/ticket-departments.integrations_sms_phones') }}</label>
                                <input type="text" class="form-control form-control-sm" name="sms_notification_phones" value="{{ old('sms_notification_phones', $ticketDepartment->sms_notification_phones) }}" placeholder="+905551234567, +905559876543">
                                <small class="text-muted">{{ __('admin/ticket-departments.integrations_sms_hint') }}</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        <x-slot:footer>
            <x-admin.form-actions :form-id="'editTicketDepartmentForm'" :submit-text="__('admin/ticket-departments.buttons_update')" :cancel-url="route('admin.ticket-departments.index')" />
        </x-slot:footer>
        </form>
    </x-admin.card>
</div>

<!-- Mail Connection Test Modal -->
<x-admin.modal id="mailTestModal" :title="__('admin/ticket-departments.mail_test_title')">
    <div id="mailTestResults">
        <div class="text-center">
            <div class="spinner-border" role="status">
                <span class="visually-hidden">{{ __('admin/ticket-departments.mail_test_testing') }}</span>
            </div>
            <p class="mt-2">{{ __('admin/ticket-departments.mail_test_testing') }}</p>
        </div>
    </div>
    <x-slot:footer>
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('admin/common.buttons_close') }}</button>
    </x-slot:footer>
</x-admin.modal>

{{-- PRC-598: Mailbox ekle/düzenle modal'ı (inline edit yerine) --}}
<x-admin.modal id="mailboxEditModal" :title="__('admin/ticket-departments.mailbox_modal_title')">
    <input type="hidden" id="mbm_index" value="">
    <div class="row g-3">
        <div class="col-md-6">
            <label class="form-label small">{{ __('admin/ticket-departments.fields_label') }}</label>
            <input type="text" class="form-control form-control-sm" id="mbm_label">
        </div>
        <div class="col-md-6">
            <label class="form-label small">{{ __('admin/ticket-departments.fields_mail_protocol') }}</label>
            <select class="form-select form-select-sm" id="mbm_protocol">
                <option value="imap">IMAP</option>
                <option value="pop3">POP3</option>
            </select>
        </div>
        <div class="col-md-8">
            <label class="form-label small">{{ __('admin/ticket-departments.fields_mail_hostname') }}</label>
            <input type="text" class="form-control form-control-sm" id="mbm_hostname" placeholder="mail.example.com">
        </div>
        <div class="col-md-4">
            <label class="form-label small">{{ __('admin/ticket-departments.fields_mail_port') }}</label>
            <input type="number" class="form-control form-control-sm" id="mbm_port" min="1" max="65535">
        </div>
        <div class="col-md-6">
            <label class="form-label small">{{ __('admin/ticket-departments.fields_mail_email') }}</label>
            <input type="email" class="form-control form-control-sm" id="mbm_email" placeholder="email">
        </div>
        <div class="col-md-6">
            <label class="form-label small">{{ __('admin/ticket-departments.fields_mail_password') }}</label>
            <input type="password" class="form-control form-control-sm" id="mbm_password" autocomplete="new-password">
            <small class="text-muted d-none" id="mbm_pw_hint">{{ __('admin/ticket-departments.mailbox_password_keep_hint') }}</small>
        </div>
        <div class="col-md-6">
            <label class="form-label small">{{ __('admin/ticket-departments.fields_post_action') }}</label>
            <select class="form-select form-select-sm" id="mbm_post_action">
                <option value="mark_read">{{ __('admin/ticket-departments.post_action_mark_read') }}</option>
                <option value="delete">{{ __('admin/ticket-departments.post_action_delete') }}</option>
                <option value="move">{{ __('admin/ticket-departments.post_action_move') }}</option>
            </select>
            <small class="text-muted">{{ __('admin/ticket-departments.fields_post_action_hint') }}</small>
        </div>
        <div class="col-md-6 d-flex align-items-end gap-4 pb-1">
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" id="mbm_ssl">
                <label class="form-check-label small" for="mbm_ssl">SSL</label>
            </div>
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" id="mbm_enabled">
                <label class="form-check-label small" for="mbm_enabled">{{ __('admin/ticket-departments.fields_enabled') }}</label>
            </div>
        </div>
    </div>
    <x-slot:footer>
        <button type="button" class="btn btn-outline-info btn-sm me-auto" id="mbm_test"><i class="fas fa-plug me-1"></i>{{ __('admin/ticket-departments.buttons_test_connection') }}</button>
        <button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">{{ __('admin/common.buttons_close') }}</button>
        <button type="button" class="btn btn-primary btn-sm" id="mbm_save"><i class="fas fa-check me-1"></i>{{ __('admin/common.buttons_save') }}</button>
    </x-slot:footer>
</x-admin.modal>
@endsection

@push('scripts')
<script>
    // JavaScript'e veri aktarımı
    window.ticketDepartment = @json($ticketDepartmentData);

    // PRC-598: mailbox modal özet etiketleri
    window.mbxLabels = {
        mark_read: @json(__('admin/ticket-departments.post_action_mark_read')),
        delete: @json(__('admin/ticket-departments.post_action_delete')),
        move: @json(__('admin/ticket-departments.post_action_move')),
        enabled: @json(__('admin/ticket-departments.fields_enabled')),
        host_email_required: @json(__('admin/ticket-departments.mailbox_host_email_required')),
    };



    // Route bilgileri
    window.routes = {
        ticketDepartments: {
            index: '{{ route("admin.ticket-departments.index") }}',
            update: '{{ route("admin.ticket-departments.update", $ticketDepartment) }}',
            testMailConnection: '{{ route("admin.ticket-departments.test-mail-connection") }}'
        }
    };

    // Çeviri bilgileri
    window.translations = {
        custom_fields: {
            field_name: '{{ __("admin/ticket-departments.fields_custom_field_name") }}',
            field_type: '{{ __("admin/ticket-departments.fields_custom_field_type") }}',
            required: '{{ __("admin/ticket-departments.fields_required") }}',
            options: '{{ __("admin/ticket-departments.fields_options") }}',
            add_option: '{{ __("admin/ticket-departments.buttons_add_option") }}',
            types: {
                text: '{{ __("admin/ticket-departments.custom_field_types_text") }}',
                textarea: '{{ __("admin/ticket-departments.custom_field_types_textarea") }}',
                select: '{{ __("admin/ticket-departments.custom_field_types_select") }}',
                checkbox: '{{ __("admin/ticket-departments.custom_field_types_checkbox") }}',
                radio: '{{ __("admin/ticket-departments.custom_field_types_radio") }}',
                email: '{{ __("admin/ticket-departments.custom_field_types_email") }}',
                number: '{{ __("admin/ticket-departments.custom_field_types_number") }}',
                date: '{{ __("admin/ticket-departments.custom_field_types_date") }}'
            }
        },
        buttons: {
            edit: '{{ __("admin/common.buttons_edit") }}',
            delete: '{{ __("admin/common.buttons_delete") }}'
        },
        messages: {
            updated: '{{ __("admin/ticket-departments.messages_updated") }}',
            error: '{{ __("admin/common.messages_error") }}'
        }
    };
</script>
@vite('resources/js/admin/ticket-departments/edit.js')
@endpush