<?php viewLoad('public/header'); ?>
<style>
.profile-box { max-width: 420px; padding: 20px; }
.profile-banner { background: #fff3cd; color: #8a6d3b; padding: 10px 14px; border-radius: 4px; margin-bottom: 16px; font-size: 13px; }
.profile-field { margin-bottom: 14px; }
.profile-field label { display: block; margin-bottom: 4px; font-size: 13px; color: #666; }
.profile-tip { font-size: 12px; color: #999; margin: -8px 0 14px; }
.profile-msg { font-size: 13px; margin-bottom: 12px; }
.profile-msg.error { color: #d0312d; }
.profile-msg.success { color: #2e7d32; }
</style>

<div class="profile-box">
<?php if ($isSuperAdmin): ?>
    <div class="profile-banner"><?=lg('系统内置账号不支持修改资料')?></div>
<?php else: ?>
    <?php if ($passwordExpired): ?>
        <div class="profile-banner"><?=lg('您的密码已超过90天未修改，建议尽快修改密码')?></div>
    <?php endif; ?>
    <div id="profileMsg"></div>
    <div id="emailBox" class="profile-field"></div>
    <div id="mobileBox" class="profile-field"></div>
    <div id="passwordBox" class="profile-field"></div>
    <div id="passwordConfirmBox" class="profile-field"></div>
    <div class="profile-tip"><?=lg('密码必须大于8位，且包含大写字母、小写字母、数字、特殊符号；不修改密码留空即可')?></div>
    <div id="submitBtn"></div>
<?php endif; ?>
</div>

<?php if (!$isSuperAdmin): ?>
<script>
$(function () {
    var email = <?=json_encode($admin['admin_email'] ?? '')?>;
    var mobile = <?=json_encode($admin['admin_mobile'] ?? '')?>;
    var password = '';
    var passwordConfirm = '';

    $('#emailBox').dxTextBox({ label: <?=json_encode(lg('邮箱'))?>, value: email, onValueChanged: function (e) { email = e.value; } });
    $('#mobileBox').dxTextBox({ label: <?=json_encode(lg('手机号'))?>, value: mobile, onValueChanged: function (e) { mobile = e.value; } });
    $('#passwordBox').dxTextBox({ label: <?=json_encode(lg('新密码'))?>, mode: 'password', onValueChanged: function (e) { password = e.value; } });
    $('#passwordConfirmBox').dxTextBox({ label: <?=json_encode(lg('确认新密码'))?>, mode: 'password', onValueChanged: function (e) { passwordConfirm = e.value; } });

    $('#submitBtn').dxButton({
        text: <?=json_encode(lg('保存'))?>,
        type: 'default',
        onClick: function () {
            var $msg = $('#profileMsg');
            $.ajax({
                url: APP.HOST + '/profile/update',
                method: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ email: email, mobile: mobile, password: password, passwordConfirm: passwordConfirm })
            }).then(function (res) {
                if (res.code === 0) {
                    $msg.attr('class', 'profile-msg success').text(<?=json_encode(lg('保存成功'))?>);
                    $('#passwordBox').dxTextBox('instance').option('value', '');
                    $('#passwordConfirmBox').dxTextBox('instance').option('value', '');
                } else {
                    $msg.attr('class', 'profile-msg error').text(res.msg);
                }
            });
        }
    });
});
</script>
<?php endif; ?>

<?php viewLoad('public/footer'); ?>
