<template>
	<view class="hzIndex">
		<hzHeader title="服务申请"></hzHeader>
		<view class="hzContent">
		<view class="main">
			<view class="tab-box">
				<view class="tab-item" :class="currentTabIndex == 0 ? 'active' : ''" @click="currentTabIndex = 0">
					服务申请
					<view class="activeLine" v-if="currentTabIndex == 0"></view>
				</view>
				<view class="tab-item" :class="currentTabIndex == 1 ? 'active' : ''" @click="currentTabIndex = 1">
					申请记录
					<view class="activeLine" v-if="currentTabIndex == 1"></view>
				</view>
			</view>
		</view>
		<view class="content" v-if="currentTabIndex == 0" style="position: relative;">
			<view class="interviewee">
				<view class="form-box" style="border-radius: 10upx;">
					<!-- <view class="form-item">
						<text class="label">部门:</text>
						<view class="ipt-box select">
							<image class="arrow" src="/static/icons/right-arrow.png" mode=""></image>
							<picker class="picker" v-if="deptList.length > 0" :range="deptList" @change="changeDepartment" range-key="name">
								<view>{{ deptList[departmentIndex].name }}</view>
							</picker>
						</view>
					</view> -->
					<view class="form-item">
						<text class="label">服务类型:</text>
						<view class="ipt-box select">
							<image class="arrow" src="/static/icons/right-arrow.png" mode=""></image>
							<picker class="picker" v-if="serviceType.length > 0" :range="serviceType" @change="changeSecondRepairItems" range-key="DATA_VALUE">
								<view>{{ serviceType[secondIndex].DATA_VALUE }}</view>
							</picker>
						</view>
					</view>
					<view class="form-item">
						<text class="label">办公室<text style="color: red;">*</text>:</text>
						<view class="ipt-box"><input class="top-t" v-model="office" placeholder="请输入办公室" type="text" maxlength="15" /></view>
					</view>
					<view class="form-item">
						<!-- <text class="label">需求描述:</text>
						<view class="ipt-box">
							<input class="top-t" v-model="demand" placeholder="请输入需求描述" type="text" maxlength="50" />
						</view> -->
						<view style="color: #333;font-size: 30upx;font-weight: bold;padding: 10upx 0px;">
							<text>需求描述<text style="color: red;">*</text>：</text>
						</view>
						<view style="width: 100%;border: 1px solid #E5E5E5;margin-top: 20upx;padding: 10upx;min-height: 200upx;color: #666;border-radius: 10upx;">
							<textarea class="textarea"  v-model="demand"  placeholder="请输入需求描述"  maxlength="100"/>
						</view>
					</view>
					<view class="form-item">
						<text class="label">数量<text style="color: red;">*</text>:</text>
						<view class="ipt-box"><input type="number" class="top-t" v-model="number" placeholder="请输入数量" @input="changeNumber()"/></view>
					</view>
					<view class="form-item">
						<text class="label">服务时间<text style="color: red;">*</text>:</text>
						<view class="ipt-box"><uni-datetime-picker :clear-icon="false" v-model="applicantTime" type="datetime" /></view>
					</view>
				</view>
				<!-- <view class="footer"><text class="input_left">图片（最多上传3张）</text></view>
				<view class="pic">
					<view class="img-box">
						<view class="img-item" v-for="(item, index) in imgs" :key="index">
							<image class="remove-btn" @click="removeImg(index)" src="/static/icons/delete-icon.png" mode=""></image>
							<image class="img" :src="item.replace(/[\r\n]/g,'')" mode=""></image>
						</view>
						<view class="img-item upload-btn" @click="chooseImg()" v-if="imgs.length < 3"><image class="img" src="/static/imgs/tianjia-img.png" mode=""></image></view>
					</view>
				</view> -->
			</view>
			<view class="occupied"></view>
			<view class="footer-btn" @click="submit"><view class="btn">提交</view></view>
		</view>
		<!-- 申请列表 -->
		<view v-if="currentTabIndex == 1" style="position: relative;">
			<view class="list-content" v-if="requestList.length > 0">
				<view class="details" v-for="(item, i) in requestList" :key="i" >
					<view class="detail_header" @click="details(item)">
						<!-- <text class="blueSign"></text> -->
						<image src="/static/img/serverIcon.png" mode="" style="width: 35upx;height: 40upx;margin-left: 20upx;vertical-align: middle;"></image>
						<text class="font-tilte">服务申请</text>
						<text v-if="item.STATE =='0'" class="statusred" style="color: #ff9000;background-color: #FFEFE3;">待处理</text>
						<text v-if="item.STATE =='1'" class="statusred" style="color: #ff9000;background-color: #FFEFE3;">进行中</text>
						<text v-if="item.STATE =='2'" class="statusred" style="color: #0DB0A7;background-color: #C1EFEA;">已完成</text>
					</view>

					<view class="detail_msg">
						<!-- <view class="info-item" @click="details(item)">
							<text>部门：</text>
							<text class="infoSpan">{{ item.repair_dept_name }}</text>
						</view>
						<view class="info-item" @click="details(item)">
							<text>报修人：</text>
							<text class="infoSpan">{{ item.repair_person_name }}</text>
						</view>
						<view class="info-item" @click="details(item)">
							<text>联系方式：</text>
							<text class="infoSpan">{{ item.repair_person_phone }}</text>
						</view> -->
						
						<!-- <view class="info-item">
							<text>报修地点：</text>
							<text class="infoSpan">{{ item.repair_place }}</text>
						</view> -->
						<view class="info-item" @click="details(item)">
							<text>服务类型：</text>
							<text class="infoSpan">{{item.DATA_VALUE}}</text>
						</view>
						<view class="info-item" @click="details(item)">
							<text>办公室：</text>
							<text class="infoSpan">{{item.OFFICE}}</text>
						</view>
						<view class="info-item" @click="details(item)">
							<text>服务时间：</text>
							<text class="infoSpan">{{ item.APPLICANT_TIME}}</text>
						</view>
						<view  v-if="item.USER_NAME"  class="info-item" @click="details(item)">
							<text>申请人：</text>
							<text class="infoSpan">{{ item.USER_NAME }}</text>
						</view>
						<!-- <view class="info-item" @click="details(item)">
							<text>位置：</text>
							<text class="infoSpan">{{item.repair_place+" "+(item.repair_floor||'')+(item.repair_room_no?"-"+item.repair_room_no+'':'')}}</text>
						</view> -->
						<!-- <view class="info-item" @click="details(item)">
							<text>是否紧急：</text>
							<text class="infoSpan">{{ urgents[item.urgent]}}</text>
						</view>
						 -->
						
						<!-- <view class="info-item" @click="details(item)">
							<text>时间：</text>
							<text class="infoSpan">{{ item.repair_time}}</text>
						</view> -->
						<!-- <view v-if="item.reply_flag=='1'" class="info-item" @click="details(item)">
							<text>回复时间：</text>
							<text class="infoSpan">{{ formatDate(item.reply_date,'dateTime')}}</text>
						</view> -->
					<!-- 	<view  class="info-item" style="width: 100%;text-align: center;" @click="details(item)">
							<text style="width: 100%;color: #4db4ea;">点击查看详情及回复</text>
						</view> -->
						<!-- <view class="comment-btn" v-if="item.is_evaluate == 0 && item.reply_flag =='2'" @click="goEvaluatePage(item.id)">评价</view> 
						<view class="comment-btn bg-ccc" v-if="item.is_evaluate != 0">已评价</view> -->
					</view>
				</view>
			</view>
			<content-none v-else :padTop="20"></content-none>
		</view>
		</view>
		<load-more v-if="currentTabIndex == 1" :loadingType="loadingType" :contentText="contentText"></load-more>
		<canvas style="width: 300px; height: 300px;" canvas-id="convertImgToBase64"></canvas>
	</view>
