<script data-cfasync="false" type="text/javascript">
    $(document).ready(function() {
        $('.update-passphrase').bind('click', function(e) {
            e.preventDefault();

            limbo = $(this).data('limbo');
            oldSelector = (limbo == 1 ? '#limbo-oldpassphrase' : '#oldpassphrase');
            newSelector = (limbo == 1 ? '#limbo-newpassphrase' : '#newpassphrase');
            confirmSelector = (limbo == 1 ? '#limbo-confirmpassphrase' : '#confirmpassphrase');

            oldpassphrase = $(oldSelector).val();
            newpassphrase = $(newSelector).val();
            confirmpassphrase = $(confirmSelector).val();

            if (oldpassphrase === '' || newpassphrase === '' || confirmpassphrase === '') {
                RichHTML.error(clientexec.lang('All fields must be filled in'));
            } else if (newpassphrase !== confirmpassphrase) {
                RichHTML.error(clientexec.lang('Your passphrases do not match!'));
            } else {
                var loadingMask = new RichHTML.window({});
                loadingMask.options.escClose = false;
                loadingMask.options.type = 'info';
                loadingMask.options.content = clientexec.lang('Currently resetting passphrases, please do not close this page.');
                loadingMask.options.hideTitle = true;
                loadingMask.options.hideButtons = true;
                loadingMask.show();

                RichHTML.center($('#'+loadingMask.id+' .window-description-elements'), $('#'+loadingMask.id+' .window-description form'));

                $.ajax({
                    url: 'index.php?fuse=admin&action=runchangepassphrasepopup&controller=index',
                    data: {
                        updatelimbo: limbo,
                        initial: '1',
                        oldpassphrase: oldpassphrase,
                        newpassphrase: newpassphrase
                    },
                    success: function(json) {
                        clientexec.msg(json.message);
                        window.setInterval(function() {
                            window.location.href = "index.php?fuse=admin&view=passphraseinfo&controller=settings&settings=security";
                        }, 3000);
                    }
                });
            }
        });

        $('#deletepassphrase').bind('click', function() {
            $.ajax({
                url: "index.php?fuse=admin&action=deletepassphraseinfo&controller=index",
                dataType: 'json',
                success: function(json) {
                    if (json.error) {
                        clientexec.msg(json.message);
                    } else {
                        window.location.href = "index.php?fuse=admin&view=passphraseinfo&controller=settings&settings=security";
                    }
                }
            });
            return false;
        });
    });
</script>


<div class="page-title">
    <h1><?php echo $this->user->lang("Security") . " " . $this->user->lang("Settings") . " - " . $this->user->lang("CC Passphrase"); ?></h1>
    <p class="alert-content"><?php echo $this->user->lang('Manage your credit card passphrase.'); ?></p>
</div>


<!-- ERROR MESSAGE -->
<?php if ($this->errorMsg != "") { ?>
    <div class="alert alert-danger text-center fw-bold"><?php echo $this->errorMsg; ?></div>
<?php } ?>

<!-- NO PASSPHRASE -->
<?php if (!$this->hasCCPassphrased) { ?>
    <div class="text-center fw-bold my-4">
        <?php echo $this->user->lang('You do not have any credit cards passphrased.'); ?>
    </div>
<?php } ?>

<!-- CURRENTLY RESETTING -->
<?php if ($this->resettingPassphrase) { ?>
    <div class="alert alert-warning">
        <?php echo $this->user->lang("You are currently resetting your passphrases, please be patient"); ?>
    </div>
<?php } ?>

