<template>
	<view>
		<view class="tab">
			<u-tabs :list="tab" @click="click" :lineHeight="5"></u-tabs>
		</view>
		<view class="goodsManageList">
			<view class="item" v-for="(item, index) in list" :key="index" @click="detail(item)">
				<view class="top">
					<view class="title omit">{{ item.goodsName }}</view>
					<view class="state" v-if="item.isRelease == 0" @click.stop="del(item)" style="color: #f44336">删除</view>
					<view class="state" style="color: #4581fb" v-if="item.isRelease == 1 && item.isOrders == 0 && item.isConfirm == 0">发布中</view>
					<view class="state" style="color: #4caf50" v-if="item.isRelease == 1 && item.isOrders == 1 && item.isConfirm == 0">已接单</view>
					<view class="state" style="color: #4caf50" v-if="item.isRelease == 1 && item.isOrders == 1 && item.isConfirm == 1">已确认</view>
				</view>
				<view class="con">
					<view class="flex fs">
						<view class="f left" style="flex: 0.5">
							<view class="tag">数量：{{ item.goodsQuantity }}</view>
						</view>
						<view class="f left">
							<view class="tag">发布于：{{ item.createTime }}</view>
						</view>
					</view>
				</view>
				<view class="op">
					<view class="flex">
						<view class="f rline" v-if="item.isRelease == 0 && item.isOrders == 0" @click.stop="edit(item)">编辑</view>
						<view class="f" v-if="item.isRelease == 0 && item.isOrders == 0 && item.isConfirm == 0" style="color: #4581fb" @click.stop="op(item, 1)">发布</view>
						<view class="f" v-if="item.isRelease == 1 && item.isOrders == 0 && item.isConfirm == 0" style="color: #f44336" @click.stop="op(item, 0)">取消发布</view>
						<view class="f" v-if="item.isRelease == 1 && item.isOrders == 1 && item.isConfirm == 0" style="color: #4581fb" @click.stop="confirm(item, 0)">前往确认</view>
						<view class="f" v-if="item.isRelease == 1 && item.isOrders == 1 && item.isConfirm == 1" @click.stop="detail(item)">查看订单</view>
					</view>
				</view>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			tab: [
				//isRelease:是否发布[0=待发布,1=已发布]  isOrders:是否被接单[0=待接单,1=已被接单]  isConfirm:收购商是否确认:0=未确认,1=已确认
				{ name: '全部', isRelease: '', isOrders: '' ,isConfirm:''},
				{ name: '待发布', isRelease: 0, isOrders: 0 ,isConfirm: 0},
				{ name: '发布中', isRelease: 1, isOrders: 0 ,isConfirm: 0},
				{ name: '已接单', isRelease: 1, isOrders: 1 ,isConfirm: 0},
				{ name: '已确认', isRelease: 1, isOrders: 1 ,isConfirm: 1},
				//{ name: '已完成', isRelease: 1 ,isOrders: 1}
			],
			list: [],
			param: { pageNo: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad(e) {
		this.getData();
		uni.$on('purchaser_buy_list', res => {
			this.refresh();
		});
	},
	methods: {
		getData() {
			this.http.request({
				url: '/level-two-server/app/TbGoodsDemand/getAppList',
				data: this.param,
				loading: 'false',
				success: res => {
					this.loadMore = parseInt(res.data.pageCount) > this.param.pageNo;
					this.list.push(...res.data.data);
				}
			});
		},
		//点击tab切换
		click(e) {
			this.param.isRelease = e.isRelease;
			this.param.isOrders = e.isOrders;
			this.param.isConfirm = e.isConfirm;
			this.refresh();
		},
		detail(item) {
			uni.navigateTo({ url: '/pages/market/two/purchaser/buy/detail?id=' + item.id });
		},
		//编辑
		edit(item) {
			uni.navigateTo({ url: '/pages/market/two/purchaser/buy/add?id=' + item.id });
		},
		//前往确认
		confirm(item) {
			uni.navigateTo({ url: '/pages/market/two/purchaser/buy/detail?id=' + item.id });
		},
		//取消发布或发布
		op(item, isRelease) {
			uni.showModal({
				title: '提示',
				content: isRelease == 0 ? '确定取消发布' : '确定发布到市场?',
				success: res => {
					if (res.confirm) {
						this.http.request({
							url: '/level-two-server/app/TbGoodsDemand/changeState',
							method: 'POST',
							data: { id: item.id, isRelease: isRelease },
							success: res => {
								uni.showToast({ title: '操作成功' });
								item.isRelease = isRelease;
							}
						});
					}
				}
			});
		},
		del(item) {
			uni.showModal({
				title: '提示',
				content: '确定删除该需求？',
				success: res => {
					if (res.confirm) {
						this.http.request({
							url: '/level-two-server/app/TbGoodsDemand/delete?id=' + item.id,
							success: res => {
								uni.showToast({ title: '删除成功' });
								this.list.splice(this.list.indexOf(item), 1);
							}
						});
					}
				}
			});
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNo = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNo++;
			this.getData();
		}
	},
	onNavigationBarButtonTap() {
		uni.navigateTo({ url: '/pages/market/two/purchaser/buy/add' });
	}
};
</script>

<style lang="scss">
page {
	background-color: $pg;
}
</style>
