<template>
	<view :class="[AppTheme]" class="all_box">
		<view class="step_bar">
			<u-steps :current="steps_status" :activeColor="primary">
				<u-steps-item title="提交资料">
				</u-steps-item>
				<u-steps-item title="平台审核"></u-steps-item>
				<u-steps-item title="审核完成"></u-steps-item>
			</u-steps>
		</view>
		<view v-if="status==3" class="reason">
			您的申请已被驳回。
			<view class="reason_text">
				驳回原因:{{reason}}
			</view>
		</view>
		<view class="content_box" v-if="status==0||status==4||status==3">
			<view class="content_detail">
				<view class="detail_title">农场资料</view>
				<view class="detail_text">请填写农场基本资料以供平台审核</view>
			</view>
			<view class="content_detail">
				<view class="detail_title">农场Logo</view>
				<view class="detail_text">
					<view class="detail_ipt">
						<text class="text">请上传农场Logo</text>
						<view @click="uploadlogo(1)" class="detail_logo" v-if="logo">
							<image class="img" :src="logo" mode=""></image>
						</view>
						<view v-if="!logo" @click="uploadlogo(1)" class="choose_img">
							<view class="icon_plus">
								<u-icon name="plus" size="50"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="content_detail">
				<view class="detail_title">农场名称</view>
				<view class="detail_text">
					<input class="input" placeholder="请输入农场名称" v-model="form.name" type="text" />
				</view>
			</view>
			<view class="content_detail">
				<view class="detail_title">农场地址</view>
				<view class="detail_text_address">
					<input class="input" placeholder="请填写农场地址" v-model="form.address" type="text" />
					<u-icon @click="map" class="icon" size="30" name="map"></u-icon>
				</view>
			</view>
			<view class="content_detail">
				<view class="detail_title">农场描述</view>
				<view class="detail_text">
					<textarea v-model="form.describe" class="detail_ipt" type="text" placeholder="对农场经营类目进行简短描述" />
				</view>
				<view class="content_detail_img">
					<view v-if="detailimg.length!=0" class="detail_img" v-for="(item,index) in detailimg" :key="index">
						<image class="image" :src="item" mode=""></image>
						<u-icon @click="deleteimg(index)" class="delete_img" name="close-circle" color="#fff" size="20">
						</u-icon>
					</view>
					<view class="detail_img" @click="uploadlogo(2)">
						<u-icon class="icon_plus" name="plus" size="50"></u-icon>
					</view>
				</view>
			</view>
			<view class="agree_terms">
				<view @click="agrees" class="agree_dot">
					<u-icon v-if="value" size="20" name="checkmark-circle-fill" :color="primary"></u-icon>
					<u-icon v-if="!value" size="20" name="checkmark-circle-fill" color="#606266"></u-icon>
				</view>
				我已阅读并同意
				<text @click="toruzhu">《商家入驻协议》</text>
			</view>
			<view class="submit bg-linear-gradient" @click="submit">提交资料</view>
		</view>
		<view v-if="status==1" class="under_review">
			<u-empty iconSize="75" text="提交成功,请等待审核" mode="history"></u-empty>
		</view>
		<view v-if="status==2" class="under_review">
			<image :src="settingFile.root_img+'/static/app/xuanzhong.png'" mode=""></image>
			<view class="text">恭喜你,入驻成功</view>
		</view>
		<u-popup :show="mapshow">
			<map width="100%" height="100%" :longitude="form.longitude" :latitude="form.latitude"></map>
		</u-popup>
	</view>
</template>

