<template>
	<view class="container">
		<view class="box">
			<!-- 报修 -->
			<view v-if="sucess">
				<view class="rows">
					<view class="rows_tit">报修小区</view>
					<view class="rows_con">
						<input type="text" placeholder="请选择报修小区" disabled @click="chooseCom" :value="CommunityInfo.name" />
					</view>
				</view>
				<view class="rows">
					<view class="rows_tit">楼宇号</view>
					<view class="rows_con">
						<input type="text" :value="FacName" placeholder="请选择楼宇号" @click="chooseFac" data-type="1" disabled />
					</view>
				</view>
				<view class="rows">
					<view class="rows_tit">房间号</view>
					<view class="rows_con">
						<input type="text" :value="RoomName" placeholder="请选择房间号" @click="chooseFac" data-type="2" disabled />
					</view>
				</view>

				<view class="rows">
					<view class="rows_tit">提交类型</view>
					<view class="rows_con">
						<input type="text" :value="currentType.text" placeholder="请选择提交类型" @click="changeShow" disabled />
					</view>
				</view>

				<view class="rows">
					<view class="rows_tit">报修人</view>
					<view class="rows_con">
						<input type="text" placeholder="请输入报修人" :value="Name" data-name='Name' @input="headerInputClick" />
					</view>
				</view>
				<view class="rows">
					<view class="rows_tit">报修人电话</view>
					<view class="rows_con">
						<input type="number" maxlength="11" placeholder="请输入报修人电话" :value="Phone" data-name='Phone' @input="headerInputClick" />
					</view>
				</view>

				<view class="information">
					<view class="containerMsg" v-if="currentType.value && currentType.value == 1">
						<view class="title">报修内容</view>
							<u--textarea :value="Msg" @input="headerInputClick2" data-name="Msg" placeholder="请输入留言"  maxlength='50' count  border="none"  />
					</view>

					<view class="containerMsg" v-if="currentType.value && currentType.value == 2">
						<view class="title">意见反馈</view>
							<u-textarea :value="Msg" @input="headerInputClick2" data-name="Msg" autoHeight placeholder="请输入留言" maxlength='50' count border="none" />
					</view>

					<view class="gray"></view>
					<view class="uploadImg">
						<view class="title">图片上传</view>
						<view style="margin-top: 20rpx;">
							<u-upload
								:fileList="fileList"
								maxCount="6"
								@beforeRead="beforeRead"
								@afterRead="beforeRead"
								@delete="deleteImg"
								uploadIcon="plus"
							/>
						</view>
					</view>

					<view class="gray"></view>
					<view class="information_row noneBorder">
						<view class="information_tit">期望上门时间</view>

						<view class="timeBox" @click="switchShow">

							<view class="information_ipt" v-if="time">
								{{time}}
							</view>
							<u-icon name="arrow-right" size="32" color="#D5AC66" />
						</view>


					</view>
				</view>

				<view class="btn" @click="submit">提交</view>
			</view>
			<!-- 报修成功 -->
			<view v-if="!sucess" class="sucess">
				<image src="https://wechat-img-file.oss-cn-beijing.aliyuncs.com/water_filter/maintenance_sucess.png"
					mode="widthFix" id="sucess" />
				<view class="sucess_msgTit">提交成功</view>
				<view class="sucess_msg">1条报修内容已提交成功</view>
				<!-- <image src="https://wechat-img-file.oss-cn-beijing.aliyuncs.com/water_filter/maintennace_msg.png" mode="widthFix" id="msg"/> -->
				<!-- <view id="msg" bind:tap="desc">
            查看报修详情>>
        </view> -->
			</view>
		</view>
		
			<u-datetime-picker
				:show="DoorTimeShow"
				mode="datetime"
				:minDate="minDate"
				:formatter="formatter"
				@confirm="onInput"
				@cancel="onClose"
			/>


		<u-picker :show="show" :columns="columns" keyName="name" @confirm="confirm" @cancel="onClose" />


	</view>
</template>

