<template>
	<cu-custom bgColor="bg-white">
		<template #backText>
			<uni-icons type="left" size="22" @click="BackPage"></uni-icons>
		</template>
		<template #right>
			 <!-- disabled="true" -->
			<view class="right_view"><button class="mini-btn" type="primary" size="mini" style="padding: 0px 10px;" @click="publish">发表</button></view>
		</template>
	</cu-custom>
	<view style="padding: 10px 15px;">
		<view>
			<uni-easyinput type="textarea" :inputBorder="false" v-model="params.content" placeholder="这一刻的想法...."></uni-easyinput>
		</view>
		<view style="height: 600rpx;">
			<sendtoImg @send="params.img_arr = $event" @videourl="video_file1 = $event" :img_arr1="img_arr1" :video_file1="video_file1"></sendtoImg>
		</view>
		<view>
			<view class="icons_view" @click="chooseLocation">
				<view style="display: flex;align-items: center;">
					<uni-icons type="location" size="30"></uni-icons> 
					<view style="font-size: 18px;margin-left: 10px;">{{positioningList.content?positioningList.content:'所在位置'}}</view>
				</view>
				<view><uni-icons type="right" size="20"></uni-icons></view>
			</view>
			<!-- <view style="display: flex;align-items: center;justify-content: space-between;border-top: 1px solid #e5e5e5;padding: 10px 0px;">
				<view style="display: flex;align-items: center;">
					<uni-icons type="person" size="30"></uni-icons> <view style="font-size: 18px;">提醒谁看</view>
				</view>
				<view><uni-icons type="right" size="18"></uni-icons></view>
			</view> -->
			<view class="icons_view" style="border-bottom: 2px solid #e5e5e5;" @click="toggle">
				<view style="display: flex;align-items: center;">
					<uni-icons type="person" size="30"></uni-icons> <view style="font-size: 18px;margin-left: 10px;">谁可以看</view>
				</view>
				<view style="display: flex;align-items: center;">
					<view style="font-size: 18px;color: #a3a3a3;">{{params.privacy==1?'公开':params.privacy==2?'部分可见':params.privacy==3?'私密':'不给谁看'}}</view>
					<uni-icons type="right" size="20"></uni-icons>
				</view>
			</view>
		</view>
	</view>
	
	<uni-popup ref="popup" background-color="#fff" style="z-index: 999999;">
		<view class="popup-content">
			<cu-custom bgColor="bg-white">
				<template #backText>
					<uni-icons type="left" size="22" @click="BackPage1"></uni-icons>
				</template>
				<template #content>
					<view>谁可以看</view>
				</template>
				<template #right>
					 <!-- disabled="true" -->
					<view class="right_view"><button class="mini-btn" type="primary" size="mini" style="padding: 0px 10px;" @click="complete">完成</button></view>
				</template>
			</cu-custom>
			<view>
				<view style="padding: 30rpx 60rpx;">
					<radio-group @change="radioChange">
						<label>
							<view style="display: flex;align-items: center;margin-bottom: 30px;">
								<radio value="1" :checked="params.privacy==1" style="transform:scale(0.7)"/>
								<view>
									<view style="font-size: 30rpx;">公开</view>
									<view style="font-size: 22rpx;color: #a3a3a3;">所有朋友可见</view>
								</view>
							</view>
						</label>
						<label>
							<view style="display: flex;align-items: center;margin-bottom: 30px;">
								<radio value="3" :checked="params.privacy==3" style="transform:scale(0.7)" />
								<view>
									<view style="font-size: 30rpx;">私密</view>
									<view style="font-size: 22rpx;color: #a3a3a3;">仅自己可见</view>
								</view>
							</view>
						</label>
						<label>
							<view style="margin-bottom: 30px;">
								<view style="display: flex;align-items: center;">
									<radio value="2" :checked="params.privacy==2" style="transform:scale(0.7)" />
									<view>
										<view style="font-size: 30rpx;">部分可见</view>
									</view>
								</view>
								<view style="padding: 40rpx 60rpx;" v-if="params.privacy==2">
									<view @click="editUser">
										<view style="color: #004f9f;font-size: 30rpx;">选择朋友</view>
										<block v-for="(item,index) in user1" :key="index" v-if="user1.length!==0" style="display: flex;">
											<text style="font-size: 22rpx;margin-right: 5px;">{{item.displayName?item.displayName:item.nickname}}</text>
										</block>
										<view v-else style="font-size: 22rpx;">选中的朋友可见</view>
									</view>
								</view>
							</view>
						</label>
						<label>
							<view>
								<view style="display: flex;align-items: center;">
									<radio value="4" :checked="params.privacy==4" style="transform:scale(0.7)" />
									<view>
										<view style="font-size: 30rpx;">不给谁看</view>
									</view>
								</view>
								<view style="padding: 40rpx 60rpx;" v-if="params.privacy==4">
									<view @click="editUser1">
										<view style="color: #004f9f;font-size: 30rpx;">选择朋友</view>
										<block v-for="(item,index) in user2" :key="index" v-if="user2.length!==0" style="display: flex;">
											<text style="font-size: 22rpx;margin-right: 5px;">{{item.displayName?item.displayName:item.nickname}}</text>
										</block>
										<view v-else style="font-size: 22rpx;">选中的朋友不可见</view>
									</view>
								</view>
							</view>
						</label>
					</radio-group>
				</view>
			</view>
		</view>
	</uni-popup>
