<template>
	<view>
		<image mode="widthFix" style="width: 100%;" :src="post" v-if="post"></image>
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-green margin-tb-sm lg" @tap="addTrain">添加培训意愿</button>
		</view>
		<block v-for="(item,index) in plist" :key="index">
			<view class="cu-card dynamic no-card solid-bottom echo-comjobs-item padding-top-sm padding-lr-sm radius">
				<view class="cu-item shadow padding-top-sm padding-bottom">
					<view class="padding-lr flex justify-between align-center">
						<view class="basis-lg text-bold text-lg text-cut">{{item.train.title}}</view>
						<view class="basis-sm text-cut text-right text-gray text-sm">
							<view class="basis-xs text-cut text-right text-red text-sm" @tap="delJoin(index, item.id)">取消报名</view>
						</view>
					</view>
					<view class="padding-lr flex justify-between align-bottom">
						<view class="basis-lg text-bold text-df text-cut">
							<view class="text-gray">{{item.train.contact}}</view>
						</view>
						<view class="basis-sm text-cut text-right text-gray text-sm">
							{{item.train.mobile}}
						</view>
					</view>
					<view class="padding-lr padding-tb-xs text-cut">
						<view class="basis-sm text-cut text-left text-gray text-sm">{{item.train.address}}</view>
					</view>
				</view>
			</view>
		</block>
		<uni-load-more :status="pstatus"></uni-load-more>
	</view>
</template>

<script>
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
	var _this;
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				userinfo: {},
				pstatus: 'more',
				ppage: 1,
				psize: 20,
				plist: [],
				post: "",
			};
		},
		onLoad: function(){
			_this = this;
			_this.userinfo = _this.checkLogin("/pages/train/joinlist");
			if (_this.userinfo===false){
				return false;
			}
			
			_this.$req.ajax({
				path: "index/getConfig",
				data: {
					code: "train_post"
				}
			}).then((data) => {
				_this.post = data;
			}).catch((err) => {
				uni.showModal({
					title: '信息提示',
					content: err,
					showCancel: false
				});
			});
		},
		onShow: function() {
			_this.ppage = 1;
			_this.pstatus = 'more';
			_this.plist = [];
			_this.getMore();
		},
		onPullDownRefresh: function() {
			_this.ppage = 1;
			_this.pstatus = 'more';
			_this.plist = [];
			_this.getMore();
		},
		onReachBottom: function() {
			if (_this.pstatus !== 'more') {
				return;
			}
			_this.getMore();
		},
		methods: {
			delJoin: function(index,id) {
				uni.showModal({
					title: '温馨提示',
					content: '确定要取消吗？',
					success(res) {
						if (res.confirm) {
							_this.$req.ajax({
								path: "train/deljoin",
								data: {
									id: id,
									userid: _this.userinfo.id
								}
							}).then((data) => {
								_this.plist.splice(index, 1);
							}).catch((err) => {
								uni.showModal({
									title: '信息提示',
									content: err,
									showCancel: false
								});
							});
						}
					}
				});
				
			},
			getMore: function() {
				_this.$req.ajax({
					path: "train/joinlist",
					data: {
						ppage: _this.ppage,
						psize: _this.psize,
						userid: _this.userinfo.id
					}
				}).then((data) => {
					_this.pstatus = data.pstatus;
					_this.plist = _this.plist.concat(data.plist);
					_this.ppage += 1;
					uni.stopPullDownRefresh();
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
				});
			},
			addTrain: function() {
				uni.navigateTo({
					url: '/pages/train/list'
				});
			}
		}
	}
</script>

<style>
</style>
