<template>
	<view class="content">
		<view v-if="state == 0">
			<view class="box">
				<view class="title">船厂信息</view>	
				<view class="hflex acenter jbetween cell">
					<view class="left">船厂名称</view>
					<u-input v-model="info.name" border="none" placeholder="请输入船厂名称" inputAlign="right"></u-input>
				</view>
				<view class="hflex acenter jbetween cell" @click="selectmap">
					<view class="left">船厂地址</view>
					<u-input v-model="addressObj.address" border="none" disabled disabledColor="#fff" placeholder="请选择" inputAlign="right" suffixIcon="map-fill" suffixIconStyle="color: #B7B7B7"></u-input>
				</view>
				<view class="hflex acenter jbetween cell">
					<view class="left">联系人</view>
					<u-input v-model="info.contacts" border="none" placeholder="请输入联系人姓名" inputAlign="right"></u-input>
				</view>
				<view class="hflex acenter jbetween cell">
					<view class="left">联系电话</view>
					<u-input v-model="info.phone" border="none" placeholder="请输入联系电话" inputAlign="right"></u-input>
				</view>
				<view class="vflex cell" style="border: none;">
					<view class="left">船厂简介</view>
					<u--textarea v-model="info.introduction" placeholder="在此输入船厂简介内容" ></u--textarea>
				</view>
				
			</view>	
			<view class="box">
				<view class="cell" style="border: none;padding: none;">
					<view class="img_title">上传营业执照</view>
					<view class="upload_img vflex acenter jcenter">
						<u-upload :fileList="info.fileList1" @afterRead="afterRead" @delete="deletePic" :maxCount="1" name="1" multiple :previewFullImage="true" width="550rpx" height="324rpx">
							<image src="/static/images/comment/upload-business.png" style="width: 550rpx;height: 324rpx;"></image>
						</u-upload>
						<view class="hflex acenter ">
							<u-icon name="camera" color="#999999" size="16"></u-icon>
							<view class="">点击上传营业执照</view>	
						</view>	
					</view>
				</view>
			</view>
			<view class="hflex acenter">
				<u-checkbox-group @change="checkboxChange">
					<u-checkbox v-model="agree" shape="circle"></u-checkbox>
				</u-checkbox-group>
				<view class="text">阅读并同意<span class="read" @click="open">《商家入驻规则》</span></view>
			</view>
			<view class="button hflex acenter jcenter" @click="settled">
				<view>立即入驻</view>
			</view>
		</view>
		<view v-if="state == 1">
			<view class="box1 vflex acenter jcenter">
				<image src="/static/images/comment/wait.png" class="box1_img"></image>
				<view class="box1_title">审核中</view>
				<view class="box1_subTitle">预计审核时间3个工作日</view>
				<view class="box1_subTitle">请留意当前页面变动</view>
				<view class="btn" @click="result">返回</view>
			</view>
		</view>
		<view v-if="state == 2">
			<view class="box1 vflex acenter jcenter">
				<image src="/static/images/comment/error.png" class="box1_img"></image>
				<view class="box1_title">审核失败</view>
				<view class="box1_subTitle">驳回原因</view>
				<view class="box1_subTitle">{{reason}}<span style="color: #506dff;margin-left: 8rpx;" @click="edit">立即修改></span></view>
				<view class="btn" @click="result">返回</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				info: {
					name: '',
					address: '',
					contacts: '',
					phone: '',
					introduction: '',
					fileList1: [],
				},
				addressObj: {},
				agree: false,
				state: 0,
				reason:''
			}
		},
		onLoad() {
			that = this
			that.getPreData()
		},
		methods: {
			getPreData() {
				var pages = getCurrentPages()
				var prePage = pages[pages.length - 2]
				var shipyard = prePage.$vm.user.shipyard
				if (shipyard) {
					that.info = {
						name: shipyard.name,
						contacts: shipyard.contact_name,
						phone: shipyard.contact_phone,
						introduction: shipyard.intro,
						fileList1: shipyard.business_img.split(','),
						id: shipyard.id
					}
					that.addressObj = {
						address: shipyard.address,
						province: shipyard.province,
						city:shipyard.city,
						area:shipyard.area,
					}
					that.state = shipyard.audit
					that.reason = shipyard.why
				}
			},
			selectmap() {
				console.log('选择地址');
				uni.getLocation({
					type: 'wgs84',
					success: (res)=>{
						console.log(res);
						if(res.longitude && res.latitude){
							$api.jump('/pages/user/map?lng='+res.longitude+'&lat='+res.latitude)
						}
					},
					fail:()=>{
						uni.getSetting({
							success: (res) => {
								console.log(res);
								var status = res.authSetting;
								if(!status['scope.userLocation']){
									uni.showModal({
										title:"是否授权当前位置",
										content:"需要获取您的地理位置，请确认授权，否则地图功能将无法使用",
										success:(tip)=>{
											if(tip.confirm){
												uni.openSetting({
													success:(data)=>{
														if(data.authSetting['scope.userLocation']===true){
															uni.showToast({
																title:"授权成功",
																icon:"success",
																duration:1000
															})
															uni.getLocation({
																type: 'wgs84',
																success: (res)=>{
																	console.log('当前位置的经度：' + res.longitude);
																	console.log('当前位置的纬度：' + res.latitude);
																	$api.jump('/pages/user/map?lng='+res.longitude+'&lat='+res.latitude)
																}
															})
														}else{
															uni.showToast({
																title:"授权失败",
																icon:"none",
																duration:1000
															})
														}
													}
												})
											}
										}
									})
								}else{
									uni.getLocation({
										type: 'wgs84',
										success: (res)=>{
											console.log('当前位置的经度：' + res.longitude);
											console.log('当前位置的纬度：' + res.latitude);
											$api.jump('/pages/user/map?lng='+res.longitude+'&lat='+res.latitude)
										}
									})
								}
							},
							fail: (res) => {
								uni.showToast({
									title:"调用授权窗口失败",
									icon:"none",
									duration:1000
								})
							}
						})
					}
				})
			},
			// 删除图片
			deletePic(event) {
				// that.userInfo.imgList.splice(event.index, 1)
				console.log(event)
				this.info[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				console.log(`fileList${event.name}`);
				console.log(this.info[`fileList${event.name}`]);
				let lists = [].concat(event.file)
				let fileListLen = this.info[`fileList${event.name}`].length
				lists.map((item) => {
					this.info[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.info[`fileList${event.name}`][fileListLen]
					this.info[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.url
					}))
					fileListLen++
				}
			},
			// 上传图片
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: $api.config.baseUrl + '/data/api.auth.Center/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'api-token': uni.getStorageSync('token').token?uni.getStorageSync('token').token:'',
							'api-name': 'wxapp'
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								var data = JSON.parse(res.data)
								resolve(data.data)
							}, 1000)
						}
					});
				})
			},
			// 阅读并同意
			checkboxChange(n) {
				that.agree = !that.agree
			},
			settled() {
				var business_img = ""
				console.log(that.addressObj);
				if ($api.formCheck(that.info.name,'required')&&$api.formCheck(that.addressObj.address,'required')&&$api.formCheck(that.info.fileList1,'required')&&$api.formCheck(that.info.contacts,'required')&&$api.formCheck(that.info.phone,'phone')&&$api.formCheck(that.info.introduction,'required')) {
					if(that.agree) {
						if(that.info.fileList1.length > 0) {
							for(var i=0;i<that.info.fileList1.length;i++) {
								business_img += that.info.fileList1[i].url
							}
						}
						$api.req({
							url: '/data/api.auth.Center/addupshipyard',
							method: 'POST',
							data: {
								name: that.info.name,
								province: that.addressObj.province,
								city: that.addressObj.city,
								area: that.addressObj.area,
								address: that.addressObj.address,
								contact_name: that.info.contacts,
								contact_phone: that.info.phone,
								intro: that.info.introduction,
								business_img: business_img,
								id: that.id?that.id: ''
							}
						}, function(res) {
							if(res.code == 1) {
								$api.info(res.info)
								that.state = 1
							}
						})
					}else {
						$api.info('请先阅读并同意《商家入驻规则》')
					}
				}
			},
			edit() {
				that.state = 0
			},
			result() {
				$api.jump('/pages/tabbar/mine/mine',3)
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		background: #F4F4F4;
		padding: 20rpx 30rpx;
		.box {
			width: 100%;
			background: #FFFFFF;
			border-radius: 28rpx;
			padding: 28rpx 20rpx;
			box-sizing: border-box;
			margin-bottom: 16rpx;
			.title {
				font-size: 28rpx;
				color: #222222;
			}
			.cell {
				width: 100%;
				padding: 20rpx 0 28rpx;
				border-bottom: 1rpx solid #f4f4f4;
				.left {
					width: 20%;
					font-size: 28rpx;
					color: #222;
					position: relative;
					padding-left: 10rpx;
				}
				.left::before {
					content: "*";
					position: absolute;
					top: 0rpx;
					left: -5rpx;
					color: #FF0D0D;
					font-size: 20rpx;
				}
				.u-input {
					font-size: 28rpx;
					color: #222;
				}
				.img_title {
					font-size: 28rpx;
					color: #444;
				}
				.upload_img {
					width: 670rpx;
					height: 404rpx;
					background-color: #fff;
					border-radius: 20rpx;
					margin: 28rpx 0 0;
					padding: 20rpx 0;
					border: 1rpx dashed #e0e0e0;
				}
				.u-upload__wrap {
					display: flex;
					align-items: center !important;
					justify-content: center !important;
				}
				.u-textarea {
					margin: 28rpx auto 0;
					background-color: #f4f4f4;
					border-radius: 28rpx;
					box-sizing: border-box;
					padding: 24rpx;
					width: 610rpx;
				}
			}
		}
		.text {
			font-size: 24rpx;
			color: #9c9c9c;
		}
		.read {
			color: #2a63f3;
		}
		.button {
			width: 100%;
			height: 88rpx;
			background-color: #506dff;
			border-radius: 50rpx;
			box-shadow: 0 4rpx 28rpx 0 rgba(132,123,255,0.4);
			font-size: 40rpx;
			color: #fff;
			margin: 40rpx 0 36rpx;
		}
		.box1 {
			width: 100%;
			// height: 100vh;
			background-color: #fff;
			padding-bottom: 80rpx;
			.box1_img {
				width: 124rpx;
				height: 124rpx;
				margin: 108rpx 0 40rpx;
			}
			.box1_title {
				font-size: 32rpx;
				color: #222;
				margin-bottom: 24rpx;
			}
			.box1_subTitle {
				margin: 12rpx 0;
				font-size: 26rpx;
				color: #999999;
			}
			.btn {
				width: 530rpx;
				height: 88rpx;
				background-color: #506dff;
				color: #fff;
				text-align: center;
				line-height: 88rpx;
				border-radius: 44rpx;
				font-size: 36rpx;
				margin-top: 122rpx;
			}
		}
	}
</style>