<template>
	<view>
		<view class="tab">
			<u-tabs :list="type" @click="click($event, 'type')"></u-tabs>
		</view>
		<view class="position">
			<view class="audits">
				<view class="audit" :class="{ active: current == index }" v-for="(item, index) in audit" :key="index" @click="click({ index: index }, 'audit')">{{ item.name }}</view>
			</view>
			<view class="item" v-for="(item, index) in list" :key="index" @click="detail(item)">
				<view class="top">
					<view class="title omit">
						<text class="icon" :style="{ color: item.state == 0 ? '#4CAF50' : '#545555' }">&#xe614;</text>
						<text>{{ item.title }}</text>
					</view>
					<view class="audit" v-if="item.audit == 0">
						<text class="icon">&#xe642;</text>
						<text>审核中</text>
					</view>
					<view class="audit" style="color: #4caf50" v-if="item.audit == 1">
						<text class="icon">&#xe612;</text>
						<text>审核通过</text>
					</view>
					<view class="audit" v-if="item.audit == 2">
						<text class="icon">&#xec72;</text>
						<text>审核不通过</text>
					</view>
				</view>
				<view class="desc">
					<text class="tag">{{ item.experience }}</text>
					<text class="tag">{{ item.salary }}</text>
					<text class="tag">{{ item.regionName }}</text>
					<text class="date">{{ item.createTime }}</text>
				</view>
				<view class="flex">
					<view class="f br" @click.stop="manageState(item)">{{ item.state == 0 ? '关闭' : '启用' }}</view>
					<view class="f danger" @click.stop="manageRemove(item)">删除</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>
		<u-action-sheet round="20" :actions="type" @select="selectClick" cancelText="取消" :show="show" @close="show = false"></u-action-sheet>
		<view class="mfooter">
			<button class="btn" @click="show = true">
				<text class="icon">&#xe7c4;</text>
				<text>发布职位</text>
			</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			type: [
				{ name: '全职', value: 0 },
				{ name: '兼职', value: 1 }
			],
			audit: [
				{ name: '全部', value: '' },
				{ name: '待审核', value: 0 },
				{ name: '审核通过', value: 1 },
				{ name: '未通过', value: 2 }
			],
			current: 0,
			list: [],
			param: { pageNum: 1, pageSize: 10, type: 0 },
			loadMore: true,
			show: false
		};
	},
	onLoad(e) {
		this.getData();
		uni.$on('position', (res) => {
			this.refresh();
		});
	},
	methods: {
		getData() {
			this.http.request({
				url: '/app/position/manage/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, tag) {
			this.param[tag] = this[tag][e.index].value;
			if (tag == 'audit') {
				this.current = e.index;
			}
			this.refresh();
		},
		selectClick(e) {
			uni.navigateTo({ url: '/pages/job/position/manage/push?type=' + e.value });
		},
		detail(item) {
			uni.navigateTo({ url: '/pages/job/position/manage/push?id=' + item.id });
		},
		manageState(item) {
			uni.showModal({
				title: '提示',
				content: item.state == 0 ? '确定关闭该职位' : '确定启用该职位',
				success: (res) => {
					if (res.confirm) {
						this.http.request({
							url: '/app/position/manage/state',
							data: { id: item.id, state: item.state == 0 ? 1 : 0 },
							method: 'POST',
							success: (res) => {
								uni.showToast({ title: '操作成功' });
								item.state = item.state == 0 ? 1 : 0;
							}
						});
					}
				}
			});
		},
		manageRemove(item) {
			uni.showModal({
				title: '提示',
				content: '确定删除该职位？删除后包括已投递简历也全部清除',
				success: (res) => {
					if (res.confirm) {
						this.http.request({
							url: '/app/position/manage/remove/' + item.id,
							success: (res) => {
								uni.showToast({ title: '删除成功' });
								this.list.splice(this.list.indexOf(item), 1);
							}
						});
					}
				}
			});
		},
		//刷新数据
		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"></style>