</template>

<script>
	import sendtoImg from '@/components/sendtoImg.vue'
	export default {
		components: {
			sendtoImg
		},
		data() {
			return {
				params:{
					content:"",
					privacy:1,
					location:'',
					status:1,
					img_arr:[],
					file_id:"",
					user_ids:[],
					posts_id:0,
					video_file:""
				},
				video_file1:{},
				video_file2:{},
				positioningList:{},
				user1:[],
				user2:[],
				img_arr1:[],
				bols:false
			}
		},
		created() {
			// console.log(this.params.privacy);
			this.getLastPosts()
		},
		onShow() {
			if(uni.getStorageSync('selectUser')){
				this.user1 = uni.getStorageSync('selectUser')
			}
			if(uni.getStorageSync('selectUser1')){
				this.user2 = uni.getStorageSync('selectUser1')
			}
		},
		methods: {
			radioChange(e){
				this.params.privacy = +e.detail.value
				// console.log(this.params.privacy);
			},
			toggle() {
				this.$refs.popup.open('bottom')
			},
			complete(){
				this.$refs.popup.close()
				if(this.params.privacy==2){
					this.user2 = [];
					uni.removeStorageSync('selectUser');
				}else if(this.params.privacy==4){
					this.user1 = [];
					uni.removeStorageSync('selectUser1');
				}else{
					this.user1 = [];
					this.user2 = [];
					uni.removeStorageSync('selectUser');
					uni.removeStorageSync('selectUser1');
				}
			},
			BackPage1(){
				if(this.bols==false){
					this.params.privacy = 1;
					this.user1 = [];
					this.user2 = [];
					uni.removeStorageSync('selectUser');
					uni.removeStorageSync('selectUser1');
				}
				this.$refs.popup.close();
			},
			editUser(){
				if(uni.getStorageSync('selectUser')){
					this.user1 = uni.getStorageSync('selectUser')
					const list = []
					this.user1.forEach((res)=>{
						list.push(res.user_id)
					})
					uni.navigateTo({
						url: `/pages/index/userSelection?type=${5}&user_ids=${JSON.stringify(list)}&num=${1}`
					})
				}else{
					uni.navigateTo({
						url: `/pages/index/userSelection?type=${5}&num=${1}`
						// url: '/pages/index/userSelection?type='+type+'&contact_id=' + this.contact.id
					})
				}
			},
			editUser1(){
				if(uni.getStorageSync('selectUser1')){
					this.user2 = uni.getStorageSync('selectUser1')
					const list1 = []
					this.user2.forEach((res)=>{
						list1.push(res.user_id)
					})
					uni.navigateTo({
						url: `/pages/index/userSelection?type=${6}&user_ids=${JSON.stringify(list1)}&num=${1}`
					})
				}else{
					uni.navigateTo({
						url: `/pages/index/userSelection?type=${6}&num=${1}`
						// url: '/pages/index/userSelection?type='+type+'&contact_id=' + this.contact.id
					})
				}
			},
			transformToArrayObject(arr) {
			  // 创建空对象
			  const resultObj = {};
			  
			  // 遍历数组，用索引作为键
			  arr.forEach((value, index) => {
			    resultObj[index] = value;
			  });
			  
			  // 将对象包裹在数组中
			  return [resultObj];
			},
			BackPage(){
				const _this = this
				console.log(this.video_file1.url);
				if(this.params.content!==''||this.params.img_arr.length!=0||this.video_file1.url){
					uni.showModal({
						content: '保留此次编辑',
						cancelText: '不保留',
						confirmText: '保留',
						success: function (res) {
							if (res.confirm) {
								console.log('用户点击确定');
								_this.params.status = 2;
								_this.publish()
							} else if (res.cancel) {
								_this.user1 = [];
								_this.user2 = [];
								uni.removeStorageSync('selectUser');
								uni.removeStorageSync('selectUser1');
								if(_this.params.posts_id){
									_this.Delete(_this.params.posts_id)
								}else{
									uni.navigateBack({
										delta: 1
									});
								}
							}
						}
					});
				}else{
					uni.showModal({
						content: '退出此次编辑？',
						cancelText: '取消',
						confirmText: '退出',
						confirmColor: "red",
						success: function (res) {
							if (res.confirm) {
								_this.user1 = [];
								_this.user2 = [];
								uni.removeStorageSync('selectUser');
								uni.removeStorageSync('selectUser1');
								uni.navigateBack({
									delta: 1
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			Delete(id){
				console.log(id);
				this.$api.compaApi.Deleteapost({posts_id:id}).then(res => {
					uni.navigateBack({
						delta: 1
					});
				})
			},
			chooseLocation(){
				uni.chooseLocation({
					success: (res) => {
							// console.log('位置名称：' + res.name);
							// console.log('详细地址：' + res.address);
							// console.log('纬度：' + res.latitude);
							// console.log('经度：' + res.longitude);
							let message={
								type:'location',
								status:'going',
								content:'[位置] '+res.name,
								name:res.name,
								latitud: res.latitude+ '+' +res.longitude,
								extends:res
							};
							this.positioningList = message
							// this.$emit('send',Object.assign(this.msgItem(), message),false);
						}
				})
			},
			getLastPosts(){
				this.$api.compaApi.getLastPosts().then(res => {
					if(res.code==0&&res.data!==null){
						console.log(res);
						this.params.content = res.data.content
						// this.params.img_arr = res.data.files
						this.positioningList.latitud = res.data.location
						this.positioningList.content = res.data.location_address
						this.params.posts_id = res.data.posts_id
						this.params.privacy = res.data.privacy
						if(res.data.files[0]&&res.data.files[0].type==1){
							this.img_arr1 = res.data.files
						}else{
							this.img_arr1 = res.data.files
							res.data.files.forEach((res1)=>{
								this.video_file1 = {url:res1.src,poster:res1.privacy,type:res1.type}
							})
						}
						this.bols = true
						if(res.data.privacy==2){
							this.user1 = res.data.privacy_user
							uni.setStorageSync('selectUser', this.user1);
						}else if(res.data.privacy==4){
							this.user2 = res.data.privacy_user
							uni.setStorageSync('selectUser1', this.user2);
						}
						console.log(res.data.privacy);
						// this.list = res.data
					}
				})
			},
			publish(){
				let list1 = []
				let list2 = []
				let listUrl = []
				
				if(this.params.privacy == 2){
					this.user1.forEach((res)=>{
						list1.push(res.user_id)
					})
				} else if(this.params.privacy == 4){
					this.user2.forEach((res)=>{
						list2.push(res.user_id)
					})
				}
				
				if(this.img_arr1&&this.img_arr1[0]?.type==1){
					this.img_arr1.forEach(res=>{
						listUrl.push(res.src)
					})
				}
				if(this.params.content==''&&this.params.img_arr.length==0&&this.video_file1.url==undefined){
					uni.showToast({title: '文字或图片、视频必须上传一个,并且文字不能是空格',icon:'none'});
					return
				}
				
				const data = {
					privacy: this.params.privacy,
					content: this.params.content||'',
					status: this.params.status,
					location: this.positioningList.latitud || '',
					address: this.positioningList.content || '',
					img_arr: this.params.img_arr.length==0?listUrl:this.params.img_arr||'',
					video_file: this.video_file1.url || '',
					poster_file: this.video_file1.poster || '',
					user_ids: this.params.privacy == 2 ? list1 : this.params.privacy == 4 ? list2 : [],
					posts_id: this.params.posts_id||''
				}
				// console.log('传值',data);
				this.$api.compaApi.wechatMomentsadd(data).then(res => {
					// this.list = res.data
					// console.log(res);
					if(res.code==0){
						list1 = []
						list2 = []
						if(this.params.status==2){
							uni.showToast({title: '保存成功'});
						}else{
							uni.showToast({title: '添加成功'});
						}
						uni.navigateBack({
							delta: 1
						});
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
.right_view{
	height: 30px;
	margin-right: 10px;
}
uni-button[disabled][type='primary']{
	background-color:#e7e7e7;
	color: #c6c6c6;
}
uni-button:after{
	border: 0px;
}
::v-deep .bg-white{
	background-color: #f1f1f1 !important;
}
::v-deep .uni-easyinput__content{
	background-color: #f1f1f1 !important;
}
.icons_view{
	display: flex;
	padding: 10px 10px;
	align-items: center;
	border-top: 2px solid #e5e5e5;
	justify-content: space-between;
}
::v-deep .uni-modal__bd{
	color: #000;
}
.popup-content {
	height: 100vh;
	align-items: center;
	background-color: #fff;
	justify-content: center;
}
</style>