<template>
	<view>
		<!-- 账单 -->
		<view class="form-input">
			<view class="input-label">标题</view>
			<view class="input-box ddflex">
				<input v-model="title" class="fflex" placeholder="请填写"/>
			</view>
		</view>
		<view class="form-input">
			<view class="input-label">统计时间</view>
			<picker mode="date" fields="year" :value="time" @change="timeChange">
				<view class="input-box ddflex">
					<view class="fflex">{{time?time:'请选择'}}</view>
					<image class="rico" src="../../static/pages/images/more.png"></image>
				</view>
			</picker>
		</view>
		<view class="form-input">
			<view class="input-label">文本一</view>
			<picker :range="text1List" range-key="name" :value="text1Index" @change="(e)=>{textChange(e,1)}">
				<view class="input-box ddflex">
					<view class="fflex">{{text1Index>-1?text1List[text1Index].name:'请选择'}}</view>
					<image class="rico" src="../../static/pages/images/more.png"></image>
				</view>
			</picker>
		</view>
		<view class="form-input">
			<view class="input-label">文本二</view>
			<view class="input-box ddflex">
				<input v-model="text2Title" class="fflex" placeholder="请填写"/>
			</view>
			<view class="input-box ddflex">
				<textarea v-model="text2Content" placeholder="请填写"></textarea>
			</view>
		</view>
		<view class="form-input">
			<view class="input-label">文本三</view>
			<picker :range="text3List" range-key="name" :value="text3Index" @change="(e)=>{textChange(e,3)}">
				<view class="input-box ddflex">
					<view class="fflex">{{text3Index>-1?text3List[text3Index].name:'请选择'}}</view>
					<image class="rico" src="../../static/pages/images/more.png"></image>
				</view>
			</picker>
		</view>
		
		<view style="height: 150rpx;"></view>
		<view class="share-btn">
			<view class="btn" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	const req = require("../../utils/request.js");

	export default {
		components: {},
		props: {},
		data() {
			return {
				time:'',
				title:'',
				
				text1Index:-1,
				text1List:[],
				
				text3Index:-1,
				text3List:[],
				
				text2Title:'',
				text2Content:'',
				
				codeDetail:[]
			}
		},
		onLoad(options) {
			this.getTextsList()
		},
		onShow() {
			
		},
		methods: {
			// 获取各下拉框的选项并初始化数据
			getTextsList(){
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				this.codeDetail = prevPage.$vm.codeDetail
				
				this.time = prevPage.$vm.time
				this.title = prevPage.$vm.title
				
				this.text2Title = prevPage.$vm.dataForm[1].title
				this.text2Content = prevPage.$vm.dataForm[1].data.value
				
				this.codeDetail.map(item=>{
					if(item.chineseName == '文本一') {
						this.text1List = item.selectValues
						if(prevPage.$vm.dataForm[0].code){
							this.text1Index = this.text1List.findIndex(item=>item.code==prevPage.$vm.dataForm[0].code)
						}
					}
					if(item.chineseName == '文本三') {
						this.text3List = item.selectValues
						if(prevPage.$vm.dataForm[2].code){
							this.text3Index = this.text3List.findIndex(item=>item.code==prevPage.$vm.dataForm[2].code)
						}
					}
				})
				console.log(this.text1List)
			},
			timeChange(e){
				this.time = e.detail.value
			},
			textChange(e,type){
				if(type==1){
					this.text1Index = e.detail.value
				}
				if(type==3){
					this.text3Index = e.detail.value
				}
			},
			async submit(){
				if(!this.title) return req.msg("请填写标题")
				if(!this.time) return req.msg('请选择统计时间')
				if(this.text1Index<0) return req.msg('请选择文本一')
				if(!this.text2Title) return req.msg('请填写文本二')
				if(!this.text2Content) return req.msg('请填写文本二内容')
				if(this.text3Index<0) return req.msg('请选择文本三')
				
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.title = this.title
				prevPage.$vm.time = this.time
				prevPage.$vm.dataForm = await this.getSubmitData()
				uni.navigateBack()
			},
			// 获取选择后的文本数据
			getSubmitData(){
				return new Promise(async (r,j)=>{
					let dataForm = []
					let textData = await this.getTextData(this.text1List[this.text1Index].code+','+this.text3List[this.text3Index].code)
					dataForm.push({
						chineseName:'文本一',
						title:this.text1List[this.text1Index].name,
						code:this.text1List[this.text1Index].code,
						data:JSON.parse(textData[this.text1List[this.text1Index].code])
					})
					dataForm.push({
						chineseName:'文本二',
						title:this.text2Title,
						code:'',
						data:{value:this.text2Content}
					})
					dataForm.push({
						chineseName:'文本三',
						title:this.text3List[this.text3Index].name,
						code:this.text3List[this.text3Index].code,
						data:JSON.parse(textData[this.text3List[this.text3Index].code])
					})
					r(dataForm)
				})
			},
			getTextData(code){
				return new Promise(async (r,j)=>{
					let param = {
						startTime:'2023-01-01 00:00:00',
						endTime:'2023-12-31 23:59:59',
						code:code
					}
					param.startTime = this.time+'-01-01 00:00:00'
					param.endTime = this.time+'12-31 23:59:59'
					req.getRequest('/api/material/longPoster/data',param,res=>{
						r(res)
					})
				})
			}
		},
		mounted() {
		}
	}
</script>

<style>
	@import "./longPosterContent.css";
</style>