<template>
	<view>
		<scroll-view scroll-x class="bg-white nav solid-bottom solid-bottom echo-fixed-top">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="status==0?'text-blue cur':''" @tap="tabSelect" data-status="0">全部</view>
				<view class="cu-item flex-sub" :class="status==1?'text-blue cur':''" @tap="tabSelect" data-status="1">待修改</view>
				<view class="cu-item flex-sub" :class="status==2?'text-blue cur':''" @tap="tabSelect" data-status="2">待审核</view>
				<view class="cu-item flex-sub" :class="status==3?'text-blue cur':''" @tap="tabSelect" data-status="3">已上架</view>
				<view class="cu-item flex-sub" :class="status==4?'text-blue cur':''" @tap="tabSelect" data-status="4">已停招</view>
				<view class="cu-item flex-sub" :class="status==5?'text-blue cur':''" @tap="tabSelect" data-status="5">已下架</view>
			</view>
		</scroll-view>
		<view class="echo-fixed-top-empty"></view>
		
		
		<block v-for="(item,index) in plist" :key="index">
			<view class="cu-card dynamic no-card solid-bottom echo-comjobs-item margin-tb-sm">
				<view class="cu-item shadow padding-top-sm padding-bottom">
					<view class="padding-lr padding-bottom-xs flex justify-between align-center">
						<view class="basis-xl text-bold text-lg text-cut">
							{{item.title}}
							<text class="text-red text-sm" v-if="item.wtype==1">(按月)</text>
							<text class="text-red text-sm" v-if="item.wtype==2">(按时)</text>
							<text class="text-red text-sm" v-if="item.wtype==3">(按件)</text>
							<text class="text-red text-sm" v-if="item.wtype==4">(按项目)</text>
							<text class="text-sm">{{item.recruit_num}}名</text>
						</view>
						<view class="basis-xs text-cut text-right text-gray text-sm">{{item.city}}</view>
					</view>
					<view class="padding-lr flex justify-between align-bottom">
						<view><text class="text-red text-bold">{{item.zwagall}}</text></view>
						<view class="text-red" v-if="item.recruitment_cate == 2">悬赏{{item.retmoney}}</view>
					</view>
					<view class="padding-lr padding-bottom-sm text-cut">
						<view v-for="(titem,tindex) in item.tags" :key="tindex" class="cu-tag light bg-gray sm">{{titem}}</view>
					</view>
					
					<view class="grid solids-top text-center col-2" v-if="item.status==1">
						<view @tap="goWcomform" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-write padding-right-xs"></text> 编辑 </view>
						<view @tap="statusComjobs" :data-comjobsid="item.id" :data-status="2" class="padding-top"><text class="cuIcon-pulldown padding-right-xs"></text> 提交审核 </view>
					</view>
					<view class="grid solids-top text-center col-1" v-if="item.status==2">
						<view @tap="goWcomform" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-write padding-right-xs"></text> 编辑 </view>
					</view>
					<view class="grid solids-top text-center col-5" v-if="item.status==3">
						<view @tap="goWcomform" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-write padding-right-xs"></text> 编辑 </view>
						<view @tap="shareComjobs" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-share padding-right-xs"></text> 分享 </view>
						<view @tap="statusComjobs" :data-comjobsid="item.id" :data-status="5" class="padding-top"><text class="cuIcon-pulldown padding-right-xs"></text> 下架 </view>
						<view @tap="updateTime" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-top padding-right-xs"></text> 置顶 </view>
						<view @tap="goComjobsDetail" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-text padding-right-xs"></text> 详情 </view>
					</view>
					<view class="grid solids-top text-center col-4" v-if="item.status==4">
						<view @tap="goWcomform" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-write padding-right-xs"></text> 编辑 </view>
						<view @tap="shareComjobs" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-share padding-right-xs"></text> 分享 </view>
						<view @tap="statusComjobs" :data-comjobsid="item.id" :data-status="5" class="padding-top"><text class="cuIcon-pulldown padding-right-xs"></text> 下架 </view>
						<view @tap="goComjobsDetail" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-text padding-right-xs"></text> 详情 </view>
					</view>
					<view class="grid solids-top text-center col-2" v-if="item.status==5">
						<view @tap="goWcomform" :data-comjobsid="item.id" class="padding-top"><text class="cuIcon-write padding-right-xs"></text> 编辑 </view>
						<view @tap="statusComjobs" :data-comjobsid="item.id" :data-status="4" class="padding-top"><text class="cuIcon-pullup padding-right-xs"></text> 上架 </view>
					</view>
					
				</view>
			</view>
		</block>
		<uni-load-more :status="pstatus"></uni-load-more>
		<view class="padding"></view>
		<button class="cu-btn bg-blue lg shadow echo-gohome" data-pageurl="/pages/worker/wcomform" @tap="goPage"> 发布招聘 </button> 
		<view class="cu-modal" :class="modal.isshow">
			<view class="cu-dialog picmodal">
				<view class="bg-img">
					<image mode="widthFix" :src="modal.comjobspic"></image>
					<view class="cu-bar justify-end text-white picmodalclose">
						<view class="action" @tap="hideModal">
							<text class="cuIcon-close "></text>
						</view>
					</view>
				</view>
				<view class="cu-bar bg-white margin-top-xs">
					<view class="action margin-0 flex-sub solid-left" @tap="saveImage">保存到相册</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue";
	var _this;
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				isRotate: false,
				status: 3,
				userinfo: false,
				workerinfo: false,
				
				param: {},
				pstatus: 'more',
				ppage: 1,
				psize: 20,
				plist: [],
				
				modal:{
					isshow: "",
					comjobspic: ""
				}
			}
		},
		onLoad: function(option) {
			_this = this;
			_this.status = option.status || 3;
			_this.userinfo = _this.checkLogin("/pages/my/my");
			_this.workerinfo = uni.getStorageSync('workerinfo') || false;
			if (_this.userinfo === false || _this.workerinfo === false) {
				uni.reLaunch({
					url: "/pages/my/my"
				});
				return false;
			}
			_this.getMore();
		},
		onPullDownRefresh: function() {
			_this.pageRefresh();
		},
		onReachBottom: function() {
			if (_this.pstatus !== 'more') {
				return;
			}
			_this.getMore();
		},
		methods: {
			
			goComjobsDetail: function(e) {
				var comjobsid = e.currentTarget.dataset.comjobsid;
				uni.navigateTo({
				    url: '/pages/comjobs/detail?comjobsid='+comjobsid
				});
			},
			
			goWcomform: function(e) {
				var comjobsid = e.currentTarget.dataset.comjobsid;
				uni.navigateTo({
				    url: '/pages/worker/wcomform?comjobsid='+comjobsid
				});
			},
			
			saveImage: function() {
				uni.downloadFile({
				    url: _this.modal.comjobspic, //仅为示例，并非真实的资源
				    success: (res) => {
				        if (res.statusCode === 200) {
				            uni.saveImageToPhotosAlbum({
				            	filePath: res.tempFilePath,
				            	success: function () {
									uni.showModal({
										title: '信息提示',
										content: "已保存到手机相册，注意查收。",
										showCancel: false
									});
				            	}
				            });
				        }
				    }
				});
			},
			hideModal: function() {
				_this.modal.isshow = "";
			},
			shareComjobs: function(e) {
				var comjobsid = e.currentTarget.dataset.comjobsid;
				_this.$req.ajax({
					title: "招聘简章生成中...",
					path: "comjobs/getcomjobscode",
					data: {
						comjobsid: comjobsid,
						userid: _this.userinfo.id
					}
				}).then((data) => {
					_this.modal.comjobspic = data.comjobspic;
					_this.modal.isshow = "show";
				}).catch((err) => {
					uni.showModal({
						title: '温馨提示',
						content: err,
						showCancel: false
					});
				});
			},
			
			statusComjobs: function(e) {
				var comjobsid = e.currentTarget.dataset.comjobsid;
				var status = e.currentTarget.dataset.status;
				_this.$req.ajax({
					path: "wcomjobs/statuscomjobs",
					data: {
						comjobsid: comjobsid,
						userid: _this.userinfo.id,
						workerid: _this.workerinfo.id,
						status: status
					}
				}).then((data) => {
					_this.pageRefresh();
				}).catch((err) => {
					uni.showModal({
						title: '信息提示',
						content: err,
						showCancel: false
					});
				});
			},
			
			updateTime: function(e) {
				var comjobsid = e.currentTarget.dataset.comjobsid;
				uni.showModal({
					title: '信息提示',
					content: '每次置顶会扣除'+_this.param.topcomjobs+'积分，确定要做置顶操作吗？',
					success: function(res) {
						if (res.confirm) {
							_this.$req.ajax({
								path: "wcomjobs/updatecomjobs",
								data: {
									comjobsid: comjobsid,
									userid: _this.userinfo.id,
									workerid: _this.workerinfo.id
								}
							}).then((data) => {
								_this.pageRefresh();
							}).catch((err) => {
								uni.showModal({
									title: '信息提示',
									content: err,
									showCancel: false
								});
							});
						}
					}
				});
			},
			
			pageRefresh: function() {
				_this.pstatus = 'more';
				_this.ppage = 1;
				_this.plist = [];
				_this.getMore();
			},
			getMore: function() {
				_this.$req.ajax({
					path: "wcomjobs/listcomjobs",
					data: {
						ppage: _this.ppage,
						psize: _this.psize,
						status: _this.status,
						workerid: _this.workerinfo.id
					}
				}).then((data) => {
					_this.param = data.param;
					_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
					});
				});
			},
			
			tabSelect: function(e) {
				_this.status = e.currentTarget.dataset.status;
				_this.pageRefresh();
			},
			
			goPage: function(e) {
				var pageurl = e.currentTarget.dataset.pageurl;
				uni.navigateTo({
					url: pageurl,
					fail: function() {
						uni.switchTab({
							url: pageurl
						});
					}
				});
			},
		},
	}
</script>

<style>
	.picmodal{ width: 640rpx; background-color: transparent; }
	.picmodal image{ width: 100%; }
	
	.picmodal .bg-img{ position: relative; }
	.picmodalclose{ position: absolute; top: 0rpx; right: 0rpx; }
</style>
