<template>
	<view class="content">
		<view class="form-block">
			<view class="form-upd">
				<view class="form-title">产品实物图片<text>（点击预览，长按删除）</text></view>
				<view class="form-imgs">
					<view
						class="item"
						v-if="extendCover.length > 0"
						@click="openImg"
						@longpress="extendCover = []"
					>
						<image class="item-cover" :src="extendCover[0].showpath" mode="aspectFill"></image>
					</view>
					<view class="item item-add" v-else @click="updCover">
						<image class="item-cover" src="@/static/icons/add-icon.png" mode="aspectFill"></image>
					</view>
				</view>
			</view>
		</view>
		<!-- 商品基本信息 -->
		<view class="form-block">
			<view class="form-box picker-flex">
				<label class="form-label">生产日期</label>
				<picker mode="date" :value="productedAt" @change="pickerChange($event, 'productedAt')">
					<view class="picker-text">
						{{productedAt || '选择商品生产日期'}}
						<uni-icons class="picker-icon" type="arrowright" color="#999"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="form-box picker-flex">
				<label class="form-label">过期时间</label>
				<picker mode="date" :value="expiriedAt" @change="pickerChange($event, 'expiriedAt')">
					<view class="picker-text">
						{{expiriedAt || '选择商品过期时间'}}
						<uni-icons class="picker-icon" type="arrowright" color="#999"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="form-box inputs-flex">
				<label class="form-label">生产商名称</label>
				<input type="text" v-model="productName" placeholder="输入商品生产商名称"/>
			</view>
			<view class="form-box inputs-flex">
				<label class="form-label">生产许可证号</label>
				<input type="text" v-model="lisence" placeholder="输入商品生产许可证号"/>
			</view>
			
			<view class="form-box inputs-flex">
				<label class="form-label">生产商地址</label>
				<input type="text" v-model="productAddress" placeholder="输入商品生产商地址"/>
			</view>
		</view>
		<!-- 安全区 -->
		<view class="ios-bottom"></view>
		<!-- footer -->
		<view class="footer">
			<button class="footer-btn" type="default" @click="submitAdd">提交认证审核</button>
			<view class="ios-bottom"></view>
		</view>
	</view>
</template>

