<template>
	<view class="main">
		<view class="tab">
			<u-tabs :list="tab" :current="current" @click="click"></u-tabs>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index">
				<view class="it">
					<view class="lable">项目名称</view>
					<view class="desc">{{ item.projectName }}</view>
				</view>
				<view class="it">
					<view class="lable">发包公司</view>
					<view class="desc">{{ item.companyName }}</view>
				</view>
				<view class="it">
					<view class="lable">项目周期</view>
					<view class="desc">{{ item.startDate }} 至 {{ item.finishDate }}</view>
				</view>
				<view class="it">
					<view class="lable">结算日期</view>
					<view class="desc">{{ item.balanceDate }}</view>
				</view>
				<view class="op">
					<text>{{ item.createTime }}</text>
					<text class="add" @click="add(item)" v-if="current == 0">接包</text>
					<text v-if="current == 1" class="state">已接包</text>
					<text v-if="current == 2" class="state">已完成</text>
				</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 {
			current: 0,
			tab: [{ name: '待接包' }, { name: '已接包' }, { name: '已完成' }],
			list: [],
			param: { pageNum: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad(e) {
		this.getData();
	},
	methods: {
		getData() {
			this.http.request({
				url: this.current === 0 ? '/app/project/list' : '/app/packages/list',
				data: this.param,
				loading: 'false',
				success: (res) => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					res.data.rows.forEach((item) => {
						item.createTime = uni.$u.timeFrom(Date.parse(item.createTime));
						this.list.push(item);
					});
				}
			});
		},
		click(e) {
			this.current = e.index;
			this.param.state = e.index === 1 ? 0 : 1;
			this.refresh();
		},
		add(item) {
			uni.showModal({
				title: '提示',
				content: '确定接包?',
				success: (res) => {
					if (res.confirm) {
						this.http.request({
							url: '/app/packages/add',
							data: { projectId: item.id, companyId: item.companyId },
							method: 'POST',
							success: (res) => {
								uni.showModal({
									title: '提示',
									content: '接包成功',
									showCancel: false,
									success: (res) => {
										this.refresh();
									}
								});
							}
						});
					}
				}
			});
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.main {
	padding: 0px 15px 10px 15px;
}
.tab {
	background-color: white;
	border-radius: 7px;
	padding-bottom: 10px;
}
.list {
	padding-top: 13px;
	.item {
		background-color: white;
		padding: 15px;
		border-radius: 5px;
		margin-bottom: 10px;
		.it {
			overflow: hidden;
			padding: 7px 0px 7px 0px;
			.lable {
				float: left;
				font-size: 14px;
				color: #787878;
			}
			.desc {
				float: right;
				font-size: 14px;
				width: 60%;
				text-align: right;
			}
		}
		.op {
			border-top: 1px solid $line;
			padding-top: 10px;
			color: #676767;
			font-size: 14px;
			.add {
				float: right;
				color: white;
				background-color: $main-color;
				padding: 3px 20px;
				border-radius: 20px;
			}
			.state {
				float: right;
			}
		}
	}
}
</style>
