<template>
	<view class="hzIndex">
		<hzHeader title="咨询服务"></hzHeader>
		<view class="container hzContent">
			<view class="main">
				<view class="tab-box">
					<view class="tab-item" :class="currentTabIndex == 0 ? 'active_tab' : ''"
						@click="currentTabIndex = 0">
						咨询服务
						<view class="tabBottom" v-if="currentTabIndex == 0"></view>
					</view>
					<view class="tab-item" :class="currentTabIndex == 1 ? 'active_tab' : ''"
						@click="currentTabIndex = 1">
						咨询记录
						<view class="tabBottom" v-if="currentTabIndex == 1"></view>
					</view>
				</view>
			</view>
			<view class="content" v-if="currentTabIndex == 0">
				<view class="interviewee">
					<view class="form-box card-box">
						<view class="form-item" style="margin-bottom: 20upx; border-bottom:none ;">
							<text class="label">标题:</text>
							<view class="ipt-box"><input class="top-t" v-model="report.title" placeholder="请输入标题"
									type="text" maxlength="50" /></view>
						</view>
					</view>
					<view class="form-box card-box">
						<view class="form-item box box-column">
							<text class="label">内容:</text>
							<view class="textarea-box">
								<textarea maxlength="120" style="font-size: 26upx;" placeholder-style="font-size:26upx"
									v-model="report.content" placeholder="请输入内容" />
							</view>
						</view>
					</view>
					<view class="form-box card-box">
						<view class="form-item box box-column">
							<view class="label" style="width: 100%;">图片<text style="color: #CACACA;">（最多上传3张）</text>
							</view>
							<view class="pic" style="padding-left: 0;">
								<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 style="font-size: 28rpx;font-weight: 600;color: red;border-bottom: 1px solid #eee;">互联网信息系统严禁存储、传输、处理涉及国家秘密企业秘密以及敏感信息，违者将严肃问责追责!</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">
				<view class="list-content" v-if="reportList.length > 0">
					<view class="details" v-for="(item, i) in reportList" :key="i">
						<view class="detail_header" @click="details(item)">
							<text class="blueSign"></text>
							<text class="font-tilte">咨询服务</text>
							<text v-if="item.report_status ==0" class="statusred">待回复</text><!-- statusred -->
							<text v-if="item.report_status ==1" class="statusred" style="color: #ff9000;">已回复</text>
							<text v-if="item.report_status ==2" class="statusred" style="color: green;">已办结</text>
						</view>

						<view class="detail_msg">
							<view class="info-item" @click="details(item)">
								<text>标题：</text>
								<text class="infoSpan">{{item.title}}</text>
							</view>
							<!-- <view v-if="item.accept_date" class="info-item" @click="details(item)">
							<text>内容：</text>
							<text class="infoSpan">{{item.content}}</text>
						</view> -->
							<view class="info-item" @click="details(item)">
								<text>咨询时间：</text>
								<text class="infoSpan">{{ formatDate(item.create_time,'dateTime')}}</text>
							</view>
							<view v-if="item.reply_date" 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.report_status ==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>
			<load-more v-if="currentTabIndex == 1" :loadingType="loadingType" :contentText="contentText"></load-more>
			<!-- <view class="common-shade box box-center box-align-center" v-if="showAlert" @click="showAlert=false" >
			<view  style="background: white;height: 160upx;width: 100%;border-radius: 10upx 10upx 0upx 0upx;position: absolute;bottom: 0upx;">
				<view @click="photo" style="border-bottom: 1px solid #000000; border-radius: 10upx 10upx 0upx 0upx; height: 80upx;line-height: 80upx;text-align: center;">
					拍摄
				</view>
				<view @click="getImage" style="height: 80upx;line-height: 80upx;text-align: center;">
					从相册选择
				</view>
			
			</view>
		</view> -->
		</view>
	</view>
</template>

