<template>
	<view class="content">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索商品">
			</view>
			<view class="one_2">
				<button size="mini" class="button" type="primary" @click="toAdd">添加</button>
			</view>
		</view>
		<view class="two">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
				<view class="list-scroll-view">
					<view class="list" v-for="(item, index) in list" :key="index">
						<view class="list_1">
							<image class="image" :src="item.file&&item.file.length>0?item.file[0].url:''" mode="">
							</image>
							<view class="other">
								<view class="name textOver">{{item.name||'暂无名称'}}</view>
								<view class="type">类型：{{item.zhType||'暂无类型'}}</view>
								<view class="type">销量：{{item.sell_num||'暂无销量'}}</view>
								<view class="type">是否使用：{{item.zhUser||'暂无'}}</view>
							</view>
						</view>
						<view class="btn">
							<button class="button" type="primary" size="mini" @click="toEdit(item)">修改</button>
							<button class="button1" type="default" size="mini" @click="toSpec(item)">新增规格</button>
							<button class="button1" type="warn" size="mini" @click="toDel(item)">删除</button>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {},
				searchInfo: {},
				user: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
				// 字典表
				typeList: [],
				is_useList: [],
			}
		},
		onLoad: async function() {
			const that = this;
			that.searchToken();
			that.searchConfig();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.searchOther();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) that.$set(that, `user`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				let user = that.user;
				let info = {
					skip: that.skip,
					limit: that.limit,
					supplier_id: user._id
				}
				const res = await that.$api(`/Good`, 'GET', {
					...info,
					...that.searchInfo
				})
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						let type = that.typeList.find(i => i.value == val.type)
						if (type) val.zhType = type.label;
						let is_use = that.is_useList.find(i => i.value == val.is_use)
						if (is_use) val.zhUser = is_use.label;
					}
					that.$set(that, `list`, list)
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 查看详情
			async toView(item) {
				console.log(item);
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.name) that.$set(that.searchInfo, `name`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 新增
			toAdd() {
				uni.navigateTo({
					url: `/pagesMy/goods/add`
				})
			},
			// 修改
			toEdit(e) {
				uni.navigateTo({
					url: `/pagesMy/goods/add?id=${e._id}`
				})
			},
			// 删除
			async toDel(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定取消该商品吗?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/Good/${e._id}`, 'DELETE', {})
							if (res.errcode == 0) {
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			// 新增规格
			toSpec(e) {
				uni.navigateTo({
					url: `/pagesMy/spec/index?id=${e._id}`
				})
			},
			async searchOther() {
				const that = this;
				let res;
				//类型
				res = await that.$api('/DictData', 'GET', {
					type: 'goods_type',
					is_use: '0'
				})
				if (res.errcode == '0') that.$set(that, `typeList`, res.data);
				//是否使用
				res = await that.$api('/DictData', 'GET', {
					type: 'is_use',
					is_use: '0'
				})
				if (res.errcode == '0') that.$set(that, `is_useList`, res.data);
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;

			.one_1 {
				padding: 0 2vw;
				width: 75vw;

				input {
					padding: 2vw;
					background-color: var(--f1Color);
					font-size: var(--font14Size);
					border-radius: 5px;
				}
			}

			.button {
				background-color: var(--f3CColor);
				color: var(--mainColor);
			}
		}

		.two {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);
			margin: 2vw 0 0 0;

			.list {
				background-color: var(--mainColor);
				border: 1px solid var(--f5Color);
				padding: 2vw;
				margin: 2vw 2vw 0 2vw;
				border-radius: 5px;

				.list_1 {
					display: flex;

					.image {
						width: 20vw;
						height: 20vw;
					}

					.other {
						width: 69vw;
						margin: 0 0 0 2vw;

						.name {
							font-size: var(--font16Size);
							margin: 0 0 1vw 0;
						}

						.type {
							color: var(--f85Color);
							font-size: var(--font14Size);
						}
					}
				}

				.btn {
					text-align: center;
					margin: 1vw 0 0 0;

					.button {
						background-color: var(--f3CColor);
						color: var(--mainColor);
						margin: 0 1vw 0 0;
					}

					.button1 {
						margin: 0 1vw 0 0;
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}

	.is_bottom {
		width: 100%;
		text-align: center;

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>
