<!-- 我的面试 -->
<!-- 
	投递状态码 status
	0      未查看
	10     已查看
	11     不合适
	20     已邀请面试
	30     已接受面试
	40     已拒绝面试
	50     面试通过
	60     不合适
	70     未参加面试
	80     已拒绝入职
	90     已入职
-->
<template>
	<view class="interview">
		<z-paging ref="paging" v-model="interviewList" @query="queryList">
			<view class="interview-list">
				<view class="interview-list-item" v-for="(item, index) in interviewList" :key="index">
					<view class="interview-list-item-title">
						<view>{{ getDictLabel(item.status, 'deliveryStatusList') }}</view>
						<view>
							<image src="../../static/img/shijian.png" />
						</view>
					</view>
					<view class="interview-list-item-date" :class="{'disable': item.status == 40 || item.status == 80}">{{ item.interviewTime }}</view>
					<view class="interview-list-item-way">{{ item.interviewAddress }}</view>
					<view class="interview-list-item-position">
						<view class="left">
							<view style="margin-right: 20rpx;">
								<u-image :src="item.companyLogoUrl" width="100" height="100" border-radius="10" />
							</view>
							<view class="info">
								<view>{{ item.postName }}</view>
								<view>
									{{ `${item.provinceName} | 经验${item.workYear}年 | ${getEducationBg(item.educationBg)}` }}
								</view>
								<view>{{ item.companyName }}</view>
							</view>
						</view>
						<view class="right">
							<view>{{ `${item.lowestSalary}k ~ ${item.highestSalary}k` }}</view>
							<view v-if="item.updateTime">{{ item.updateTime.split(' ')[0] }}</view>
							<view v-else>{{ item.createTime.split(' ')[0] }}</view>
						</view>
					</view>
					<!-- 当状态为20时,拒绝面试/接受面试 -->
					<view class="interview-list-item-button double" v-if="Number(item.status) === 20">
						<view class="btn" @click="changeInterviewState(item, 30)">接受</view>
						<view class="btn" @click="changeInterviewState(item, 40)">拒绝</view>
					</view>
					<!-- 当状态为30时且面试时间大于当前时间 -->
					<view class="interview-list-item-button"
						v-if="Number(item.status) === 30 && new Date(item.interviewTime).valueOf() < new Date().valueOf()">
						<view class="submit btn" @click="videoInterview(item)">发起视频面试</view>
					</view>
					<!-- 当状态为30时且面试时间小于当前时间 -->
					<view class="interview-list-item-button"
						v-else-if="Number(item.status) === 30 && new Date(item.interviewTime).valueOf() >= new Date().valueOf()">
						<view class="not btn">未到视频面试</view>
					</view>
					<!-- 当状态为50时,拒绝入职/接受入职 -->
					<view class="interview-list-item-button double" v-else-if="Number(item.status) === 50">
						<view class="btn" @click="changeInterviewState(item, 90)">接受</view>
						<view class="btn" @click="changeInterviewState(item, 80)">拒绝</view>
					</view>
				</view>
			</view>
		</z-paging>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 面试列表
				interviewList: [],
				// 面试状态
				deliveryStatusList: [],
				degrEducList: [],
				pageNum: 1,
				pageSize: 10
			}
		},
		onLoad() {
			this.getDict('delivery_status')
			this.getDictEduc();
		},
		methods: {
			/**
			 * 获取教育等级名称
			 * @param {Object} value
			 */
			getEducationBg(value) {
				let name;
				this.degrEducList.forEach(item => {
					if (item.text == value) {
						name = item.label
					}
				})
				return name;
			},
			/**
			 * 获取字典
			 */
			getDictEduc() {
				this.$u.api.getDictdataUrl({
					key: 'degr_educ'
				}).then(res => {
					if (res.code === 200) {
						this.degrEducList = res.data;
					}
				})
			},
			/**
			 * 获取我的面试列表
			 * @param { Number } pageNum 
			 * @param { Number } pageSize
			 */
			getInterviewList(pageNum, pageSize) {
				this.$u.api.interview.getInterviewList({
					pageNum,
					pageSize
				}).then(res => {
					if (res.code === 200) {
						this.pageNum = pageNum
						this.pageSize = pageSize
						this.$refs.paging.complete(res.rows)
					} else {
						this.$refs.paging.complete([])
						this.$refs.uToast.show({
							title: res.msg,
							type: 'error'
						})
					}
				}).catch(err => {
					this.$refs.uToast.show({
						title: '系统异常!',
						type: 'error'
					})
				})
			},
			/**
			 * 组件下拉加载触发
			 * @param { Number } pageNo
			 * @param { Number } pageSize
			 */
			queryList(pageNo, pageSize) {
				this.getInterviewList(pageNo, pageSize);
			},
			/**
			 * 获取数据字典
			 * @param { String } key
			 */
			getDict(key) {
				this.$u.api.getDictdataUrl({
					key
				}).then(res => {
					if (res.code === 200) {
						switch (key) {
							case 'delivery_status':
								this.deliveryStatusList = res.data;
								break;
						}
					}
				})
			},
			/**
			 * 获取字典名称
			 * @param { String } val
			 * @param { String } key
			 */
			getDictLabel(val, key) {
				let label;
				this[key].forEach(item => {
					if (Number(item.text) === Number(val)) {
						label = item.label
					}
				})
				return label;
			},
			/**
			 * 修改面试状态
			 * @param {Number} state
			 */
			changeInterviewState(item, state) {
				this.$u.api.interview.changeInterviewStateApi({ status: state, id: item.id }).then(res => {
					console.log(res)
					if (res.code === 200) {
						let msg;
						switch(state) {
							case 30:
								msg = '已接受面试'
								break;
							case 40:
								msg = '已拒绝面试'
								break;
							case 80:
								msg = '已拒绝入职'
								break;
							case 90:
								msg = '已接受入职'
								break;
							default:
								msg = '更新成功'
								break
						}
						this.$refs.uToast.show({
							title: msg,
							type: 'success'
						})
						this.getInterviewList(this.pageNum, this.pageSize);
					}
				})
			},
			/**
			 * 视频面试
			 * @param {Object} item
			 */
			videoInterview(item) {
				let url =
					`${this.config.viewInterviewUrl}?veteMemberId=${item.companyId}&inviterId=${this.vuex_user.userId}&from=veterans`
				this.$u.route({
					url: '/pages/webView/webView',
					params: {
						url: encodeURIComponent(url)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import './myInterview.scss';
</style>
