<?php echo message_box('success') ?>
<?php echo message_box('error') ?>

<?php
$email_error = $this->session->userdata('email_error');
?>

<div class="space-y-6">
    <?php if (!empty($email_error)) { ?>
        <div class="rounded-2xl border border-amber-200 bg-amber-50 p-4 text-sm text-amber-900" id="copyright-wrap">
            <div class="flex items-start justify-between gap-3">
                <div>
                    <p class="font-semibold">Not Connected . Please Follow The instruction !</p>
                    <pre class="mt-2 whitespace-pre-wrap text-xs text-amber-800"><?= $email_error ?></pre>
                </div>
                <button type="button" class="text-amber-700" data-target="#copyright-wrap" data-dismiss="alert" aria-label="<?= lang('close') ?>">&times;</button>
            </div>
        </div>
    <?php } ?>

    <form method="post" action="<?php echo base_url() ?>admin/settings/update_email" class="space-y-6">
        <div class="rounded-2xl border border-border bg-card shadow-sm">
            <div class="flex items-center justify-between border-b border-border px-6 py-4">
                <div>
                    <h3 class="text-lg font-semibold text-foreground"><?= lang('email_settings') ?></h3>
                    <p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('email_settings') ?></p>
                </div>
                <button type="submit" class="inline-flex items-center justify-center gap-2 rounded-lg bg-primary px-6 py-2.5 text-sm font-medium text-primary-foreground shadow-sm hover:bg-primary/90 transition-colors">
                    <i data-lucide="save" class="h-4 w-4"></i>
                    <?= lang('save_changes') ?>
                </button>
            </div>
            <div class="p-6">
                <input type="hidden" name="settings" value="<?= $load_setting ?>">

                <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                    <div class="rounded-xl border border-border bg-card p-5 space-y-4">
                        <div class="flex items-center gap-2 border-b border-border pb-3">
                            <i data-lucide="mail" class="h-5 w-5"></i>
                            <h4 class="text-base font-semibold text-foreground"><?= lang('company_email') ?></h4>
                        </div>
                        <?= form_input_group('company_email', lang('company_email'), $this->config->item('company_email'), 'email', true, array('data-type' => 'email')) ?>
                        <?= form_checkbox('use_postmark', lang('use_postmark'), config_item('use_postmark') == 'TRUE', false, array('id' => 'use_postmark', 'value' => 'TRUE')) ?>

                        <div id="postmark_config" class="space-y-4">
                            <?= form_input_group('postmark_api_key', lang('postmark_api_key'), config_item('postmark_api_key')) ?>
                            <?= form_input_group('postmark_from_address', lang('postmark_from_address'), config_item('postmark_from_address'), 'email') ?>
                        </div>
                    </div>

                    <div class="rounded-xl border border-border bg-card p-5 space-y-4">
                        <div class="flex items-center gap-2 border-b border-border pb-3">
                            <i data-lucide="settings" class="h-5 w-5"></i>
                            <h4 class="text-base font-semibold text-foreground"><?= lang('email_protocol') ?></h4>
                        </div>
                        <?php $prot = config_item('protocol'); ?>
                        <?php
                        $protocol_options = array(
                            'mail' => lang('php_mail'),
                            'smtp' => lang('smtp'),
                            'sendmail' => lang('sendmail'),
                        );
                        ?>
                        <?= form_select_group('protocol', lang('email_protocol'), $protocol_options, $prot, true, array('id' => 'protocol')) ?>

                        <div id="smtp_config" class="space-y-4">
                            <?= form_input_group('smtp_host', lang('smtp_host'), $this->config->item('smtp_host')) ?>
                            <p class="text-xs text-muted-foreground">SMTP Server Address.</p>
                            <?= form_input_group('smtp_user', lang('smtp_user'), $this->config->item('smtp_user')) ?>
                            <?php $password = strlen(decrypt(config_item('smtp_pass'))); ?>
                            <div class="space-y-1">
                                <label class="text-sm font-medium text-foreground"><?= lang('smtp_pass') ?></label>
                                <div class="flex items-center gap-2">
                                    <input type="password" name="smtp_pass" placeholder="<?php
                                    if (!empty($password)) {
                                        for ($p = 1; $p <= $password; $p++) {
                                            echo '*';
                                        }
                                    }
                                    ?>" value="" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-secondary/30">

									<?= btn_modal(
												'admin/client/see_password/smtp',
												lang('see_password'),
												lang('see_password'),
												'',
												'',
												array('class' => 'inline-flex h-10 shrink-0 items-center whitespace-nowrap rounded-lg border border-border px-3 text-xs font-semibold text-foreground hover:bg-muted')
											) ?>
                                </div>
                            </div>

                            <?= form_input_group('smtp_port', lang('smtp_port'), $this->config->item('smtp_port')) ?>
                            <?php
                            $crypt = config_item('smtp_encryption');
                            $encryption_options = array(
                                '' => lang('none'),
                                'ssl' => 'SSL',
                                'tls' => 'TLS',
                            );
                            ?>
                            <?= form_select_group('smtp_encryption', lang('email_encryption'), $encryption_options, $crypt) ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-6 py-4">
            <h3 class="text-lg font-semibold text-foreground"><?= lang('sent_test_email') ?></h3>
        </div>
        <form method="post" action="<?php echo base_url() ?>admin/settings/sent_test_email" class="p-6 space-y-4">
            <?= form_input_group('test_email', lang('email') . ' ' . lang('address'), '', 'email', true) ?>
            <div class="flex justify-end">
                <button type="submit" class="inline-flex items-center justify-center gap-2 rounded-lg bg-primary px-6 py-2.5 text-sm font-medium text-primary-foreground shadow-sm hover:bg-primary/90 transition-colors">
                    <i data-lucide="send" class="h-4 w-4"></i>
                    <?= lang('sent_test_email') ?>
                </button>
            </div>
        </form>
    </div>
</div>

<script>
    $(function () {
        function togglePostmark() {
            $('#postmark_config').toggle($('#use_postmark').is(':checked'));
        }
        function toggleSmtp() {
            $('#smtp_config').toggle($('#protocol').val() === 'smtp');
        }
        togglePostmark();
        toggleSmtp();
        $('#use_postmark').on('change', togglePostmark);
        $('#protocol').on('change', toggleSmtp);
    });
</script>