</template>

<script>
import { getPropertyType,addProperty,getPropertyByUserId  } from '@/common/api.js';
import { callbackRequest, getStorage, alertTip, convertImgToBase64, formatDate,wxSdk ,uploadImg} from '@/common/util.js';
import loadMore from '@/components/loadMore.vue';
import contentNone from '@/components/contentNone.vue';

import { pathToBase64 } from '@/js_sdk/mmmm-image-tools/index.js';
export default {
	components: {
		loadMore,
		contentNone
	},
	data() {
		return {
			today:new Date().getTime(),
			formatDate: formatDate,
			//分页
			page: 1,
			pageSize: 10,
			loadingType: 0, //0-loading前；1-loading中；2-没有更多了
			contentText: {
				contentdown: '上拉加载更多',
				contentrefresh: '正在加载...',
				contentnomore: '没有更多数据了'
			},
			places: [{ id: '1', name: '科技楼' }, { id: '2', name: '调度楼' }, { id: '3', name: '后勤楼' }, { id: '4', name: '会议中心' }],//, { id: '4', name: '丽景酒店' }, { id: '5', name: '其它' }
			deptList: [], 
			placeIndex: 0,
			departmentIndex:0,
			userInfo: getStorage('userInfo'),
			entryOrderPic: [],
			showIndex: 0,
			currentTabIndex: 0,
			requestList: [
			],
			office:'',
			demand:'',
			number: 0,
			applicantTime: '',
			serviceType:[],
			secondIndex:0,
			imgs: [],
			imgsId:[],
			index: 0,
			
			// repairFloor:'',
			// repairRoomNo:'',
			// urgents:['不紧急','紧急'],
			// urgentIndex:0,
			pageTitle:['报修','报事'],
			submitFlag:true,
			showAlert:false,
		};
	},
	
	watch: {
		currentTabIndex: function(newVal, oldVal) {
			if (newVal ==1) {
				this.page = 1;
				this.loadingType = 0;
				this.dataList();
			}else{
				// this.getDepartment();
			}
		}
	},
	
	onLoad(option) {
		this.currentTabIndex = option.currentTabIndex || 0;
		// 部门
		
	},
	onShow() {
		this.getServiceType();
		if (this.currentTabIndex == 0) {
			//this.getDepartment();
		}else{
			this.page = 1;
			this.loadingType = 0;
			this.dataList();
		}
	},
	methods: {
		getServiceType(){
			this.secondIndex=0;
			// var parentCode = this.firstRepairItems[this.firstIndex].code;
			let params = {
				method: getPropertyType,
				data: {
					userId: this.userInfo.id,
				}
			};
			callbackRequest(params).then(res => {
				console.log(res)
				res = res.data;
				if (res.returnCode == 1) {
					if(res.returnData.length>0){
						this.serviceType = res.returnData;
					}else{
						this.serviceType = [{id:'',name:'请直接描述'}]
					}
					
				} else {
					alertTip(res.returnMsg);
				}
			});
		},
		dataList() {
			let params = {
				method: getPropertyByUserId,
				data: {
					type:'0',
					userId: this.userInfo.id,
					pageNum: this.page,
					pageSize: this.pageSize,
				}
			};
			callbackRequest(params).then(res => {
				res = res.data;
				if (res.returnCode == 1) {
					if (this.page == 1) {
						this.requestList = res.returnData || [];
					} else {
						if (res.returnData.length > 0) {
							this.requestList = this.requestList.concat(res.returnData);
							console.log(this.requestList)
							this.loadingType = 0;
						} else {
							this.loadingType = 2;
						}
					}
				} else {
					alertTip(res.returnMsg);
				}
			});
		},
		
		submit() {
			// var regPhone = /^1[3|4|5|6|7|8|9]\d{9}$/;
			// if (!regPhone.test(this.formTelp)) {
			// 	alertTip('请输入正确的手机号');
			// 	return false;
			// }
			// if (!this.formBxwz) {
			// 	alertTip('请输入详细位置');
			// 	return false;
			// } 
			if(!this.submitFlag){
				return false;
			}
			this.submitFlag=false;
			if (!this.office) {
				alertTip('请输入办公室');
				this.submitFlag = true;
				return false;
			}
			if (!this.number) {
				alertTip('请输入数量');
				this.submitFlag = true;
				return false;
			}
			if (!this.demand) {
				alertTip('请输入需求描述');
				this.submitFlag = true;
				return false;
			}
			if (!this.applicantTime) {
				alertTip('请选择服务时间');
				this.submitFlag = true;
				return false;
			}
			// console.log(this.userInfo)
			let params = {
				method: addProperty,
				data: {
					userId:this.userInfo.id,
					userName:this.userInfo.userName,
					phone:this.userInfo.mobile,
					orgId:this.userInfo.orgId,
					orgName:this.userInfo.orgName,
					
					propertyId: this.serviceType[this.secondIndex].DATA_CODE,
					propertyName: this.serviceType[this.secondIndex].DATA_VALUE,
					office: this.office,
					demand: this.demand,
					number: this.number,
					office: this.office,
					applicantTime: this.applicantTime,
					
				}
			};
			// console.log(params)
			callbackRequest(params).then(res => {
				res = res.data;
				if (res.returnCode == 1) {
					alertTip(res.returnMsg);
					
					setTimeout(_ => {
						this.requestList = [];
						this.firstIndex = 0;
						this.secondIndex = 0;
						this.office = '';
						this.demand = '';
						this.number = 0;
						this.currentTabIndex = 1;
						this.applicantTime = '';
						this.submitFlag = true;
						this.dataList()
					}, 2000);
				} else {
					alertTip(res.returnMsg);
					this.submitFlag = true;
				}
				
			});
		},
		showradio: function(index) {
			var self = this;
			self.firstIndex = index;
			this.getServiceType();
		},
		//上传图片
		async chooseImg() {
			let chec = 0;
			let sty = [];
			let _this = this;
			uni.showActionSheet({
				itemList:['拍照','相册'],
				success:function(resp){
					chec = resp.tapIndex;
					if (chec == 0) {
						sty = ['camera']; //从相册相机选择 
					} else {
						sty = ['album']; //从相册相机选择
					}
					uni.chooseImage({
						count:3,
						sizeType: ['compressed'],
						sourceType: sty, 
						quality: 1,
						success: async function(res){
							let resData = res.tempFilePaths;
							for(let i=0;i<resData.length;i++){
								if(_this.imgs.length>3){
									break;
								}
								uni.getImageInfo({
									src: resData[i],
									success:async (path) => {
										convertImgToBase64(path.path, async function(base64) {
											if(_this.imgs.length<3){
												// _this.imgs.push(base64);
												uni.showLoading({
													title: '图片上传中'
												})
												let blength = base64.length;
												let glength = 7500;
												if(blength<=glength){
													let uploadParam = {
														base64: base64,
														userId:  getStorage('userInfo').userId+i,
														groupEnd: '1'
													}
													await uploadImg(uploadParam).then(resu => {
														resu = resu.data ;
														uni.hideLoading();
														if (resu.returnCode != 1) {
															alertTip(resu.returnMsg);
														}else{
															_this.imgs.push(base64);
															_this.imgsId.push(resu.returnData+".jpg");
														}
													});
												}else{
													let fnum = parseInt(blength/glength);
													for(let j=1;j<fnum+1;j++){
														if(j==fnum){
															let groupBase = base64.slice((j-1)*glength);
															console.log(groupBase.length,"groupBaselength");
															let uploadParam = {
																base64: groupBase,
																userId: getStorage('userInfo').userId+i,
																groupEnd: '1'
															}
															await uploadImg(uploadParam).then(resu => {
																resu = resu.data ;
																if (resu.returnCode == 1) {
																	_this.imgs.push(base64);
																	_this.imgsId.push(resu.returnData+".jpg");
																} else {
																	uni.hideLoading();
																	alertTip(resu.returnMsg);
																	return;
																}
															});
														}else{
															let groupBase = base64.slice((j-1)*glength,glength*j);
															let uploadParam = {
																base64: groupBase,
																userId: getStorage('userInfo').userId+i,
																groupEnd: '0'
															}
															await uploadImg(uploadParam).then(resu => {
																resu = resu.data ;
																if (resu.returnCode == 1) {
																} else {
																	uni.hideLoading();
																	alertTip(resu.returnMsg);
																	return;
																}
															});
														}
														
													}
													uni.hideLoading();
												}
												
												
											}
											
										});
									},
								});
							}
							
						}
					});
				},
				fail:function(resp){
					console.log(resp.errMsg);
				}
			});
		},
		//删除图片
		removeImg(index) {
			this.imgs.splice(index, 1);
			this.imgsId.splice(index, 1);
		},
		getDepartment() {
			let data = {
				method: getAllDept,
				data: {}
			};
			callbackRequest(data).then(res => {
				console.log(res);
				if (res.data.returnCode == 1) {
					this.deptList = res.data.returnData;
					for(var i=0;i<this.deptList.length;i++){
						if(this.deptList[i].id==getStorage('userInfo').deptId){
							this.departmentIndex = i;
						}
					}
				}
			});
		},
		navTo(url) {
			uni.navigateTo({
				url
			});
		},
		goEvaluatePage(id) {
			uni.navigateTo({
				url: `/pages/malfunc-repair/malfunc-evaluate?type=9&typeId=${id}`
			});
		},
		changePlace(e) {
			this.placeIndex = e.detail.value;
		},
		changeSecondRepairItems(e) {
			// console.log(e)
			this.secondIndex = e.detail.value;
		},
		changeNumber() {
			console.log(this.number)
			console.log(typeof(this.number))
			if(Number(this.number)>0){
				if(Number(this.number)>=100){
					setTimeout(()=>{
						this.number = 100;
					},100)
				}else if(Number(this.number)>0 && Number(this.number)<10){
					this.number = this.number.replace('0',"");
					// console.log(this.number)
				}
			}else{
				setTimeout(()=>{
					this.number = 0;
				},100)
			}
			console.log(this.number)
		},
		changeUrgent(e){
			this.urgentIndex = e.detail.value;
		},
		details: function(item) {
			uni.navigateTo({
				url: '/pages/malfunc-repair/service-request-detail?id=' + item.ID
			});
		},
		//选择部门
		changeDepartment(f) {
			this.departmentIndex = f.detail.value;
		},
		
	},
	onReachBottom() {
		if (this.loadingType !== 0 || this.currentTabIndex == 0) {
			return;
		}
		this.loadingType = 1;
		this.page = this.page + 1;
		this.dataList();
	},
};
</script>

