<template>
	<view>
		<view class="align-items" style="margin-top: 80rpx;">
			<back></back>
			<view class="title-text" style="margin-left: 20rpx;">
				阀管施工
			</view>
		</view>
		<view style="margin-top: 60rpx;">
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('district')">
					{{districtName ? districtName :'选择行政区'}}
					<!-- <u-select v-model="houseShow" :list="chooseHouseList" @confirm="chooseHouse"></u-select> -->
				</view>
			</view>

			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('xiaoqu')">
					{{house ? house : '选择小区'}}
					<!-- <u-select v-model="bulidShow" :list="chooseBuildList" @confirm="chooseBuild"></u-select> -->
				</view>

			</view>

			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('louyu')">
					{{bulid ? bulid : '选择楼宇'}}
					<!-- <u-select v-model="bulidShow" :list="chooseBuildList" @confirm="chooseBuild"></u-select> -->
				</view>

			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('danyuan')">
					{{unit ? unit : '选择单元'}}
					<!-- openUnit(), <u-select v-model="unitShow" :list="chooseUnitList" @confirm="chooseUnit"></u-select> -->
				</view>

			</view>
			<view>
				<view class="font-fifty-six SourceHanSansCN choose" @click="openHouse('menpai')">
					{{houseNumber ? houseNumber  : '选择门牌'}}
					<!-- open(), <u-select v-model="houseNumberShow" :list="chooseNumberList" @confirm="chooseNumber"></u-select> -->
				</view>
			</view>

			<view>
				<view class="font-fifty-six SourceHanSansCN select-type" @click="openType('shows')">
					上门类型 <text class="font-fifty-six SourceHanSansCN" style="color: #232146;">
						{{type ? '----' +  type : ''}}</text>
					<u-select v-model="shows" :list="mounicipalList" @confirm="confirm"></u-select>
				</view>
				<view v-if="typeValue">
					<view v-if=" typeValue === '002' ">
						<view class="font-fifty-six SourceHanSansCN select-type" @click="projectType()">
							自闭阀类型 <text class="font-fifty-six SourceHanSansCN" style="color: #232146;">
								{{valueType ? '----' +  valueType : ''}}</text>
							<u-select v-model="listTypeShow" :list="typeList" @confirm="listType"></u-select>
						</view>
						<view class="font-fifty-six SourceHanSansCN tube" @click="openType('tubeType')">
							管子类别
							<text class="font-fifty-six SourceHanSansCN" style="color: #232146;">
								{{tubeType ?'----' + tubeType:''}}</text>
							<u-select v-model="tubeTypeShow" :list="tubeTypeList" @confirm="tubeTypeConfirm"></u-select>
						</view>
						<view class="font-fifty-six SourceHanSansCN tube" @click="openType('tubeLong')">
							管子长度
							<text class="font-fifty-six SourceHanSansCN" style="color: #232146;">
								{{tubeLong ?'----' + tubeLong  :''}}</text>
							<u-select v-model="tubeLongShow" :list="tubeTypeLongList" @confirm="tubeLongConfirm">
							</u-select>
						</view>
					</view>
					<view class="upload align-items">
						<view class="" style="display: flex; flex-wrap: wrap;">
							<image src="/static/icon/chooseimg.png" mode=""
								style="width: 190rpx; height: 190rpx; margin: 0 12rpx;; " @click="choose()"></image>
							<view v-for="(item,index) in imgArr" :key="index" style="position: relative;">
								<view
									v-if="item.substring(item.length - 3) == 'png' || item.substring(item.length - 3) == 'jpg' ">
									<image :src="item" mode="" style="width: 190rpx; height: 190rpx; margin: 0 12rpx;"
										@click="showPhoto(index)">
									</image>
								</view>
								<view v-else>
									<video :src="item" style="width: 190rpx; height: 190rpx; margin: 0 12rpx;"></video>
								</view>
								<view @click="remove(index)"
									style="position: absolute; top: 0; right: 14rpx; border-radius: 50%; background-color: #FF0000;">
									<u-icon name="close" color="#FFFFFF" size="35"></u-icon>
								</view>
							</view>
						</view>
						<view class="gray font-forty-eight SourceHanSansCN" v-if="photo">
							此处添加照片或视频
						</view>
					</view>
					<view class="upload">
						<text class="font-forty SourceHanSansCN">备注：</text>
						<textarea value="" placeholder="请输入备注" v-model="remark" />
					</view>
				</view>

				<view class="font-fifty-six SourceHanSansCN total background-color1" @click="submit()">
					提交数据
				</view>
				<!-- <u-select v-model="shows" :list="mounicipalList" @confirm="confirm"></u-select> -->
				<!-- <u-select v-model="tubeTypeShow" :list="tubeTypeList" @confirm="tubeTypeConfirm"></u-select> -->
				<!-- <u-select v-model="tubeLongShow" :list="tubeTypeLongList" @confirm="tubeLongConfirm"></u-select> -->
			</view>
		</view>
		<u-popup v-model="pop" mode="bottom" height="1000rpx">
			<view style="margin: 20rpx;">
				<u-search v-show="inputShow" placeholder="请输入搜索内容" v-model="keyword"
					:action-style="{'font-size':'40rpx'}" @custom="searchinfo(selectType)"></u-search>
			</view>
			<view>
				<scroll-view scroll-y="true" style="height: 600rpx; text-align: center;">
					<view v-for="(item,index) in chooseHouseList" :key="item.value">
						<view :class="current === index ? 'active' : '' " style="color: #000000;font-size: 56rpx;"
							@click="active(item,index)">
							{{item.label}}
						</view>
					</view>
				</scroll-view>
				<view class="">
					<button type="default" class="font-fifty-six" @click="search(selectType)">确定</button>
				</view>
				<view class="">
					<button type="default" @click="pop = false;" class="font-fifty-six">取消</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import service from '@/api/index.js'
	export default {
		components: {

		},
		data() {
			return {
				keyword: '',
				selectType: '',
				pop: false,
				inputShow: false,
				housename: '',
				houseid: 0,
				buildid: 0,
				unitid: 0,
				numberid: 0,
				houseShow: false,
				current: -1,
				chooseHouseList: [],
				districtId: 0,
				districtName: '选择行政区',
				house: '选择小区',
				bulid: '选择楼宇',
				unit: '选择单元',
				houseNumber: '选择门牌',
				action: this.$HTTP.webUrl + `/obs`,
				shows: false,
				tubeTypeShow: false,
				tubeLongShow: false,
				mounicipalList: [],
				tubeTypeList: [],
				tubeTypeLongList: [],
				type: '',
				tubeType: '',
				tubeLong: '',
				// url: [],
				photo: true,
				typeValue: '',
				headers: {
					MAuthorization: "wxBearer " + uni.getStorageSync('token')
				},
				housedata: '',
				typeList: [],
				listTypeShow: false,
				valueType: '',
				uploading: false,
				imgArr: [],
				remark: '',
				show: false,
				progress: 0, //上传图片进度百分比
				loading: false
			}
		},
		onLoad() {
			// this.getArea();
			this.select();
			this.getTubeType();
			this.getTubeLong();
			this.getProjectType();
		},
		methods: {
			openHouse(selectType) {
				if (selectType === 'district') {
					this.inputShow = false
					//选择行政区
					this.housename = null;
					this.selectType = 'district'
					service.getDistrict().then(res => {
						// console.log(res);
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.dictValue,
								label: item.dictLabel,
								selectType: 'district'
							})
						})
					})
				} else if (selectType === 'xiaoqu') {
					this.inputShow = true
					//选择小区
					this.selectType = 'xiaoqu'
					this.housename = null;
					// this.district = '';
					service.getArea({
						district: this.districtId
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'xiaoqu'
							})
						})
					})
				} else if (selectType === 'louyu') {
					this.inputShow = true
					//选择楼宇
					this.selectType = 'louyu'
					this.housename = null;
					service.getBuilding({
						areaId: this.houseid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'louyu'
							})
						})
					})

				} else if (selectType === 'danyuan') {
					this.inputShow = true
					//选择单元
					this.selectType = 'danyuan';
					this.housename = null;
					service.getUnit({
						buildingId: this.buildid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'danyuan'
							})
						})
					})

				} else if (selectType === 'menpai') {
					this.inputShow = true
					//选择门牌
					this.selectType = 'menpai';
					this.housename = null;
					service.getHouse({
						unitId: this.unitid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'menpai'
							})
						})
					})
				}
				this.pop = !this.pop;
			},
			searchinfo(selectType) {
				if (selectType == 'xiaoqu') {
					console.log('ssss', this.selectType)
					service.getArea({
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'xiaoqu'
							})
						})
					})
					this.keyword = '';
				} else if (selectType == 'louyu') {
					console.log('ssss', this.selectType)
					service.getBuilding({
						areaId: this.houseid,
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'louyu'
							})
						})
					})
					this.keyword = '';
				} else if (selectType == 'danyuan') {
					console.log('ssss', this.selectType)
					service.getUnit({
						buildingId: this.buildid,
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'danyuan'
							})
						})
					})
					this.keyword = '';
				} else if (selectType == 'menpai') {
					console.log('ssss', this.selectType)
					service.getHouse({
						unitId: this.unitid,
						name: this.keyword
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach((item, index) => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'menpai'
							})
						})
					})
					this.keyword = '';
				}

			},
			showPhoto(index) {
				uni.previewImage({
					current: index,
					urls: this.imgArr,
				})
				console.log('sss')
			},
			openBuild() {
				this.bulidShow = !this.bulidShow
			},
			projectType() {
				this.listTypeShow = !this.listTypeShow
			},
			// 	//获取小区
			// 	getArea() {

			// 	},
			getProjectType() {
				service.getProject().then(res => {
					console.log('getProjectType', res)
					this.typeList = [];
					res.forEach(item => {
						this.typeList.push({
							value: item.id,
							label: item.valveType
						})
					})

				})
			},
			search(selectType) {
				if (selectType == 'district') {
					this.districtName = this.housename;
					service.getArea({
						district: this.districtId
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.dictValue,
								label: item.dictLabel,
								selectType: 'xiaoqu'
							})
						})
					})
					this.current = -1;
					this.house = null;
					this.bulid = null;
					this.unit = null;
					this.houseNumber = null;

					this.houseid = 0;
					this.buildid = 0;
					this.unitid = 0;
					this.housedata = 0;


				} else if (selectType == 'xiaoqu') {
					this.house = this.housename;
					service.getBuilding({
						areaId: this.houseid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'louyu'
							})
						})
					})
					this.current = -1;
					this.bulid = null;
					this.unit = null;
					this.houseNumber = null;

					this.buildid = 0;
					this.unitid = 0;
					this.housedata = 0;
				} else if (selectType == 'louyu') {
					this.bulid = this.housename;
					service.getUnit({
						buildingId: this.buildid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'danyuan'
							})
						})
					})

					this.unit = null;
					this.houseNumber = null;
					this.current = -1;

					this.unitid = 0;
					this.housedata = 0;
				} else if (selectType == 'danyuan') {
					this.unit = this.housename;
					service.getHouse({
						unitId: this.unitid
					}).then(res => {
						this.chooseHouseList = [];
						res.forEach(item => {
							this.chooseHouseList.push({
								value: item.id,
								label: item.name,
								selectType: 'menpai'
							})
						})
					})
					this.houseNumber = null;
					this.current = -1;

					this.housedata = 0;

				} else if (selectType == 'menpai') {
					this.houseNumber = this.housename;
					this.housedata = this.numberid;
					this.current = -1;
				}
				this.pop = false;
			},
			active(item, index) {
				this.current = index;
				if (item.selectType == 'district') { //行政区
					this.housename = item.label;
					this.districtId = item.value;
				} else if (item.selectType == 'xiaoqu') { //小区
					this.housename = item.label;
					this.houseid = item.value;
				} else if (item.selectType == 'louyu') { //楼宇
					this.housename = item.label;
					this.buildid = item.value;
				} else if (item.selectType == 'danyuan') { //单元
					this.housename = item.label;
					this.unitid = item.value;
				} else if (item.selectType == 'menpai') { //门牌
					this.housename = item.label;
					this.numberid = item.value
				}
				this.selectType = item.selectType;
				// console.log("xxxxx_type:" + this.selectType + "_" + this.housename)

			},

			openUnit() {
				this.unitShow = !this.unitShow
			},
			//选择门牌

			open() {
				this.houseNumberShow = !this.houseNumberShow
			},
			//选择上门类型
			confirm(e) {
				this.type = e[0].label
				this.typeValue = e[0].value

				this.imgArr = [];
				this.tubeType = null;
				this.tubeLong = null;
				this.valueType = null;
				this.photo = true;
			},
			//选择类型
			select() {
				service.getDic({
					dicType: 'service_type'
				}).then(res => {
					res.forEach(item => {
						this.mounicipalList.push({
							value: item.value,
							label: item.name
						})
					})
				})
			},
			openType(type) {
				if (type === 'shows') {
					this.shows = !this.shows
				} else if (type === 'tubeType') {
					this.tubeTypeShow = !this.tubeTypeShow
				} else {
					this.tubeLongShow = !this.tubeLongShow
				}

			},
			tubeTypeConfirm(e) {
				this.tubeType = e[0].label
			},
			listType(e) {
				this.valueType = e[0].label
			},
			tubeLongConfirm(e) {
				this.tubeLong = e[0].label
			},
			getTubeType() {
				service.getPipeType().then(res => {
					res.forEach(item => {
						this.tubeTypeList.push({
							value: item.id,
							label: item.pipeType
						})
					})
				})
			},
			getTubeLong() {
				service.getPipeLength().then(res => {
					res.forEach(item => {
						this.tubeTypeLongList.push({
							value: item.id,
							label: item.pipeLength
						})
					})
				})
			},
			remove(index) {
				uni.showModal({
					title: '提示',
					content: '是否删除该图片或视频？',
					success: (res) => {
						if (res.confirm) {
							this.imgArr.splice(index, 1)
							console.log('this.imgarr', this.imgArr)
							if (this.imgArr.length <= 0) {
								this.photo = true;
							}
						}
					}
				})
			},
			choose() {
				let _this = this;
				uni.showActionSheet({
					title: '上传',
					itemList: ['图片', '视频'],
					success: (res) => {
						console.log(res)
						if (res.tapIndex == 0) {
							this.chooseimage()
						} else {
							this.choosevideo()
						}
					}
				})
			},
			chooseimage() {
				console.log('图片')
				let _this = this;
				uni.chooseImage({
					sizeType: ['album', 'camera'],
					success(resp) {
						console.log('res--uni.chooseMedia', resp);
						resp.tempFiles.forEach((item, index) => {
							const task = uni.uploadFile({
								url: _this.$HTTP.webUrl + `/obs`,
								filePath: item.path,
								name: 'file',
								formData: {},
								header: _this.headers,
								success: res => {
									// 判断是否json字符串，将其转为json格式
									let data = _this.$u.test.jsonString(res
										.data) ? JSON.parse(res.data) : res.data;
									if (![200, 201, 204].includes(res.statusCode)) {
										// this.uploadError(index, data);
										_this.$UTILS.showPrompt('上传失败！')
									} else {
										if (_this.progress === 100) {
											console.log('_this.progress', _this.progress)
											console.log('data----', data)
											console.log('res--', res)
											_this.imgArr.push(data.data.url)
											console.log('imgArr', _this.imgArr)
											_this.$UTILS.showPrompt('上传成功！')
											_this.photo = false;
										}
									}
								},
								fail: e => {
									_this.$UTILS.showPrompt('上传失败！')
									this.uploadError(index, e);
								},
								complete: res => {
									uni.hideLoading();
									_this.uploading = false;

								}
							});
							task.onProgressUpdate(res => {
								_this.progress = res.progress;
								console.log('onProgressUpdate', res)
								console.log('task.onProgressUpdate', _this.progress)
								uni.showLoading({
									title: '上传中'
								})
								if (_this.progress != 100) {
									_this.loading = false
									console.log('_this.loading false', _this.loading)
								} else {
									_this.loading = true
									console.log('_this.loading true', _this.loading)
								}
							});

						})

					},
				})
			},
			choosevideo() {
				let _this = this;
				console.log('视频')
				uni.chooseVideo({
					sourceType: ['album', 'camera'],
					maxDuration: 30,
					success(resp) {
						const task = uni.uploadFile({
							url: _this.$HTTP.webUrl + `/obs`,
							filePath: resp.tempFilePath,
							name: 'file',
							formData: {},
							header: _this.headers,
							success: res => {
								// 判断是否json字符串，将其转为json格式
								let data = _this.$u.test.jsonString(res
									.data) ? JSON.parse(res.data) : res.data;
								if (![200, 201, 204].includes(res.statusCode)) {
									this.uploadError(index, data);
								} else {
									//上传成功
									if (_this.progress === 100) {
										console.log('_this.progress', _this.progress)
										_this.imgArr.push(data.data.url)
										console.log('imgArr', _this.imgArr)
										_this.$UTILS.showPrompt('上传成功！')
										_this.photo = false;
									}
								}
							},
							fail: e => {
								_this.$UTILS.showPrompt('上传失败！')
								this.uploadError(index, e);
							},
							complete: res => {
								uni.hideLoading();
								_this.uploading = false;
							}
						});
						task.onProgressUpdate(res => {
							_this.progress = res.progress;
							console.log('onProgressUpdate', res)
							uni.showLoading({
								title: '上传中'
							})
							if (_this.progress != 100) {
								_this.loading = false
								console.log('_this.loading false', _this.loading)
							} else {
								_this.loading = true
								console.log('_this.loading true', _this.loading)
							}
						});

					},
				})
			},
			submit() {
				if (!this.housedata) {
					this.$UTILS.showPrompt('请选择门牌号！')
					return
				}
				if (!this.type) {
					this.$UTILS.showPrompt('请选择上门类型！')
					return
				}
				if (this.typeValue == '002') {
					if (!this.valueType) {
						this.$UTILS.showPrompt('请选择自闭阀类别！')
						return
					}
					if (!this.tubeType) {
						this.$UTILS.showPrompt('请选择管子类型！')
						return
					}
					if (!this.tubeLong) {
						this.$UTILS.showPrompt('请选择管子长度！')
						return
					}
				}

				if (this.imgArr.length <= 0) {
					this.$UTILS.showPrompt('请上传照片或视频！')
					return
				}
				if (this.loading == false) {
					this.$UTILS.showPrompt('照片或视频未上传完毕，无法提交！')
					return
				}
				let param = {
					serviceType: this.typeValue,
					pipeType: this.tubeType,
					pipeLength: this.tubeLong,
					//小区
					areaId: this.houseid,
					areaName: this.house,
					//楼宇
					buildingId: this.buildid,
					buildingName: this.bulid,
					//单元
					unitId: this.unitid,
					unitName: this.unit,
					//房间
					houseId: this.housedata,
					houseName: this.houseNumber,

					valveType: this.valueType,
					picUrl: this.imgArr,
					remark: this.remark,
				}
				console.log('param-----', param)
				service.submitOrder(param).then(res => {
					this.$UTILS.showPrompt('提交成功！')
					this.houseNumber = null;
					this.type = null;
					this.typeValue = null;
					this.valueType = null;
					this.tubeType = null;
					this.tubeLong = null;
					this.housedata = null;
					this.typeValue = null;
					this.remark = null;
					this.imgArr = [];
				}).catch(e => {
					this.$UTILS.showPrompt('提交失败！')
					console.error(e)
				})


			},
		}
	}
</script>

<style lang="scss" scoped>
	.choose {
		text-align: center;
		padding: 20rpx;
		margin: 30rpx;
		color: #FFFFFF;
		background: #43CEB1;
		border-radius: 64rpx;
	}

	.active {
		background-color: #1890FF;
	}

	.select-type {
		border: 2rpx solid #3857F3;
		text-align: center;
		padding: 20rpx;
		margin: 30rpx;
		color: #3857F3;
		border-radius: 64rpx;
	}

	.upload {
		margin: 30rpx;
		padding: 30rpx 20rpx;
		border: 2rpx solid #3857F3;
		border-radius: 48rpx;
	}

	.tube {
		text-align: center;
		padding: 20rpx;
		margin: 30rpx;
		color: #43CEB1;
		border: 2rpx solid #43CEB1;
		border-radius: 64rpx;
	}

	.total {
		width: 100%;
		padding: 40rpx 30rpx;
		text-align: center;
		color: #FFFFFF;
	}
</style>
