<template>
    <view class="markdown-body">
        <rich-text class="markdownRich" id="markdown-content" :nodes="renderedHtml" @itemclick="handleItemClick" />
        <!-- <view class="markdown-body" v-html="renderedHtml"></view> -->
    </view>
</template>

<script setup>
import { computed, onMounted, inject } from 'vue';
import { parseMarkdown, codeDataList } from '@/utils/markdownParser';
const { navTo } = inject('globalFunction');
const props = defineProps({
    content: {
        type: String,
        default: '',
    },
    typing: {
        type: Boolean,
        default: false,
    },
});

const renderedHtml = computed(() => parseMarkdown(props.content));

const handleItemClick = (e) => {
    let { attrs } = e.detail.node;
    console.log(attrs);
    let { 'data-copy-index': codeDataIndex, 'data-job-id': jobId, class: className } = attrs;
    switch (className) {
        case 'custom-card':
            return navTo('/packageA/pages/post/post?jobId=' + jobId);
        case 'custom-more':
            return navTo('/packageA/pages/moreJobs/moreJobs?jobId=' + jobId);
        case 'copy-btn':
            uni.setClipboardData({
                data: codeDataList[codeDataIndex],
                showToast: false,
                success() {
                    uni.showToast({
                        title: '复制成功',
                        icon: 'none',
                    });
                },
            });
            break;
    }
};
</script>

<style lang="scss">
.cursor-blink {
    display: inline-block;
    width: 8px;
    height: 1.2em;
    background-color: black;
    animation: blink 1s step-start infinite;
    margin-left: 2px;
    vertical-align: bottom;
}

@keyframes blink {
    50% {
        opacity: 0;
    }
}
.markdown-body {
    h2,
    h3,
    h4,
    h5,
    h6 {
        // line-height: 1;
    }
    ul {
        // display: block;
        padding-inline-start: 40rpx;
        li {
            margin-bottom: -30rpx;
            display: list-item;
            list-style-position: outside; /* 确保数字/点在左侧 */
            word-break: break-word;
            p {
                display: inline;
                margin: 0;
                padding: 0;
            }
        }
        li:nth-child(1) {
            margin-top: -20rpx;
        }
    }
    ol {
        li {
            display: list-item;
            list-style-position: outside; /* 确保数字/点在左侧 */
            word-break: break-word;
            p {
                display: inline;
                margin: 0;
                padding: 0;
            }
        }
        li:nth-child(1) {
            margin-top: -20rpx;
        }
    }
    p {
        font-weight: 500;
        line-height: 1.5;
    }
}
.markdown-body {
    user-select: text;
    -webkit-user-select: text;
    white-space: pre-wrap;
}

/* 表格 */
table {
    // display: block; /* 让表格可滚动 */
    // width: 100%;
    // overflow-x: auto;
    // white-space: nowrap; /* 防止单元格内容换行 */
    border-collapse: collapse;
}

th,
td {
    padding: 16rpx;
    border: 2rpx solid #ddd;
    font-size: 24rpx;
}

th {
    background-color: #007bff;
    color: white;
    font-weight: bold;
}

/* 隔行变色 */
tr:nth-child(even) {
    background-color: #f9f9f9;
}

/* 鼠标悬停效果 */
tr:hover {
    background-color: #f1f1f1;
    transition: 0.3s;
}

/* 代码块 */
pre,
code {
    user-select: text;
    display: flex;
    flex-direction: column;
    margin-top: 0;
    margin-bottom: 0;
}
pre code {
    padding: 0;
    margin: 0;
    min-height: 0;
    line-height: 1;
}
pre code {
    white-space: pre-wrap; /* 保证换行处理 */
}

/* #ifndef MP-WEIXIN */
pre code:empty,
pre code:not(:has(*)):not(:has(text)) {
    display: none;
}
/* #endif */
/* #ifdef MP-WEIXIN */
pre code:empty {
    display: none;
}
/* #endif */

