<template>
	<view class="">
		<!-- 输入框 -->
		<view class="fir_li" v-if="objInfo.form_type == 'input'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box">
				<view class="form_right">
					<input type="text" class="inputBox" :value="objInfo.value" @input="changeInput" :placeholder="objInfo.input_tips">
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</view>
		</view>
		<!-- 时间选择器  年月日 -->
		<view class="fir_li" v-if="objInfo.form_type == 'DatePicker'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box" style="display: flex;">
				<picker @change="changeStartDate" mode="date" :end="objInfo.end_time"
					style="width:50%;border-right:1rpx solid #CCCCCC">
					<view class="form_right">
						<view style="flex:1;text-align: center;"
							:class="objInfo.start_time ? 'valueactive' : '' ">
							{{objInfo.start_time ? objInfo.start_time : '开始时间'}}</view>
					</view>
				</picker>
				<picker @change="changeEndDate" mode="date" :start="objInfo.start_time" style="width:50%">
					<view class="form_right">
						<view style="flex:1;text-align: center;"
							:class="objInfo.end_time ? 'valueactive' : '' ">{{objInfo.end_time ? objInfo.end_time : '结束时间'}}
						</view>
					</view>
				</picker>
			</view>
		</view>
		<!-- 时间选择器  年月日  时分 -->
		<view class="fir_li" v-if="objInfo.form_type == 'TimePicker'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box" style="display: flex;">
				<picker mode="multiSelector" 
					:range="dateTimeArray" 
					:value="dateTime" 
					@change="changeStartTime" 
					@columnchange="changeColumn" 
					style="width:50%;border-right:1rpx solid #CCCCCC">
					<view class="form_right">
						<view style="flex:1;text-align: center;"
							:class="objInfo.start_time ? 'valueactive' : '' ">
							{{objInfo.start_time ? objInfo.start_time : '开始时间'}}</view>
					</view>
				</picker>
				<picker 
					mode="multiSelector"
					:range="dateTimeArray"
					:value="dateTime" 
					@change="changeEndTime" 
					@columnchange="changeColumn"
					style="width:50%">
					<view class="form_right">
						<view style="flex:1;text-align: center;"
							:class="objInfo.end_time ? 'valueactive' : '' ">{{objInfo.end_time ? objInfo.end_time : '结束时间'}}
						</view>
					</view>
				</picker>
			</view>
		</view>
		<!-- 文本框 -->
		<view class="fir_li" v-if="objInfo.form_type == 'textarea'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box_text">
				<view class="form_right">
					<textarea :value="objInfo.value ? objInfo.value : ''" :placeholder="objInfo.input_tips" @input="changeTextarea" />
				</view>
			</view>
		</view>
		<!-- 数字输入框 -->
		<view class="fir_li" v-if="objInfo.form_type == 'input-number'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box">
				<view class="form_right">
					<input type="digit" class="inputBox" @input="changeInputNumber" :class="objInfo.value ? 'valueActive' : '' "
						placeholder-style="color:#999;font-size:32rpx;" :value="objInfo.value"
						:placeholder="objInfo.input_tips" />
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</view>
		</view>
		<!-- 单选框 -->
		<view class="fir_li" v-if="objInfo.form_type == 'radio'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box_radio">
				<view class="ra_box" v-for="(item,index) in objInfo.content" :key="index" @click="changeRadio(item)">
					<radio :value="item" color="#008EFF" :checked="item == objInfo.value" />{{item}}
				</view>
			</view>
		</view>
		<!-- 多选框 -->
		<view class="fir_li" v-if="objInfo.form_type == 'checkbox'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box_radio">
				<checkbox-group @change="changeCheckbox" style="display: flex;justify-content: flex-start;flex-wrap: wrap;" :value="objInfo.value">
					<label class="" style="margin-right:15rpx;margin-bottom:25rpx;display: block;" v-for="(item,index) in objInfo.content" :key="index">
						<checkbox class="checkbox" color="#ff7800" :value="item" :checked="objInfo.value.indexOf(item) != -1"/>
						<span>{{item}}</span>
					</label>
				</checkbox-group>
			</view>
		</view>
		<!-- 评分 -->
		<view class="fir_li" v-if="objInfo.form_type == 'Rate'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box_radio">
				<u-rate @change="changeRate" v-model="objInfo.value" active-color="#FFD049" inactive-color="#b2b2b2" :size="35" :gutter="10"></u-rate>
			</view>
		</view>
		<!-- 区域筛选 -->
		<!-- #ifdef MP-WEIXIN || APP-PLUS-->
		<view class="fir_li" v-if="objInfo.form_type == 'Cascader'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box">
				<picker @change="changeAddress" mode="region">
					<view class="form_right">
						<view :class="objInfo.value ? 'valueActive' : '' ">
							{{objInfo.value ? objInfo.value : objInfo.input_tips}}
						</view>
						<view class="iconfont icon-arrows_right"></view>
					</view>
				</picker>
			</view>
		</view>
		<!-- #endif -->
		
		<!-- #ifdef MP-ALIPAY || H5 -->
		<view class="fir_li" v-if="objInfo.form_type == 'Cascader'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box">
				<view class="form_right" @click="show = true" >
					<view :class="objInfo.value ? 'valueActive' : '' ">{{objInfo.value ? objInfo.value : objInfo.input_tips}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view>
			</view>
		</view>
		<!-- #endif -->
		<!-- 下拉选择 -->
		<view class="fir_li" v-if="objInfo.form_type == 'select'">
			<view class="li_label">{{objInfo.name}}</view>
			<view class="li_box">
				<picker @change="changeSelect" :range="objInfo.content" :range-key="'label'">
					<view class="form_right">
						<view :class="objInfo.value ? 'valueActive' : '' ">
							{{objInfo.value ? objInfo.value : objInfo.input_tips}}
						</view>
						<view class="iconfont icon-arrows_right"></view>
					</view>
				</picker>
				<!-- <view class="form_right" @click="changeSelect" >
					<view :class="objInfo.value ? 'valueActive' : '' ">{{objInfo.value ? objInfo.value : objInfo.input_tips}}</view>
					<view class="iconfont icon-arrows_right"></view>
				</view> -->
			</view>
		</view>
		
		<!-- 位置选择 -->
		<u-select 
			v-model="show" 
			mode="mutil-column-auto" 
			value-name="code" 
			label-name="name" 
			child-name="children" 
			:list="addressList"
			 @confirm="confirm"
		></u-select>
		
		<!-- 单选 -->
		<tki-tree
			ref="treeChild" 
			:range="objInfo.content"
			:rangeKey="'label'" 
			:idKey="'nodeKey'"
			confirmColor="#008EFF"
			@confirm="selectTree" 
		></tki-tree>
		
	</view>
</template>

<script>
	import { dateTimePicker, getMonthDay } from '@/util/dateTimePicker.js'
	import tkiTree from "@/components/tki-tree/tki-tree.vue"
	const citysJSON = require('@/util/address.js');
	
	export default{
		
		data() {
			return{
				dateTimeArray: [],
				dateArr: [],
				dateTime: [],
				showTree:false,
				//地址
				addressList:[],
				show:false,
			}
		},
		props:{
			objInfo: {
				type: Object,
				default: {}
			},
		},
		created() {
			if (this.objInfo.form_type == 'TimePicker') {
				this.initTime()
			}
			// #ifdef MP-ALIPAY || H5
			if (this.objInfo.component == 'Cascader') {
				this.addressList = citysJSON.citys
			}
			// #endif
		},
		methods:{
			withData(param) {
				return param < 10 ? '0' + param : '' + param;
			},
			initTime() {
				let date = new Date()
				let endYear = date.getFullYear()
				let mont = this.withData(date.getMonth() + 1)
				let day = this.withData(date.getDate())
				let hour = this.withData(date.getHours())
				let minu = this.withData(date.getMinutes())
				let seco = this.withData(date.getSeconds())
				// 获取完整的年月日 时分秒，以及默认显示的数组
				let obj = dateTimePicker();
				// 精确到分的处理，将数组的秒去掉
				let lastArray = obj.dateTimeArray.pop();
				let lastTime = obj.dateTime.pop();
				let lastDate = obj.dateArray.pop()
				this.dateTimeArray = obj.dateTimeArray
				this.dateArr = obj.dateArray
				this.dateTime = obj.dateTime
			},
			changeColumn(e) {
				let index = e.detail.column
				let value = e.detail.value
				if (index == 1) {
					let month = this.dateArr[index][value]
					let date = new Date();
					let endYear = date.getFullYear();
					let obj = dateTimePicker();
					// 精确到分的处理，将数组的秒去掉
					let lastArray = obj.dateTimeArray.pop();
					let lastTime = obj.dateTime.pop();
					let lastDate = obj.dateArray.pop()
					this.dateTimeArray = obj.dateTimeArray
					this.dateArr = obj.dateArray
					this.dateTime = obj.dateTime
				}
			},
			handleTime(arr) {
				let year = this.dateArr[0][arr[0]]
				let month = this.dateArr[1][arr[1]]
				let day = this.dateArr[2][arr[2]]
				let hour = this.dateArr[3][arr[3]]
				let minu = this.dateArr[4][arr[4]]
				let str = year + '-' + month + '-' + day + ' ' + hour + ':' + minu
				return str
			},
			//开始时间  年月日时分
			changeStartTime(e) {
				let arr = e.detail.value
				if (arr.includes(-1)) {
					uni.showToast({
						title: '请选择正确的时间',
						icon: 'none'
					})
					return
				}
				let str = this.handleTime(arr)
				this.$emit('changeSearchData',this.objInfo.field,'start_time',str)
				if(!this.objInfo.end_time){
					this.$emit('changeSearchData',this.objInfo.field,'end_time',str)
				}
			},
			//结束时间  年月日时分
			changeEndTime(e) {
				let arr = e.detail.value
				if (arr.includes(-1)) {
					uni.showToast({
						title: '请选择正确的时间',
						icon: 'none'
					})
					return
				}
				let str = this.handleTime(arr)
				this.$emit('changeSearchData',this.objInfo.field,'end_time',str)
				if(!this.objInfo.start_time){
					this.$emit('changeSearchData',this.objInfo.field,'start_time',str)
				}
			},
			//输入框
			changeInput(e) {
				let value = e.detail.value
				this.$emit('changeSearchData',this.objInfo.field,'value',value)
			},
			//开始时间  年月日
			changeStartDate(e) {
				let value = e.detail.value
				this.$emit('changeSearchData',this.objInfo.field,'start_time',value)
				if(!this.objInfo.end_time){
					this.$emit('changeSearchData',this.objInfo.field,'end_time',value)
				}
			},
			//结束时间  年月日
			changeEndDate(e) {
				let value = e.detail.value
				this.$emit('changeSearchData',this.objInfo.field,'end_time',value)
				if(!this.objInfo.start_time){
					this.$emit('changeSearchData',this.objInfo.field,'start_time',value)
				}
			},
			//文本框 
			changeTextarea(e) {
				let value = e.detail.value
				this.$emit('changeSearchData', this.objInfo.field, 'value',value)
			},
			//数字输入框
			changeInputNumber(e) {
				let value = e.detail.value
				this.$emit('changeSearchData', this.objInfo.field, 'value',value)
			},
			//单选框
			changeRadio(item) {
				this.$emit('changeSearchData', this.objInfo.field, 'value',item)
			},
			//多选框
			changeCheckbox(e) {
				let arr = e.detail.value
				this.$emit('changeSearchData', this.objInfo.field, 'value',arr.join(','))
			},
			//评分
			changeRate(e) {
				this.$emit('changeSearchData', this.objInfo.field, 'value', e)
			},
			//区域筛选
			changeAddress(e) {
				let arr = e.detail.value
				this.$emit('changeSearchData', this.objInfo.field, 'value', arr.join('/'))
			},
			confirm(e) {
				let address = e[0].label+e[1].label+e[2].label
				this.$emit('changeSearchData', this.objInfo.field, 'value', address)
			},
			//下拉选择
			changeSelect(e) {
				// this.$refs.treeChild._show()
				let index = e.detail.value
				let value = this.objInfo.content[index].label
				this.$emit('changeSearchData', this.objInfo.field, 'value', value)
			},
			selectTree(arr) {
				let value = []
				arr.forEach(ele=>{
					value.push(ele.label)
				})
				this.$emit('changeSearchData', this.objInfo.field, 'value', value.join(','))
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.fir_li {
		margin-bottom: 20rpx;
		
		.li_label {
			font-size: 28rpx;
			color: #333333;
			margin-bottom: 15rpx;
		}
		
		.li_box {
			border-radius: 15rpx;
			border: 1rpx solid #CCCCCC;
			height: 60rpx;
			padding: 0 35rpx;
			line-height: 60rpx;
			.form_right {
				display: flex;
				justify-content: space-between;
				color: #999;
				.inputBox{
					width:85%;
					height: 60rpx;
					line-height: 60rpx;
				}
			}
		}
		.li_box_text{
			border-radius: 15rpx;
			border: 1rpx solid #CCCCCC;
			padding: 0 35rpx;
			.form_right{
				textarea{
					width:100%;
				}
			}
		}
		.li_box_radio{
			margin:24rpx 0;
			display: flex;
			justify-content: flex-start;
			flex-wrap: wrap;
			.ra_box{
				display: flex;
				justify-content: center;
				margin-right:15rpx;
				radio{
					transform: scale(0.7);
				}
			}
		}
	}
</style>