<template>
    <view class="container">
        <!-- 自定义头部 -->
        <view class="custom-header">
            <view class="header-left" @click="navBack">
                <image class="back-icon" src="/static/icons/chevron_right_dark.svg" style="transform: rotate(180deg);">
                </image>
            </view>
            <text class="header-title">账号与安全</text>
            <view class="header-right"></view>
        </view>
        <view class="header-placeholder"></view>
        <view class="section-title-security">安全设置</view>

        <view class="group-card">
            <view class="list-item" @click="changeMobile">
                <text class="item-title">手机号</text>
                <view class="item-right">
                    <text class="item-value">{{ maskPhone(phone) || '未设置' }}</text>
                    <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
                </view>
            </view>
            <view class="list-item" @click="changePassword">
                <text class="item-title">登录密码</text>
                <view class="item-right">
                    <text class="item-value">{{ hasPassword ? '已设置' : '未设置' }}</text>
                    <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
                </view>
            </view>
        </view>

        <view class="section-title-security">高级设置</view>
        <view class="group-card">
            <view class="list-item no-border" @click="deleteAccount">
                <text class="item-title">注销账号</text>
                <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
            </view>
        </view>
    </view>
</template>

<script>
import { getMyProfile, deleteAccount } from '@/api/fulfiller/fulfiller'

export default {
    data() {
        return {
            phone: '',
            hasPassword: false
        }
    },
    onLoad() {
        this.loadProfile()
    },
    methods: {
        navBack() {
            uni.navigateBack({
                delta: 1
            });
        },
        async loadProfile() {
            try {
                const res = await getMyProfile()
                if (res.code === 200 && res.data) {
                    this.phone = res.data.phone || ''
                    this.hasPassword = !!res.data.hasPassword
                }
            } catch (e) {
                console.error('加载个人信息失败', e)
                uni.showToast({ title: e.message || e.msg || '请求失败', icon: 'none' })
            }
        },
        maskPhone(phone) {
            if (!phone || phone.length < 11) return phone
            return phone.substring(0, 3) + '****' + phone.substring(7)
        },
        changeMobile() {
            uni.navigateTo({
                url: '/pages/mine/settings/security/change-phone/index'
            })
        },
        changePassword() {
            uni.navigateTo({
                url: '/pages/mine/settings/security/change-password/index'
            })
        },
        async deleteAccount() {
            uni.showModal({
                title: '警示',
                content: '注销账号后将无法恢复，确定要继续吗？',
                success: async (res) => {
                    if (res.confirm) {
                        try {
                            const result = await deleteAccount()
                            if (result.code === 200) {
                                uni.showToast({ title: '账号已注销', icon: 'success' })
                                setTimeout(() => {
                                    uni.reLaunch({ url: '/pages/login/index' })
                                }, 1500)
                            } else {
                                uni.showToast({ title: result.msg || '注销失败', icon: 'none' })
                            }
                        } catch (e) {
                            console.error('注销账号失败', e)
                            uni.showToast({ title: e.message || e.msg || '注销失败', icon: 'none' })
                        }
                    }
                }
            });
        }
    }
}
</script>

<style>
page {
    background-color: #F8F8F8;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

.custom-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 88rpx;
    padding-top: var(--status-bar-height);
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-left: 30rpx;
    padding-right: 30rpx;
    box-sizing: content-box;
    z-index: 100;
}

.header-placeholder {
    height: calc(88rpx + var(--status-bar-height));
}

.back-icon {
    width: 40rpx;
    height: 40rpx;
}

.header-title {
    font-size: 28rpx;
    font-weight: bold;
    color: #333;
}

.header-right {
    width: 40rpx;
}

.container {
    padding: 20rpx 30rpx;
}

.section-title-security {
    font-size: 24rpx;
    color: #999;
    margin-bottom: 20rpx;
    margin-top: 20rpx;
    padding-left: 10rpx;
}

.group-card {
    background-color: #fff;
    border-radius: 20rpx;
    padding: 0 30rpx;
    margin-bottom: 30rpx;
}

.list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100rpx;
    border-bottom: 1px solid #F5F5F5;
}

.list-item.no-border {
    border-bottom: none;
}

.item-title {
    font-size: 28rpx;
    color: #333;
}

.item-right {
    display: flex;
    align-items: center;
}

.arrow-icon {
    width: 28rpx;
    height: 28rpx;
    opacity: 0.5;
    margin-left: 10rpx;
}

.item-value {
    font-size: 28rpx;
    color: #999;
}
</style>
