<template>
    <div class="page">
        <NavBar :showIcon="false" :showTitle="true" title="推荐"></NavBar>
        <view class="tabs" :style="{ top: contentHeight }">
            <text @tap="changeActiveTab('求职推荐')" :class="activeTab == '求职推荐' ? 'active' : ''">求职推荐</text>
            <text @tap="changeActiveTab('个人投递')" :class="activeTab == '个人投递' ? 'active' : ''">个人投递</text>
        </view>
        <view class="wrap">
            <view class="recommend">
                <!-- 筛选 -->
                <view class="filter flex-between" v-show="activeTab == '求职推荐'">
                    <view class="filter-opstion">
                        <view class="t1">按以下职位推荐</view>
                        <wd-picker
                            :columns="jobListColumns"
                            value-key="id"
                            label-key="name"
                            v-model="getResumeRecommendListParams.positionId"
                            @confirm="positionConfirm"
                            use-default-slot
                        >
                            <view class="t2">{{ jobTypeText || "无" }}</view>
                            <view class="arrow"></view>
                        </wd-picker>
                    </view>

                    <view class="filter-other flex-between">
                        <wd-col-picker
                            label="选择地址"
                            v-model="valueCity"
                            :columns="columnsCity"
                            :column-change="columnChangeCity"
                            @confirm="handleConfirmCity"
                            label-key="name"
                            value-key="code"
                            use-default-slot
                        >
                            <view class="item">{{ cityText }} <view class="arrow"></view> </view>
                        </wd-col-picker>
                        <view class="filter-opstion">
                            <wd-col-picker
                                v-model="majorValue"
                                :columns="majorOpstion"
                                value-key="name"
                                label-key="name"
                                :column-change="columnChangeMajor"
                                @confirm="handleConfirmMajor"
                                use-default-slot
                            >
                                <view class="item">{{ majorText || "不限" }} <view class="arrow"></view> </view>
                            </wd-col-picker>
                        </view>
                        <!-- <view class="item">默认排序<view class="arrow"></view> </view> -->
                        <view class="item" @click="showOtherFilter = true">筛选<view class="arrow"></view> </view>
                    </view>
                </view>
                <scroll-view
                    class="recommend-list"
                    :refresher-triggered="refresherEnabled"
                    @refresherrefresh="fresherrefresh"
                    refresher-enabled="true"
                    scroll-y="true"
                    @scrolltolower="getResumeRecommendList"
                >
                    <view class="item" v-for="(v, i) in applicationList" :key="v.userId + v.positionId + v.id">
                        <jobSeekersCard :data="v" :type="'application'" />
                    </view>
                    <wd-status-tip v-if="!applicationList || !applicationList.length" image="content" tip="暂无内容" />
                </scroll-view>
            </view>
        </view>
        <!-- 筛选弹窗 -->
        <Filter v-show="showOtherFilter" :show="showOtherFilter" @confirm="filterConfirm" @cancel="showOtherFilter = false" />
    </div>
