<template>
	<view>
		<navBar leftText="信息完善"></navBar>
		<image class="topBg absolute" src="/static/image/mine/myTopBg.png" mode="widthFix"></image>
		<view class="avatarC fixed" :style="'top:'+avatarTop+'px;'">
			<image class="avatar" :src="avatar?avatar:avatarUrl" mode="aspectFill"></image>
			<image class="icon absolute" src="/static/icon/mine/basicInfo/edit.png" mode="widthFix"></image>
			<button class="absolute" open-type="chooseAvatar" @chooseavatar="chooseAvatar"></button>
		</view>
		<view class="preferenceC fixed" :style="'top:'+contentTop+'px;'">
			<view class="title">投资偏好设置</view>
			<view class="section first">投资偏好</view>
			<view class="list flexWrap"> 
				<view :class="'item '+(selectInvestIndex==index?'select':'')" v-for="(item,index) in investPreferenceList" :key="index" @click="clickInvestItem(index)">
					{{item}}
				</view>
			</view>
			<view class="section">炒股年限</view>
			<view class="list flexWrap">
				<view :class="'item '+(selectYearIndex==index?'select':'')" v-for="(item,index) in stockYearList" :key="index" @click="clickYearItem(index)">
					{{item}}
				</view>
			</view>
			<view class="section">风险偏好</view>
			<view class="list flexWrap">
				<view :class="'item '+(selectRiskIndex==index?'select':'')" v-for="(item,index) in riskPreferenceList" :key="index" @click="clickRiskItem(index)">
					{{item}}
				</view>
			</view>
			<view class="section">投资规模</view>
			<view class="list flexWrap">
				<view :class="'item '+(selectScaleIndex==index?'select':'')" v-for="(item,index) in investmentScaleList" :key="index" @click="clickScaleItem(index)">
					{{item}}
				</view>
			</view>
			<view class="section">偏好市场（可多选）</view>
			<view class="list flexWrap">
				<view :class="'item '+(item.select?'select':'')" v-for="(item,index) in preferredMarketList" :key="index" @click="clickMarketItem(index)">
					{{item.title}}
				</view>
			</view>
		</view>
		<view class="bottomC fixed flex">
			<view class="pre btn" @click="clickPre()">上一步</view>
			<view class="finish btn flex1" @click="clickFinish()">完成</view>
		</view>
	</view>
</template>