<script>
	import { managesGoodsAuth, managesGoodsExtends } from '@/apis/interfaces/goods'
	import { uploads } from '@/apis/interfaces/uploading'
	export default {
		data() {
			return {
				productedAt 	: '',  // 生产日期
				expiriedAt  	: '',  // 过期时间
				lisence	    	: '',  // 生产许可证号
				productName		: '',  // 生产商名称
				productAddress	: '',  // 生产商地址
				extendCover		: []   // 实物图片
			};
		},
		created() {
			if(this.$Route.query.edit == 'true'){
				managesGoodsExtends(this.$Route.query.id).then(res => {
					this.productedAt 	= res.producted_at
					this.expiriedAt  	= res.expiried_at
					this.lisence	    = res.lisence
					this.productName	= res.product_name
					this.productAddress	= res.product_address
					this.extendCover	= [{...res.cover}]
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			}
		},
		methods: {
			// picker选择
			pickerChange(e, key){
				this[key] = e.detail.value
			},
			// 图片预览
			openImg(){
				let paths = this.extendCover.map(val => {
					return val.showpath
				})
				uni.previewImage({
					urls	 : paths,
					current	 : 0,
					indicator: 'number'
				})
			},
			// 上传图片
			updCover(){
				uni.chooseImage({
					count	: 1,
					success	: res => {
						uploads([{
							uri : res.tempFilePaths[0]
						}]).then(updRes => {
							this.extendCover.push({
								path	: updRes.path[0],
								showpath: updRes.url[0]
							})
						}).catch(err => {
							uni.showToast({
								title: err.message,
								icon : 'none'
							})
						})
					}
				})
			},
			// 发布产品
			submitAdd(){
				let submitData = {
					producted_at	: this.productedAt,
					expiried_at		: this.expiriedAt,
					lisence			: this.lisence,
					product_name	: this.productName,
					product_address	: this.productAddress,
					extend_cover	: this.extendCover[0].path
				}
				let submitFund = managesGoodsAuth(this.$Route.query.id, submitData)
				submitFund.then(res => {
					uni.showModal({
						title		: '提示',
						content 	: '商品权证认证信息已提交，请耐心等待平台审核',
						showCancel	: false,
						success		: res => {
							if(res.confirm){
								if(this.$Route.query.type == 'goodsAdd'){
									this.$Router.back(this.$Route.query.edit == 'true' ? 2 : 3)
								}else{
									this.$Router.back()
								}
							}
						}
					})
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon : 'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		padding-bottom: 150rpx;
	}
	// 表单
	.form-block{
		background: white;
		margin-top: $margin - 10;
		.form-box{
			position: relative;
			padding-left: 240rpx;
			padding-right: $padding;
			font-size: $title-size-lg;
			min-height: 80rpx;
			&::after{
				position: absolute;
				bottom: 0;
				left: $margin;
				right: 0;
				height: 1rpx;
				content: " ";
				background: $border-color;
			}
			&:last-child::after{
				display: none;
			}
			.form-label{
				position: absolute;
				left: $margin;
				line-height: 80rpx;
				top: 0;
				width: calc(240rpx - #{$margin});
			}
		}
		.inputs-flex{
			input{
				height: 80rpx;
				line-height: 80rpx;
			}
		}
		.input-unit{
			padding-right: 200rpx;
			.units{
				position: absolute;
				right: 0;
				top: 0;
				line-height: 80rpx;
				height: 80rpx;
				width: 200rpx;
				padding-right: $padding;
				text-align: right;
				box-sizing: border-box;
			}
		}
		.picker-flex{
			.picker-text{
				position: relative;
				line-height: 80rpx;
				min-height: 80rpx;
				padding-right: 80rpx;
				@extend .nowrap;
				.picker-icon{
					right: 0;
					position: absolute;
				}
			}
			.picker-switch{
				line-height: 80rpx;
				min-height: 80rpx;
				text-align: right;
				margin-right: -15rpx;
				switch{
					transform:scale(0.7)
				}
			}
		}
		.form-upd{
			.form-title{
				font-size: $title-size-lg;
				line-height: 80rpx;
				padding: 0 $padding;
				text{
					font-size: 80%;
					color: $text-gray;
				}
			}
			.form-imgs{
				margin-top: -($margin/3);
				padding: 0 20rpx 20rpx;
				display: flex;
				flex-wrap: wrap;
				.item{
					width: calc(20% - 14rpx);
					padding-top: calc(20% - 14rpx);
					margin: 7rpx;
					position: relative;
					.item-cover{
						position: absolute;
						top: 0;
						left: 0;
						width: 100%;
						height: 100%;
					}
				}
				.item-add{
					border: dashed 2rpx $border-color;
					box-sizing: border-box;
					.item-cover{
						top: calc(15% - 2rpx);
						left: calc(15% - 2rpx);
						width: 70%;
						height: 70%;
					}
				}
			}
		}
	}
	// 发布
	.footer{
		background: white;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx $padding;
		box-shadow: 0 0 4rpx 4rpx rgba($color: #000000, $alpha: .02);
		z-index: 9;
		.footer-btn{
			border: none;
			border-radius: 0;
			background: $text-price;
			height: 90rpx;
			line-height: 90rpx;
			font-weight: bold;
			font-size: $title-size;
			color: white;
			&::after{
				border: none;
			}
		}
	}
	// 可选服务
	.category-popup{
		background: #F5F5F5;
		padding: 0 $padding * 2 $padding * 2 $padding * 2;
		.header{
			padding: $padding*2 0;
			box-sizing: border-box;
			@extend .vertical;
			.title{
				text-align: center;
				font-size: $title-size + 14;
				font-weight: bold;
				line-height: 90rpx;
			}
			.subtitle{
				font-size: $title-size-m;
				color: $text-gray;
				text-align: center;
			}
		}
		.category-flex{
			max-height: 50vh;
			overflow-y: scroll;
			.category-flex-item{
				margin-bottom: $margin;
				padding: $padding;
				background: white;
				border:solid 1rpx white;
				box-sizing: border-box;
				.category-name{
					padding-bottom: $padding/2;
					font-weight: bold;
					font-size: $title-size-lg;
				}
				.category-content{
					font-size: $title-size-sm;
					color: $text-gray;
					@extend .ellipsis;
				}
				&.show{
					color: $text-price;
					border:solid 1rpx $text-price;
				}
				&:last-child{
					margin-bottom: 0;
				}
			}
		}
		.btns{
			padding-top: $padding * 2;
			button{
				background: $text-price;
				border-radius: 0;
				height: 90rpx;
				line-height: 90rpx;
				font-size: $title-size;
				color: white;
				font-weight: bold;
				&::after{
					border: none;
				}
			}
		}
	}
</style>
