<template>
	<view class="page">
		<!-- 反馈内容 -->
		<view class="feedback-data">
			<view class="titleall-box">
				<view class="titleall-left">
					<view class="titleall-left-line"></view>填写公示公告信息
				</view>
			</view>
			<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" style="width: 96%;margin: 0 auto;">
				<uni-forms-item label="标题" required name="title">
					<uni-easyinput v-model="valiFormData.title" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="摘要" required name="subTitle">
					<uni-easyinput v-model="valiFormData.subTitle" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="详细内容">
					<uni-easyinput type="textarea" v-model="valiFormData.bodyContent" placeholder="请输入" />
				</uni-forms-item>
			</uni-forms>
			<view class="example-body">
				<uni-file-picker limit="9" @select="select" title="信息图片"></uni-file-picker>
			</view>
			<!-- 选择反馈类型 -->
			<view class="feedback-type">
				<view class="feedback-type-left">隶属类别</view>
				<view class="feedback-type-right">
					<view class="title">
						<text>{{TypeArray[TypeIndex]}}</text>
					</view>
					<view class="picker">
						<picker @change="FeedbackTypeCh" :value="TypeIndex" :range="TypeArray">
							<view class="uni-input">{{TypeArray[TypeIndex]}}</view>
						</picker>
					</view>
					<view class="more">
						<image src="../../static/down1.png"></image>
					</view>
				</view>
			</view>
			<view class="isNotice" style="display: flex; align-items: center; margin-top: 20rpx;justify-content: space-between;">
				<view class="lef" style="margin-left: 30rpx;">
					已选择<span>{{companysId.length}}</span>家企业
				</view>
				<view class="rig" @click="toTellInfo()" style="display: flex;align-items: center;">
					<text>选择企业</text>
					<view class="more-content">
						<view class="more-content-img">
							<image src="../../static/right2.png"></image>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 提交 -->
		<view v-if="have" class="submit-btn" @click="submitContent('valiForm')">
			<text>发布</text>
		</view>
		<view v-else class="submit-btn" @click="updateEvent('valiForm')">
			<text>保存</text>
		</view>
	</view>
</template>

<script>
	import {
		data
	} from '../../uview-ui/libs/mixin/mixin';
	import request from '@/utils/request.js'