<script>
	import mineApi from "@/api/mine/index.js"
	import {
		pathToBase64
	} from "@/js/pathto64.js"

	export default {
		data() {
			return {
				primary: this.$theme.primary,
				settingFile: getApp().globalData.siteinfo,
				reason: '', //驳回原因
				//steps状态  默认为0
				steps_status: 0,
				status: 0, //申请状态
				//顶部steps列表
				numList: [{
					name: '提交资料'
				}, {
					name: '平台审核'
				}, {
					name: "审核完成"
				}],
				agree: 1,
				value: false, //商家入驻协议
				logo: '', //农场展示的logo
				detailimg: [], //农场描述附加图
				//表单数据
				form: {
					logo: "", //农场logo的base64格式
					name: '', //农场名称
					address: '', //农场地址
					describe: "", //农场描述
					longitude: '', //位置经度
					latitude: '', //位置纬度
					detailimg: [] ,//农场描述附加图
					addres:''
				},
				mapshow: false, //地图组件的显示隐藏
			}
		},
		onLoad() {
			this.getstatus()
		},
		methods: {
			deleteimg(idx) { //图片删除
				this.detailimg.splice(idx, 1)
				this.form.detailimg.splice(idx, 1)
			},
			getstatus() { //获取steps状态
				mineApi.apply_status()
					.then(res => {
						if (res.status == 1) {
							console.log(res, "农场入驻状态")
							this.status = res.data.status
							this.reason = res.data.remark
							if (this.status == 3 || this.status == 4) {
								this.steps_status = 0
							} else if (this.status == 1) {
								this.steps_status = 1
							} else {
								this.steps_status = 2
							}
						}
					})
			},
			agrees() {
				this.value = !this.value
			},
			toruzhu() {
				this.$until.toarticle('商家入驻协议', 0, 2)
			},
			//提交资料
			submit() {
				if (this.form.name == '') {
					this.$api.msg('请填写农场名称')
					return
				}
				if (this.form.logo == '') {
					this.$api.msg('请上传农场Logo')
					return
				}
				if (this.form.address == '') {
					this.$api.msg('请填写农场地址')
					return
				}
				if (this.form.describe == '') {
					this.$api.msg('请填写农场描述')
					return
				}
				if (this.value == false) {
					this.$api.msg('请同意《商家入驻协议》')
					return
				}

				let data = {
					farm_name: this.form.name,
					farm_logo: this.form.logo,
					longitude: this.form.longitude,
					latitude: this.form.latitude,
					farm_address: this.form.address,
					farm_detail: this.form.describe,
					// detail_img: JSON.stringify(this.form.detailimg)
					detail_img: this.form.detailimg,
					site:this.form.addres,

				}
				mineApi.farm_apply(data)
					.then(res => {
						if (res.status === 1) {
							this.steps_status = 1
							this.status = 1
						} else {
							uni.showToast({
								title: res.msg,
								icon: "none"
							})
						}
					})
					.catch(error => {
						console.log(error, 'error')
					})
			},
			//上传Logo和农场描述附加图
			uploadlogo(num) {
				let that = this
				// #ifdef MP
				if (num == 1) {
					uni.chooseImage({
						count: 1,
						success: function(res) {
							that.logo = res.tempFilePaths[0]
							uni.getFileSystemManager().readFile({
								filePath: res.tempFilePaths[0],
								encoding: 'base64',
								success: function(ress) {
									//返回不带data:image/jpeg;base64,'头的base64格式
									that.form.logo = 'data:image/jpeg;base64,' + ress.data;
								}
							})
						},
						fail: function(res) {
							console.log(res)
						}
					})
				} else {
					uni.chooseImage({
						count: 9,
						success: function(res) {
							res.tempFilePaths.forEach(ele => {
								that.detailimg.push(ele)
								uni.getFileSystemManager().readFile({
									filePath: ele,
									encoding: 'base64',
									success: function(ress) {
										//返回不带data:image/jpeg;base64,'头的base64格式
										let imageurl = 'data:image/jpeg;base64,' + ress
											.data;
										that.form.detailimg.push(imageurl)
									}
								})
							})
						}
					})
				}
				// #endif

				// #ifdef APP||H5
				if (num == 1) {
					uni.chooseImage({
						count: 1, //默认9
						sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
						success: function(res) {
							that.logo = res.tempFilePaths[0]
							console.log(res, "选择的图片")
							pathToBase64(res.tempFilePaths[0]).then(base64 => {
								console.log(base64, "base64")
								that.form.logo = base64;
							})
						}
					})
				} else {
					uni.chooseImage({
						count: 9,
						success: function(res) {
							res.tempFilePaths.forEach(ele => {
								that.detailimg.push(ele)
								pathToBase64(res.tempFilePaths[0]).then(base64 => {
									console.log(base64, "base64")
									that.form.detailimg.push(base64)
								})
							})
						}
					})
				}
				// #endif
			},
			//打开地图
			map() {
				let that = this
				// #ifdef MP-WEIXIN || APP-PLUS
				that.$until.chooseLocation(function(res) {
					that.form.latitude = res.latitude
					that.form.longitude = res.longitude
					that.form.address = res.address + res.name
					that.form.addres=res.address
				});
				// #endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	.all_box {
		background-color: #ffffff;

		.step_bar {
			padding: 50rpx;
			padding-bottom: 80rpx;
			color: #fff;
		}

		.reason {
			background-color: #f97c00;
			color: #fff;
			line-height: 50rpx;
			padding: 0 20rpx;

			.reason_text {
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}

		.content_box {
			padding-bottom: 20rpx;

			// margin: 20rpx;
			.content_detail {
				margin: 25rpx;

				.detail_title {
					font-weight: 700;
					// margin: 15rpx;
				}

				.detail_text {
					// font-size: 25rpx;
					margin-top: 20rpx;
					display: flex;

					.input {
						width: 100%;
						padding-left: 10rpx;
						height: 80rpx;
						border-radius: 5rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						border: 1rpx solid #eae8e9;
					}

					.detail_ipt {
						border: 1rpx solid #eae8e9;
						padding: 15rpx;
						width: 100%;

						.text {
							margin-bottom: 20rpx;
						}

						.detail_logo {
							margin-top: 20rpx;

							.img {
								width: 120rpx;
								height: 120rpx;
							}
						}

						.choose_img {
							margin-top: 20rpx;
							width: 120rpx;
							height: 120rpx;
							text-align: center;
							overflow: hidden;
							border: 1rpx solid #eae8e9;

							.icon_plus {
								margin: 15rpx;
							}
						}
					}

					.detail_ipt1 {
						border: 1rpx solid #eae8e9;
						padding: 15rpx;
						height: 70rpx;
					}

					.icon {
						position: absolute;
						top: 10rpx;
						right: 20rpx;
						z-index: 10;
					}
				}

				.detail_text_address {
					width: 100%;
					margin-top: 20rpx;
					height: 80rpx;
					border-radius: 5rpx;
					border: 1rpx solid #eae8e9;
					display: flex;
					align-items: center;
					justify-content: flex-start;

					.input {
						padding-left: 10rpx;
						width: 92%;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.icon {}
				}

				.content_detail_img {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					flex-wrap: wrap;

					.detail_img {
						position: relative;
						margin-top: 10rpx;
						width: 150rpx;
						height: 150rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						border: 1rpx solid #eae8e9;
						margin-right: 20rpx;

						.image {
							width: 150rpx;
							height: 150rpx;
						}

						.delete_img {
							position: absolute;
							top: 5rpx;
							right: 5rpx;
						}
					}
				}


			}

			.agree_terms {
				display: flex;
				justify-content: center;
				align-items: center;

				.agree_dot {
					margin-right: 10rpx;
				}

				text {
					color: #2375f4;
				}
			}

			.submit {
				font-size: 30rpx;
				color: #ffffff;
				padding: 25rpx 0;
				border-radius: 35rpx;
				text-align: center;
				width: 90%;
				margin: 20rpx auto;
				margin-bottom: 40rpx;
			}
		}

		.under_review {
			margin-top: 100rpx;
			text-align: center;

			image {
				width: 150rpx;
				height: 150rpx;
			}

			.text {
				margin-top: 20rpx;
			}
		}
	}
</style>
