<template>
    <div class="page">
        <NavBar
            :showIcon="true"
            :showTitle="true"
            backIcon="#fff"
            textColor="#fff"
            title="邀请面试"
            backgroundBox="transparent"
        ></NavBar>
        <view class="bg"></view>

        <view class="card">
            <image src="@/static/image/user/default-avatar.png" mode="" />
            <view class="name">{{ userInfo.name || "无" }}</view>
            <view class="school">{{ userInfo.schoolName || "无" }}·{{ userInfo.major || "无" }}</view>
            <view class="info-list">
                <view class="info flex-between">
                    <veiw class="label">面试时间</veiw>
                    <!-- <wd-datetime-picker
                        v-model="date"
                        type="date"
                        :min-date="new Date().getTime()"
                        use-default-slot
                        @confirm="dateConfirm"
                    >
                    </wd-datetime-picker> -->
                    <view :class="{ data: 1, placeholder: !date }" @click="isShowDateSelect = true">{{
                        dateText || "选择面试时间"
                    }}</view>
                    <view class="arrow"></view>
                </view>
                <view class="info flex-between">
                    <veiw class="label">面试职位</veiw>
                    <!-- <view class="data">{{ data.positionName }}，{{ data.salary }}</view> -->
                    <!-- 需要选择岗位 -->
                    <template v-if="jobIdTemp">
                        <view class="data">{{ data.positionName }}，{{ data.negotiableSalary ? "面议" : data.salary }}</view>
                    </template>
                    <template v-else>
                        <wd-picker
                            :columns="jobListColumns"
                            value-key="id"
                            label-key="name"
                            @confirm="positionConfirm"
                            use-default-slot
                        >
                            <view class="data" v-if="data.positionName">{{ data.positionName }}，{{ data.salary }}</view>
                            <view class="t2" v-else>请选择职位</view>
                        </wd-picker>
                    </template>
                    <view class="arrow"></view>
                </view>
                <view class="info flex-between">
                    <veiw class="label">联系人</veiw>
                    <view class="data">{{ data.contactPerson }}</view>
                    <view class="arrow"></view>
                </view>
                <view class="info flex-between">
                    <veiw class="label">联系电话</veiw>
                    <view class="data">{{ data.contactPhone }}</view>
                    <view class="arrow"></view>
                </view>
                <view class="info flex-between">
                    <veiw class="label">面试地址</veiw>
                    <!-- <view class="data">{{ data.interviewLocation }}</view> -->
                    <input type="text" v-model="data.interviewLocation" placeholder="请输入面试地址" />
                    <view class="arrow"></view>
                </view>
            </view>
        </view>

        <wd-action-sheet v-model="isShowDateSelect" @close="isShowDateSelect = false">
            <div class="action-sheet-header">
                <wd-text text="取消" @click="isShowDateSelect = false"></wd-text>
                <wd-text type="primary" text="确认" @click="selectDateConfirm"></wd-text>
            </div>
            <div class="date-sheet-wrap" v-if="isShowDateSelect">
                <div class="date" style="width: 75%">
                    <wd-datetime-picker-view type="date" v-model="date" :min-date="new Date().getTime()" />
                </div>
                <div class="time-slot" style="width: 25%">
                    <wd-picker-view :columns="columns" v-model="timeSlot" />
                </div>
            </div>
        </wd-action-sheet>

        <view class="btn-wrap">
            <button class="bottom-btn" @click="submit">确定邀请</button>
        </view>
    </div>
</template>

<script setup>
import dayjs from "dayjs";
import { getApplicationByIdApi, inviteInterviewApi, getJobByIdApi, getPositionListApi, getResumeByUserId } from "@/api/postion";
import { reactive, ref } from "vue";
/* 时间选择器 */
const isShowDateSelect = ref(false);
const date = ref("");
const columns = ref([
    [
        {
            label: "上午",
            value: "上午"
        },
        {
            label: "下午",
            value: "下午"
        }
    ]
]);
const timeSlot = ref("上午");
const dateText = ref("");
const selectDateConfirm = e => {
    isShowDateSelect.value = false;
    dateText.value = dayjs(date.value).format("YYYY-MM-DD") + " " + timeSlot.value;
};
const jobIdTemp = ref("");
let userIdTemp = "";
const data = ref({});
/* 获取职位列表 */
const jobListColumns = ref([]);
function getJobListApiFn() {
    getPositionListApi({ status: 3 }).then(res => {
        if (res.code == 200) {
            jobListColumns.value = res.data;
        }
    });
}
getJobListApiFn();
const positionConfirm = ({ selectedItems }) => {
    data.value = selectedItems;
    // data.value.interviewLocation = selectedItems.address;
    data.value.salary = selectedItems.minSalary + "~" + selectedItems.maxSalary;
    data.value.positionName = selectedItems.name;
    jobIdTemp.value = selectedItems.id;
};
onLoad(({ id, jobId, userId }) => {
    userIdTemp = userId;
    // 投递获取
    if (Number(id)) {
        getApplicationByIdApi(id).then(res => {
            data.value = res.data;
            data.value.applicationId = id;
        });
    } else if (Number(jobId)) {
        getJobById(jobId);
    }
    console.log(1111);
    // 查询用户信息
    getUserinfo();
});
// 查询用户信息
const userInfo = reactive({
    name: "",
    schoolName: "",
    major: ""
});
const getUserinfo = () => {
    getResumeByUserId(userIdTemp).then(res => {
        console.log(res);
        userInfo.name = res.data.userInfo.name;
        const educationExperience = JSON.parse(res.data.userResume.educationExperience || [{}])[0];
        userInfo.schoolName = educationExperience.schoolName;
        userInfo.major = educationExperience.major;
    });
};
const getJobById = jobId => {
    // 数据凭借
    getJobByIdApi(jobId).then(res => {
        res.data.salary = res.data.minSalary + "~" + res.data.maxSalary;
        res.data.positionName = res.data.name;
        // res.data.interviewLocation = res.data.address;
        jobIdTemp.value = jobId;

        data.value = res.data;
        console.log(res);
    });
};
const back = () => {
    uni.navigateBack();
};

