<style>
.postPublishing {
  width: 100%;
  height: 100%;
  background: #FFF;
 
}
.postPublishing .box{
	padding: 0 15px;
}
.postPublishing .box-item{
	width: 100%;
	height: 50px;
	border-bottom: 1px solid #E2E2E2;
	display: flex;
	align-items: center;
}
.postPublishing .pubox{
	width: 100%;
	height:calc(100vh * 0.8) ;
	padding:0 15px;
}
.postPublishing .pubox-top{
	width: 100%;
	height: 45px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	font-size: 14px;
	color: #111;
}
::-webkit-scrollbar{
 
   display:none;
 
}
.postPublishing .foritem{
	width: 100%;
	height: 45px;
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
}
.postPublishing .flex-row{
	display: flex;align-items: center;
}
.postPublishing .slot-btn{
	width: 200rpx;
	height: 200rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	background: rgb(244, 245, 246);
	border-radius: 10rpx;
	margin-left: 5px;
}
.postPublishing .slot-btn .mian-title {
	font-size: 28rpx;
	color: #888;
	font-weight: 600;
	margin: 10rpx 0;
}
.postPublishing .release{
	width: 100%;
	height: 45px;
	border-radius: 22px;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 16px;
	color: #FFFFFF;
	margin: 30px 0;
}
</style>

<template>
  <view class="postPublishing" :style="{ height: contentHeight }">
    <view class="box">
		<view class="box-item" style="justify-content: space-between;" @click="show=true">
			<text style="font-size:12px;color: #666666;">选择分类</text>
			<view class="flex-row" >
				<text style="margin-right: 5px;font-size:12px;color: #999999" v-if="addMsg.ForumId==0">选择合适的贴吧</text>
				<text style="margin-right: 5px;font-size:12px;color: #111111" v-if="addMsg.ForumId>0">{{ForumName}}</text>
				<u-icon name="arrow" color="#A5A4AC" size="30" ></u-icon>
			</view>
			
		</view>
		<view class="box-item" style="justify-content: space-between;" >
			<text style="font-size:12px;color: #666666;">是否开启报名</text>
			<view class="flex-row">
				<u-radio-group v-model="addMsg.IsOpenEnroll" >
					<u-radio 
						shape="circle"
						v-for="(item, index) in kqlist" :key="index" 
						:name="item.id"
						:active-color="mainColor"
						icon-size='34'
						label-size='24'
						
					>
						{{item.name}}
					</u-radio>
				</u-radio-group>
			</view>
		</view>
		<view class="box-item" style="justify-content: space-between;" v-if="addMsg.IsOpenEnroll==1" @click="showtime = true">
			<text style="font-size:12px;color: #666666;">报名截止日期</text>
			<view class="flex-row">
				<text style="margin-right: 5px;font-size:12px;color: #999999" v-if="addMsg.Deadline==''">请选择时间</text>
				<text style="margin-right: 5px;font-size:12px;color: #111111" v-else>{{addMsg.Deadline}}</text>
				<u-icon name="arrow" color="#A5A4AC" size="30" ></u-icon>
			</view>
		</view>
		<view class="box-item">
			<input type="text" v-model="addMsg.ActivityTitle" placeholder="请将标题写在这里~" style="width: 100%;">
		</view>
		<view>
			
			<textarea
			  style="height: 100px;width: 100%;padding: 15px 0;"
			  placeholder="来吧，尽情发挥吧..."
			  v-model="addMsg.Content"
			  maxlength='2000'
			/>
		</view>
		<view style="width: 100%;margin-top: 15px;">
			<u-upload :action="action" :file-list="fileList" @on-remove="onRemove1"  :custom-btn="true"
			 @on-success="uploadSuccessHandler">
				<view slot="addBtn" class="slot-btn">
					<u-icon name="photograph" size="40" color="#888"></u-icon>
					<view class="mian-title">上传图片</view>
				</view>
			</u-upload>
		</view>
		<view class="release" @click="releasebtn" :style="{background:mainColor}">
			发布
		</view>
	</view>
	<u-popup v-model="show" mode="bottom" border-radius="20">
		<view class="pubox" >
			<view class="pubox-top">
				<view></view>
				<text >选择吧</text>
				<u-icon name="cross" color="#555" size="40" @click='show=false'></u-icon>
			</view>
			<scroll-view scroll-y="true" style="height: calc(100vh * 0.8 - 65px);padding-bottom: 20px;">
				<view>
					<view v-for="(x,y) in forumList" :key="y" class="foritem" @click="select(x)">
						<div style='display: flex;align-items: center;'>
							<image :src="x.Icon"  style="width: 30px;height: 30px;border-radius: 5px;"></image>
							<text style="font-size: 14px;color: #111;margin-left: 10px;">{{x.Name}}</text>
						</div>
						<view >
							<u-icon name="success" :color="mainColor" size="40" @click='show=false' v-if='addMsg.ForumId == x.Id'></u-icon>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</u-popup>
	<u-picker v-model="showtime" mode="time" @confirm='confirm' :params="params" :default-time='addMsg.Deadline'></u-picker>
	<auth v-if="showAuth" @changeuserinfo="reloadUserinfo" @gbAuth= 'gbAuth'></auth>
  </view>