<style lang="scss">
@import url('../../static/css/repair/malfunction-repair.css');
@import '@/static/css/common.scss';

.nolist {
	font-size: 30upx;
	color: #797979;
	text-align: center;
	margin-top: 20upx;
}

 

.main {
	margin: 180upx 40upx 0;
	position: relative;
	z-index: 10;

	.tab-box {
		overflow: hidden;
		// border-radius: 10upx 10upx 0 0;
		// background: rgba(132, 149, 167, 0.8);
		height: 80upx;

		.tab-item {
			float: left;
			height: 70upx;
			width: 30%;
			font-size: 30upx;
			// color: #e5f6ff;
			// text-align: center;
			line-height: 70upx;
		}

		.active {
			// background: #fff;
			color: #333;
			border-radius: 10upx 10upx 0 0;
			font-weight: bold;
		}
		.activeLine{
			background: linear-gradient( 90deg, #50CBA1 0%, #06AEA6 100%), #5685F2;
			border-radius: 10upx;
			width: 80upx;
			height: 10upx;
		}
	}
}


.list-content {
	font-size: 28upx;
	margin: 0 25rpx;
	margin-top: 20upx;
	
	.details {
		background: white;
		padding: 0 20upx 20upx;
		border-radius: 15upx;
		margin-bottom: 20rpx;
		.detail_header {
			border-bottom: #f8f8f8 5upx solid;
			height: 100upx;
			line-height: 100upx;
			// margin: 0;
			// border-radius: 10upx;

			.blueSign {
				height: 28upx;
				width: 5upx;
				background: #00c277;
				display: inline-block;
				margin-left: 20upx;
				vertical-align: middle;
			}

			.font-tilte {
				font-size: 32upx;
				font-weight: bold;
				margin-left: 20upx;
				vertical-align: middle;
			}
		}

		.detail_msg {
			margin-left: 20upx;
			border-radius: 10upx;
			overflow: hidden;
			view {
				height: 60upx;
				line-height: 60upx;
			}

			.status {
				position: absolute;
				color: red;
				margin-left: 580upx;
				margin-top: -15upx;
			}
		}
	}
}

.statusgr {
	float: right;
	margin-right: 30upx;
	color: #00c277;
}

.statusred {
	width: 160upx;
	height: 60upx;
	line-height: 60upx;
	text-align: center;
	border-radius: 50upx;
	float: right;
	font-size: 24upx;
	margin-right: 20upx;
	margin-top: 25upx;
}

.img-box {
	overflow: hidden;
	padding-top: 20rpx;
	padding-bottom: 10rpx;

	.img-item {
		float: left;
		width: 200upx;
		height: 200upx;
		border: 1px solid #ddd;
		margin: 0 22rpx 20upx 0upx;
		position: relative;
		box-sizing: border-box;

		.img {
			display: block;
			width: 100%;
			height: 100%;
		}

		.remove-btn {
			position: absolute;
			top: -18upx;
			right: -18upx;
			width: 44upx;
			height: 44upx;
			z-index: 2;
		}
	}

	.upload-btn {
		.img {
			width: 60upx;
			height: 60upx;
			margin: unset;
		}
	}
}
.detail_msg .info-item {
	font-size: 28upx;
	margin-right: 30upx;
	text {
		display: inline-block;
		width: 140upx;
		color: #999;
	}

	.infoSpan {
		width: auto;
		font-size: 28upx;
		color: #999;
		float: right;
	}
}
.form-box {
	background: #fff;
	.form-item {
		overflow: hidden;
		border-bottom: 1px solid #eee;
		padding: 30upx 20upx;
		.label {
			float: left;
			width: 150upx;
			font-size: 28upx;
			color: #333;
			font-weight: bold;
			line-height: 50upx;
		}
		.ipt-box {
			margin-left: 160upx;
			input {
				width: 100%;
				height: 50upx;
				line-height: 50upx;
				font-size: 26upx;
				color: #666;
			}
		}
		.select {
			.arrow {
				float: right;
				width: 16upx;
				height: 29upx;
				margin-top: 10upx;
			}
			.picker {
				height: 50upx;
				line-height: 50upx;
				font-size: 26upx;
				color: #666;
				margin-left: 10upx;
			}
		}
	}
}

.comment-btn {
	float: right;
	font-size: 24upx;
	border-radius: 10upx;
	color: #fff;
	// line-height: 30upx;
	text-align: center;
	min-width: 120upx;
	margin-right: 30upx;
	background-color: #00c277;
}

.common-shade{
	position:fixed;
	top:0;
	left:0;
	width:100%;
	height:100vh;
	background: rgba(0,0,0,0.7);
	z-index:99;
}

.box {
	display: flex;
	display: -webkit-flex;
}

.box-row-reverse {
	flex-direction: row;
}

/*上下排列*/
.box-column {
	flex-direction: column;
}

/*上下排列*/
.box-left {
	justify-content: flex-start;
}

/*居右*/
.box-right {
	justify-content: flex-end;
}

/*居左*/
.box-center {
	justify-content: center;
}

/*居中*/
.box-between {
	justify-content: space-between;
}

/*两端对齐*/
.box-align-center {
	align-items: center;
}

.box-wrap {
	flex-wrap: wrap;
}
</style>
