<style>
	.listsignUp {
		width: 100%;
		height: 100%;
		/* overflow-y: auto; */
		background: #F6F6F6;
		
	}

	.listsignUp .single_Phone {
		font-size: 28rpx;
		color: #1F1F1F;
		font-weight: bold;
		margin-bottom: 10rpx;
		margin-top: 40rpx;
	}

	.listsignUp .single_Input {
		border-bottom: 1px solid #DADCE6;
		padding: 10rpx 0 20rpx 0;

	}

	.listsignUp .single_Remark {
		margin: 10rpx 0 0 0;
		font-size: 25rpx;
		color: #808080;
	}

	.listsignUp .btn {
		width: 100%;
		height: 44px;
		border-radius: 10px;
		background: #F70027;
		font-size: 16px;
		color: #FFF;
		text-align: center;
		line-height: 44px;
		margin: 20px 0;
	}
</style>
<template>
	<view class="listsignUp" v-if="!loading">
		<view style="width: 100%;height: 100%;padding: 18px;">
			<view style="width: 100%;padding: 15px;border-radius: 15px;background: #FFF;">
				<view>
					<view class="single_Phone" style="margin-top: 0;">
						<span style="color:red;margin-right:5rpx;" >*</span>{{getNameids()}}
					</view>
					<view class="single_Input" @click="show=true" style="width: 100%;display: flex;align-items: center;justify-content: space-between;">
						<input style="width: 90%"  v-model="ObjectName" :disabled="true" placeholder="请选择" />
						<u-icon name="arrow" color="#909399"  size="36" ></u-icon>
						
					</view>
					<u-picker v-model="show" mode="selector" :range="CarrierList" range-key="Name" @confirm='confirm'></u-picker>
				</view>
				<template v-for="(item,index) in dataList">
					<singleText v-if="item.CompKey=='SingleLineText'" :CompData="item.CompData" :index='index'
						@success="datasuccess()"></singleText>
					<multipleText v-if="item.CompKey=='MultiLineText'" :CompData="item.CompData" :index='index'
						@success="datasuccess()"></multipleText>
					<selectChoice v-if="item.CompKey=='DorpDownList'" :CompData="item.CompData" :index='index'
						@success="datasuccess()"></selectChoice>
					<pictureUpload v-if="item.CompKey=='ImageUploadComp'" :CompData="item.CompData" :index='index'
						@success="datasuccess()"></pictureUpload>
					<videoUpload v-if="item.CompKey=='VideoUploadComp'" :CompData="item.CompData" :index='index' 
						@success="datasuccess()"></videoUpload>
					<fileType v-if="item.CompKey=='CommonUploadComp'" :CompData="item.CompData" :index='index' 
							@success="datasuccess()"></fileType>
				</template>
				<view class="btn" @click="setapply()">
					提交申请
				</view>
			</view>
		</view>
		
		<u-modal
		  v-model="rzshow"
		  content="还未进行认证,现在去认证"
		  :show-cancel-button="true"
		  @confirm="queren"
		  @cancel='cancel'
		></u-modal>
	</view>

</template>

