<template>
	<view class="content">
		<view class="container">
			<headers :titles="titles"><uni-icons type="arrow-left" color="#fff" size="22px"></uni-icons></headers>
			<view class="box_">
				<view class="">头像</view>
				<view class="d-s">

					<view class="ttou">
						<u-upload imageMode="aspectFit" :fileList="fileList1" @afterRead="addFigure" @delete="deletePic"
							name="1" :maxCount="1" width="140" height="70">
							<u-avatar size="75" default-url="/static/imgs/ic_user_header_default.png"
								:src="agentInfo.userIcon"></u-avatar>
						</u-upload>
					</view>

					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="box_">
				<view class="">昵称</view>
				<view class="d-s" @click="gomod()">
					<view class="">{{agentInfo.nickName}}</view>
					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="box_" @click="goChangePassword()">
				<view class="">修改密码</view>
				<view class="d-s">

					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="box_">
				<view class="">隐私政策</view>
				<view class="d-s" @click="goH5(1)">

					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="box_">
				<view class="">服务协议</view>
				<view class="d-s" @click="goH5(2)">

					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="box_">
				<view class="">检查更新</view>
				<view class="d-s" @click="appVersionPost()">
					<view class="">{{getVersion()}}</view>
					<u-icon name="arrow-right" size="16"></u-icon>
				</view>
			</view>
			<view class="anniu" @click="logOut()">
				退出登录
			</view>
		</view>
		<u-modal :show="show" :title="title" :showCancelButton="true" @confirm="confirm" @cancel="cancel"></u-modal>
	</view>
</template>

<script>
	import headers from '../../components/header/headers.vue'
	import tabbar from '../../components/tabbar/tabbar.vue'
	import request from '../../utils/request.js'
	import upload from '@/utils/upload.js'
	import config from '@/config'
	export default {
		data() {
			return {
				titles: "设置",
				agentInfo: {},
				fileList1: [],
				firm: '',
				typeStr: '0',
				userType: '3',
				type: 'Android',
				apkUrl: '',
				show: false,
				title: '您是否要更新',
			}
		},
		onShow() {
			this.getInfo()
		},
		components: {
			headers,
			tabbar
		},
		methods: {
			goH5(type) {
				let url;
				if (type == 1) {
					url =
						'https://51pay.xlpayment.com/H5/%E4%BF%A1%E8%81%9451%E6%94%B6%E9%92%B1%E5%95%86%E6%88%B7%E9%9A%90%E7%A7%81%E6%94%BF%E7%AD%96.htm'
				} else if (type == 2) {
					url =
						'https://51pay.xlpayment.com/H5/51%E6%94%B6%E9%92%B1%E5%95%86%E6%88%B7%E6%B3%A8%E5%86%8C%E5%8F%8A%E6%9C%8D%E5%8A%A1%E5%8D%8F%E8%AE%AE1.0.htm'
				}
				uni.navigateTo({
					url: '/components/h5/webView?url=' + url
				})
			},
			confirm() {
				this.show = false
				// #ifdef APP-PLUS
				plus.runtime.openURL(this.apkUrl)
				// #endif
			},
			cancel() {
				this.show = false

			},
			getVersion() {
				if (uni.getSystemInfoSync().platform == 'ios') {
					return config.appInfo.iosVersion
				} else {
					return config.appInfo.apkVersion
				}
			},
			appVersionPost() {
				if (uni.getSystemInfoSync().osName == 'ios') {
					this.type = 'iOS'

				} else if (uni.getSystemInfoSync().osName == 'android') {
					this.type = 'Android'
				}
				request({
					url: 'app/base/appVersion',
					method: 'post',
					data: {
						type: this.type,
						firm: this.firm,
						userType: this.userType,
						typeStr: this.typeStr

					},
				}).then(res => {
					if (res.code == 0) {
						console.log(uni.getSystemInfoSync(), 'uni.getSystemInfoSync()');
						if (uni.getSystemInfoSync().osName == 'ios') {
							if (config.appInfo.iosVersion != res.appVersion.versionNo) {
								this.apkUrl = res.appVersion.wgtUrl
								this.show = true
							}

						} else if (uni.getSystemInfoSync().osName == 'android') {
							if (config.appInfo.apkVersion != res.appVersion.versionNo) {
								this.apkUrl = res.appVersion.apkUrl
								this.show = true
							}
						}

					}

				})
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)

			},
			async uploadFile(url, fileName, type) {
				uni.showLoading({
					title: '上传中'
				});
				let that = this
				upload({
					url: 'app/base/upload',
					filePath: url,
					name: 'test',
					formData: {

						fileName: fileName
					}

				}).then((res) => {

					that.changeInfo(res.url)


				}).finally((res) => {

					uni.hideLoading();
				})
			},
			async addFigure(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,

					})
				})
				for (let i = 0; i < lists.length; i++) {

					const result = await this.uploadFile(lists[i].url, lists[i].name, 1)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}

			},
			changeInfo(userImg) {
				request({
					url: 'app/uaLogin/changeInfo?roleType=agent',
					method: 'post',
					data: {
						userImg: userImg,
						infoType: "0"
					}

				}).then(res => {
					this.getInfo()
				})
			},
			getInfo() {
				request({
					url: 'app/uaLogin/info?roleType=mer',
					method: 'post',

				}).then(res => {
					this.agentInfo = res.user
				})
			},
			gomod() {
				uni.navigateTo({
					url: '/MyPages/my/modifyname'
				})
			},
			goChangePassword() {
				uni.navigateTo({
					url: '/MyPages/my/ChangePassword'
				})
			},
			logOut() {
				uni.clearStorage();
				uni.redirectTo({
					url: '/loginPages/login/login'
				})
			},
			goback() {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		background: #fff;
		height: 100vh;
	}

	.d-s {
		display: flex;
		align-items: center;
	}

	.container {
		width: 100%;
		background: #fff;
		box-sizing: border-box;
		padding-top: 88px;
	}

	.box_ {
		width: 100%;
		box-sizing: border-box;
		padding: 15px 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #ebebeb;

	}

	.touxiang {
		width: 50px;
		height: 50px;
		border-radius: 50%;
		background: #dfdfdf;
		margin-right: 5px;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.ttou {
		width: 70px;
		height: 70px;
		border-radius: 50%;
		overflow: hidden;
	}

	.anniu {
		width: 60%;
		height: 40px;
		background: #E4612E;
		border-radius: 50px;
		margin: 40px auto;
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