</template>
<script setup>
/*"code": "0",
"text": "待审核",
"code": "1",
"text": "审核通过",
"code": "2",
"text": "审核不通过",
"code": "3",
"text": "上线",
"code": "4",
"text": "下线", */
import { ref } from "vue";
import jobSeekersCard from "@/components/jobSeekersCard/index.vue";
import { getJobListApi, getApplicationListApi, getRecommendedResumeListApi, getPositionListApi } from "@/api/postion";
import { getTreeListApi } from "@/api/common";
import { getLoginUserApi, companyReviewInfo } from "@/api/user";
import { getMajorTreeApi } from "@/api/common";
import { getEumData, getEnumText } from "@/utils/utils";
import { useAsync } from "@/hooks/useAsync";
import _ from "lodash";
import Filter from "@/components/filter/index.vue";
/* tab */
const contentHeight = ref("50px");
const activeTab = ref("求职推荐");
const changeActiveTab = tab => {
    activeTab.value = tab;
    isEnd.value = false;
    applicationList.value = [];
    // resetParams();
    getResumeRecommendList();
    /* getTopFilter();
    getJobListApiFn(); */
};
let refresherEnabled = ref(true);
/* 获取职位列表 */
const jobListColumns = ref([]);
const positionConfirm = ({ selectedItems }) => {
    jobTypeText.value = selectedItems.name;
    resetPageParams();
    getResumeRecommendListParams.value.recommend = true;
    getResumeRecommendList();
};
function getJobListApiFn() {
    getPositionListApi({ status: 3 }).then(res => {
        if (res.code == 200) {
            jobListColumns.value = res.data;
            if (res.data.length) {
                getResumeRecommendListParams.value.positionId = res.data[0].id;
                jobTypeText.value = res.data[0].name;
            }
            getResumeRecommendList();
        }
    });
}
getJobListApiFn();
const jobTypeText = ref("");
function fresherrefresh() {
    refresherEnabled.value = true;
    refresh().then(() => {
        getResumeRecommendList();
    });
}
// 数据列表
const applicationList = ref([]);
/* 投递接口参数 */
const getApplicationListParams = ref({
    jobType: "",
    provinceCode: "",
    cityCode: "",
    districtCode: "",
    minWorkYear: "",
    maxWorkYear: "",
    education: "",
    minAge: "",
    maxAge: "",
    pageNo: 1,
    pageSize: 10
});
/* 获取推荐简历列表接口参数 */
const getResumeRecommendListParams = ref({
    recommend: true,
    positionId: "",
    major: "",
    sex: "",
    jobType: "",
    provinceCode: "",
    cityCode: "",
    districtCode: "",
    education: "",
    minAge: "",
    maxAge: "",
    minWorkAge: "",
    maxWorkAge: "",
    pageNo: 1,
    pageSize: 10
});
const isEnd = ref(false);

const resetParams = () => {
    isEnd.value = false;
    applicationList.value = [];
    getApplicationListParams.value = {
        jobType: "",
        provinceCode: "",
        cityCode: "",
        districtCode: "",
        minWorkYear: "",
        maxWorkYear: "",
        education: "",
        minAge: "",
        maxAge: "",
        pageNo: 1,
        pageSize: 10
    };
    getResumeRecommendListParams.value = {
        recommend: true,
        positionId: "",
        sex: "",
        major: "",
        jobType: "",
        provinceCode: "",
        cityCode: "",
        districtCode: "",
        education: "",
        minAge: "",
        maxAge: "",
        minWorkAge: "",
        maxWorkAge: "",
        pageNo: 1,
        pageSize: 10
    };
};
const resetPageParams = () => {
    applicationList.value = [];
    isEnd.value = false;
    getResumeRecommendListParams.value.pageNo = 1;
    getApplicationListParams.value.pageNo = 1;
};
const refresh = () => {
    return new Promise((resolve, reject) => {
        resetPageParams();
        resolve();
    });
};
// 获取数据
const getResumeRecommendList = () => {
    if (isEnd.value === true)
        return new Promise(resolve => {
            resolve();
        });
    xma.showLoading({
        title: "加载中...",
        mask: true
    });
    // 获取推荐简历
    if (activeTab.value === "求职推荐") {
        return getRecommendedResumeListApi(getResumeRecommendListParams.value).then(res => {
            xma.hideLoading();
            res.data = res.data?.filter(v => !v.applied);
            refresherEnabled.value = false;
            if (!res.data) {
                res.data = [];
            }
            if (res.data.length < getResumeRecommendListParams.value.pageSize) {
                isEnd.value = true;
            }
            getResumeRecommendListParams.value.pageNo += 1;
            applicationList.value = applicationList.value.concat(res.data);

            // applicationList.value = res.data || [];
        });
    } else {
        // 获取已投递
        return getApplicationListApi(getApplicationListParams.value).then(res => {
            xma.hideLoading();
            refresherEnabled.value = false;
            if (!res.data) {
                res.data = [];
            }
            if (res.data.length < getApplicationListParams.value.pageSize) {
                isEnd.value = true;
            }
            getApplicationListParams.value.pageNo += 1;
            applicationList.value = applicationList.value.concat(res.data);
        });
    }
};
const companyReviewInfoFn = () => {
    companyReviewInfo().then(res => {
        if (!res.data) {
            uni.navigateTo({
                url: `/pages/user/businessLicense/fillIn?steps=3`
            });
        }
    });
};
companyReviewInfoFn();
// 第一次不执行推荐列表需要(需要拉取到列表在执行)
let notFirst = false;
onShow(() => {
    resetPageParams();
    if (notFirst) {
        getResumeRecommendList();
    }
    notFirst = true;
    getUserInfo();
});
// 城市筛选
const valueCity = ref([]);
const cityText = ref("全国");
const columnsCity = ref([]);
const getColumnsCity = () => {
    getTreeListApi().then(res => {
        // 给说有子选项都添加全部
        res.data.forEach(v => {
            v.children.unshift({
                code: "",
                name: "全部"
            });
            v.children.forEach(vv => {
                if (vv.children?.length) {
                    vv.children.unshift({
                        code: "",
                        name: "全部"
                    });
                }
            });
        });
        res.data.unshift({
            code: "",
            name: "全国"
        });
        columnsCity.value.push(res.data);
    });
};
const columnChangeCity = ({ resolve, selectedItem, finish }) => {
    if (selectedItem.children?.length) {
        resolve(selectedItem.children);
    } else {
        finish();
    }
};
getColumnsCity();

