<template>
    <view v-for="job in listData" :key="job.id">
        <view
            v-if="!job.isTitle"
            class="cards"
            @click="handleCardClick(job, $event)"
            :class="{ 'card-selected': selectedJobs.includes(job.jobId) }"
        >
            <!-- 新增复选框 -->

            <view class="card-content">
                <view class="card-company">
                    <view class="company">{{ job.jobTitle }}</view>
                    <view class="salary">
                        <Salary-Expectation
                            :max-salary="job.maxSalary"
                            :min-salary="job.minSalary"
                        ></Salary-Expectation>
                    </view>
                </view>
                <!-- 其他原有内容保持不变 -->
                <view class="card-companyName">{{ job.companyName }}</view>
                <view class="card-tags">
                    <view class="card-tag">
                        <view class="tag">
                            <dict-Label dictType="education" :value="job.education"></dict-Label>
                        </view>
                        <view class="tag">
                            <dict-Label dictType="experience" :value="job.experience"></dict-Label>
                        </view>
                        <view class="tag">
                            {{ vacanciesTo(job.vacancies) }}
                        </view>
                    </view>
                    <view class="custom-checkbox" :class="{ checked: selectedJobs.includes(job.jobId) }">
                        <view
                            class="check-icon"
                            @click.stop="toggleSelect(job.jobId)"
                            :checked="selectedJobs.includes(job.jobId)"
                            color="#4C6EFB"
                        />
                    </view>
                </view>
                <view class="card-bottom">
                    <view>{{ job.postingDate }}</view>
                    <view>
                        <convert-distance
                            :alat="job.latitude"
                            :along="job.longitude"
                            :blat="latitude"
                            :blong="longitude"
                        ></convert-distance>
                        <dict-Label class="mar_le10" dictType="area" :value="job.jobLocationAreaCode"></dict-Label>
                    </view>
                </view>
            </view>
        </view>
        <view class="date-jobTitle" v-else>
            {{ job.title }}
        </view>
    </view>
</template>

<script setup>
import { inject, computed, toRaw, ref } from 'vue';
const { insertSortData, navTo, vacanciesTo } = inject('globalFunction');
import { useRecommedIndexedDBStore } from '@/stores/useRecommedIndexedDBStore.js';
const recommedIndexDb = useRecommedIndexedDBStore();
// 选中的岗位ID集合
const selectedJobs = ref([]);
const props = defineProps({
    list: {
        type: Array,
        default: '标题',
    },
    longitude: {
        type: Number,
        default: 120.382665,
    },
    latitude: {
        type: Number,
        default: 36.066938,
    },
    seeDate: {
        type: String,
        default: '',
    },
});
const emit = defineEmits(['update:selected']);

const listData = computed(() => {
    if (props.seeDate && props.list.length) {
        const ulist = toRaw(props.list);
        const [reslist, lastDate] = insertSortData(ulist, props.seeDate);
        return reslist;
    }
    return props.list;
});

function nextDetail(job) {
    // 记录岗位类型，用作数据分析
    if (job.jobCategory) {
        const recordData = recommedIndexDb.JobParameter(job);
        recommedIndexDb.addRecord(recordData);
    }
    navTo(`/packageA/pages/post/post?jobId=${encodeURIComponent(job.jobId)}&encryptJobId=${encodeURIComponent(job.encryptJobId)}`);
}

function toggleSelect(jobId) {
    const index = selectedJobs.value.indexOf(jobId);
    if (index > -1) {
        selectedJobs.value.splice(index, 1);
    } else {
        selectedJobs.value.push(jobId);
    }
    emit('update:selected', selectedJobs.value);
}

// 修改：卡片点击事件，避免点击复选框时触发跳转
function handleCardClick(job, e) {
    if (job.jobCategory) {
        const recordData = recommedIndexDb.JobParameter(job);
        recommedIndexDb.addRecord(recordData);
    }
    navTo(`/packageA/pages/post/post?jobId=${encodeURIComponent(job.jobId)}&encryptJobId=${encodeURIComponent(job.encryptJobId)}`);
}

// 新增：提供选中状态和切换方法给父组件
defineExpose({
    selectedJobs,
    toggleSelect,
});
</script>

<style lang="stylus" scoped>
// 新增样式
.cards {
    position: relative;
    padding-left: 60rpx; /* 给复选框留出空间 */
}

.checkbox {
    // position: absolute;
    // left: 20rpx;
    // top: 50%;
    // transform: translateY(-50%);
    // z-index: 10;
}

.card-content {
    width: 100%;
}

// 选中状态样式
.card-selected {
    border: 2rpx solid #4C6EFB;
    background-color: #F0F5FF;
}

.date-jobTitle{
    font-weight: 400;
    font-size: 28rpx;
    color: #495265;
    padding: 28rpx 0 0 20rpx
}
.cards{
    padding: 32rpx;
    background: #FFFFFF;
    box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0,0,0,0.04);
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    margin-top: 22rpx;
    .card-company{
        display: flex
        justify-content: space-between
        align-items: flex-start
        .company{
            font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
            font-weight: 500;
            font-size: 32rpx;
            color: #333333;
            flex: 1
        }
        .salary{
            font-family: DIN-Medium;
            font-weight: 500;
            font-size: 28rpx;
            color: #4C6EFB;
            white-space: nowrap
            line-height: 48rpx
        }
    }
    .card-companyName{
        font-weight: 400;
        font-size: 28rpx;
        color: #6C7282;
    }
    .card-tags{
        display: flex
        justify-content: space-between
    }
    .card-tag{
        display: flex
        flex-wrap: wrap
        .tag{
            font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
            width: fit-content;
            height: 30rpx;
            background: #F4F4F4;
            border-radius: 4rpx;
            padding: 6rpx 20rpx;
            line-height: 30rpx;
            font-weight: 400;
            font-size: 24rpx;
            color: #6C7282;
            text-align: center;
            margin-top: 14rpx;
            white-space: nowrap
            margin-right: 20rpx
        }
    }
    .card-bottom{
        margin-top: 32rpx
        display: flex
        justify-content: space-between
        font-size: 28rpx;
        color: #6C7282;
    }
}

/* 复选框容器样式 */
.checkbox {
    /* 保留之前的定位样式，确保复选框在正确位置 */
    position: absolute;
    left: 20rpx;
    top: 50%;
    transform: translateY(-50%);
    z-index: 10;
}

/* 自定义圆形复选框样式 */
.custom-checkbox {
    width: 40rpx;       /* 宽度 */
    height: 40rpx;      /* 高度（与宽度一致形成圆形） */
    border-radius: 50%; /* 圆角设为50%变成圆形 */
    border: 2rpx solid #ccc; /* 未选中时的边框 */
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff; /* 背景色 */
    transition: all 0.3s; /* 过渡动画 */
}

/* 选中状态样式 */
.custom-checkbox.checked {
    border-color: #4C6EFB; /* 选中时的边框颜色 */
    background-color: #4C6EFB; /* 选中时的背景色 */
}
/* 选中时的对勾图标 */
.check-icon {
    margin-top: -4rpx;
    width: 20rpx;
    height: 10rpx;
    border-bottom: 3rpx solid #fff; /* 对勾下边框 */
    border-left: 3rpx solid #fff;   /* 对勾左边框 */
    transform: rotate(-45deg);      /* 旋转形成对勾形状 */
}
</style>