const submit = () => {
    if (!date.value) {
        uni.showToast({
            title: "请选择面试时间",
            icon: "none"
        });
        return;
    }
    if (!data.value.applicationId && !jobIdTemp.value) {
        uni.showToast({
            title: "请选择面试岗位",
            icon: "none"
        });
        return;
    }
    if (!data.value.interviewLocation) {
        uni.showToast({
            title: "请输入面试地址",
            icon: "none"
        });
        return;
    }
    /* 时间拼接 */
    let dateTemp = dayjs(date.value).format("YYYY-MM-DD");
    if (timeSlot.value == "上午") {
        dateTemp += " 09:00:00";
    } else {
        dateTemp += " 15:00:00";
    }
    inviteInterviewApi({
        ...data.value,
        // interviewTime: date.value,
        interviewTime: dateTemp,
        userId: userIdTemp,
        positionId: jobIdTemp.value
    }).then(res => {
        if (res.code === 200) {
            uni.showToast({
                title: "邀请成功",
                icon: "none"
            });
            setTimeout(() => {
                uni.switchTab({
                    url: "/pages/recommend/index"
                });
            }, 1000);
        }
    });
};
</script>

<style lang="scss" scoped>
.page {
    height: 100%;
}
.nav-bar {
    .arrow {
        width: 20rpx;
        height: 20rpx;
        border-color: #ffffff;
        transform: rotateZ(135deg);
    }
}
.bg {
    position: absolute;
    top: 0rpx;
    left: 0rpx;
    z-index: -1;
    width: 752rpx;
    height: 522rpx;
    background: linear-gradient(181deg, #1f86ff 0%, #1f86ff 7%, rgb(241 248 255 / 0%) 90%);
}
.card {
    position: relative;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 686rpx;
    height: 678rpx;
    padding-top: 112rpx;
    margin: 100rpx auto 0;
    background: #ffffff;
    border-radius: 12rpx;
    box-shadow: 0rpx 12rpx 31.4rpx rgb(185 217 255 / 56%);
    image {
        position: absolute;
        top: -80rpx;
        left: 50%;
        width: 160rpx;
        height: 160rpx;
        transform: translateX(-50%);
    }
    .name {
        font-size: 36rpx;
        font-weight: 500;
        line-height: 36rpx;
        color: #1b2026;
    }
    .school {
        padding-bottom: 32rpx;
        margin: 8rpx 32rpx 0;
        font-size: 24rpx;
        font-weight: normal;
        line-height: 36rpx;
        color: #1b2026;

        // border-bottom: 2rpx solid #F3F4F8;
    }
    .info-list {
        display: flex;
        flex-direction: column;
        gap: 48rpx;
        width: 620rpx;
        padding-top: 32rpx;
        border-top: 2rpx solid #f3f4f8;
        .info {
            box-sizing: border-box;
            .label {
                flex: 0 0 160rpx;
                font-size: 24rpx;
                line-height: 36rpx;
                color: #77818f;
            }
            .wd-picker {
                flex: 1;
            }
            .data {
                flex: 1;
                font-size: 28rpx;
                line-height: 36rpx;
                color: #1b2026;
            }
            input {
                flex: 1;
            }
            .placeholder {
                color: #bfcacf;
            }
            .arrow {
                border-color: #77818f;
            }
        }
    }
}
.action-sheet-header {
    display: flex;
    justify-content: space-between;
    padding: 36rpx;
}
.date-sheet-wrap {
    display: flex;
    padding: 32rpx 0;
}
.btn-wrap {
    position: fixed;
    bottom: 0;
    box-sizing: border-box;
    width: 100%;
    padding: 8px 32px;
    background-color: #ffffff;
    border-top: 1px solid #f3f4f8;
}
</style>