import utils from '../../service/utils';
	export default {
		data() {
			return {
				userCode: {},
				// 选择图片路径
				filePath: [],
				uploadPath: [],
				type: "",
				lastModifyUserId: '',
				// 校验表单数据
				// 需要通知的公司的id
				companysId:[],
				valiFormData: {
					title: '',
					bodyContent: '',
					subTitle: '',
				},
				creater: '',
				rules: {
					title: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					subTitle: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
				},
				baseUrl: "https://www.dygxq-es.cn",
				// 判断是否传了id
				isId:'',
				have:false,
				detailData:[],
				categoryId:['360234116366468357','530362479830631685'],
				TypeArray: ['公示', '通知'],
				TypeIndex: 0,
				tellInfo:[]
			};

		},
		// onShow() {
		// 	this.getType()
		// },
		onUnload() {
			uni.$off('idInfo')
			uni.$off('tellInfo')
		},
		onLoad(options) {
			console.log("options",options)
			 uni.$on('idInfo', res => {
			  	console.log("选择公司页面返回的公司ID", res)
			  	if(res.length > 0)
			  	this.companysId = res
			 })
			 uni.$on('tellInfo', res => {
			 	console.log("tell信息", res)
			 	if(res.length > 0)
			 	this.tellInfo = res
			 })
			if(JSON.stringify(options) == "{}"){
				this.have = true
			}else{
				this.isId = JSON.parse(options.data)
				console.log("Id",this.isId)
				this.getConDetail(this.isId)
			}
			this.getUser()
			let user = uni.getStorageSync('user')
			this.lastModifyUserId = user.userInfo.userId

		},
		methods: {
			/**
			 * 反馈类型
			 * @param {Object} val
			 */
			FeedbackTypeCh(val) {
				console.log(val);
				this.TypeIndex = val.detail.value;
			},
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				this.filePath = e.tempFilePaths
			},
			// 跳转到选择通知企业页面
			toTellInfo() {
				uni.navigateTo({
					url: '/pages/tellInfo/tellInfo'
				})
			},
			// 获取用户信息
			getUser() {
				if (uni.getStorageSync('user')) {
					this.userCode = uni.getStorageSync('user').userInfo
					// this.userInfo = this.userCode.userAccount
					// this.userInfo = '123456'
					console.log('用户已登录！', this.userCode)
				} else {
					uni.showToast({
						title: '请登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/index'
						})
					})
				}
			},
			// 提交内容
			submitContent(ref) {
				let that = this
				this.$refs[ref].validate().then(res => {
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})

					this.uploadPath = []
					for (let i = 0; i < this.filePath.length; i++) {
						uni.uploadFile({
							url: "https://www.dygxq-es.cn/api/file/Uploader/1",
							filePath: this.filePath[i],
							name: 'file',
							success: (res) => {
								console.log(JSON.parse(res.data))
								let data = JSON.parse(res.data).data
								console.log(data)
								this.uploadPath.push(data)
							}
						})
					}
					console.log('需要', this.uploadPath)
					setTimeout(function(){
						that.postAll()
					},1500)
				})
			},

			// 上传所有
			postAll() {
				console.log("进入POST")
				let that = this
				let categoryId = that.categoryId[that.TypeIndex]
				console.log(categoryId)
				console.log(that.uploadPath)
				if (that.uploadPath) {
					console.log('jinru')
					// console.log(uni.getStorageSync('token'))
					var info = {
							title: that.valiFormData.title,
							bodyContent: that.valiFormData.bodyContent,
							categoryId:categoryId,
							creatorUserId: that.userCode.userId,
							subTitle: that.valiFormData.subTitle,
							imgUrl: that.uploadPath,
							
					}
					console.log(info)
					this.API.postInfo(info).then(res=>{
						console.log("1111",res);
						console.log("res",res);
						if (res.code == 200) {
							console.log("上传后的值", res)
							if(this.companysId.length > 0){
								this.tellNotice(res.data.Id)
							}
							uni.showToast({
								icon: "success",
								title: "发布成功！"
							}).then(()=>{
								uni.navigateTo({
									url:'/pages/notice/notice'
								})
							})
						}
					}).catch((err)=>{
						console.log(err)
					})
				}
			},
			// /**
			//  * 反馈类型
			//  * @param {Object} 
			//  */
			// FeedbackTypeCh(val) {
			// 	console.log(val);
			// 	this.TypeIndex = val.detail.value;
			// }
			// 跳转到选择通知企业页面
			toChooseCom() {
				uni.navigateTo({
					url: '/pages/chooseCom/chooseCom'
				})
			},
			// 进行通知
			tellNotice(id){
				// 进行通知
				request({
					url:'/api/extend/demo/SendMessageByApp',
					method:'post',
					data:{
						title:this.tellInfo[0],
						companys:this.companysId,
						desc:this.tellInfo[1],
						reid:id,
						link:'/pages/conDetail/conDetail',
						fangshi:'通知',
						type:'公示公告通知'
					}
				}).then(res=>{
					console.log('通知结果',res)
				})
			},
			// 获取信息
			getConDetail(id){
				let that = this
				let currentId = id
				console.log(currentId)
				that.API.getConstructDetail(currentId).then(res =>{
					console.log(res)
					let createTime = utils.formatTime(res.data.creatorTime)
					res.data.creatorTime = createTime
					that.detailData = res.data
					console.log(that.detailData)
					this.valiFormData.title = that.detailData.tItle
					this.valiFormData.subTitle = that.detailData.summary
					this.valiFormData.bodyContent = that.detailData.content
				})
			},
			
			// 信息更新
			updateEvent(ref){
				let that = this
				this.$refs[ref].validate().then(res => {
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})
				
					this.uploadPath = []
					for (let i = 0; i < this.filePath.length; i++) {
						uni.uploadFile({
							url: "https://www.dygxq-es.cn/api/file/Uploader/1",
							filePath: this.filePath[i],
							name: 'file',
							success: (res) => {
								console.log(JSON.parse(res.data))
								let data = JSON.parse(res.data).data
								console.log(data)
								this.uploadPath.push(data)
							}
						})
					}
					console.log('需要', this.uploadPath)
					setTimeout(function(){
						that.updateData()
					},1500)
				})
			},
			// 更新接口
			updateData(){
				let that = this
				if (that.uploadPath) {
					console.log('jinru')
					// console.log(uni.getStorageSync('token'))
					var info = {
							tItle: that.valiFormData.title,
							content: that.valiFormData.bodyContent,
							creatorUserId: that.userCode.userId,
							summary: that.valiFormData.subTitle,
							image: that.uploadPath,
							id:this.isId
					}
					console.log(info)
					this.API.modifyCon(info).then(res=>{
						console.log("1111",res);
						console.log("res",res);
						if (res.code == 200) {
							console.log("上传后的值", res)
							uni.showToast({
								icon: "success",
								title: "更新成功！"
							})
						}
					}).catch((err)=>{
						console.log(err)
					})
			}
		}
	},
	}
</script>

<style scoped lang="scss">
	@import 'Feedback.scss';
	.more-content{
		display: flex;
		align-items: center;
		image{
			width: 100rpx;
			height: 100rpx;
			border-radius: 100%;
		}
		.more-content-img {
			image {
				width: 24rpx;
				height: 24rpx;
			}
		}
	}
</style>