<script>
	import { inject } from 'vue';
	import { userInfo, updateInvestPreference } from '@/request/api';
	
	export default {
		data() {
			return {
				avatarTop:'',
				contentTop:'',
				avatar:'',		//选择头像临时地址
				avatarUrl:'',	//已上传的链接
				investPreferenceList:['长期投资','中短期投资','风险控制型'],
				selectInvestIndex:-1,
				stockYearList:['新手入门','1年以内','1-3年','3-5年','5-10年','10年以上'],
				selectYearIndex:-1,
				riskPreferenceList:['保守型','稳健型','保守型'],
				selectRiskIndex:-1,
				investmentScaleList:['50万以下','50-100万','100万以上'],
				selectScaleIndex:-1,
				preferredMarketList:[{title:'A股'},{title:'港股'},{title:'美股'},{title:'期货'},{title:'虚拟货币'},{title:'新兴市场'}]
			}
		},
		onLoad() {
			this.avatarTop = inject('navHeight') + 60/750*inject('windowWidth')
			this.contentTop = this.avatarTop + 75/750*inject('windowWidth')
			this.getUserInfoData()
		},
		methods: {
			/**
			 * 点击选择头像
			 * @param {Object} e
			 */
			chooseAvatar(e)
			{
				this.avatar = e.detail.avatarUrl
			},
			/**
			 * 点击选择投资偏好
			 * @param {Object} index
			 */
			clickInvestItem(index)
			{
				if(this.selectInvestIndex!=index)
				{
					this.selectInvestIndex = index
				}
			},
			/**
			 * 点击选择风险偏好
			 * @param {Object} index
			 */
			clickYearItem(index)
			{
				if(this.selectYearIndex!=index)
				{
					this.selectYearIndex = index
				}
			},
			/**
			 * 点击选择风险偏好
			 * @param {Object} index
			 */
			clickRiskItem(index)
			{
				if(this.selectRiskIndex!=index)
				{
					this.selectRiskIndex = index
				}
			},
			/**
			 * 点击选择投资规模
			 * @param {Object} index
			 */
			clickScaleItem(index)
			{
				if(this.selectScaleIndex!=index)
				{
					this.selectScaleIndex = index
				}
			},
			/**
			 * 点击选择市场偏好
			 * @param {Object} index
			 */
			clickMarketItem(index)
			{
				this.preferredMarketList[index].select = !this.preferredMarketList[index].select;
			},
			/**
			 * 点击上一步
			 */
			clickPre()
			{
				uni.navigateBack()
			},
			/**
			 * 点击完成
			 */
			clickFinish()
			{
				if(this.selectInvestIndex<0)
				{
					uni.showToast({
						title:'请选择投资偏好',
						icon:'none'
					})
					return
				}
				if(this.selectYearIndex<0)
				{
					uni.showToast({
						title:'请选择炒股年限',
						icon:'none'
					})
					return
				}
				if(this.selectRiskIndex<0)
				{
					uni.showToast({
						title:'请选择风险偏好',
						icon:'none'
					})
					return
				}
				if(this.selectScaleIndex<0)
				{
					uni.showToast({
						title:'请选择投资规模',
						icon:'none'
					})
					return
				}
				let arr = []
				for (let item of this.preferredMarketList) {
					if(item.select)
					{
						arr.push(item.title)
					}
				}
				if(arr.length==0)
				{
					uni.showToast({
						title:'请选择偏好市场',
						icon:'none'
					})
					return
				}
				if(this.avatar)
				{
					//如果选择了新头像
					let param = {avatar:this.avatar,isFile:1}
					updateBasicInfo(param).then(res=>{
						this.uploadInvestPreferenceData()
					}).catch(error=>{
						
					})
				}else
					this.uploadInvestPreferenceData()
			},
			/**
			 * 更新投资偏好设置
			 */
			uploadInvestPreferenceData()
			{
				let param = {trading_experience: this.selectYearIndex,investment_style: this.investPreferenceList[this.selectInvestIndex],
				    risk_preference: this.riskPreferenceList[this.selectRiskIndex],
				    investment_amount: this.investmentScaleList[this.selectScaleIndex],
				    preferred_markets: arr}
				updateInvestPreference(param).then(res=>{
					uni.navigateBack({
						delta:2
					})
				}).catch(error=>{
					
				})
			},
			/**
			 * 获取用户偏好设置数据
			 */
			getUserInfoData()
			{
				userInfo().then(res=>{
					if(res.code==200)
					{
						let data = res.data.investment_preferences
						//投资偏好
						for (var i = 0; i < this.investPreferenceList.length; i++) {
							let item = this.investPreferenceList[i]
							if(item==data.investment_style)
							{
								this.selectInvestIndex = i
								break
							}
						}
						//炒股年限
						this.selectYearIndex = data.trading_experience
						// for (var i = 0; i < this.stockYearList.length; i++) {
						// 	let item = this.stockYearList[i]
						// 	if(item==data.investment_style)
						// 	{
						// 		this.selectYearIndex = i
						// 		break
						// 	}
						// }
						//风险偏好
						for (var i = 0; i < this.riskPreferenceList.length; i++) {
							let item = this.riskPreferenceList[i]
							if(item==data.risk_preference)
							{
								this.selectRiskIndex = i
								break
							}
						}
						//投资规模
						for (var i = 0; i < this.investmentScaleList.length; i++) {
							let item = this.investmentScaleList[i]
							if(item==data.investment_amount)
							{
								this.selectScaleIndex = i
								break
							}
						}
						//偏好市场
						for (let item of this.preferredMarketList) {
							let arr = JSON.parse(data.preferred_markets)
							let arr1 = arr[0].split(',')
							if(arr1.indexOf(item.title)>-1)
							{
								item.select = true
							}else
							{
								item.select = false
							}
						}
					}else 
						wx.showToast({
							title:res.message,
						})
				}).catch(error=>{
					
				})
			}
		}
	}
</script>

<style lang="less">
.topBg 
{
	top: 0;
	left: 0;
	width: 100%;
	height: auto;
}
.avatarC 
{
	left: 0;
	margin-left:calc((100% - 150rpx)/2);
	width: 150rpx;
	.avatar 
	{
		width: 100%;
		height: 150rpx;
		border-radius: 50%;
		border: solid 2rpx white;
	}
	.icon 
	{
		right: 20rpx;
		bottom: 0;
		width: 40rpx;
		height: auto;
	}
	button
	{
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
	}
	z-index: 10;
}
.preferenceC 
{
	background-color: white;
	left: 0;
	right: 0;
	bottom: calc(73rpx + 17rpx + 80rpx);
	border-radius: 20rpx 20rpx 0 0;
	overflow-y: scroll;
	.title
	{
		margin: 150rpx 0 0 60rpx;
		font-size: 36rpx;
		font-weight: bold;
	}
	.section
	{
		margin: 10rpx 60rpx 0;
		line-height: 66rpx;
		font-size: 26rpx;
		font-weight: bold;
	}
	.section.first 
	{
		margin-top: 20rpx;
	}
	.list 
	{
		padding: 0 60rpx;
		.item 
		{
			background-color: #FBFBFD;
			margin: 0 14rpx 12rpx 0;
			width: calc((100% - 28rpx)/3);
			line-height: 76rpx;
			border: solid 2rpx #EFEFF2;
			border-radius: 10rpx;
			font-size: 24rpx;
			font-weight: 500;
			color: #555;
			text-align: center;
		}
		.item.select 
		{
			background-color: #FFE9D9;
			border: solid 2rpx #F97316;
		}
		.item:nth-child(3n)
		{
			margin-right: 0;
		}
	}
}
.bottomC 
{
	left: 0;
	right: 0;
	bottom: 73rpx;
	margin: 0 25rpx;
	
	.btn 
	{
		line-height: 80rpx;
		border-radius: 20rpx;
		font-size: 26rpx;
		text-align: center;
	}
	.pre 
	{
		background-color: #FFE9D9; 
		width: 226rpx;
		color: #F97316;
	}
	.finish
	{
		background-color: #F97316; 
		margin-left: 20rpx;
		color: white;
	}
}
</style>
