<template>
	<view class="pad-x20" v-if="isLoading">
		<!-- 头部 -->
		<status-nav :ifReturn="true" :navBarTitle="pageTitle"></status-nav>
		<!-- 提示 -->
		<view class="notice-tips color-orange font26 border-box pad-all20 flex"><image src="/static/icon-notice.png" mode="widthFix"></image>请确保商品信息及联系方式的真实性!</view>
		<!-- 商品编辑 -->
		<view class="release-form color-48 background-white border-box">
			<view class="item font26 mar-x30 flex">
				<view class="title">商品分类<text class="color-red">*</text></view>
				<picker mode="selector" :range="cateList" @change="choiceCate" :value="cateIndex" :range-key="'name'">
					<view class="cate-name">
						<text>{{cateList[cateIndex].name}}</text>
						<image src="/static/icon-arrow-down.png" mode=""></image>
					</view>
				</picker>
			</view>
			<view class="item font26 mar-x30 flex">
				<view class="title">商品名称<text class="color-red">*</text></view>
				<input type="text" v-model="goodsDetail.name" placeholder="请输入商品名称" placeholder-style="color:#8c8c9b">
			</view>
			<view class="item font26 mar-x30 flex">
				<view class="title">商品原价<text class="color-red">*</text></view>
				<input type="number" v-model="goodsDetail.original_price" placeholder="请输入商品原价" placeholder-style="color:#8c8c9b">
			</view>
			<view class="item font26 mar-x30 flex">
				<view class="title">商品售价<text class="color-red">*</text></view>
				<input type="number" v-model="goodsDetail.price" placeholder="请输入商品售价" placeholder-style="color:#8c8c9b">
			</view>
			<view class="item font26 mar-x30 flex">
				<view class="title">商品描述<text class="color-red">*</text></view>
				<textarea v-model="goodsDetail.msg" placeholder="请输入商品描述" placeholder-style="color:#8c8c9b"></textarea>
			</view>
			<view class="item font26 mar-x30 flex">
				<view class="title">商品图片<text class="color-red">*</text></view>
				<view class="upload-box border-box flex">
					<view class="img radius10 border-box" v-for="(item,index) in goodsImg" :key="index">
						<image :src="item" mode="widthFix"></image>
					</view>
					<view class="upload-btn radius10 border-box flex" @tap="changeImg"><image src="/static/icon-release.png" mode="widthFix"></image></view>
				</view>
			</view>
			<view class="item font26 mar-x30 flex">
				<view class="title">联系方式<text class="color-red">*</text></view>
				<input type="number" v-model="goodsDetail.phone" placeholder="请输入联系方式" placeholder-style="color:#8c8c9b">
			</view>
		</view>
		<view class="release-btn background-blue font32 radius10 color-ff flex">提交</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				statusHeight:uni.getSystemInfoSync().statusBarHeight, //状态栏高度
				pageTitle:'', //页面名称
				cateList:[], //分类列表
				cateIndex:0, //分类选择
				goodsDetail:{}, //商品详情
				goodsImg:[], //商品图片
				isEdit:false, //是否编辑进入
				isLoading:false, //是否记载完成
			}
		},
		onLoad(op) {
			if(op.id){
				// 判断是否是编辑进入
				this.isEdit = true;
				this.pageTitle = '编辑商品';
			}else{
				this.pageTitle = '新增商品';
			}
		},
		onShow() {
			// 查询分类
			this.getCateList();
		},
		onShareAppMessage(res) {
			let shareObj = {
				title:'老农极鲜',
				path: uni.getStorageSync('page-path-options')+'?invite_code='+uni.getStorageSync('invite_code'),
				imageUrl:'/static/img/shear-index.jpg',
			}
			// 返回shareObj
			return shareObj;
		},
		onShareTimeline(res){
			let shareObj = {
				title:'老农极鲜',
				query: '?invite_code='+uni.getStorageSync('invite_code'),
				path: uni.getStorageSync('page-path-options')+'?invite_code='+uni.getStorageSync('invite_code'),
				imageUrl:'/static/img/shear-index.jpg',
			}
			// 返回shareObj
			return shareObj;
		},
		methods: {
			// 查询分类
			getCateList(){
				this.cateList = [
					{id:1,name:'图文书籍'},
					{id:2,name:'母婴用品'},
					{id:3,name:'儿童玩具'},
					{id:4,name:'数码设备'},
					{id:5,name:'家用电器'}
				]
				if(this.isEdit){
					// 查询商品详情
					this.getGoodsDetail();
				}else{
					this.isLoading = true;
				}
			},
			
			// 查询商品详情
			getGoodsDetail(){
				this.goodsDetail = {
					name:'儿童自行车',
					category_id:3,
					original_price:'120.00',
					price:'30.00',
					phone:'18683958573',
					imgSrc:'/static/index-01.png,/static/index-02.png,/static/index-03.png',
					msg:'九成新儿童自行车，现在小孩到了，已经用不到了，有需要的可以联系哟！'
				}
				// 获取图片
				this.goodsImg = this.goodsDetail.imgSrc.split(',');
				// 获取当前分类
				this.cateIndex =  this.cateList.findIndex(item=> item.id === this.goodsDetail.category_id);
				this.isLoading = true;
			},
			
			// 选择分类
			choiceCate(e){
				this.cateIndex = e.detail.value;
			},
			
			// 选择图片
			changeImg(){
				uni.chooseImage({
					count: 1, //默认9
					sourceType: ['album','camera'], //从相册选择
					success: (res)=> {
						this.headImg = res.tempFilePaths[0];
						this.uploadImg();
					}
				})
			},
			
			// 上传图片
			uploadImg(){
				uni.showLoading({
					title: '上传中'
				});
				this.$requst.upload('/api/file/upload/image',{path:this.headImg}).then(res=>{
					if(res.code==0) {
						this.headImgUrl = this.$hostHttp+res.data.src;
					}
					uni.hideLoading();
				})
			}
		}
	}
