<script data-cfasync="false">
    var oldstatus = <?php echo $this->oldstatus; ?>;
    var oldstatusAliasto = <?php echo $this->oldstatusAliasto; ?>;
    var oldgroup = <?php echo $this->oldgroup; ?>;
    var adminid = <?php echo $this->userId; ?>;
    var deleteId = <?php echo $this->userId; ?>;
</script>

<div class="cm-holder">
    <div class="alert alert-info d-flex align-items-center staff-profile-header">
        <div class="staff-profile-image me-3">
            <img class="ce-default-avatar-dark rounded-circle" src="<?php echo $this->profileurl; ?>" alt="Profile Image" />
        </div>
        <div class="staff-profile-label">
            <strong><?php echo $this->userName; ?></strong>&nbsp;(<?php echo $this->userId; ?>)<br />
            <span class="badge <?php echo ($this->statusenum == 1) ? "bg-success" : "bg-secondary"; ?>">
                <?php echo $this->statusname; ?>
            </span>
        </div>
    </div>

    <div class="form-wrapper">

        <ul class="nav nav-tabs mb-4">
            <li class="nav-item">
                <a class="nav-link active" href="#"><?php echo $this->user->lang("Profile"); ?></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="index.php?fuse=admin&view=staffsettings&controller=staff<?php echo ($this->backtolist) ? "&adminid=" . $this->userId : ""; ?>">
                    <?php echo $this->user->lang("Preferences"); ?>
                </a>
            </li>
        </ul>

        <form method="post" action="index.php?fuse=admin&action=updateadmin&controller=staff">
            <input type="hidden" name="id" value="<?php echo $this->userId; ?>">

            <div class="row g-4">
                <div class="col-12">
                    <div class="card profile-card h-100">
                        <div class="card-header">
                            <h5 class="mb-0"><?php echo $this->user->lang("Profile Details"); ?></h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <div class="form-elem">
                                        <label for="dropdown_adminstatus" class="form-label"><?php echo $this->user->lang("Status"); ?>:</label>
                                        <select
                                                id="dropdown_adminstatus"
                                                name="dropdown_adminstatus"
                                                class="form-select normalSelect2"
                                                <?php if (!$this->parseProfileHeader['editAdminStatus']) { ?>disabled<?php } ?>
                                        >
                                            <option value="1" <?php echo $this->activeSelected; ?>>
                                                <?php echo $this->user->lang("Active"); ?>
                                            </option>
                                            <option value="-1" <?php echo $this->inactiveSelected; ?>>
                                                <?php echo $this->user->lang("Inactive"); ?>
                                            </option>
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-6 mb-3">
                                    <div class="form-elem">
                                        <label for="dropdown_adminGroup" class="form-label"><?php echo $this->user->lang("Group"); ?>:</label>
                                        <select
                                                id="dropdown_adminGroup"
                                                name="dropdown_adminGroup"
                                                class="form-select normalSelect2"
                                                <?php if (!$this->parseProfileHeader['editAdminGroup']) { ?>disabled<?php } ?>
                                        >
                                            <?php foreach ($this->parseProfileHeader['adminGroupOption'] as $adminGroupOption) { ?>
                                                <option
                                                        value="<?php echo $adminGroupOption['adminGroupValue']; ?>"
                                                        <?php echo $adminGroupOption['adminGroupSelected']; ?>
                                                >
                                                    <?php echo $adminGroupOption['adminGroupName']; ?>
                                                </option>
                                            <?php } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <?php foreach ($this->parseCustomFields as $i => $customFields) {
                                    $NameAddId = trim($customFields['fieldId']);
                                    if ($NameAddId != 51 && $NameAddId != 52) { ?>
                                        <div class="col-md-6 mb-3">
                                            <div class="form-elem">
                                                <label class="form-label"><?php echo $customFields['rowLabel']; ?></label>

                                                <?php switch ($customFields['fieldType']) {
                                                    case 'LANGUAGE': ?>
                                                        <select name="CT_<?php echo $customFields['fieldId']; ?>" class="form-select searchSelect2">
                                                            <?php foreach ($customFields['languageOptions'] as $languageOptions) { ?>
                                                                <option
                                                                        value="<?php echo $languageOptions['typeLanguageOptionValue']; ?>"
                                                                        <?php echo $languageOptions['typeLanguageOptionSelected']; ?>
                                                                >
                                                                    <?php echo $languageOptions['typeLanguageOptionLabel']; ?>
                                                                </option>
                                                            <?php } ?>
                                                        </select>
                                                        <?php break;

                                                    case 'COUNTRY': ?>
                                                        <select name="CT_<?php echo $customFields['fieldId']; ?>" class="form-select searchSelect2">
                                                            <?php echo $customFields['countryOptions']; ?>
                                                        </select>
                                                        <?php break;

                                                    case 'TEXTAREA': ?>
                                                        <textarea
                                                                name="CT_<?php echo $customFields['fieldId']; ?>"
                                                                class="form-control"
                                                                rows="4"
                                                        ><?php echo $customFields['value']; ?></textarea>
                                                        <?php break;

                                                    case 'DROPDOWN': ?>
                                                        <select name="CT_<?php echo $customFields['fieldId']; ?>" class="form-select normalSelect2">
                                                            <?php foreach ($customFields['dropdownOptions'] as $dropdownOptions) { ?>
                                                                <option
                                                                        value="<?php echo $dropdownOptions['typeDropdownOptionValue']; ?>"
                                                                        <?php echo $dropdownOptions['typeDropdownOptionSelected']; ?>
                                                                >
                                                                    <?php echo $dropdownOptions['typeDropdownOptionLabel']; ?>
                                                                </option>
                                                            <?php } ?>
                                                        </select>
                                                        <?php break;

                                                    case 'DATE': ?>
                                                        <input
                                                                type="text"
                                                                class="form-control datepicker"
                                                                name="CT_<?php echo $customFields['fieldId']; ?>"
                                                                id="CT_<?php echo $customFields['fieldId']; ?>"
                                                                value="<?php echo $customFields['value']; ?>"
                                                        >
                                                        <?php break;

                                                    case 'YESNO': ?>
                                                        <select name="CT_<?php echo $customFields['fieldId']; ?>" class="form-select normalSelect2">
                                                            <option value="1" <?php echo $customFields['yesSelected']; ?>>
                                                                <?php echo $this->user->lang("Yes"); ?>
                                                            </option>
                                                            <option value="0" <?php echo $customFields['noSelected']; ?>>
                                                                <?php echo $this->user->lang("No"); ?>
                                                            </option>
                                                        </select>
                                                        <?php break;

                                                    default: ?>
                                                        <input
                                                                type="text"
                                                                class="form-control"
                                                                name="CT_<?php echo $customFields['fieldId']; ?>"
                                                                value="<?php echo $customFields['value']; ?>"
                                                        >
                                                        <?php break;
                                                } ?>

                                                <?php if ($customFields['parseFieldInformation']) { ?>
                                                    <img
                                                            id="fieldInformation_<?php echo $customFields['fieldId']; ?>"
                                                            class="ms-2"
                                                            src="../templates/admin/images/information-button.png"
                                                            alt="Info"
                                                    />
                                                <?php } ?>

                                                <?php if ($customFields['parseFieldRequired']) { ?>
                                                    <?php if ($customFields['parseEmailOriginalName']) { ?>
                                                        <input
                                                                type="hidden"
                                                                name="e_CT_<?php echo $customFields['fieldId']; ?>"
                                                                value="<?php echo $customFields['OriginalName']; ?> Format"
                                                        >
                                                    <?php } ?>
                                                    <input
                                                            type="hidden"
                                                            name="r_CT_<?php echo $customFields['fieldId']; ?>"
                                                            value="<?php echo $customFields['OriginalName']; ?>"
                                                    >
                                                <?php } ?>
                                            </div>
                                        </div>
                                    <?php }
                                } ?>
                            </div>

                            <div class="row">
                                <div class="col-12">
                                    <div class="form-elem">
                                        <label for="signature" class="form-label"><?php echo $this->user->lang("Signature"); ?></label>
                                        <textarea
                                                class="form-control"
                                                rows="8"
                                                name="signature"
                                                id="signature"
                                        ><?php echo $this->signature_value; ?></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <?php if ($this->twoFactorEnabled) {?>
                    <div class="col-12">
                        <div class="card profile-card">
                            <div class="card-header">
                                <h5><?php echo $this->user->lang("Two-Factor Authentication"); ?></h5>
                            </div>

                            <div class="card-body">
                                <div class="mb-3">
                                    <strong><?php echo $this->user->lang("Status"); ?>:</strong>
                                    <?php if ($this->userEnabled) { ?>
                                        <span class="badge bg-success">
                                        <?php echo $this->user->lang("Enabled"); ?>
                                    </span>
                                    <?php } else { ?>
                                        <span class="badge bg-secondary">
                                        <?php echo $this->user->lang("Disabled"); ?>
                                    </span>
                                    <?php } ?>
                                </div>

                                <?php if (!$this->userEnabled) { ?>
                                    <?php if ($this->showTwoFactorButton) { ?>
                                        <p class="text-muted mb-3">
                                            <?php echo $this->user->lang("Add an extra layer of security to your account by enabling two-factor authentication."); ?>
                                        </p>
                                        <button class="cm-btns orange" id="enable2FA">
                                            <?php echo $this->user->lang("Enable Two-Factor Authentication"); ?>
                                        </button>
                                    <?php } ?>
                                <?php } else { ?>
                                    <!--                                <div class="mb-3">-->
                                    <!--                                    <strong>--><?php //echo $this->user->lang("Method"); ?><!--:</strong>-->
                                    <!--                                    --><?php //echo $this->user->lang("Authenticator App (TOTP)"); ?>
                                    <!--                                </div>-->

                                    <div class="d-flex flex-wrap gap-2">
                                        <button class="cm-btns red" id="disable2FA">
                                            <?php echo $this->user->lang("Disable"); ?>
                                        </button>
                                    </div>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>

                <div class="col-12">
                    <div class="card profile-card">
                        <div class="card-header">
                            <h5 class="mb-0"><?php echo $this->user->lang("Security"); ?></h5>
                        </div>
                        <div class="card-body">
                            <div class="alert alert-warning">
                                <strong><?php echo $this->user->lang("Note"); ?>:</strong>
                                <?php echo $this->user->lang("Supply only if you want password changed"); ?>
                            </div>

                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <div class="form-elem">
                                        <label for="new_password" class="form-label">
                                            <?php echo $this->user->lang("New Password"); ?>
                                        </label>
                                        <input type="password" class="form-control" id="new_password" name="new_password" autocomplete="off">
                                    </div>
                                </div>

                                <div class="col-md-6 mb-3">
                                    <div class="form-elem">
                                        <label for="confirm_password" class="form-label">
                                            <?php echo $this->user->lang("Confirm password"); ?>
                                        </label>
                                        <input type="password" class="form-control" id="confirm_password" name="confirm_password" autocomplete="off">
                                    </div>
                                </div>
                            </div>

                            <div class="d-flex justify-content-end gap-2">
                                <button class="cm-btns orange me-2" type="submit">
                                    <?php echo $this->user->lang("Update"); ?>
                                </button>

                                <?php if ($this->parseDeleteAdminButtons) { ?>
                                    <button id="deleteStaffButton" class="cm-btns red" type="button">
                                        <?php echo $this->user->lang("Delete"); ?>
                                    </button>
                                <?php } ?>

                                <?php if ($this->user->hasPermission("admin_staff_view")) { ?>
                                <button class="cm-btns dark cancel-go-back" data-url="index.php?fuse=admin&controller=staff&view=adminlist">
                                    <?php echo $this->user->lang('Cancel'); ?>
                                </button>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

<script data-cfasync="false">
    $(document).ready(function() {
        $('#deleteStaffButton').on('click', function(e) {
            e.preventDefault();

            RichHTML.msgBox(clientexec.lang("Are you sure you want to delete this account? WARNING: All support ticket information for this user will be replaced to generic Tech Support"), {
                type: 'confirm'
            }, function(result) {
                if (result.btn == clientexec.lang('Yes')) {
                    $.post('index.php?fuse=admin&action=deleteadminaccount&controller=staff', { deleteid: deleteId }, function(data) {
                        window.location = 'index.php?fuse=admin&controller=staff&view=adminlist';
                    });
                }
            });
        });

        $('#dropdown_adminstatus').change(function(e) {
            $.post('index.php?fuse=admin&action=updateadminstatus&controller=staff', {
                adminId: adminid,
                statusid: $(this).val()
            }, function(data) {
                clientexec.parseResponse(data);
            });
        });

        $('#dropdown_adminGroup').change(function(e) {
            $.post('index.php?fuse=admin&action=updateadmingroup&controller=staff', {
                adminId: adminid,
                groupid: $(this).val()
            }, function(data) {
                clientexec.parseResponse(data);
            });
        });

        let enableWindow = new RichHTML.window({
            url: 'index.php?fuse=admin&view=enableform&controller=twofactor',
            actionUrl: 'index.php?fuse=admin&action=verifyenable&controller=twofactor',
            showSubmit: true,
            title: clientexec.lang("Enable Two-Factor Authentication"),
            onSubmit: function (data) {
                jsonData = clientexec.parseResponse(data);
                if (jsonData.success == true) {
                    window.location.reload();
                }
            }
        });

        $('#enable2FA').on('click', function(e) {
            e.preventDefault();
            enableWindow.show();
        });

        $('#disable2FA').on('click', function(e) {
            e.preventDefault();

            RichHTML.msgBox(clientexec.lang('Are you sure you want to disable two factor authentication?'),
                {
                    type:"yesno"
                }, function(result) {
                    if (result.btn === clientexec.lang("Yes")) {
                        $.post("index.php?fuse=admin&controller=twofactor&action=disable&user_id=<?php echo $this->userId; ?>", { },
                            function(data){
                                jsonData = clientexec.parseResponse(data);
                                if (jsonData.success == true) {
                                    window.location.reload();
                                }
                            });
                    }
                });
        })
    });
</script>