<script>
	import {
		request,
		picUrl,
		NavgateTo,
		upload,
		isPhone,
	} from '../../utils';
	import {
		apiArr
	} from '../../api/community';
	export default {
		data() {
			return {
				value: '',
				fileList: [],
				fileList2: [],

				CommunityInfo: {}, //小区信息
				FacId: "", //
				FacName: "",
				RoomId: "",
				RoomName: "",
				Name: "", //报修人
				Phone: "", //报修人电话
				Msg: "", //报修电话
				time: "", //上门时间
				DoorTimeShow: false,
				sucess: true, //是否报修成功

				minDate: new Date().getTime(),
				formatter(type, value) {
					if (type === 'year') {
						return `${value}年`;
					}
					if (type === 'month') {
						return `${value}月`;
					}
					if (type === 'day') {
						return `${value}日`;
					}
					return value;
				},

				columns: [
					[{
							name: "报事报修",
							type: 1
						},
						{
							name: "意见反馈",
							type: 2
						},
					]
				],
				show: false,
				currentType: {}
			}
		},
		methods: {
			chooseCom(){
				NavgateTo(`/community/index/index?joinType=3`, { isLogin: false })
			},
			
			chooseFac(e){
				console.log('eeee', e);
				const { type } = e.currentTarget.dataset;
				if(type == '1'){
					NavgateTo(`/community/chooseMsg/index?type=${type}&id=${this.CommunityInfo.id}`, { isLogin: false });
				}else{
					console.log(this.FacId);
					if(this.FacId){
						NavgateTo(`/community/chooseMsg/index?type=${type}&id=${this.CommunityInfo.id}&FacId=${this.FacId}`, { isLogin: false });
					}else{
						uni.showToast({
							title: '请先选择楼宇号',
							icon:"none"
						})
					}
				}
			},
			
			
			
		  changeShow(){
				this.show = true;
		  },
			
			confirm(e){
				console.log('eee', e)
				let pickerValue = {
					text: e.value[0].name,
					value: e.value[0].type
				}
				this.currentType = pickerValue;
				this.Msg = '',
				this.show = false;
			},
			
			onClose(){
				this.show = false;
				this.DoorTimeShow = false;
			},
			
			// 上传图片
			beforeRead(e){
				console.log(e);
				let that = this
				upload(e.file.url,(res)=>{
					let datas = JSON.parse(res)
					console.log(datas.data);
					let url = picUrl + datas.data.path
					let fileList = this.fileList
					let fileList2 = this.fileList2
					let obj = {
							url:url,
							name:'avatar'
					}
					let obj2 = {
							url:datas.data.path,
							name:'avatar'
					}
					fileList.push(obj);
					fileList2.push(obj2);
					this.fileList = fileList;
					this.fileList2 = fileList2;
					
				})
				
			},
			
			switchShow(){
				this.DoorTimeShow = true;
			},
			
			headerInputClick(e) {
				console.log('e', e);
				const { name } = e.currentTarget.dataset;
				const { value } = e.detail;
				this[name] = value;
			},
			
			
			headerInputClick2(e) {
				this.Msg = e;
			},
			
			
			onInput(event){
				const date = new Date(event.value); // 获取选中的 Date 对象
				const year = date.getFullYear(); // 获取年份
				const month = date.getMonth() + 1; // 获取月份（注意月份从 0 开始，需要 +1）
				const day = date.getDate(); // 获取日期
				const hours =date.getHours()
				const minutes = date.getMinutes(); 
				const time = `${year}-${month}-${day} ${hours}:${minutes}`;
				console.log(time);
				this.time = time;
				this.DoorTimeShow = false;
			},
			
			async submit(){
				console.log('123131', this);
				if(!this.CommunityInfo.name){
					uni.showToast({
						title: '请选择小区',
						icon:"none"
					})
					return
				}
		
				if(!this.FacName){
					uni.showToast({
						title: '请选择楼宇号',
						icon:"none"
					})
					return
				}
		
				if(!this.RoomName){
					uni.showToast({
						title: '请选择楼宇号',
						icon:"none"
					})
					return
				}
		
				if(!this.Name){
					uni.showToast({
						title: '请输入报修人',
						icon:"none"
					})
					return
				}
				if(!this.currentType){
					uni.showToast({
						title: '请选择提交类型',
						icon:"none"
					})
					return
				}
		
				if(!this.Phone){
					uni.showToast({
						title: '请输入报修人电话',
						icon:"none"
					})
					return
				}
		
				if(!isPhone(this.Phone)){
					uni.showToast({
						title: '请输入正确电话号',
						icon:"none"
					})
					return
				}
		
				if(!this.Msg){
					uni.showToast({
						title: '请填写报修内容/意见反馈',
						icon:"none"
					})
					return
				}
				if(this.fileList2.length === 0){
					uni.showToast({
						title: '请上传图片信息',
						icon:"none"
					})
					return
				}
				if(!this.time){
					uni.showToast({
						title: '请选择上门时间',
						icon:"none"
					})
					return
				}
			  let photos = this.fileList2.map(item=>item.url).join(',');
				const res = await request(apiArr.submit, 'POST', {
					community_id: this.CommunityInfo.id,
					facility_id: this.FacId,
					room_id: this.RoomId,
					hope_order_time: this.time,
					mobile: this.Phone,
					user_name: this.Name,   
					photos,
					title: this.currentType.text,
					current_remark: '',
					details: this.Msg,
					type: this.currentType.value,
				}, {silent: true, nested: true});
				
				console.log('1231', res);
				if(res.code == 1){
				// if(res.msg == '创建反馈信息成功'){
					uni.showToast({
						title: '创建反馈信息成功' || res.msg,
						icon:"none"
					})
					this.sucess = !this.sucess;
					uni.removeStorageSync('FacName');
					uni.removeStorageSync('FacId');
					uni.removeStorageSync('RoomName');
					uni.removeStorageSync('RoomId');
					uni.removeStorageSync('currentCommunity');
					uni.removeStorageSync('currentCommunityId');
				}
			},
			
			
			
			
			
		},

		onLoad(options) {
		},
		
		onShow() {
			let RoomName = uni.getStorageSync('RoomName')
			let RoomId = uni.getStorageSync('RoomId')
			let FacName = uni.getStorageSync('FacName')
			let FacId = uni.getStorageSync('FacId')
	
			let currentCommunity = uni.getStorageSync('currentCommunity')
			let currentCommunityId = uni.getStorageSync('currentCommunityId')
			let CommunityInfo = {
					id: currentCommunityId,
					name: currentCommunity
			};
			this.RoomName = RoomName;
			this.RoomId = RoomId;
			this.FacName = FacName;
			this.FacId = FacId;
			this.CommunityInfo = CommunityInfo;
		},
		
	}
</script>

<style>
	@import url("./index.css");
</style>