<template>
	<view class="container">
		<u-cell-group class="form-info" :border="false">
			<u-field label="商品名称" placeholder="请输入商品名称" :disabled="ifEdit()" label-width="180" v-model="goodInfo.productName"></u-field>
			<u-cell-item :title="'请上传商品图片（' + fileList.length + '/5）'" :arrow="false">
				<view slot="label">
					<u-image width="98px" height="98px" border-radius="5px" v-for="(item, index) in fileList" :src="item" class="good-img"
					 v-if="ifEdit()"></u-image>
					<u-upload :action="uploadUrl" :file-list="defaultList" :form-data="uploadData" @on-success="uploadSuccess"
					 @on-error="uploadError" @on-remove="uploadRemove" max-count="5" :disabled="ifEdit()"></u-upload>
				</view>
			</u-cell-item>
			<u-cell-item title="原价" :arrow="false">
				<u-number-box :positive-integer="false" :min="0" v-model="goodInfo.originalPrice" bg-color="#51A539" color="#ffffff"
				 :disabled="ifEdit()"></u-number-box>
			</u-cell-item>
			<u-cell-item :title="formType == 1 ? '售价' : '采摘价格'" :arrow="false" v-if="formType != 2">
				<u-number-box :positive-integer="false" :min="0" v-model="goodInfo.bizPrice" bg-color="#51A539" color="#ffffff"
				 :disabled="ifEdit()"></u-number-box>
			</u-cell-item>
			<u-cell-item title="起拍价" :arrow="false" v-if="formType == 2">
				<u-number-box :positive-integer="false" :min="0" v-model="goodInfo.auctionStartPrice" bg-color="#51A539" color="#ffffff"
				 :disabled="ifEdit()"></u-number-box>
			</u-cell-item>
			<u-cell-item title="每次最低加价金额" :arrow="false" v-if="formType == 2">
				<u-number-box :positive-integer="false" :min="0" v-model="goodInfo.auctionMinAddPrice" bg-color="#51A539" color="#ffffff"
				 :disabled="ifEdit()"></u-number-box>
			</u-cell-item>
			<u-field label="计量单位" placeholder="请输入计量单位" label-width="180" v-model="goodInfo.unit" :disabled="ifEdit()"></u-field>
			<u-cell-item title="库存" :arrow="false">
				<u-number-box :min="1" v-model="goodInfo.stock" bg-color="#51A539" color="#ffffff" :disabled="ifEdit()"></u-number-box>
			</u-cell-item>
			<u-field label="商品说明" type="textarea" placeholder="请输入商品说明" label-width="180" v-model="goodInfo.productDescribe"
			 :disabled="ifEdit()"></u-field>
			<u-cell-item title="商品分类" :value="sortText" @click="selectShow = !ifEdit()"></u-cell-item>
			<u-cell-item title="拍卖截止时间" :value="goodInfo.auctionEndTime" @click="dateShow = !ifEdit()" v-if="formType == 2"></u-cell-item>
		</u-cell-group>
		<view class="form-handle">
			<u-button type="success" shape="circle" :ripple="true" @click="submitData" :disabled="getPermit()" class="handle-custom">{{type == 'add' ? '提交' : (type == 'detail' ? '编辑' : '保存')}}</u-button>
		</view>
		<u-picker mode="time" v-model="dateShow" :start-year="startYear" :params="params" @confirm="setDate"></u-picker>
		<u-select v-model="selectShow" mode="mutil-column-auto" :list="sortList" @confirm="setSort"></u-select>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	export default {
		data() {
			return {
				goodId: '',
				type: '',
				formType: '1',
				goodInfo: {
					productName: '',
					originalPrice: 1,
					bizPrice: 1,
					unit: '',
					stock: 1,
					productDescribe: '',
					categoryId: '',
					auctionStartPrice: 1,
					auctionMinAddPrice: 1,
					auctionEndTime: '',
				},
				selectShow: false,
				parentId: '',
				sortList: [],
				sortText: '',
				dateShow: false,
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				startYear: '',
				uploadData: {
					folderId: 0,
				},
				uploadUrl: '',
				fileList: [],
				defaultList: [],
			}
		},
		onLoad(options) {
			this.uploadUrl = API.uploadFile
			this.goodId = options.goodId
			this.type = options.type
			this.formType = options.formType
			this.startYear = new Date().getFullYear()
			NET.request(API.getSortList, {}, 'POST').then(res => {
				this.sortList = res.data.map(item => {
					return {
						value: item.cateCode,
						label: item.cateValue,
						children: item.oneCategory ? item.oneCategory.map(site => {
							return {
								value: site.cateCode,
								label: site.cateValue,
							}
						}) : []
					}
				})
				if (this.parentId && this.goodInfo.categoryId && !this.sortText) {
					let sort = this.sortList.find(site => site.value == this.parentId)
					this.sortText = sort.label + '-' + sort.children.find(site => site.value == this.goodInfo.categoryId).label
				}
			}).catch(res => {
				this.$refs.uTips.show({
					title: '获取商品分类失败',
					type: 'warning',
				})
			})
			if (options.type == 'detail') {
				NET.request(API.getGoodDetail, {
					id: options.goodId
				}, 'POST').then(res => {
					this.goodInfo = {
						productName: res.data.productName,
						originalPrice: parseFloat(res.data.originalPrice),
						bizPrice: parseFloat(res.data.bizPrice),
						unit: res.data.unit,
						stock: res.data.stock,
						productDescribe: res.data.productDescribe,
						categoryId: res.data.categoryId,
						auctionStartPrice: parseFloat(res.data.auctionStartPrice),
						auctionMinAddPrice: parseFloat(res.data.auctionMinAddPrice),
						auctionEndTime: res.data.auctionEndTime,
					}
					this.parentId = res.data.parentId
					this.fileList = res.data.imgPath
					this.defaultList = res.data.imgPath.map(site => {
						return {
							url: site
						}
					})
					if (this.sortList.length && !this.sortText) {
						let sort = this.sortList.find(site => site.value == res.data.parentId)
						this.sortText = sort.label + '-' + sort.children.find(site => site.value == res.data.categoryId).label
					}
				}).catch(res => {
					this.$refs.uTips.show({
						title: '获取商品详情失败',
						type: 'warning',
					})
				})
			}
		},
		methods: {
			//  校验当前模式
			ifEdit() {
				return this.type == 'add' || this.type == 'edit' ? false : true
			},
			//  文件上传成功回调
			uploadSuccess(res, index, lists, name) {
				this.fileList.push(res.data.url)
				this.$refs.uTips.show({
					title: '文件上传成功',
					type: 'success',
				})
				return true
			},
			//  文件上传失败回调
			uploadError(res, index, lists, name) {
				this.$refs.uTips.show({
					title: '文件上传失败',
					type: 'warning',
				})
			},
			//  移除文件回调
			uploadRemove(index, lists, name) {
				this.fileList.splice(index, 1)
			},
			//  设置分类
			setSort(data) {
				this.goodInfo.categoryId = data[1].value
				this.sortText = data[0].label + '-' + data[1].label
			},
			//  设置时间
			setDate(data) {
				this.goodInfo.auctionEndTime = data.year + '-' + data.month + '-' + data.day + ' ' + data.hour + ':' + data.minute +
					':' + data.second
			},
			//  检查必填项
			getPermit() {
				if (this.type == 'detail') {
					return false
				} else {
					if (!this.fileList.length) {
						return true
					}
					if (!this.goodInfo.productName || !this.goodInfo.unit || !this.goodInfo.categoryId || !this.goodInfo.stock) {
						return true
					}
					if (this.formType == 2 && (!this.goodInfo.auctionStartPrice || !this.goodInfo.auctionMinAddPrice || !this.goodInfo
							.auctionEndTime)) {
						return true
					} else if (this.formType != 2 && !this.goodInfo.bizPrice) {
						return true
					}
					return false
				}
			},
			//  提交
			submitData() {
				if (this.formType == 2) {
					this.goodInfo.bizPrice = this.goodInfo.auctionStartPrice
				}
				if (this.type == 'detail') {
					this.type = 'edit'
				} else if (this.type == 'edit') {
					NET.request(API.editGood, {
						...this.goodInfo,
						id: this.goodId,
						imgPath: this.fileList,
					}, 'POST').then(res => {
						this.$refs.uTips.show({
							title: '编辑成功',
							type: 'success',
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}).catch(res => {
						this.$refs.uTips.show({
							title: '编辑失败',
							type: 'warning',
						})
					})
				} else {
					NET.request(API.addGood, {
						...this.goodInfo,
						imgPath: this.fileList,
						productType: this.formType
					}, 'POST').then(res => {
						this.$refs.uTips.show({
							title: '新增成功',
							type: 'success',
						})
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}).catch(res => {
						this.$refs.uTips.show({
							title: '新增失败',
							type: 'warning',
						})
					})
				}
			},
		},
	}
</script>

<style lang="less" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.container {
		width: 100%;
		height: 100%;
		float: left;
		overflow-y: auto;

		.form-info {
			width: 100%;
			float: left;

			/deep/.u-label-text {
				color: #333333;
			}

			/deep/.u-cell_title {
				color: #333333;
			}

			.good-img {
				float: left;
				margin: 5px;
			}
		}

		.form-handle {
			width: calc(100% - 30px);
			float: left;
			height: 40px;
			margin: 30px 15px 20px 15px;


			.handle-custom {
				background-color: #51A539;
			}
		}
	}
</style>
