{include file="app/manage/view/index/emlog_nav"}

<h1 class="page-title">
    <i class="fas fa-user-edit"></i>
    修改资料
</h1>

<div class="row">
    <div class="col-lg-8">
        <div class="card shadow mb-4">
            <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-primary">个人资料设置</h6>
            </div>
            <div class="card-body">
                <form id="profile-form" action="index.php?r=manage/manage/profile" method="post">
                    <div class="form-group">
                        <label for="username" class="font-weight-bold">登录账号 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="username" name="username" placeholder="登录账号" value="<?php echo isset($ret['username']) ? htmlspecialchars($ret['username']) : ''; ?>">
                    </div>

                    <div class="form-group">
                        <label for="nickname" class="font-weight-bold">对外昵称</label>
                        <input type="text" class="form-control" id="nickname" name="nickname" placeholder="前台发布文章显示的作者名" value="<?php echo isset($ret['nickname']) ? htmlspecialchars($ret['nickname']) : ''; ?>">
                        <small class="form-text text-muted">发布文章时默认调用此昵称作为作者名，可手动修改。</small>
                    </div>

                    <hr>

                    <h6 class="font-weight-bold text-primary mb-3"><i class="fas fa-key"></i> 修改密码（留空则不修改）</h6>

                    <div class="form-group">
                        <label for="oldpass" class="font-weight-bold">当前密码</label>
                        <input type="password" class="form-control" id="oldpass" name="oldpass" placeholder="请输入当前密码" autocomplete="off">
                    </div>
                    <div class="form-group">
                        <label for="newpass" class="font-weight-bold">新密码</label>
                        <input type="password" class="form-control" id="newpass" name="newpass" placeholder="请输入新密码" autocomplete="off">
                    </div>
                    <div class="form-group">
                        <label for="confirmpass" class="font-weight-bold">确认新密码</label>
                        <input type="password" class="form-control" id="confirmpass" name="confirmpass" placeholder="请再次输入新密码" autocomplete="off">
                    </div>

                    <hr>

                    <div class="form-group">
                        <label class="font-weight-bold d-block">头像</label>
                        <div class="d-flex align-items-center">
                            <div class="upload-preview mr-3" id="picPreview" onclick="upthis()" title="点击上传头像" style="width:96px;height:96px;border:2px dashed #d1d3e2;border-radius:50%;overflow:hidden;cursor:pointer;display:flex;align-items:center;justify-content:center;background:#f8f9fc;flex:0 0 auto;">
                                <?php if(!empty($ret['pic'])): ?>
                                <img src="<?php echo htmlspecialchars($ret['pic']); ?>" alt="头像" style="width:100%;height:100%;object-fit:cover;">
                                <?php else: ?>
                                <i class="fas fa-cloud-upload-alt text-muted" style="font-size:26px;"></i>
                                <?php endif; ?>
                            </div>
                            <div>
                                <button type="button" class="btn btn-outline-primary btn-sm" onclick="upthis()">上传头像</button>
                                <p class="form-text text-muted mb-0">建议尺寸 100×100，支持 jpg/png。</p>
                            </div>
                        </div>
                        <input type="text" class="form-control d-none" name="pic" id="tupian" value="<?php echo isset($ret['pic']) ? htmlspecialchars($ret['pic']) : ''; ?>">
                        <input type="file" name="file" id="up_tupian" class="d-none" onchange="Uploadthis('tupian','up_tupian')">
                    </div>

                    <div class="form-group mb-0 mt-4">
                        <button type="submit" class="btn btn-primary" id="save"><i class="fas fa-save mr-1"></i> 保存</button>
                        <a href="index.php?r=manage/index/index" class="btn btn-light ml-2">返回</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

{include file="app/manage/view/index/emlog_footer"}

<script>
// 头像上传类型：manageuser
window.ZhiCmsUploadType = 'manageuser';

function upthis() {
    document.getElementById('up_tupian').click();
}

// 头像上传成功后刷新预览并统一弹窗提示
function refreshAvatarPreview(url) {
    if (!url) return;
    document.getElementById('tupian').value = url;
    document.getElementById('picPreview').innerHTML = '<img src="'+url+'" alt="头像" style="width:100%;height:100%;object-fit:cover;">';
}

document.getElementById('up_tupian').addEventListener('change', function(){
    var fileInput = this;
    if (!fileInput.files || !fileInput.files[0]) return;
    var loading = (typeof layer !== 'undefined') ? layer.load(2) : null;
    var type = window.ZhiCmsUploadType || 'manageuser';
    zhicmsUpload(type, 'up_tupian', function(url){
        if (loading !== null) layer.close(loading);
        refreshAvatarPreview(url);
        if (window.showToast) showToast('头像上传成功', 'success');
    });
});

jQuery(function($){
    $('#profile-form').on('submit', function(e){
        e.preventDefault();
        var btn = $('#save'), text = btn.html();
        btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin mr-1"></i> 保存中...');
        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            data: $(this).serialize(),
            dataType: 'json',
            success: function(res){
                if(res && res.status == 'y'){
                    showToast(res.info || '保存成功', 'success');
                    setTimeout(function(){ window.location.href = 'index.php?r=manage/index/index'; }, 1500);
                } else {
                    showToast((res && res.info) || '保存失败', 'danger');
                    btn.prop('disabled', false).html(text);
                }
            },
            error: function(){
                showToast('请求异常，请重试', 'danger');
                btn.prop('disabled', false).html(text);
            }
        });
    });
});
</script>