<script>
	import singleText from "./plugin/singleText";
	import multipleText from "./plugin/multipleText"
	import selectChoice from "./plugin/selectChoice"
	import pictureUpload from "./plugin/pictureUpload"
	import videoUpload from "./plugin/videoUpload"
	import fileType from "./plugin/fileType"
	
	export default {
		components: {
			singleText,
			multipleText,
			selectChoice,
			pictureUpload,
			videoUpload,
			fileType
			
		},
		data() {
			return {
				pageTitle: '榜单报名',
				loading:true,
				rzshow:false,
				addMsg: {
					Id: 0,
					ListId: 0,
					ObjectType: 0, //面向群体类型 1品牌 2载体
					ObjectId: 0, //品牌/载体id
					Content: '', //表达
				},
				dataList: [],
				pageCount: 1,
				formData: null,
				CarrierList: [],
				ObjectName:'',
				show:false,
				companyStatus:0,
				
			}
		},
		created() {
			uni.setNavigationBarTitle({
				title: "榜单报名",
			});
			this.getComponyStatus()
		},
		mounted() {

		},
		onLoad(option) {

			if (option && option.formData) { //解码
				this.formData = JSON.parse(decodeURIComponent(option.formData))
				if(this.formData.ObjectId && this.formData.ObjectId>0){
					this.addMsg.Id = this.formData.Id
					this.addMsg.ListId = this.formData.ListId
					this.addMsg.ObjectType = this.formData.ObjectType
					this.addMsg.ObjectId = this.formData.ObjectId
					this.ObjectName = this.formData.ObjectName
					this.dataList = this.formData.Content
					// this.dataList.forEach(x=>{ 
					// 	if(x.CompKey=="DorpDownList"){
					// 		x.CompData.OptionValue = x.CompData.OptionValue.split(',')
					// 	}
					// })
					
				}else{
					this.addMsg.ListId = this.formData.Id;
					this.getCustomForm()
				}
				this.getMyBrandCarrierList()
				

			}
		},
		methods: {
			// 获取公司认证信息
			getComponyStatus(){
				let that = this
				let parms = {
					url: "/api/AppletTrade/GetCommpanyStatus",
				}
				this.request2(parms, (res) => {
					if (res.resultCode == 1&&res.data!=null) {
						// 获取公司名字和认证状态
						that.companyStatus=res.data.CompanyStatus
						if(that.companyStatus!=5){
							this.rzshow=true
						}
						
						
					}
				})
			},
			queren(){//去认证
				uni.reLaunch({
				  url: "/pages/kotra/identification",
				});
			},
			cancel(){//返回上一页
				uni.navigateBack()
			},
			getMyBrandCarrierList() {
				this.loading = true
				let parms = {
					url: "/api/AppletTrade/GetAppletMyBrandCarrierList",
					data: {
						ListId: this.formData.Id,
						EnrollId: 0
					}
				}
				this.request2(parms, (res) => {
					if (res.resultCode == 1) {
						this.CarrierList = res.data
					}
					setTimeout(()=>{
						this.loading = false
					},100)
					
					
				})
			},
			
			getCustomForm() {
				let parms = {
					url: "/api/AppletCustomForm/GetCustomForm",
					data: {
						FormType: this.formData.ListType
					}
				}
				this.request2(parms, (res) => {
					if (res.resultCode == 1) {
						if (res.data.FormData && res.data.FormData.length > 0) {
							this.dataList = res.data.FormData;
						} else { //默认数据
							this.dataList = [{
									"CompKey": "ImageUploadComp",
									"isCked": false,
									"CompData": {
										"Name": "图片上传",
										"FileCount": 9,
										"FileSizeLimit": 0,
										"FileList": [],
										"Remark": "最多9张图片"
									}
								},
								{
									"CompKey": "VideoUploadComp",
									"isCked": false,
									"CompData": {
										"Name": "视频",
										"FileCount": 1,
										"FileSizeLimit": 0,
										"FileList": [],
										"Remark": "最多上传一个视频"
									}
								},
								{
									"CompKey": "MultiLineText",
									"isCked": false,
									"CompData": {
										"Name": "参与说明",
										"WordsLength": 0,
										"IsRequire": true,
										"TextValue": "",
										"Remark": ""
									}
								},
								{
									"CompKey": "SingleLineText",
									"isCked": false,
									"CompData": {
										"Name": "联系电话",
										"WordsLength": 11,
										"IsRequire": true,
										"TextValue": "",
										"Remark": "输入正确的电话号码"
									}
								},
							]
						}

					}
				})
			},
			confirm(e){
				this.addMsg.ObjectId = this.CarrierList[e].Id;
				this.ObjectName = this.CarrierList[e].Name;
				this.addMsg.ObjectType = this.CarrierList[e].ObjectType;
			},
			datasuccess(data, index) {
				this.dataList[index].CompData = data
				
			},
			setapply() {
				
				let show = false
				//验证必填
				if(this.addMsg.ObjectId==0 || this.addMsg.ObjectId==null || this.addMsg.ObjectId==''){
					uni.showToast({
						title: '请选择参加品牌/载体',
						icon: 'none'
					})
					
					return
				}
				let dataList = JSON.parse((JSON.stringify(this.dataList)))
				dataList.map(x => {
					if (show == true) {
						return
					}
					if(x.CompKey=='DorpDownList' && x.CompData.IsMultiple==true){
						if(Array.isArray(x.CompData.OptionValue)){
							x.CompData.OptionValue = x.CompData.OptionValue.join(',')
						}
						
					}
					if (x.CompData.IsRequire && x.CompData.IsRequire == true) {
						if ((x.CompKey == 'SingleLineText' || x.CompKey == 'MultiLineText') && x.CompData
							.TextValue == '') { //单行输入或者多行
							uni.showToast({
								title: x.CompData.Name + '不能为空',
								icon: 'none'
							})
							show = true
							return
						}
						if (x.CompKey == 'DorpDownList' && x.CompData.OptionValue == '') { //下啦
							uni.showToast({
								title: '请选择' + x.CompData.Name,
								icon: 'none'
							})
							show = true
							return
						}
					}
					
				})
				if (show == true) {
					return
				}
				
				
				this.addMsg.Content = JSON.stringify(dataList) 
				let that = this
				if(this.companyStatus==5){
					uni.requestSubscribeMessage({
						tmplIds: that.formData.template_message_list,
						complete(res) {
							that.request2({
									url: '/api/AppletTrade/SetAppletFirstShopListInfo',
									data: that.addMsg
								},
								(res) => {
									let pages = getCurrentPages(); // 当前页面
									let beforePage = pages[pages.length - 2]; // 前一个页面
									uni.showToast({
										title: '申请成功',
										icon: 'none'
									})
									setTimeout(() => {
											uni.navigateBack({
												 success: function() {
													 beforePage.$vm.getDetails(); // 执行前一个页面的方法
												 }
											});
									}, 1000)
								
									
									
								}
							);
						}
					})
				}else{
					
				}
				
			},
			getNameids(){
				
				let text = '';
				if(this.formData.ObjectTypeIds.indexOf('1')!=-1){
					text='参与品牌'
				}
				if(this.formData.ObjectTypeIds.indexOf('2')!=-1){
					text='参与载体'
				}
				if(this.formData.ObjectTypeIds.indexOf('1')!=-1 && this.formData.ObjectTypeIds.indexOf('2')!=-1){
					text='参与品牌/载体'
				}
				
				return text
			},
		}
	}
</script>
