<?php
$to_options = array('' => '');
if (!empty($get_user_info)) {
    foreach ($get_user_info as $v_user_info) {
        $user = $this->mailbox_model->check_by(array('user_id' => $v_user_info->user_id), 'tbl_account_details');
        if (!empty($user)) {
            if ($v_user_info->role_id == 1) {
                $role = lang('admin');
            } elseif ($v_user_info->role_id == 3) {
                $role = lang('staff');
            } else {
                $role = lang('client');
            }
            $to_options[$v_user_info->email] = $user->fullname . ' (' . $role . ')';
        }
    }
}

$selected_to = array();
if (!empty($get_draft_info->to)) {
    $saved_email = unserialize($get_draft_info->to);
    foreach ($saved_email as $v_email) {
        if (!isset($to_options[$v_email])) {
            $to_options[$v_email] = $v_email;
        }
        $selected_to[] = $v_email;
    }
}
if (!empty($inbox_info)) {
    if (!isset($to_options[$inbox_info->from])) {
        $to_options[$inbox_info->from] = $inbox_info->from;
    }
    $selected_to[] = $inbox_info->from;
}

$subject_value = !empty($get_draft_info->subject) ? $get_draft_info->subject : '';
$body_value = !empty($get_draft_info->message_body) ? $get_draft_info->message_body : '';
?>

<form id="form" enctype="multipart/form-data" action="<?php echo base_url() ?>client/mailbox/send_mail/<?php
if (!empty($get_draft_info->draft_id)) {
    echo $get_draft_info->draft_id;
}
?>" method="post" class="space-y-6">
    <div class="rounded-2xl border border-border bg-card shadow-sm">
        <div class="border-b border-border px-5 py-4">
            <div class="flex flex-col gap-2 md:flex-row md:items-center md:justify-between">
                <div>
                    <h3 class="text-base font-semibold text-foreground"><?= lang('compose') ?></h3>
                    <p class="text-sm text-muted-foreground"><?= lang('new') . ' ' . lang('message') ?></p>
                </div>
                <div class="flex flex-wrap items-center gap-2">
                    <button name="draf" value="1" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-4 py-2 text-xs font-semibold text-foreground hover:bg-muted">
                        <i data-lucide="pencil" class="h-4 w-4"></i><?= lang('draft') ?>
                    </button>
                    <button type="submit" class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-4 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
                        <i data-lucide="send" class="h-4 w-4"></i><?= lang('send') ?>
                    </button>
                    <button onclick="return confirm('<?= lang('discard_msg') ?>')" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-4 py-2 text-xs font-semibold text-foreground hover:bg-muted" name="discard" value="2">
                        <i data-lucide="x" class="h-4 w-4"></i><?= lang('discard') ?>
                    </button>
                </div>
            </div>
        </div>
        <div class="p-5 space-y-5">
            <div class="grid grid-cols-1 gap-4 lg:grid-cols-2">
                <div class="lg:col-span-2">
                    <?= form_select_group('to[]', lang('to'), $to_options, $selected_to, true, array('id' => 'mail_to', 'multiple' => 'multiple', 'class' => 'select_2_to', 'data-placeholder' => lang('to'), 'data-tags' => 'true')); ?>
                </div>
                <div class="lg:col-span-2">
                    <?= form_input_group('subject', lang('subject'), $subject_value, 'text', true, array('placeholder' => lang('subject'))); ?>
                </div>
                <div class="lg:col-span-2">
                    <?= form_textarea_group('message_body', lang('message'), $body_value, 6, true, array('class' => 'textarea_')); ?>
                </div>
            </div>
            <div class="rounded-xl border border-dashed border-border bg-muted/20 p-4">
                <?= form_attachment_group('files[]', lang('attachment'), 'dropzone', [
                    'dropzone_id' => 'mailbox-files-dropzone',
                    'list_id' => 'mailbox-files-list',
                    'comment' => true
                ]); ?>
                <?php
                if (!empty($get_draft_info->attach_file)) {
                    $uploaded_file = !empty($get_draft_info->attach_file) ? json_decode($get_draft_info->attach_file) : array();
                }
                if (!empty($uploaded_file)) {
                    foreach ($uploaded_file as $v_files_image) { ?>
                        <div class="pull-left mt pr-lg mb" style="width:100px;">
                            <span data-dz-remove class="pull-right existing_image" style="cursor: pointer"><i data-lucide="x" class="h-4 w-4"></i></span>
                            <?php if ($v_files_image->is_image == 1) { ?>
                                <img data-dz-thumbnail src="<?php echo base_url() . $v_files_image->path ?>" class="upload-thumbnail-sm"/>
                            <?php } else { ?>
                                <span data-toggle="tooltip" data-placement="top" title="<?= $v_files_image->fileName ?>" class="mailbox-attachment-icon"><i data-lucide="file-text" class="h-4 w-4"></i></span>
                            <?php } ?>
                            <input type="hidden" name="path[]" value="<?php echo $v_files_image->path ?>">
                            <input type="hidden" name="fileName[]" value="<?php echo $v_files_image->fileName ?>">
                            <input type="hidden" name="fullPath[]" value="<?php echo $v_files_image->fullPath ?>">
                            <input type="hidden" name="size[]" value="<?php echo $v_files_image->size ?>">
                            <input type="hidden" name="is_image[]" value="<?php echo $v_files_image->is_image ?>">
                        </div>
                    <?php }; ?>
                <?php }; ?>
            </div>
        </div>
    </div>
</form>

<link href="<?php echo base_url() ?>asset/css/select2.css" rel="stylesheet"/>
<script src="<?php echo base_url() ?>asset/js/select2.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".existing_image").on("click", function () {
            $(this).parent().remove();
        });

        function initToSelect() {
            if (!$.fn.select2) return;
            var $to = $("#mail_to");
            if ($to.data("select2")) {
                $to.select2("destroy");
            }
            var wrapper = $to.next('[data-selectpicker-wrapper="true"]');
            if (wrapper.length) {
                wrapper.remove();
            }
            $to.removeAttr("data-enhanced");
            $to.removeClass("tw-selectpicker");
            $to.css({ display: "", visibility: "" });
            $to.select2({
                tags: true,
                tokenSeparators: [",", ";", " "],
                placeholder: "<?= lang('to') ?>",
                width: "100%",
                createTag: function (params) {
                    var term = $.trim(params.term || "");
                    if (term === "") {
                        return null;
                    }
                    return { id: term, text: term, newTag: true };
                }
            });
        }

        initToSelect();
        setTimeout(initToSelect, 0);
        setTimeout(initToSelect, 500);
        $(window).on("load", initToSelect);
        if (window.lucide && typeof window.lucide.createIcons === "function") {
            window.lucide.createIcons();
        }
    });
</script>