const handleConfirmCity = ({ value, selectedItems }) => {
    const textIndex = _.compact(value).length - 1;
    if (textIndex === -1) {
        cityText.value = "全国";
    } else {
        cityText.value = selectedItems[textIndex].name;
    }
    const [provinceCode, cityCode, districtCode] = value;

    getApplicationListParams.value.provinceCode = provinceCode || "";
    getApplicationListParams.value.cityCode = cityCode || "";
    getApplicationListParams.value.districtCode = districtCode || "";

    getResumeRecommendListParams.value.recommend = false;
    getResumeRecommendListParams.value.provinceCode = provinceCode || "";
    getResumeRecommendListParams.value.cityCode = cityCode || "";
    getResumeRecommendListParams.value.districtCode = districtCode || "";

    resetPageParams();
    getResumeRecommendList();
};
/* 专业筛选 */
const majorValue = ref([]);
const majorOpstion = ref([]);
const columnChangeMajor = ({ selectedItem, resolve, finish }) => {
    if (selectedItem.children) {
        resolve(selectedItem.children);
    } else {
        finish();
    }
};
const handleConfirmMajor = ({ value }) => {
    if (value[1]) {
        majorText.value = value[1];
        getResumeRecommendListParams.value.major = value.join('-');
    } else {
        majorText.value = "不限";
        getResumeRecommendListParams.value.major = "";
    }
    resetPageParams();
    getResumeRecommendListParams.value.recommend = false;
    getResumeRecommendList();
};
const majorText = ref("");
getMajorTreeApi().then(res => {
    if (res.code === 200) {
        majorOpstion.value.push(
            Object.keys(res.data).map(key => ({
                name: key,
                children: res.data[key]
            }))
        );
        majorOpstion.value[0].unshift({
            name: "不限"
        });
    }
});
// 显示其他筛选
const showOtherFilter = ref(false);
const filterConfirm = data => {
    Object.keys(data).forEach(key => {
        getResumeRecommendListParams.value[key] = data[key];
    });

    getResumeRecommendListParams.value.recommend = false;
    getResumeRecommendListParams.value.positionId = "";
    showOtherFilter.value = false;
    resetPageParams();
    getResumeRecommendList();
};
/* 获取用户信息 */
const getUserInfo = async () => {
    const userRes = await getLoginUserApi();
    if (!userRes.data) {
        xma.navigateTo({
            url: `/pages/user/businessLicense/fillIn?steps=1`
        });
    } else {
        if (userRes.data.status == 0) {
            xma.navigateTo({
                url: `/pages/user/businessLicense/fillIn?steps=3`
            });
        }
    }
};
onLoad(() => {
    xma.xh.getSystemInfo({
        success(res) {
            // console.log('设备信息', res); // 设备信息
        }
    });
    xma.xh.getMenuButtonBoundingClientRect({
        success(res) {
            contentHeight.value = (res.top + res.bottom) / 2 + 25 + "px";
        }
    });
});
</script>