<script>
	import {
		addReport,
		getReportList
	} 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 {
				formatDate: formatDate,
				//分页
				page: 1,
				pageSize: 10,
				loadingType: 0, //0-loading前；1-loading中；2-没有更多了
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				},
				currentTabIndex: 0,
				userInfo: getStorage('userInfo'),
				formName: getStorage('userInfo').userName,
				formTelp: getStorage('userInfo').telnumber,
				report: {
					title: '',
					content: '',
					createBy: getStorage('userInfo').userId,
					reportType: this.reportType,
				},
				reportType: 0,
				reportList: [],
				imgs: [],
				imgsId: [],
				showAlert: false,
			};
		},
		watch: {
			currentTabIndex: function(newVal, oldVal) {
				if (newVal == 1) {
					this.page = 1;
					this.loadingType = 0;
					this.dataList();
				} else {}
			}
		},

		onLoad(option) {
			this.currentTabIndex = option.currentTabIndex || 0;
		},
		onShow() {
			if (this.currentTabIndex == 0) {

			} else {
				this.page = 1;
				this.loadingType = 0;
				this.dataList();
			}
		},
		methods: {
			dataList() {
				let params = {
					method: getReportList,
					data: {
						createBy: 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.reportList = res.returnData || [];
						} else {
							if (res.returnData.length > 0) {
								this.reportList = this.reportList.concat(res.returnData);
								this.loadingType = 0;
							} else {
								this.loadingType = 2;
							}
						}
					} else {
						alertTip(res.returnMsg);
					}
				});
			},
			submit() {
				if (!this.report.title) {
					alertTip('请输入标题');
					return false;
				}
				if (!this.report.content) {
					alertTip('请输入内容');
					return false;
				}
				this.report.reportType = this.reportType;
				this.report.reportPic1 = this.imgsId[0];
				this.report.reportPic2 = this.imgsId[1];
				this.report.reportPic3 = this.imgsId[2];
				// this.report.reportPic1 = this.imgs[0];
				// this.report.reportPic2 = this.imgs[1];
				// this.report.reportPic3 = this.imgs[2];
				let params = {
					method: addReport,
					data: this.report
				};
				console.log(params);
				callbackRequest(params).then(res => {
					res = res.data;
					if (res.returnCode == 1) {
						alertTip(res.returnMsg);
						setTimeout(_ => {
							this.currentTabIndex = 1;
						}, 2000);
					} else {
						alertTip(res.returnMsg);
					}
				});
			},
			//上传图片

			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);
				console.log(this.imgsId);
			},
			goEvaluatePage(id) {
				uni.navigateTo({
					url: `/pages/malfunc-repair/malfunc-evaluate?type=7&typeId=${id}`
				});
			},
			details: function(item) {
				uni.navigateTo({
					url: '/pages/report/report-detail?id=' + item.id + '&queryType=0'
				});
			},
			//选择部门
			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';

	.uni-textarea-textarea {
		text-align: left
	}

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

	.container {
		margin-top: 150upx;

		.main {}
	}


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

	.statusred {
		float: right;
		margin-right: 30upx;
		color: red;
	}

	.detail_msg .info-item {
		font-size: 28upx;
		margin-right: 30upx;

		text {
			display: inline-block;
			width: 140upx;
		}

		.infoSpan {
			width: auto;
			font-size: 28upx;
			color: #666;
			float: right;
		}
	}

	.form-box {
		margin-top: 24upx;
		padding: 0 24upx;

		.form-item {
			overflow: hidden;
			border-bottom: 1px solid #EBEFF7;
			padding: 26upx 0;

			.label {
				float: left;
				width: 100upx;
				font-size: 28upx;
				color: #555555;
				font-weight: 400;
				line-height: 50upx;
			}

			.ipt-box {
				margin-left: 100upx;

				input {
					width: 100%;
					height: 50upx;
					line-height: 50upx;
					font-size: 26upx;
					color: #999;
				}
			}

			.textarea-box {
				margin-top: 24upx;

				textarea {
					height: 99*2upx;
					background: #F2F6FA;
					border-radius: 6px 6px 6px 6px;
					padding-top: 24upx;
					padding-left: 24upx;
					font-size: 14*2upx;
				}
			}

			.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;
				}
			}
		}

		.form-item:last-child {
			border-bottom: 1px solid rgba(0, 0, 0, 0);
		}
	}

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

	.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;
	}

	.detail_msg .info-item:nth-of-type(3) {
		padding-bottom: 24upx;
		border-bottom: 1px solid #EBEFF7;
	}
</style>