<!--
 * @Author: ch
 * @Date: 2022-03-28 15:38:23
 * @LastEditors: ch
 * @LastEditTime: 2022-04-19 15:55:24
 * @Description: file content
-->
<template>
	<view>
		<view class="userInfo">
			<u-upload  class="userInfo--upload" @afterRead="headUpload" >
				<image class="userInfo--head" :src="userInfo.avatar || require('@/static/account/tx.png')" ></image>
			</u-upload>
			<view>{{userInfo.nickname}}</view>
		</view>
		<UiCell class="cell" title="昵称" :value="userInfo.nickname" @click="$Router.push('/setNickname')"></UiCell>
		<UiCell class="cell cell--last" title="性别" :value="sexName" @click="sexShow = true"></UiCell>
		<u-picker :show="sexShow" :columns="sexData" :defaultIndex="sexDefault" 
			keyName="label" @cancel="sexShow = false" @confirm="sexChange"></u-picker>
	</view>
</template>
<script>
import UiButton from '@/components/UiButton.vue'
import UiCell from '@/components/UiCell.vue';
import {Request} from '@/common/utils';
import {ApiPutUser} from '@/common/api/account';
import {ApiPostGetOssConfig} from '@/common/api/oss';

export default {
	components: { UiCell, UiButton },
	data(){
		return {
			sexShow : false,
			sexData : [[{label:'女',val:2},{label:'男',val:1}]]
		}
	},
	computed:{
		userInfo (){
			return this.$store.state.userInfo
		},
		sexName (){
			const curSex = this.sexData[0].find(i => i.val === this.userInfo.gender);
			return curSex ? curSex.label : '未知';
		},
		sexDefault (){
			return [this.sexData[0].findIndex(i => i.val === this.userInfo.gender)];
		}
	},
	methods:{
		/**
		 * 切换性别，发起性别修改请求
		 */
		async sexChange(val){
			const value = val.value[0].val;
			const {error, result} = await ApiPutUser({gender:value});
			if(error){
				ui.$u.totas(error.message);
				return false
			}
			this.$store.commit('SET_USER_INFO', {...this.userInfo, gender:value});
			this.sexShow = false;
		},
		async getOssCon(){
			const {error, result} = await ApiPostGetOssConfig({
				configId : 'account-avatar/',
				serviceName : 'uc'
			});
			if(error){
				uni.$u.toast(error.message);
				return false
			}
			return result;
		},
		async headUpload(val){

			const file = val.file;
			const oss = await this.getOssCon();
			uni.uploadFile({
				name : 'file',
				filePath : file.url,
				url : oss.host,
				formData : {
					name : file.name,
					key : `${oss.dir}${'${filename}'}`,
					policy : oss.policy,
					OSSAccessKeyId : oss.accessId,
					success_action_status : 200,
					signature : oss.signature
				},
				 success : async (res)=>{
					const avatar = `${oss.host}/${oss.dir}${file.name}`;
					const {error, result} = await ApiPutUser({avatar});
					if(error){
						ui.$u.totas(error.message);
						return false
					}
					this.$store.commit('SET_USER_INFO', {...this.userInfo, avatar});
				}
			})


		}
	}
	
}
</script>
<style lang="scss" scoped>
page{
	background: $color-grey1;
}
.userInfo{
	padding: 77rpx 0 60rpx;
	text-align: center;
	font-size: $font-size-base;
	color: $color-grey6;
	&--head,&--upload{
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		margin: 0 auto 13rpx;
	}
}
.cell{
	padding: 0 40rpx;
	&--last{
		border: 0;
	}
}
/deep/{
	.userInfo .uiCell--title{
		font-size: $font-size-lg;
	}
	.uiCell--value{
		color: $color-grey4;
	}
} 
</style>