</script>
<style scoped>
	.notice-tips{
		align-items: center;
		background-color: rgba(255,133,0,.2);
		height: 70rpx;
		line-height: 36rpx;
	}
	.notice-tips>image{
		width: 40rpx;
		height: 40rpx;
		margin-right: 8rpx;
		animation: opacity_name 0.8s linear alternate infinite;
	}
	@keyframes opacity_name {
		from {
			opacity: .1;
			transform: scale(.8);
		}
		to {
			opacity: 1;
			transform: scale(1.2);
		}
	}
	.release-form{
		padding: 20rpx;
	}
	.release-form .item{
		align-items: center;
	}
	.release-form .item:last-child{
		margin-bottom: 0;
	}
	.release-form .item>.title{
		width: 140rpx;
	}
	.release-form .item>picker,
	.release-form .item>input,
	.release-form .item>textarea{
		box-sizing: border-box;
		width: calc(100% - 140rpx);
		height: 80rpx;
		padding: 0 10rpx;
		border: 2rpx solid #f4f5f6;
		border-radius: 5rpx;
	}
	.release-form .item>textarea{
		padding: 10rpx;
		height: 200rpx;
		line-height: 1.5;
	}
	.release-form .item .cate-name{
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
		height: 76rpx;
	}
	.release-form .item image{
		width: 28rpx;
		height: 16rpx;
	}
	.release-form .item>.upload-box{
		box-sizing: border-box;
		width: calc(100% - 140rpx);
	}
	.release-form .item .img,
	.release-form .item .upload-btn{
		width: 140rpx;
		height: 140rpx;
		margin-right: 10rpx;
		overflow: hidden;
	}
	.release-form .item .img>image{
		width: 100%;
		min-height: 100%;
	}
	.release-form .item .upload-btn{
		justify-content: center;
		align-items: center;
		margin-right: 0;
		background-color: #ccc;
	}
	.release-form .item .upload-btn>image{
		width: 48rpx;
		height: 48rpx;
		filter: grayscale(100%);
	}
	.release-btn{
		justify-content: center;
		align-items: center;
		width: 80%;
		height: 90rpx;
		margin: 40rpx auto 0;
	}
</style>