<style lang="scss" scoped>
.page {
    display: flex;
    flex-direction: column;
    height: 100%;
}
.tabs {
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 14rpx;
    background-color: #ffffff;
    text {
        width: 280rpx;
        height: 80rpx;
        font-size: 36rpx;
        line-height: 80rpx;
        color: #1b2026;
        text-align: center;
        &.active {
            position: relative;
            font-size: 36rpx;
            font-weight: 600;
            background-image: url("https://lygsh-image.wjzpgz.com/community/communityActiveTab.png");
            background-repeat: no-repeat;
            background-position: 50% 100%;
            background-size: 20% 20%;
        }
    }
}
.wrap {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    .recommend {
        position: relative;
        display: flex;
        flex-direction: column;
        flex-grow: 1;
        .filter {
            position: relative;
            padding: 0 32rpx 0 44rpx;
            .filter-opstion {
                max-width: 230rpx;
                :deep(.wd-picker__field) {
                    display: flex;
                    align-items: center;
                }
                .t1 {
                    font-size: 24rpx;
                    font-weight: normal;
                    line-height: 36rpx;
                    color: #77818f;
                }
                .t2 {
                    flex-grow: 1;
                    width: 100rpx;
                    overflow: hidden;
                    font-size: 32rpx;
                    font-weight: 600;
                    line-height: 48rpx;
                    color: #1b2026;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    .arrow {
                        width: 0;
                        height: 0;
                        margin-left: 20rpx;
                        border-color: #c9cdd4;
                        border-width: 8rpx;
                        transform: rotateZ(45deg) translateY(-4rpx);
                    }
                }
            }
            .filter-other {
                gap: 16rpx;
                .item {
                    /* display: flex;
                    align-items: flex-end; */
                    position: relative;
                    max-width: 120rpx;
                    padding: 12rpx 18rpx;
                    overflow: hidden;
                    font-size: 24rpx;
                    font-weight: normal;
                    line-height: 24rpx;
                    color: #1f86ff;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    background: rgb(31 134 255 / 10%);
                    .arrow {
                        position: absolute;
                        right: 6rpx;
                        bottom: 12rpx;
                        width: 0;
                        height: 0;
                        margin-left: 4rpx;
                        border-color: #1f86ff;
                        border-width: 4rpx;
                        transform: rotateZ(0deg);
                    }
                }
            }
        }
        .other-view {
            position: absolute;
            top: 0;
            left: 0;
            z-index: 11;
            width: 100%;
            height: 100%;
            background-color: #00000066;
            .wrapper {
                padding: 1rpx 40rpx;
                background-color: #ffffff;
                .other-view-item {
                    margin-top: 24rpx;
                    label {
                        font-size: 24rpx;
                        color: #1b2026;
                    }
                    .select-list {
                        display: flex;
                        flex-wrap: wrap;
                        gap: 24rpx;
                        margin-top: 24rpx;
                        .select-item {
                            padding: 12rpx 28rpx;
                            font-size: 24rpx;
                            color: #a3a7b3;
                            border: 2rpx solid #e1e2e6;
                            border-radius: 8rpx;
                            &.active {
                                color: #1f86ff;
                                background: rgb(31 134 255 / 10%);
                                border: 0;
                            }
                        }
                    }
                    :deep(.wd-slider) {
                        height: 36rpx;
                        padding-top: 36rpx;
                        .wd-slider__button-wrapper {
                            width: 30rpx;
                            height: 30rpx;
                            .wd-slider__button {
                                width: 30rpx;
                                height: 30rpx;
                            }
                            .wd-slider__label {
                                bottom: 36rpx;
                                width: 30rpx;
                                font-size: 20rpx;
                            }
                        }
                        .wd-slider__bar-wrapper {
                            margin: 0 40rpx;
                        }
                    }
                }
                .button-wrap {
                    display: flex;
                    gap: 24rpx;
                    justify-content: flex-end;
                    padding: 32rpx 0;
                    div {
                        padding: 10rpx 24rpx;
                        font-size: 28rpx;
                        color: #1f86ff;
                        border-radius: 8rpx;
                    }
                    .reset {
                        color: #ffffff;
                        background: #1f86ff;
                    }
                    .confirm {
                        color: #1f86ff;
                        border: 2rpx solid #1f86ff;
                    }
                }
            }
        }
        .recommend-list {
            flex-grow: 1;
            height: 200rpx;
            .item {
                padding: 16rpx 32rpx;
                border-bottom: 16rpx solid #f3f4f8;
            }
            .wd-status-tip {
                padding-top: 280rpx;
            }
        }
    }
}
</style>
