<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <h3 class="mb-4 fw-bold text-dark"><i class="bi bi-building me-2"></i> <?php echo __('company_profile'); ?></h3>
            
            <?php if ($message): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?php echo $message; ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
            <?php endif; ?>

            <div class="card shadow-sm border-0">
                <div class="card-header bg-white">
                    <ul class="nav nav-tabs card-header-tabs" id="settingsTabs" role="tablist">
                        <li class="nav-item" role="presentation">
                            <button class="nav-link active" id="general-tab" data-bs-toggle="tab" data-bs-target="#general" type="button" role="tab" aria-controls="general" aria-selected="true">
                                <i class="bi bi-info-circle me-2"></i><?php echo __('general_settings'); ?>
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="system-tab" data-bs-toggle="tab" data-bs-target="#system" type="button" role="tab" aria-controls="system" aria-selected="false">
                                <i class="bi bi-gear me-2"></i><?php echo __('system_settings'); ?>
                            </button>
                        </li>
                        <li class="nav-item" role="presentation">
                            <button class="nav-link" id="email-tab" data-bs-toggle="tab" data-bs-target="#email" type="button" role="tab" aria-controls="email" aria-selected="false">
                                <i class="bi bi-envelope me-2"></i><?php echo __('email_settings'); ?>
                            </button>
                        </li>
                    </ul>
                </div>
                <div class="card-body p-4">
                    <form action="settings.php" method="POST" enctype="multipart/form-data">
                        <div class="tab-content" id="settingsTabsContent">
                            
                            <!-- Tab 1: General Settings -->
                            <div class="tab-pane fade show active" id="general" role="tabpanel" aria-labelledby="general-tab">
                                <div class="row g-4">
                                    <div class="col-12 mb-2">
                                        <h6 class="fw-bold text-dark"><i class="bi bi-building me-2"></i> <?php echo __('company_details'); ?></h6>
                                    </div>
                                    <!-- Basic Information -->
                                    <div class="col-md-6">
                                        <label for="company_name" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('company_name'); ?></label>
                                        <input type="text" class="form-control" id="company_name" name="company_name" value="<?php echo htmlspecialchars($settings['company_name'] ?? ''); ?>" required>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="company_email" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('company_email'); ?></label>
                                        <input type="email" class="form-control" id="company_email" name="company_email" value="<?php echo htmlspecialchars($settings['company_email'] ?? ''); ?>">
                                    </div>

                                    <div class="col-md-6">
                                        <label for="company_phone" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('company_phone'); ?></label>
                                        <input type="text" class="form-control" id="company_phone" name="company_phone" value="<?php echo htmlspecialchars($settings['company_phone'] ?? ''); ?>">
                                    </div>
                                    <div class="col-md-6">
                                        <label for="company_address" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('company_address'); ?></label>
                                        <input type="text" class="form-control" id="company_address" name="company_address" value="<?php echo htmlspecialchars($settings['company_address'] ?? ''); ?>">
                                    </div>

                                    <!-- Registration Details -->
                                    <div class="col-md-4">
                                        <label for="company_ctr_no" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('ctr_no'); ?></label>
                                        <input type="text" class="form-control" id="company_ctr_no" name="company_ctr_no" value="<?php echo htmlspecialchars($settings['company_ctr_no'] ?? ''); ?>">
                                    </div>
                                    <div class="col-md-4">
                                        <label for="company_registration_no" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('registration_no'); ?></label>
                                        <input type="text" class="form-control" id="company_registration_no" name="company_registration_no" value="<?php echo htmlspecialchars($settings['company_registration_no'] ?? ''); ?>">
                                    </div>
                                    <div class="col-md-4">
                                        <label for="company_vat_no" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('vat_no'); ?></label>
                                        <input type="text" class="form-control" id="company_vat_no" name="company_vat_no" value="<?php echo htmlspecialchars($settings['company_vat_no'] ?? ''); ?>">
                                    </div>

                                    <!-- Timezone & Working Hours -->
                                    <div class="col-12 mt-4"><hr></div>
                                    <div class="col-md-4">
                                        <label for="timezone" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('timezone'); ?></label>
                                        <select class="form-select" id="timezone" name="timezone">
                                            <?php
                                            $timezones = DateTimeZone::listIdentifiers();
                                            $current_tz = $settings['timezone'] ?? 'UTC';
                                            foreach ($timezones as $tz) {
                                                $selected = ($tz === $current_tz) ? 'selected' : '';
                                                echo "<option value=\"{$tz}\" {$selected}>{$tz}</option>";
                                            }
                                            ?>
                                        </select>
                                    </div>
                                    <div class="col-md-4">
                                        <label for="working_hours_start" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('working_hours_start'); ?></label>
                                        <input type="time" class="form-control" id="working_hours_start" name="working_hours_start" value="<?php echo htmlspecialchars($settings['working_hours_start'] ?? '08:00'); ?>">
                                    </div>
                                    <div class="col-md-4">
                                        <label for="working_hours_end" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('working_hours_end'); ?></label>
                                        <input type="time" class="form-control" id="working_hours_end" name="working_hours_end" value="<?php echo htmlspecialchars($settings['working_hours_end'] ?? '17:00'); ?>">
                                    </div>
                                    
                                    <!-- Branding -->
                                    <div class="col-12 mt-4"><hr></div>
                                    <div class="col-md-6 mt-2">
                                        <label for="company_logo" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('company_logo'); ?></label>
                                        <?php if (!empty($settings['company_logo'])): ?>
                                        <div class="mb-2">
                                            <img src="<?php echo htmlspecialchars($settings['company_logo']); ?>" alt="Logo" class="img-thumbnail" style="max-height: 80px;">
                                        </div>
                                        <?php endif; ?>
                                        <input type="file" class="form-control" id="company_logo" name="company_logo" accept="image/*">
                                    </div>
                                    <div class="col-md-6 mt-2">
                                        <label for="company_favicon" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('company_favicon'); ?></label>
                                        <?php if (!empty($settings['company_favicon'])): ?>
                                        <div class="mb-2">
                                            <img src="<?php echo htmlspecialchars($settings['company_favicon']); ?>" alt="Favicon" class="img-thumbnail" style="max-height: 32px;">
                                        </div>
                                        <?php endif; ?>
                                        <input type="file" class="form-control" id="company_favicon" name="company_favicon" accept="image/x-icon,image/png">
                                    </div>
                                </div>
                            </div>

                            <!-- Tab 2: System Settings -->
                            <div class="tab-pane fade" id="system" role="tabpanel" aria-labelledby="system-tab">
                                <div class="row g-4">
                                    <!-- Visit Settings -->
                                    <div class="col-12 mb-2">
                                        <h6 class="fw-bold text-dark"><i class="bi bi-gear me-2"></i> <?php echo __('visit_settings'); ?></h6>
                                    </div>
                                    <div class="col-12">
                                        <div class="form-check form-switch">
                                            <input type="hidden" name="disable_visit_edit_24h" value="0">
                                            <input class="form-check-input" type="checkbox" id="disable_visit_edit_24h" name="disable_visit_edit_24h" value="1" <?php echo !empty($settings['disable_visit_edit_24h']) ? 'checked' : ''; ?>> 
                                            <label class="form-check-label" for="disable_visit_edit_24h"> <?php echo __('disable_visit_edit_24h'); ?></label>
                                            <div class="form-text text-muted"> <?php echo __('disable_visit_edit_24h_desc'); ?></div>
                                        </div>
                                    </div>

                                    <!-- Currency Settings -->
                                    <div class="col-12 mt-4"><hr></div>
                                    <div class="col-12 mb-2">
                                        <h6 class="fw-bold text-dark"><i class="bi bi-cash-coin me-2"></i> <?php echo __('currency_settings'); ?></h6>
                                    </div>
                                    <div class="col-md-6">
                                        <label for="currency_symbol" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('currency_symbol'); ?></label>
                                        <input type="text" class="form-control" id="currency_symbol" name="currency_symbol" value="<?php echo htmlspecialchars($settings['currency_symbol'] ?? '$'); ?>" placeholder="$">
                                    </div>
                                    <div class="col-md-6">
                                        <label for="decimal_digits" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('decimal_digits'); ?></label>
                                        <input type="number" class="form-control" id="decimal_digits" name="decimal_digits" value="<?php echo htmlspecialchars($settings['decimal_digits'] ?? '2'); ?>" min="0" max="4">
                                    </div>

                                    <!-- Pharmacy Settings -->
                                    <div class="col-12 mt-4"><hr></div>
                                    <div class="col-12 mb-2">
                                        <h6 class="fw-bold text-dark"><i class="bi bi-capsule me-2"></i> <?php echo __('pharmacy_settings'); ?></h6>
                                    </div>
                                    <div class="col-12">
                                        <div class="form-check form-switch">
                                            <input type="hidden" name="allow_negative_stock" value="0">
                                            <input class="form-check-input" type="checkbox" id="allow_negative_stock" name="allow_negative_stock" value="1" <?php echo !empty($settings['allow_negative_stock']) ? 'checked' : ''; ?>> 
                                            <label class="form-check-label" for="allow_negative_stock"> <?php echo __('allow_negative_stock'); ?></label>
                                            <div class="form-text text-muted"> <?php echo __('allow_negative_stock_desc'); ?></div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Tab 3: Email Settings -->
                            <div class="tab-pane fade" id="email" role="tabpanel" aria-labelledby="email-tab">
                                <div class="row g-4">
                                    <div class="col-12 mb-2">
                                        <h6 class="fw-bold text-dark"><i class="bi bi-envelope me-2"></i> <?php echo __('email_settings'); ?></h6>
                                        <div class="alert alert-info py-2 small">
                                            <i class="bi bi-info-circle me-1"></i> Use these settings to configure outgoing emails.
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-8">
                                        <label for="smtp_host" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('smtp_host'); ?></label>
                                        <input type="text" class="form-control" id="smtp_host" name="smtp_host" value="<?php echo htmlspecialchars($settings['smtp_host'] ?? ''); ?>" placeholder="smtp.example.com">
                                    </div>
                                    <div class="col-md-4">
                                        <label for="smtp_port" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('smtp_port'); ?></label>
                                        <input type="number" class="form-control" id="smtp_port" name="smtp_port" value="<?php echo htmlspecialchars($settings['smtp_port'] ?? '587'); ?>" placeholder="587">
                                    </div>

                                    <div class="col-md-6">
                                        <label for="smtp_user" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('smtp_user'); ?></label>
                                        <input type="text" class="form-control" id="smtp_user" name="smtp_user" value="<?php echo htmlspecialchars($settings['smtp_user'] ?? ''); ?>" placeholder="user@example.com">
                                    </div>
                                    <div class="col-md-6">
                                        <label for="smtp_pass" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('smtp_pass'); ?></label>
                                        <input type="password" class="form-control" id="smtp_pass" name="smtp_pass" value="<?php echo htmlspecialchars($settings['smtp_pass'] ?? ''); ?>" placeholder="********">
                                    </div>

                                    <div class="col-md-4">
                                        <label for="smtp_secure" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('smtp_secure'); ?></label>
                                        <select class="form-select" id="smtp_secure" name="smtp_secure">
                                            <option value="tls" <?php echo ($settings['smtp_secure'] ?? '') === 'tls' ? 'selected' : ''; ?>><?php echo __('tls'); ?></option>
                                            <option value="ssl" <?php echo ($settings['smtp_secure'] ?? '') === 'ssl' ? 'selected' : ''; ?>><?php echo __('ssl'); ?></option>
                                            <option value="" <?php echo ($settings['smtp_secure'] ?? '') === '' ? 'selected' : ''; ?>><?php echo __('none'); ?></option>
                                        </select>
                                    </div>

                                    <div class="col-12 mt-4"><hr></div>
                                    <div class="col-12 mb-2">
                                        <h6 class="fw-bold text-dark"><i class="bi bi-person-badge me-2"></i> <?php echo __('smtp_from_name'); ?> & <?php echo __('email'); ?></h6>
                                    </div>
                                    
                                    <div class="col-md-6">
                                        <label for="smtp_from_email" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('smtp_from_email'); ?></label>
                                        <input type="email" class="form-control" id="smtp_from_email" name="smtp_from_email" value="<?php echo htmlspecialchars($settings['smtp_from_email'] ?? ''); ?>" placeholder="noreply@example.com">
                                    </div>
                                    <div class="col-md-6">
                                        <label for="smtp_from_name" class="form-label fw-semibold text-muted small text-uppercase"> <?php echo __('smtp_from_name'); ?></label>
                                        <input type="text" class="form-control" id="smtp_from_name" name="smtp_from_name" value="<?php echo htmlspecialchars($settings['smtp_from_name'] ?? ''); ?>" placeholder="My Hospital">
                                    </div>

                                </div>
                            </div>
                        </div>

                        <div class="col-12 mt-5 text-end">
                            <button type="submit" name="submit" class="btn btn-primary btn-lg px-5">
                                <i class="bi bi-check2-circle me-2"></i> <?php echo __('save_changes'); ?>
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
