<template>
	<view class="u-page se-w-vw-100">
		<!-- 招聘信息头部 -->
		<view class="se-m-20 se-br-20 se-bs-b se-bgc-white se-py-20 se-px-30" v-if="jobInfo">
			<view class="se-flex se-flex-h-sb se-mb-10">
				<view class="se-flex se-fw-6">
					<text class="se-c-black se-fs-30">{{jobInfo.title}}</text>
					<text class="se-mx-10 se-b-l"></text>
					<text class="se-fs-28 se-c-orange">日薪:{{jobInfo.salaryDay}}</text>
				</view>
				<view class="se-c-orange se-fs-32 se-fw-6">
					{{jobInfo.salaryMin}}-{{jobInfo.salaryMax}}k
				</view>
			</view>
			<view class="se-py-10">
				<text class="se-c-text-sub se-fs-22 se-mr-10 se-py-5 se-px-20 se-br-5 se-bgc-f5"
					v-if="jobInfo.categoryOne_dictText">{{jobInfo.categoryOne_dictText}}</text>
				<text class="se-c-text-sub se-fs-22 se-mr-10 se-py-5 se-px-20 se-br-5 se-bgc-f5"
					v-if="jobInfo.categoryTwo_dictText">{{jobInfo.categoryTwo_dictText}}</text>
			</view>
			<view class="se-flex se-flex-h-sb">
				<view class="se-flex se-flex-h-c">
					<u-icon name="map"></u-icon>
					<text class="se-c-text-sub se-fs-24 se-pl-10 se-toe-3">{{jobInfo.address}}</text>
				</view>
				<view 
                style="flex-shrink: 0;"
                class="se-c-orange se-fs-24 se-fw-6">
					共{{orderList.length}}个订单
				</view>
			</view>
		</view>
		
		<view class="se-p-40">
			<view class="se-px-40 se-mb-30 se-py-30 se-bgc-white se-br-40 se-bs-b" 
				v-for="(item, index) in orderList" 
				:key="index"
				@click="onOrderDetail(item)">
				<view class="se-flex se-flex-h-sb se-fw-6 se-fs-32 se-pb-20">
					<view class="se-flex">
						
					</view>
					<view class="se-c-red">
						{{getStatusText(item.status)}}
					</view>
				</view>
				<view class="se-flex se-bgc-f5 se-br-20 se-p-20">
					<view class="se-w-160 se-h-160">
						<image class="se-w-160 se-h-160 se-br-10" :src="item.employResume ? 
                        item.employResume.headImage
                        : '/static/image/user.png'" mode="aspectFill"></image>
					</view>
					<view class="se-ml-20 se-flex se-flex-v-sa se-flex-ai-fs se-flex-1">
						<view class="se-fw-6 se-c-black se-fs-30 se-display-ib se-mb-10">求职者：{{item.jobName}}</view>
						<view class="se-fs-24 se-c-text-third se-display-ib se-mb-5">联系电话：{{item.jobPhone}}</view>
						<view class="se-fs-24 se-c-text-third se-display-ib se-mb-5">接单时间：{{item.createTime | formatTime}}</view>
						<view class="se-fs-24 se-c-text-third se-display-ib se-mb-5">工作地址：{{item.jobAddress}}</view>
					</view>
				</view>
			</view>
			<u-empty v-if="orderList && orderList.length==0" mode="list" text="暂无订单信息"></u-empty>
		</view>
	</view>
</template>

<script>
	import {
		getTaskById
	} from "@/common/api.js"
	
	export default {
		data() {
			return {
				orderList: [],
				jobInfo: null,
				jobId: null
			}
		},
		filters: {
			formatTime(time) {
				if (!time) return '';
				// 处理时间戳格式（可能是毫秒或字符串）
				let timestamp;
				if (typeof time === 'number') {
					// 如果是数字，直接使用
					timestamp = time;
				} else {
					// 如果是字符串，转换为时间戳
					timestamp = new Date(time).getTime();
				}
				
				const currentTime = new Date().getTime();
				const diff = (currentTime - timestamp) / 1000; // 时间差，单位：秒

				// 计算月差，判断是否超过一个月
				const oneMonthInSeconds = 30 * 24 * 60 * 60;
				if (diff > oneMonthInSeconds) {
					let date = new Date(timestamp);
					let year = date.getFullYear();
					let month = date.getMonth() + 1;
					let day = date.getDate();

					if (month < 10) month = "0" + month;
					if (day < 10) day = "0" + day;

					return `${year}-${month}-${day}`;
				} else {
					// 计算秒、分钟、小时的差值
					if (diff < 60) {
						return `${Math.floor(diff)}秒钟前`;
					} else if (diff < 60 * 60) {
						return `${Math.floor(diff / 60)}分钟前`;
					} else if (diff < 60 * 60 * 24) {
						return `${Math.floor(diff / 60 / 60)}小时前`;
					} else {
						// 显示天数
						return `${Math.floor(diff / 60 / 60 / 24)}天前`;
					}
				}
			}
		},
		onLoad(options) {
			if (options.jobId) {
				this.jobId = options.jobId;
			}
		},
        onShow(){
			this.getJobDetail();
        },
		methods: {
			getJobDetail() {
				getTaskById({ id: this.jobId }).then(response => {
					if (response.success) {
						this.jobInfo = response.result;
						this.orderList = this.jobInfo.employOrder || [];
						// 设置页面标题
						uni.setNavigationBarTitle({
							title: `${this.jobInfo.title} - 订单列表`
						});
					} else {
						uni.showToast({
							title: '获取工作详情失败',
							icon: 'none'
						});
					}
				}).catch(error => {
					console.error('获取工作详情失败:', error);
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				});
			},
			getStatusText(status) {
				const statusMap = {
					0: '待聘用',
					1: '进行中',
					2: '试工完成',
					3: '企业确认',
					4: '已支付',
					5: '已完成',
					6: '已取消'
				};
				return statusMap[status] || '未知状态';
			},
			onOrderDetail(order) {
				uni.navigateTo({
					url: `/pages_subpack/order-detail/boss?orderId=${order.id}&type=true`
				});
			}
		}
	}
</script>

<style>
</style>