<template>
	<view class="container">
		<view class="page">
			<text class="num actived pass">1</text>
			<text class="line line_pass"></text>
			<text class="num actived pass">2</text>
			<text class="line line_pass"></text>
			<text class="num actived pass">3</text>
			<text class="line line_pass"></text>
			<text class="num actived pass">4</text>
			<text class="line line_pass"></text>
			<text class="num actived pass">5</text>
			<text class="line line_pass"></text>
			<text class="num actived">6</text>
			<text class="line"></text>
			<text class="num">7</text>
			<text class="line"></text>
			<text class="num">8</text>
		</view>
		<view class="wrapper">
			<view class="title">
			   <view>
					<text class="line"></text>个人声明
				</view>
			   <text class="page_mark">6/8</text>
			</view>
			<view class="content">
				<view class="ulBox">
				   <view class="liBox" v-for="personalStatements in dropOptionsInfoList" @click="selectStatements(personalStatements)"
				    >
					  <text class="iconfont icon-yuanxingweixuanzhong" v-if="!personalStatements.status"></text>
					  <text class="iconfont icon-selected-copy" v-if="personalStatements.status"></text>
				      <text class="itemName"> {{personalStatements.dropOptionName}}</text> 
				      <text class="iconfont icon-gougou" v-if="personalStatements.status"></text>
				    </view>
				</view>
			</view>
		</view>
		<view class="fixed" @click="saveInfo()">
		   {{(this.applyParam.approvalStatus== 0 || this.applyParam.approvalStatus == 2)?'保存并下一步':'下一步'}}
		</view>
	</view>
</template>

<script>
	import api from '../../api/api';
	export default{
		data(){
			return {
				dropOptionsInfoList:[],
				shareId: null,
				applyParam:{
					personalStatement:'',
					approvalStatus:''
				},
				personalStatementList:[]
			}
		},
		components:{},
		onLoad(options){
			this.shareId = options.shareId
		},
		onShow() {
			uni.showLoading({
			  title: '加载中...'
			});
			this.dropOptionsQuery();
			setTimeout(()=>{
				if(uni.getStorageSync('applyId')){
					this.queryById(uni.getStorageSync('applyId'),null)
				}else if(this.shareId){
					this.queryById(this.shareId,null)
				}else{
					this.queryById(null,uni.getStorageSync('cffp_userId'))
				}
			},500)
		},
		methods:{
			dropOptionsQuery(){
				api.dropOptionsQuery({code:'CFFP_Partner_Statements'}).then((res)=>{
					this.dropOptionsInfoList = res["data"]["dropMasterInfoList"][0]["dropOptionsInfoList"];
				})
			},
			selectStatements(personalStatements) {
				if(this.applyParam.approvalStatus== 0 || this.applyParam.approvalStatus == 2){
					if (personalStatements.status == 1) {
					  personalStatements.status = 0;
					} else {
					  personalStatements.status = 1;
					}
				}
			},
			queryById(id,userId){
				api.queryById({id:id,userId:userId}).then((res)=>{
					if(res['success']){
						this.applyParam = res['data']['data'];
						if(this.applyParam.personalStatement){
							this.personalStatementList = this.applyParam.personalStatement.split(',');
							for(let i=0;i<this.dropOptionsInfoList.length;i++){
								for(let j=0;j<this.personalStatementList.length;j++){
									if(this.dropOptionsInfoList[i]['dropOptionName'] == this.personalStatementList[j]){
										this.dropOptionsInfoList[i].status = 1;
									}
								}
							}
						}
					}
					uni.hideLoading()
				})
			},
			saveInfo(){
				if(this.applyParam.approvalStatus== 0 || this.applyParam.approvalStatus == 2){
					const selectedPersonalStatement = this.dropOptionsInfoList.filter((item) => {
					    return item.status == 1;
					});
					if (!selectedPersonalStatement.length) {
						uni.showToast({
							title: '请选择至少一项个人声明！',
							duration: 2000,
							icon: 'none'
						})
					return;
					}
					this.personalStatementList = [];
					for(let i=0;i<selectedPersonalStatement.length;i++){
						this.personalStatementList.push(selectedPersonalStatement[i]['dropOptionName']);
					}
					this.applyParam.personalStatement = this.personalStatementList .join(',');
					this.applyParam.operatStep = 6;
					api.saveApplyInfo(this.applyParam).then((res)=>{
						if(res['success']){
							uni.navigateTo({
								// url:`contract?id=${res['data']['id']}`
								url:`contract`
							});
						}else{
							uni.showToast({
								title: res['message'],
								duration: 2000,
								icon: 'none'
							})
							return;
						}
					})
				}else{
					uni.navigateTo({
						// url:`contract?id=${res['data']['id']}`
						url:`contract`
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	@import 'applyCommon.scss';
	.container {
		background-color: #FBFBFB;
	}
</style>