</template>

<script>
import auth from "@/components/auth/index.vue";
export default {
  components: {
    auth
  },
  data() {
    return {
      pageTitle: "发布帖子",
      showAuth:false,
	  show:false,
	  u:{},
	  fileList:[],
      list: [],
      mainColor: "",
      contentHeight: 0,
	  forumList:[],
	  showtime:false,
	  addMsg:{
		  Id:0,
		  ForumId:0,
		  ActivityTitle:'',
		  Content:'',
		  ImageList:[],
		  Deadline:'',
		  IsOpenEnroll:1,//1 是开启 2 是不开启
	  },
	  params: {
		year: true,
		month: true,
		day: true,
		hour: true,
		minute: true,
		second: true
	},
	  kqlist:[{id:1,name:'开启'},{id:2,name:'不开启'}],
	  ForumName:'',
	  action: this.host2 + "/api/File/UploadTencent",
    };
  },
  created() {
    this.contentHeight = this.$utils.calcContentHeight(-40) + "px";
    this.mainColor = this.$uiConfig.mainColor;
  },
  mounted() {
    let currentPages = getCurrentPages();
    let u = "/" + currentPages[currentPages.length - 1].route;
    let pages = wx.getStorageSync("basedata")
      ? wx.getStorageSync("basedata").bar_title
      : [];
    pages.forEach((x) => {
      if (x.value == u) {
        this.pageTitle = x.new_name ? x.new_name : x.name;
      }
    });
    uni.setNavigationBarTitle({
      title: this.pageTitle,
    });
	let MallBaseId = uni.getStorageSync("mall_UserInfo").MallBaseId ? uni.getStorageSync("mall_UserInfo").MallBaseId : 1;
	this.action = this.host2 + '/api/File/UploadTencent?MallBaseId=' + MallBaseId
  },
  onLoad: function (option) {    
    this.u = uni.getStorageSync("mall_UserInfo");
    if (!this.u) {
      this.u = {
    	nickName: "未登录",
    	avatarUrl: ""
      };
      this.showAuth = true;
    }	else{
		this.GetForumList()
    }
	
  },
  methods: {
   
	GetForumList(){
		this.request2(
			{
				url: '/api/AppletMiai/GetForumList',
				data: {}
			},
			res => {
				if(res.resultCode==1){
					this.forumList = res.data
					
					
				}
			}
		);
	},
   
   select(x){
	   this.addMsg.ForumId =x.Id;
	   this.ForumName =x.Name;
	   this.show = false;
   },
	reloadUserinfo() {
	  this.u = uni.getStorageSync("mall_UserInfo");
	  // this.showAuth=false;
	  this.GetForumList();
	},
	//关闭登录窗口
	gbAuth(){
		uni.navigateBack()
	},
	onRemove1(index) {
		this.addMsg.ImageList.splice(index, 1);
	},
	uploadSuccessHandler(data, index, lists) {
		let r = JSON.parse(data);
		this.addMsg.ImageList.push(r.data);
	},
	confirm(val){
		let time = val.year + '-' + val.month + '-' + val.day +' ' + val.hour + ':' + val.minute + ':' + val.second;
		if((new Date()).getTime() >= (new Date(time)).getTime()){
			uni.showToast({
				title: '截止时间需要大于当前时间',
				icon: 'none',
				duration: 2500
			})
		}else{
			this.addMsg.Deadline = time
		}
		
	},
	releasebtn(){
		if(this.addMsg.ForumId == 0){
			uni.showToast({
				title: '请选择分类板块',
				icon: 'none',
			})
			return
		}
		if(this.addMsg.ActivityTitle == ''){
			uni.showToast({
				title: '编辑不能为空',
				icon: 'none',
			})
			return
		}
		if(this.addMsg.Content == ''){
			uni.showToast({
				title: '内容不能为空',
				icon: 'none',
			})
			return
		}
		if(this.addMsg.IsOpenEnroll==1 && this.addMsg.Deadline == ''){
			uni.showToast({
				title: '请选择报名截止时间',
				icon: 'none',
			})
			return
		}
		let pages = getCurrentPages(); // 当前页面
		let beforePage = pages[pages.length - 2]; // 前一个页面
		uni.showLoading({
			title:'发布中~'
		})
		this.request2(
			{
				url: '/api/AppletMiai/SetActivityInfo',
				data: this.addMsg
			},
			res => {
				uni.hideLoading()
				if(res.resultCode==1){
					uni.showToast({
						title: '发布成功',
						icon: 'success',
					})
					let that = this
					setTimeout(()=>{
						 uni.navigateBack({
							 success: function() {
								 beforePage.$vm.Children(that.addMsg.ForumId); // 执行前一个页面的方法
							 }
						 });
					},1000)
					
				}
			}
		);
		
	}


  },
};
</script>
