<template>
	<view class="container">
		<tui-list-cell arrow :size="32" :hover="false" padding="30rpx" @click="toSetPwd">修改密码</tui-list-cell>
		<tui-list-cell arrow :size="32" :hover="false" padding="30rpx" @click="displayModal('upgradation')">检查更新</tui-list-cell>
		<tui-list-cell arrow unlined :size="32" :hover="false" padding="30rpx" @click="displayModal('clearCache')">
			<view class="fs-flex__between fs-pr20">
				<view>清理缓存</view><view class="fs-size__h4 fs-color__label">0kb</view>
			</view>
		</tui-list-cell>
		<view class="fs-flex__center fs-mt96">
			<tui-button width="560rpx" height="86rpx" shadow shape="circle" @click="loginOutBtn">退出登录</tui-button>
		</view>
		<tui-modal :show="showModal" @click="handleModal" @cancel="hideModal" :content="content"></tui-modal>
		<tui-actionsheet :show="showAction" :item-list="actionList" @click="handleAction" @cancel="hideAction" tips="退出登录会清除您的登录信息，确认退出吗？"></tui-actionsheet>
	</view>
</template>

<script>
import { version, OSSURL } from '@/config/app'
import { apiAccountLogOut, apiVersion } from '@/api/app'
export default {
	data() {
		return {
			showModal: false,
			showAction: false,
			type: '',
			content: '',
			downloadUrl: '',
			actionList: [{text:"退出登录", color:"#577ee3"}]
		}
	},
	onLoad() {

	},
	methods: {
		// 修改密码
		toSetPwd() {
			uni.$tui.href('/pages/mine/setting/setPwd')
		},
		// 检查升级/清理缓存
		displayModal(type) {
			this.type = type
			if (this.type == 'upgradation') {
				// 检查版本更新
				apiVersion().then(res => {
					const newVersion = res.data.version
					this.downloadUrl = OSSURL + res.data.url
					if (version < newVersion) {
						this.content = '发现新版本，是否升级到最新版本？'
						this.showModal = true
					} else {
						uni.$tui.toast('已是最新版本')
					}
				})
			} else if (this.type == 'clearCache') {
				this.content = '您确定要清理缓存吗？'
				this.showModal = true
			}
		},
		// 确定升级/清理
		handleModal(event) {
			this.hideModal()
			if (event.index == 1) {
				if (this.type == 'upgradation') {
					// 打开系统浏览器下载
					plus.runtime.openURL(this.downloadUrl)
				} else if (this.type == 'clearCache') {
					uni.$tui.toast('清理成功')
				}
			}
		},
		// 取消升级/清理
		hideModal() {
			this.showModal = false
		},
		// 退登录出
		loginOutBtn() {
			this.showAction = true
		},
		// 确定退出
		handleAction() {
			apiAccountLogOut().then(res => {
				this.loginOut()
				uni.$tui.href('/pages/login', 3)
			})
		},
		// 取消退出
		hideAction() {
			this.showAction = false
		}
	}
}
</script>

<style lang="scss" scoped>

</style>
