<template>
	<view class="content">
		<view class="box">
			<view class="title requ">图片和标题</view>
			<view class="upload">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="1" width="166rpx" height="166rpx">
				</u-upload>
			</view>
			<u-textarea v-model="name" border="none" confirmType="done" placeholder="标题示例：【现货】夏季新款 韩版碎花雪纺连衣裙" count maxlength="100"></u-textarea>
		</view>
		<view class="box">
			<view class="box_item hflex acenter jbetween">
				<view class="box_left requ">规格</view>
				<view class="hflex acenter" @click="addSpec">
					<view class="box_right text_blue" v-if="items == ''">+新增规格</view>
					<view class="box_right text_blue" v-else>共{{items.length}}个规格</view>
					<u-icon name="arrow-right" size="12" color="#617BFE"></u-icon>
				</view>
			</view>
			<view class="box_item hflex acenter jbetween">
				<view class="box_left requ">价格和库存</view>
				<view class="hflex acenter" @click="toSetting">
					<view class="box_right" v-if="items == ''">未设置</view>
					<view class="box_right" v-else>已设置</view>
					<u-icon name="arrow-right" size="12" color="#ACACAC"></u-icon>
				</view>
			</view>
			<view class="box_item hflex acenter jbetween" @click="open">
				<view class="box_left requ">类目</view>
				<view class="hflex acenter">
					<view class="box_right" v-if="cateids == ''">请选择商品类目</view>
					<view class="box_right" style="max-width: 500rpx;" v-else>{{cate}}</view>
					<u-icon name="arrow-right" size="12" color="#ACACAC"></u-icon>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="box_item hflex acenter jbetween">
				<view class="box_left">物流配送</view>
				<view class="hflex acenter">
					<view class="box_right">快递发货</view>
					<u-icon name="arrow-right" size="12" color="#ACACAC"></u-icon>
				</view>
			</view>
		</view>
		<view class="box">
			<view class="title">商品详情</view>
			<u-textarea v-model="content" border="none" confirmType="done" placeholder="描述一下商品特色吧~" ></u-textarea>
			<view class="title">图片/视频</view>
			<view class="upload hflex acenter fwrap">
				<block v-for="(item,index) in fileList2" :key="index">
					<view v-if="item.type == 'image'" style="position: relative;">
						<image :src="item.url" class="img" mode="aspectFill" v-if="item.type == 'image'"></image>
						<image src="/static/images/common/popu_close.png" class="close_icon" @click="deletePic2(index)"></image>
					</view>
					<view v-if="item.type == 'video'" style="position: relative;">
						<video :src="item.url" direction="0" class="img" v-if="item.type == 'video'"></video>
						<image src="/static/images/common/popu_close.png" class="close_icon" @click="deletePic2(index)"></image>
					</view>
					<!-- <image :src="item.url" class="img" mode="aspectFill" v-if="item.type == 'image'"></image>
					<video :src="item.url" direction="0" class="img" v-if="item.type == 'video'"></video> -->
				</block>
				<!-- <u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple :maxCount="9" width="200rpx" height="200rpx">
					<image src="/static/images/common/upload_img.png" class="upload_img2"></image>
				</u-upload> -->
				<image src="/static/images/common/upload_img.png" class="img"  @click="choose"></image>
			</view>
		</view>
		<view class="box" style="margin-bottom: 186rpx;">
			<view class="box_item hflex acenter jbetween">
				<view class="box_left">商品状态</view>
				<view class="hflex acenter">
					<block v-for="(item,index) in radioList" :key="index">
						<view class="hflex acenter" style="padding-left: 32rpx;" @click="changeRadio(index)">
							<image src="/static/images/common/select1.png" style="width: 36rpx;height: 36rpx;" v-if="active !== index"></image>
							<image src="/static/images/common/select2.png" style="width: 36rpx;height: 36rpx;" v-else></image>
							<view class="radio_text">{{item.name}}</view>
						</view>
					</block>
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="btn hflex acenter jcenter" @click="create" v-if="id == ''">创建商品</view>
			<view class="btn_group hflex acenter jbetween" v-else>
				<view class="btn1 hflex acenter jcenter" @click="dele">删除</view>
				<view class="btn2 hflex acenter jcenter" @click="save">保存</view>
			</view>
		</view>
		<u-popup :show="cate_show" round="10" mode="bottom" @close="close" :safeAreaInsetBottom="false">
			<view class="popu">
				<view class="popu_top hflex acenter jbetween">
					<view></view>
					<view class="popu_title">选择类目</view>
					<image src="/static/images/common/close_icon.png" style="width: 28rpx;height: 28rpx;" @click="close"></image>
				</view>
				<view class="center">
					<view class="menuList hflex acenter jbetween">
						<scroll-view scroll-y="true" class="menu_left">
							<view class="vflex">
								<block v-for="(item,index) in cateList" :key="index">
									<view class="left_item text_hide hflex acenter" :class="index == left_active? 'left_active':''" @click="clickTab1(index)">
										<text class="text_hide" style="width: 100%;text-align: center;">{{item.name}}</text>
									</view>
								</block>
							</view>
						</scroll-view>
						<scroll-view scroll-y="true" class="menu_right">
							<view class="vflex" v-if="cateList[left_active]">
								<block v-for="(item,index) in cateList[left_active].sub" :key="index">
									<view class="right_item hflex acenter jbetween" @click="selectItem(index)">
										<view class="item_text hflex acenter jcenter" :class="index == right_active? 'right_active':''">{{item.name}}</view>
										<image src="/static/images/common/check.png" style="width: 36rpx;height: 36rpx;" v-if="index == right_active"></image>
									</view>
								</block>
							</view>
						</scroll-view>
					</view>
				</view>
				<view class="popu_bottom">
					<view class="btn hflex acenter jcenter"@click="sure">确定</view>
					
				</view>
			</view>
		</u-popup>
		<u-popup :show="show_choose" @close="close">
			<view class="vflex acenter">
				<view class="hflex acenter jcenter choose" @click="choose_img">
					<view>图片</view>
				</view>
				<view class="hflex acenter jcenter choose" @click="choose_video">
					<view>视频</view>
				</view>
				<view class="hflex acenter jcenter choose" @click="close">
					<view>取消</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import $api, { info } from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				fileList1: [],
				fileList2: [],
				cateList: [],
				left_active: 0,
				right_active: -1,
				name: '',
				content: '',
				radioList: [
					{
						name: '上架售卖'
					},
					{
						name: '暂不上架'
					}
				],
				active: 0,
				items: '',
				cover: '',
				slider: '',
				content: '',
				status: 1,
				cateids: '',
				cate: '',
				spec: '',
				stock_total: 0,
				price_selling: '',
				cate_show: false,
				id: '',
				data: [],
				spect: '',
				data_specs: '',
				show_choose: false
			}
		},
		onLoad(options) {
			that = this
			if(options.id) {
				that.id = options.id
				that.getData()
			}
			if(options.data) {
				that.data = JSON.parse(options.data)
				that.spect = options.spec
				that.items = that.data
				that.spec = that.spect
				for(var i=0;i<that.items.length;i++) {
					that.stock_total += Number(that.items[i].stock_total)
					if(that.items[i].status) {
						that.items[i].status = 1
					} else {
						that.items[i].status = 2
					}
				}
				that.price_selling = that.items[0].price_selling
			}
			
			that.getCate()
		},
		onShow() {
			var data = uni.getStorageSync('specData')
			var spec = uni.getStorageSync('spectitle')
			if(data) {
				that.data = JSON.parse(data)
				that.spect = JSON.parse(spec)
				that.items = that.data
				that.spec = that.spect
				for(var i=0;i<that.items.length;i++) {
					that.stock_total += Number(that.items[i].stock_total)
					if(that.items[i].status) {
						that.items[i].status = 1
					} else {
						that.items[i].status = 2
					}
				}
				that.price_selling = that.items[0].price_selling
			}
		},
		methods: {
			getData() {
				$api.req({
					url: '/data/api.business.Goods/goods_details',
					method: 'POST',
					data: {
						goods_id: that.id
					}
				}, function(res) {
					if(res.code == 1) {
						console.log(res);
						that.data_specs = res.data.data_specs
						that.cover = res.data.cover
						var imgList = res.data.cover.split('|')
						for(var i=0;i<imgList.length;i++) {
							var data = {
								url: imgList[i]
							}
							that.fileList1.push(data)
						}
						that.name = res.data.name
						that.items = res.data.item
						that.cateids = res.data.cateids
						that.cate = res.data.cate_name[0].name
						that.content = res.data.content
						that.slider = res.data.slider
						imgList = res.data.slider.split('|')
						for(var i=0;i<imgList.length;i++) {
							var data = {
								url: imgList[i]
							}
							that.fileList2.push(data)
						}
						that.status = res.data.status
						that.stock_total = res.data.stock_total
						that.price_selling = res.data.price_selling
						if(that.status == 1) {
							that.active = 0
						} else {
							that.active = 1
						}
						if(that.data.length!==0) {
							that.items = that.data
							that.spec = that.spect
							for(var i=0;i<that.items.length;i++) {
								that.stock_total += Number(that.items[i].stock_total)
							}
							that.price_selling = that.items[0].price_selling
						}
					}
				})
			},
			getCate() {
				$api.req({
					url: '/data/api.business.Goods/getCate'
				}, function(res) {
					if(res.code == 1) {
						that.cateList = res.data
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			deletePic2(index) {
				that.fileList2.splice(index,1)
			},
			// 新增图片
			async afterRead(event) {
				uni.showLoading({
					title: '上传中',
					mask: true
				})
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`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[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						// status: 'success',
						type: result.type,
						url: result.url
					}))
					fileListLen++
					if(lists.length - 1 == i) {
						uni.hideLoading()
					}
				}
				console.log(that.fileList1);
			},
			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: {
							'token': uni.getStorageSync('token')?uni.getStorageSync('token'):'',
							'api-name': 'iosapp'
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								var data = JSON.parse(res.data)
								console.log('data:',data);
								var type = data.data.key.split('.')
								if(type[1] == 'mp4') {
									that.$set(data.data,'type','video')
								} else {
									that.$set(data.data,'type','image')
								}
								resolve(data.data)
							}, 1000)
						}
					});
				})
			},
			changeRadio(index) {
				that.active = index
				if(that.active == 0) {
					that.status = 1
				} else {
					that.status = 0
				}
			},
			addSpec() {
				if(that.id) {
					return
				}
				$api.jump('/pages/good/spec?items=' + that.data_specs+'&good_id=' + that.id)
			},
			toSetting() {
				var data = uni.getStorageSync('specData')
				var list = uni.getStorageSync('spectitle')
				if(that.id) {
					$api.jump('/pages/good/setting?good_id=' + that.id + '&list=' + JSON.stringify(that.items) + '&data=' +that.data_specs)
				} else {
					$api.jump('/pages/good/setting?list=' + data + '&data=' +list)
				}
			},
			open() {
				that.cate_show = true
			},
			clickTab1(e) {
				that.left_active = e
				that.right_active = -1
			},
			selectItem(index) {
				that.right_active = index
			},
			sure() {
				if(that.left_active == -1 && that.right_active == -1) {
					$api.info("请先选择类目")
					return
				}
				if(that.right_active == -1) {
					that.cateids = that.cateList[that.left_active].id
					that.cate = that.cateList[that.left_active].name
				} else {
					that.cateids = that.cateList[that.left_active].sub[that.right_active].id
					that.cate = that.cateList[that.left_active].sub[that.right_active].name
				}
				that.close()
			},
			close() {
				that.cate_show = false
				that.show_choose = false
			},
			create() {
				uni.removeStorageSync("specData")
				uni.removeStorageSync("spectitle")
				console.log('总库存',that.stock_total);
				for(var i=0;i<that.fileList1.length;i++) {
					that.cover += that.fileList1[i].url + '|'
				}
				for(var i=0;i<that.fileList2.length;i++) {
					that.slider += that.fileList2[i].url + '|'
				}
				that.cover = that.cover.substr(0,that.cover.length - 1)
				that.slider = that.slider.substr(0,that.slider.length - 1)
				console.log(that.spec);
				$api.req({
					url: '/data/api.business.Goods/goods_add',
					method: 'POST',
					data: {
						name: that.name,
						cateids: that.cateids,
						cover: that.cover,
						slider: that.slider,
						content: that.content,
						stock_total: that.stock_total,
						price_selling: that.price_selling,
						status: that.status,
						spec: JSON.stringify(that.spec),
						items: JSON.stringify(that.items)
					}
				}, function(res) {
					if(res.code == 1) {
						$api,info(res.info)
						
						$api.jump('/pages/tabbar/index/index',2)
					}
				})
			},
			save() {
				that.cover = ""
				that.slider = ""
				uni.removeStorageSync("specData")
				uni.removeStorageSync("spectitle")
				console.log('总库存',that.stock_total);
				for(var i=0;i<that.fileList1.length;i++) {
					that.cover += that.fileList1[i].url + '|'
				}
				for(var i=0;i<that.fileList2.length;i++) {
					that.slider += that.fileList2[i].url + '|'
				}
				var items = []
				for(var i=0;i<that.items.length;i++) {
					var temp = {
						id: that.items[i].id,
						goods_spec: that.items[i].goods_spec,
						stock_total: that.items[i].stock_total,
						price_selling: that.items[i].price_selling,
						sku_image: that.items[i].show_image?that.items[i].show_image: that.items[i].sku_image,
						status: that.items[i].status
					}
					items.push(temp)
				}
				that.cover = that.cover.substr(0,that.cover.length - 1)
				that.slider = that.slider.substr(0,that.slider.length - 1)
				$api.req({
					url: '/data/api.business.Goods/goods_edit',
					method: 'POST',
					data: {
						goods_id: that.id,
						name: that.name,
						cateids: that.cateids,
						cover: that.cover,
						slider: that.slider,
						content: that.content,
						stock_total: that.stock_total,
						price_selling: that.price_selling,
						status: that.status,
						items: JSON.stringify(items)
					}
				}, function(res) {
					if(res.code == 1) {
						$api,info(res.info)
						
						$api.jump('/pages/tabbar/index/index',2)
					}
				})
			},
			dele() {
				$api.req({
					url: '/data/api.business.Goods/del_goods',
					method: 'POST',
					data: {
						goods_id: that.id
					}
				}, function(res) {
					if(res.code == 1) {
						$api.info(res.info) 
						$api.jump('/pages/tabbar/index/index',2)
					}
				})
			},
			choose() {
				that.show_choose = true
			},
			choose_img() {
				that.close()
				
				uni.chooseImage({
					count: 9, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: function (res) {
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						let fileListLen = that.fileList2.length
						let file = res.tempFiles[0].path
						console.log(file);
						uni.uploadFile({
							url: $api.config.baseUrl + '/data/api.auth.Center/upload',  
							filePath: file,
							name: 'file',
							header: {
								'token': uni.getStorageSync('token')?uni.getStorageSync('token'):'',
								'api-name': 'iosapp'
							},
							formData: {
								user: 'test'
							},
							success: (res2) => {
								var data = JSON.parse(res2.data)
								console.log('data:',data);
								let item = {
									type: 'image',
									url: data.data.url
								}
								that.fileList2.push(item)
								uni.hideLoading()
							},
							fail(err) {
								console.log(err);
							}
						});
						
					}
				});
			},
			choose_video() {
				that.close()
				
				uni.chooseVideo({
					sourceType: ['camera', 'album'],
					success: function (res) {
						uni.showLoading({
							title: '上传中',
							mask: true
						})
						let fileListLen = that.fileList2.length
						let file = res.tempFilePath
						console.log(file);
						uni.uploadFile({
							url: $api.config.baseUrl + '/data/api.auth.Center/upload',  
							filePath: file,
							name: 'file',
							header: {
								'token': uni.getStorageSync('token')?uni.getStorageSync('token'):'',
								'api-name': 'iosapp'
							},
							formData: {
								user: 'test'
							},
							success: (res2) => {
								var data = JSON.parse(res2.data)
								let item = {
									type: 'video',
									url: data.data.url
								}
								that.fileList2.push(item)
								uni.hideLoading()
							},
							fail(err) {
								console.log(err);
							}
						});
					}
				});
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content::v-deep {
		background: #F5F5F5;
		.box {
			width: 100%;
			background: #FFFFFF;
			box-sizing: border-box;
			padding: 0 30rpx;
			margin-top: 20rpx;
			.title {
				font-size: 30rpx;
				font-weight: 400;
				color: #222222;
				line-height: 42rpx;
				margin: 28rpx 0 20rpx;
			}
			.requ {
				position: relative;
			}
			.requ:before {
				content: '*';
				top: 0;
				left: -15rpx;
				position: absolute;
				font-size: 30rpx;
				font-weight: 400;
				color: #FF3636;
				
			}
			.upload {
				margin-bottom: 20px;
				.upload_img1 {
					width: 166rpx;
					height: 166rpx;
				}
				
				.upload_img2 {
					width: 200rpx;
					height: 200rpx;
				}
				.img {
					width: 200rpx;
					height: 200rpx;
					margin: 0 20rpx 20rpx 0;
					border-radius: 16rpx;
				}
				video {
					width: 200rpx;
					height: 200rpx;
					margin: 0 20rpx 20rpx 0;
				}
				.close_icon {
					position: absolute;
					top: 0;
					right: 15rpx;
					width: 30rpx;
					height: 30rpx;
					z-index: 99;
				}
			}
			.u-textarea {
				padding: 0 !important;
			}
			.box_item {
				padding: 28rpx 0;
				border-bottom: 1rpx solid #F5F5F5;
				.box_left {
					font-size: 30rpx;
					font-weight: 400;
					color: #222222;
					line-height: 42rpx;
				}
				.box_right {
					font-size: 30rpx;
					font-weight: 400;
					color: #ACACAC;
					line-height: 42rpx;
					padding-right: 10rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					box-orient: vertical;
					line-clamp: 2;
					-webkit-line-clamp: 2;
				}
				.text_blue {
					color: #617BFE;
				}
				.radio_text {
					font-size: 30rpx;
					font-weight: 400;
					color: #222222;
					line-height: 42rpx;
					padding-left: 12rpx;
				}
			}
		}
		.bottom {
			width: 100%;
			height: 166rpx;
			background: #FFFFFF;
			position: fixed;
			bottom: 0;
			z-index: 99;
			.btn {
				width: 690rpx;
				height: 84rpx;
				background: #5471FF;
				border-radius: 46rpx;
				margin: 8rpx auto 0;
				font-size: 36rpx;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 50rpx;
			}
			.btn_group {
				width: 100%;
				box-sizing: border-box;
				padding: 8rpx 30rpx 0;
				.btn1 {
					width: 330rpx;
					height: 84rpx;
					border-radius: 46rpx;
					border: 1rpx solid #5471FF;
					font-size: 32rpx;
					font-weight: 500;
					color: #5471FF;
					line-height: 50rpx;
				}
				.btn2 {
					width: 330rpx;
					height: 84rpx;
					background: #5471FF;
					border-radius: 46rpx;
					font-size: 32rpx;
					font-weight: 500;
					color: #FFFFFF;
					line-height: 50rpx;
				}
			}
		}
		.popu {
			background: #FFFFFF;
			border-radius: 40rpx 40rpx 0px 0px;
			// border: 2rpx solid #979797;
			.popu_top {
				width: 690rpx;
				margin: 0 auto;
				padding: 44rpx 0 28rpx;
				border-bottom: 1px solid #F7F7F7;
				.popu_title {
					font-size: 36rpx;
					font-weight: 500;
					color: #222222;
					line-height: 50rpx;
				}
			}
			.center {
				margin: 0 0 188rpx;
				.menuList {
					width: 100%;
					.menu_left {
						width: 200rpx;
						height: 850rpx;
						background: #F5F5F5;
						border-radius: 0px 28rpx 0px 0px;
						.left_item {
							max-width: 200rpx;
							font-size: 28rpx;
							font-weight: 400;
							color: #222222;
							line-height: 40rpx;
							padding: 30rpx 20rpx;
						}
						.left_active {
							color: #506DFF;
							background: #fff;
						}
					}
					.menu_right {
						width: 550rpx;
						height: 850rpx;
						background: #FFFFFF;
						.right_item {
							padding: 30rpx 30rpx 30rpx 64rpx;
							.item_text {
								font-size: 30rpx;
								font-weight: 400;
								color: #393939;
								line-height: 42rpx;
							}
							.right_active {
								color: #5B77FF;
							}
						}
						.tabs_text {
							width: 80rpx;
							height: 56rpx;
							background: #FFFFFF;
							font-size: 20rpx;
							font-weight: 400;
							color: #393939;
							line-height: 28rpx;
						}
					}
				}
			}
			.popu_bottom {
				width: 100%;
				height: 166rpx;
				background: #FFFFFF;
				position: fixed;
				bottom: 0;
				z-index: 99;
				.btn {
					width: 690rpx;
					height: 84rpx;
					background: #5471FF;
					border-radius: 46rpx;
					margin: 8rpx auto 0;
					font-size: 36rpx;
					font-weight: 500;
					color: #FFFFFF;
					line-height: 50rpx;
				}
				
			}
		}
		.choose {
			padding: 20rpx 0;
		}
	}
</style>