<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 :range="formList" range-key="name" :value="formIndex" @change="formTypeChange">
				<view class="input-box ddflex">
					<view class="fflex">{{formIndex>-1?formList[formIndex].name:'请选择'}}</view>
					<image class="rico" src="../../static/pages/images/more.png"></image>
				</view>
			</picker>
		</view>
		<view class="form-input">
			<view class="input-label">统计时间</view>
			<picker mode="date" :fields="formType==1?'year':formType==2?'month':'day'" :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>
			<picker :range="text2List" range-key="name" :value="text2Index" @change="(e)=>{textChange(e,2)}">
				<view class="input-box ddflex">
					<view class="fflex">{{text2Index>-1?text2List[text2Index].name:'请选择'}}</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="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 class="form-input">
			<view class="input-label">文本四</view>
			<picker :range="text4List" range-key="name" :value="text4Index" @change="(e)=>{textChange(e,4)}">
				<view class="input-box ddflex">
					<view class="fflex">{{text4Index>-1?text4List[text4Index].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 {
				formIndex:0,
				formList:[{name:'年报',value:1},{name:'月报',value:2},{name:'日报',value:3}],
				formType:1,
				
				time:'',
				
				title:'',
				
				text1Index:-1,
				text1List:[],
				
				text2Index:-1,
				text2List:[],
				
				text3Index:-1,
				text3List:[],
				
				text4Index:-1,
				text4List:[],
				
				codeDetail:[]
			}
		},
		onLoad(options) {
			this.getTextsList()
		},
		onShow() {
			
		},
		methods: {
			// 获取各下拉框的选项并初始化数据
			getTextsList(){
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				this.codeDetail = prevPage.$vm.codeDetail
				
				this.formType = prevPage.$vm.formType
				this.formIndex = this.formList.findIndex(item=>item.value==prevPage.$vm.formType)
				this.time = prevPage.$vm.time
				this.title = prevPage.$vm.title
				
				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.text2List = item.selectValues
						if(prevPage.$vm.dataForm[1].code){
							this.text2Index = this.text2List.findIndex(item=>item.code==prevPage.$vm.dataForm[1].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)
						}
					}
					if(item.chineseName == '文本四') {
						this.text4List = item.selectValues
						if(prevPage.$vm.dataForm[3].code){
							this.text4Index = this.text4List.findIndex(item=>item.code==prevPage.$vm.dataForm[3].code)
						}
					}
				})
			},
			formTypeChange(e){
				this.formIndex = e.detail.value
				this.formType = this.formList[this.formIndex].value
				this.time = ''
			},
			timeChange(e){
				this.time = e.detail.value
			},
			textChange(e,type){
				if(type==1){
					this.text1Index = e.detail.value
				}
				if(type==2){
					this.text2Index = e.detail.value
				}
				if(type==3){
					this.text3Index = e.detail.value
				}
				if(type==4){
					this.text4Index = 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.text2Index<0) return req.msg('请选择文本二')
				if(this.text3Index<0) return req.msg('请选择文本三')
				if(this.text4Index<0) return req.msg('请选择文本四')
				
				
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.formType = this.formType
				prevPage.$vm.time = this.time
				prevPage.$vm.title = this.title
				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.text2List[this.text2Index].code+','+this.text3List[this.text3Index].code+','+this.text4List[this.text4Index].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.text2List[this.text2Index].name,
						code:this.text2List[this.text2Index].code,
						data:JSON.parse(textData[this.text2List[this.text2Index].code])
					})
					dataForm.push({
						chineseName:'文本三',
						title:this.text3List[this.text3Index].name,
						code:this.text3List[this.text3Index].code,
						data:JSON.parse(textData[this.text3List[this.text3Index].code])
					})
					dataForm.push({
						chineseName:'文本四',
						title:this.text4List[this.text4Index].name,
						code:this.text4List[this.text4Index].code,
						data:JSON.parse(textData[this.text4List[this.text4Index].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
					}
					if(this.formType==1){
						param.startTime = this.time+'-01-01 00:00:00'
						param.endTime = this.time+'12-31 23:59:59'
					}else if(this.formType==2) {
						//获取当月最后一天的日期
						let date = new Date()
						var year = date.getFullYear(); //获取年份
						var month = date.getMonth() + 1; //获取月份
						var lastDate = new Date(year, month , 0).getDate(); //获取当月最后一日
						month = month < 10 ? '0' + month : month ; //月份补 0

						param.startTime = this.time+'-01 00:00:00'
						param.endTime = [year,month ,lastDate ].join("-")+' 23:59:59'
					}else if(this.formType==3){
						param.startTime = this.time+' 00:00:00'
						param.endTime = this.time+' 23:59:59'
					}
					req.getRequest('/api/material/longPoster/data',param,res=>{
						r(res)
					})
				})
			}
		},
		mounted() {
		}
	}
</script>

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