<!-- LIMBO MODE -->
<?php if ($this->passphrasePopulated && $this->inlimbo) { ?>

    <h3><?php echo $this->user->lang('Reset Passphrase'); ?></h3>

    <div class="text-danger fw-bold">
        <?php echo $this->user->lang('You have some accounts that did not complete the latest passphrase update!'); ?>
    </div>

    <div class="fw-bold mb-3">
        <?php echo $this->user->lang('This could be caused by accidentally closing the status window before all accounts were updated.'); ?>
    </div>

    <div class="mb-3">
        <?php echo $this->user->lang('Please follow these steps'); ?><br>
        <?php echo $this->user->lang("1- Make sure 'Old Passphrase' contains the same value used in the previous update"); ?><br>
        <?php echo $this->user->lang("2- Make sure you also contain the same 'New Passphrase' used in your last update attempt"); ?>.<br><br>
        <span class="text-danger">
            <?php echo $this->user->lang('Performing this action could take some time, please be patient'); ?>
        </span>
    </div>

    <form autocomplete="off" class="mb-4">
        <div class="row mb-3">
            <label class="col-sm-3 col-form-label"><?php echo $this->user->lang('Old Passphrase'); ?>:</label>
            <div class="col-sm-4">
                <input id="limbo-oldpassphrase" type="password" class="form-control">
            </div>
        </div>

        <div class="row mb-3">
            <label class="col-sm-3 col-form-label"><?php echo $this->user->lang('New Passphrase'); ?>:</label>
            <div class="col-sm-4">
                <input id="limbo-newpassphrase" type="password" class="form-control">
            </div>
        </div>

        <div class="row mb-3">
            <label class="col-sm-3 col-form-label"><?php echo $this->user->lang('Confirm Passphrase'); ?>:</label>
            <div class="col-sm-4">
                <input id="limbo-confirmpassphrase" type="password" class="form-control">
            </div>
        </div>

        <button data-limbo="1" class="cm-btns orange update-passphrase">
            <?php echo $this->user->lang("Update All Accounts"); ?>
        </button>
    </form>

<?php } ?>

<!-- NORMAL RESET MODE -->
<?php if ($this->passphrasePopulated && !$this->inlimbo) { ?>

    <h3><?php echo $this->user->lang('Reset Passphrase'); ?></h3>

    <div class="mb-3">
        <?php echo $this->user->lang("Encrypt your client's credit card numbers using a new passphrase."); ?><br>
        <span class="text-danger">
            <?php echo $this->user->lang('Performing this action could take some time, please be patient'); ?><br>
            <?php echo $this->user->lang('Backup your database before proceeding.'); ?>
        </span>
    </div>

    <form autocomplete="off" class="mb-4">

        <div class="row mb-3">
            <label class="col-sm-3 col-form-label"><?php echo $this->user->lang('Old Passphrase'); ?>:</label>
            <div class="col-sm-4">
                <input id="oldpassphrase" type="password" class="form-control">
            </div>
        </div>

        <div class="row mb-3">
            <label class="col-sm-3 col-form-label"><?php echo $this->user->lang('New Passphrase'); ?>:</label>
            <div class="col-sm-4">
                <input id="newpassphrase" type="password" class="form-control">
            </div>
        </div>

        <div class="row mb-3">
            <label class="col-sm-3 col-form-label"><?php echo $this->user->lang('Confirm Passphrase'); ?>:</label>
            <div class="col-sm-4">
                <input id="confirmpassphrase" type="password" class="form-control">
            </div>
        </div>

        <button data-limbo="0" class="cm-btns orange update-passphrase">
            <?php echo $this->user->lang("Update All Accounts"); ?>
        </button>

    </form>

<?php } ?>

<!-- DELETE PASSPHRASE -->
<?php if ($this->showDeleteButtons) { ?>

    <h3><?php echo $this->user->lang('Delete Passphrase'); ?></h3>

    <div class="mb-3">
        <?php echo $this->user->lang("Delete the passphrase that was used to further encrypt your client's credit cards."); ?><br>
        <span class="text-danger">
            <?php echo $this->user->lang("Warning: Performing this action will require that your clients re-enter their credit card numbers"); ?>
        </span>
    </div>

    <button id="deletepassphrase" class="cm-btns red">
        <?php echo $this->user->lang("Delete Passphrase"); ?>
    </button>

<?php } ?>