.code-container {
    position: relative;
    border-radius: 10rpx;
    overflow: hidden;
    font-size: 28rpx;
    height: fit-content;
}

.code-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10rpx 20rpx;
    background: #161b22;
    color: #888;
    font-size: 24rpx;
    border-radius: 10rpx 10rpx 0 0;
}

.copy-btn {
    background: rgba(255, 255, 255, 0.1);
    color: #ddd;
    border: none;
    padding: 6rpx 16rpx;
    font-size: 24rpx;
    cursor: pointer;
    border-radius: 6rpx;
}
.copy-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #259939;
    text-decoration: underline;
}

pre.hljs {
    padding: 0 24rpx;
    margin: 0;
    border-radius: 0 0 16rpx 16rpx;
    background-color: #ffffff;
    padding: 20rpx;
    overflow-x: auto;
    font-size: 24rpx;
    margin-top: 10rpx;
    margin-top: -66rpx;
}
pre code {
    margin: 0;
    padding: 0;
    display: block;
    white-space: pre-wrap; /* 允许自动换行 */
    word-break: break-word;
}

ol {
    list-style: decimal-leading-zero;
    padding-left: 60rpx;
}

.line-num {
    display: inline-block;
    text-align: right;
    color: #666;
    margin-right: 20rpx;
    display: inline-block;
    text-align: right;
}

#markdown-content ::v-deep div > pre:first-of-type {
    margin-top: 20rpx;
}

#markdown-content ::v-deep > div {
    display: flex;
    flex-direction: column;
}
.markdownRich > div {
    display: flex;
    flex-direction: column;
}
</style>

<style lang="stylus">
.custom-more{
    display: flex
    justify-content: flex-end
    color: #256BFA
    padding-top: 5rpx
    padding-bottom: 14rpx
    .more-icon{
        width: 60rpx;
        height: 40rpx;
        background: url('@/static/svg/seemore.svg') center center no-repeat;
        background-size: 100% 100%
    }
}
.custom-card
    background: #FFFFFF;
    box-shadow: 0rpx 0rpx 8rpx 0rpx rgba(0,0,0,0.04);
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    padding: 28rpx 24rpx;
    font-weight: 400;
    font-size: 28rpx;
    color: #333333;
    margin-bottom: 20rpx;
    position: relative;
    display: flex;
    flex-direction: column
    .card-title
        font-weight: 600;
        display: flex;
        align-items: center;
        justify-content: space-between
        .title-text
            font-family: 'PingFangSC-Medium', 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
            max-width: calc(100% - 160rpx);
            overflow: hidden
            text-overflow: ellipsis
            font-size: 30rpx
        .card-salary
            font-family: DIN-Medium;
            font-size: 28rpx;
            color: #FF6E1C;

    .card-company
        margin-top: 16rpx;
        max-width: calc(100%);
        overflow: hidden;
        text-overflow: ellipsis
        color: #6C7282;
    .card-info
        margin-top: 22rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-right: 40rpx;
        .info-item
            display: flex;
            position: relative;
            align-items: center;
            color: #256BFA;
            font-size: 28rpx;
            padding-right: 10rpx
    .position-nav
        position: absolute;
        right: -10rpx;
        top: 50%;
    .position-nav::before
        position: absolute;
        left: 0;
        top: -4rpx;
        content: '';
        width: 4rpx;
        height: 16rpx;
        border-radius: 2rpx
        background: #256BFA;
        transform: translate(0, -50%) rotate(-45deg) ;
    .position-nav::after
        position: absolute;
        left: 0;
        top: -4rpx;
        content: '';
        width: 4rpx;
        height: 16rpx;
        border-radius: 2rpx
        background: #256BFA;
        transform: rotate(45deg)
    .card-tag
        font-weight: 500;
        font-size: 24rpx;
        color: #333333;
        width: fit-content;
        background: #F4F4F4;
        border-radius: 4rpx 4rpx 4rpx 4rpx;
        padding: 4rpx 20rpx;
        margin-right: 16rpx;
</style>
