<script setup>
import goodsIcon from '../../static/icons/goodsIcon.png';
import XNav from "../../components/XNav.vue";
import {defineAsyncComponent, getCurrentInstance, nextTick, onMounted, reactive, ref} from 'vue';
import {onLoad} from "@dcloudio/uni-app";
import XLink from "../../components/XLink.vue";
import XNoticeBar from "../../components/XNoticeBar.vue";
import XImage from "../../components/XImage.vue";
import Api from "../../api/index.js";
import dayjs from "dayjs";
import XPrompt from "../../components/XPrompt.vue";
import {numberToCharacter} from "../../utils/uils.js";
import AcceptAssignmentModal from "../../components/AcceptAssignmentModal.vue";

// #ifdef APP-PLUS
// #endif

// #ifndef APP-PLUS
const TaskDetails = defineAsyncComponent(() => import('./components/TaskDetails.vue'));
const CollectMaterials = defineAsyncComponent(() => import('./components/CollectMaterials.vue'));
const MyBackfill = defineAsyncComponent(() => import('./components/MyBackfill.vue'));
const ReviewCommunication = defineAsyncComponent(() => import('./components/ReviewCommunication.vue'));
// #endif

const details = ref(null);
const mainDetailWidth = ref(0);
const currentTabs = ref(0);
const tabs = reactive([
    {
        name: "任务详情",
        component: TaskDetails,
    },
    {
        name: "领取素材",
        component: CollectMaterials,
    },
    {
        name: "我的回填",
        component: MyBackfill,
    },
    {
        name: "审核沟通",
        component: ReviewCommunication,
    },
]);

const getData = async (id) => {
    const {data} = await Api.system.getTaskinfo(id || details.value.id);
    details.value = data;
}

onLoad((options) => {
    const {id} = options;
    getData(id);
});

onMounted(() => {
    nextTick(() => {
        const instancs = getCurrentInstance();
        uni.createSelectorQuery()
            .in(instancs)
            .select("#mainDetailBox")
            .boundingClientRect()
            .exec((result) => {
                mainDetailWidth.value = result[0].width;
            })
    });
})
</script>

<template>
    <!--任务详情-->
    <XNav></XNav>
    <x-notice-bar></x-notice-bar>

    <view class="!p-[20rpx] !flex flex-col gap-[20rpx] box-border" v-if="details">
        <view class="rounded-[16rpx] !p-[24rpx] bg-[#fff] !flex flex-col gap-[20rpx]">
            <view class="!flex gap-[30rpx]">
                <image class="!size-[84rpx]" :src="goodsIcon"></image>
                <view class="flex-grow">
                    <view class="goods-title !w-full !flex items-center justify-between">
                        {{ details.goods_name }}
                        <view :class="['goods-state']" v-if="details.is_use !== 0">{{ details.status_text }}</view>
                    </view>
                    <view v-if="details.is_use === 0" class="goods-bh">
                        {{ dayjs(details.end_time).format('YYYY-MM-DD HH:mm') }} 前可领取
                    </view>
                    <view v-else class="goods-bh">
                        任务编号：{{ details.is_use === 0 ? details.code : details.children.uid }}
                        <x-link>复制</x-link>
                    </view>
                </view>
            </view>

            <view
                v-if="details.is_use !== 0"
                class="bg-[#F7F8FA] px-[28rpx] py-[16rpx] rounded-[8rpx] !flex justify-around"
                style="font-size: 24rpx">
                <view>发布账号</view>
                <view class="bg-[#E5E6EB] w-[5rpx] h-auto"></view>
                <view>昵称: {{ details.children.account.nickname }}</view>
                <view class="bg-[#E5E6EB] w-[5rpx] h-auto"></view>
                <view>账号: {{ details.children.account.account }}</view>
            </view>

            <view class="!grid grid-cols-4 gap-[20rpx]">
                <view class="bg-[#E8F3FF] px-[26rpx] py-[20rpx] rounded-[8rpx]">
                    <view class="goods-st">{{ details.platform }}</view>
                    <view class="goods-st-info">发布平台</view>
                </view>
                <view class="bg-[#E8F3FF] px-[26rpx] py-[20rpx] rounded-[8rpx]">
                    <view class="goods-st">¥ {{ details.real_price.toFixed(2) }}</view>
                    <view class="goods-st-info">任务报酬</view>
                </view>
                <view class="bg-[#E8F3FF] px-[26rpx] py-[20rpx] rounded-[8rpx]">
                    <view class="goods-st">{{ details.material_name }}</view>
                    <view class="goods-st-info">素材类型</view>
                </view>
                <view class="bg-[#E8F3FF] px-[26rpx] py-[20rpx] rounded-[8rpx]">
                    <view class="goods-st">{{ details.children_num }}</view>
                    <view class="goods-st-info">剩余名额</view>
                </view>
            </view>

            <view class="!flex flex-col gap-[10px]">
                <view class="!flex px-[10px] py-[12px] bg-[#F2F3F5] gap-[8px] items-center">
                    <view class="text-[#4E5969] test-24r !flex items-center gap-[5px]">
                        安全评分:
                        <x-prompt
                            info="由该任务或类似任务的历史禁言率、素材是否绿色决定。分值越高，说明完成任务对账号影响最小"
                            title="分值说明">
                        </x-prompt>
                    </view>
                    <view class="!flex items-center gap-[5px]">
                        <view class="text-[#165DFF]">{{ details.security_level }}分</view>
                        <view class="test-24r">{{ details.security_text }}</view>
                    </view>
                </view>
                <view class="!flex px-[10px] py-[12px] bg-[#F2F3F5] gap-[8px] items-center">
                    <view class="text-[#4E5969] test-24r !flex items-center gap-[5px]">
                        耗时评分:
                        <x-prompt
                            info="由该任务完整完成所需要的时间和操作步骤决定。分值越高，说明完成任务越简单"
                            title="分值说明">
                        </x-prompt>
                    </view>
                    <view class="!flex items-center gap-[5px]">
                        <view class="text-[#165DFF]">{{ details.time_level }}分</view>
                        <view class="test-24r">{{ details.time_text }}</view>
                    </view>
                </view>
            </view>
        </view>

        <view id="mainDetailBox" class="rounded-[16rpx] bg-[#fff] main-details overflow-hidden">
            <tui-tabs
                v-if="mainDetailWidth && details.is_use !== 0"
                :tabs="tabs"
                :currentTab="currentTabs"
                :width="mainDetailWidth"
                @change="({index})=>currentTabs=index">
            </tui-tabs>

            <view :style="{padding: currentTabs===3?'0':'24rpx'}">
                <Suspense>
                    <template #default>
                        <div class="h-full flex flex-col items-start">
                            <component :is="tabs[currentTabs].component" :data="details"></component>
                        </div>
                    </template>

                    <template #fallback>
                        加载中...
                    </template>
                </Suspense>
            </view>
        </view>

        <template v-if="currentTabs === 0">
            <view class="rounded-[16rpx] bg-[#fff] main-details overflow-hidden !p-[24rpx]">
                <view class="w-full">
                    <view class="title">
                        发布要求
                    </view>
                    <view class="block items-center">
                        <view class="block-title">发布平台:</view>
                        <view class="block-info">
                            {{ details.platform }}
                        </view>
                    </view>
                    <view class="block items-center">
                        <view class="block-title">发布方式:</view>
                        <view class="block-info">
                            {{
                                numberToCharacter(details.fb_num - 1)
                            }}连发/{{ details.is_identical === 0 ? '每次不同' : '每次相同' }}
                            <x-prompt title="发布方式说明">
                                <template #info>
                                    <view>
                                        领取素材后，按照要求，连续发布
                                        <text class="text-[#FF7D00]">{{ details.fb_num }}遍</text>
                                        ，每遍素材
                                        <text class="text-[#FF7D00]">
                                            {{ details.is_identical === 0 ? '不同' : '相同' }}
                                        </text>
                                    </view>
                                </template>
                            </x-prompt>
                        </view>
                    </view>
                    <view class="block items-center">
                        <view class="block-title">每次间隔时长:</view>
                        <view class="block-info">
                            {{ details.duration }}分钟
                        </view>
                    </view>
                    <view class="block">
                        <view class="block-title">特殊要求:</view>
                        <view class="block-info gap-[20rpx]">
                            <view>{{ details.special_text }}</view>
                            <view class="!grid grid-cols-3 !mt-[12rpx]">
                                <x-image
                                    v-for="(v, index) in details.special_images_arr"
                                    :key="index"
                                    class="!size-[96rpx]"
                                    :src="v"
                                    mode="aspectFill"
                                    :draggable="false">
                                </x-image>
                            </view>
                        </view>
                    </view>
                </view>
            </view>

            <view class="rounded-[16rpx] bg-[#fff] main-details overflow-hidden !p-[24rpx]">
                <view class="w-full">
                    <view class="title">
                        回填要求
                    </view>
                    <view
                        v-for="(v,index) in details.task_content"
                        :key="v.id"
                        class="block flex-col">
                        <view class="block-title">第{{ index + 1 }}次回填:</view>
                        <view class="block-info !flex w-full">
                            <view
                                class="bg-[#F7F8FA] py-[12rpx] px-[20rpx] border-1 border-[rgb(229,230,235)] w-[60%] whitespace-nowrap">
                                {{ dayjs(v.start_time).format('MMDD-HH:mm') }} 至
                                {{ dayjs(v.end_time).format('MMDD-HH:mm') }}之间
                            </view>
                            <view
                                class="bg-[#F7F8FA] py-[12rpx] px-[20rpx] border-1 border-[rgb(229,230,235)] flex-grow">
                                {{ v.content }}
                            </view>
                        </view>
                    </view>
                </view>
            </view>

            <view class="rounded-[16rpx] bg-[#fff] main-details overflow-hidden !p-[24rpx]">
                <view class="w-full">
                    <view class="title">
                        时间要求
                    </view>
                    <view class="block items-center">
                        <view class="block-title">作品保留时间:</view>
                        <view class="block-info">
                            {{ details.retention_time }} {{ details.retention_type === 1 ? '分钟' : '小时' }}
                            <x-prompt title="保留时间说明">
                                <template #info>
                                    <view>
                                        该任务结算后，要保留
                                        <text class="text-[#FF7D00]">{{ details.retention_time }}
                                            {{ details.retention_type === 1 ? '分钟' : '小时' }}
                                        </text>
                                        。
                                        <view>到期请自行删除，避免违规。若提前删除，将影响后续收益</view>
                                    </view>
                                </template>
                            </x-prompt>
                        </view>
                    </view>
                    <view class="block-extend">
                        *若未到保留时间，提前删除。有对应惩罚
                    </view>
                    <view class="block items-center">
                        <view class="block-title !w-[300rpx]">每次回填将在该时间内审核:</view>
                        <view class="block-info">
                            {{ details.check_time }} {{ details.check_type === 1 ? '分钟' : '小时' }}内
                            <x-prompt
                                title="审核时间说明"
                                :info="`回填后，我们将于${details.check_time}${details.check_type === 1 ? '分钟' : '小时'}内完成审核，超时自动通过`">
                            </x-prompt>
                        </view>
                    </view>
                    <view class="block-extend">
                        *若您的回填，未在上述时间内审核，可在沟通中，发起申诉
                    </view>
                </view>
            </view>

            <view class="rounded-[16rpx] bg-[#fff] main-details overflow-hidden !p-[24rpx]">
                <view class="w-full">
                    <view class="title">
                        扣款说明
                    </view>
                    <view
                        v-for="(v, index) in details.settlement"
                        :key="v.id"
                        class="block !flex !gap-0">
                        <view class="test-24r px-[20rpx] py-[12rpx] bg-[#F7F8FA] border-1 border-[rgb(229,230,235)]">
                            {{ index + 1 }}.
                        </view>
                        <view
                            class="test-24r px-[20rpx] py-[12rpx] bg-[#F7F8FA] border-1 border-[rgb(229,230,235)] flex-grow">
                            {{ v.intro }}
                        </view>
                        <view
                            class="test-24r px-[20rpx] py-[12rpx] bg-[#F7F8FA] border-1 border-[rgb(229,230,235)] whitespace-nowrap">
                            扣除{{ v.ratio }}%收益
                        </view>
                    </view>
                    <view class="block-extend">
                        *请认真阅读该说明，商家将按照提前约定的事由扣款
                    </view>
                </view>
            </view>

            <view class="rounded-[16rpx] bg-[#fff] main-details overflow-hidden !p-[24rpx]">
                <view class="w-full">
                    <view class="title">
                        其他说明
                    </view>
                    <view class="block !flex-col">
                        <view class="test-24r" v-for="(v, index) in details.settlement_arr" :key="index">
                            {{ v.intro }}
                        </view>
                    </view>
                    <x-prompt title="信用分说明"
                              info="扣除信用分，将影响您后续接任务以及提现。请按约定完成任务。请在接受任务前认真阅读各要求，有能力完成再接受。">
                        <template #button>
                            <view
                                class="py-[7rpx] px-[32rpx] rounded-full text-[var(--primary-color)] bg-[#E8F3FF] w-fit">
                                查看信用分管理方法
                            </view>
                        </template>
                    </x-prompt>
                </view>
            </view>

            <accept-assignment-modal v-if="details.is_use === 0" :details="details" @success="getData">
                <view
                    class="bg-[var(--primary-color)] rounded-[8rpx] py-[26rpx] text-[#fff] !flex justify-center items-center">
                    接受任务
                </view>
            </accept-assignment-modal>

            <view
                v-else
                class="bg-[#94BFFF] rounded-[8rpx] py-[26rpx] text-[#fff] !flex justify-center items-center">
                已接受
            </view>
        </template>
    </view>
</template>

<style lang="scss" scoped>
page {
    background-color: #6AA1FF;
}

.block-extend {
    color: rgb(255, 125, 0);
    font-size: 20rpx;
    font-weight: 400;
    letter-spacing: 0;
    text-align: left;
    margin-top: -8rpx;
    margin-bottom: 24rpx;
}

.block {
    display: flex;
    gap: 20rpx;
    margin-bottom: 20rpx;

    .block-title {
        flex-shrink: 0;
        color: rgb(134, 144, 156);
        font-size: 24rpx;
        font-weight: 500;
        line-height: 140%;
        letter-spacing: 0;
        text-align: left;
        width: 160rpx;
    }

    .block-info {
        color: rgb(78, 89, 105);
        font-size: 24rpx;
        font-weight: 500;
        line-height: 140%;
        letter-spacing: 0;
    }
}

.title {
    color: rgb(29, 33, 41);
    font-size: 28rpx;
    font-weight: 500;
    line-height: 140%;
    letter-spacing: 0;
    text-align: left;
    margin-bottom: 32rpx;
}

.main-details {
    :deep(.tui-tabs-view) {
        width: 100% !important;
    }
}

.goods-title {
    color: rgb(29, 33, 41);
    font-size: 32rpx;
    font-weight: 500;
    line-height: 140%;
    letter-spacing: 0;
    text-align: left;
    margin-bottom: 8rpx;
}

.goods-bh {
    color: rgb(134, 144, 156);
    font-size: 12px;
    font-weight: 400;
    line-height: 140%;
    letter-spacing: 0;
    text-align: left;
    display: flex;
    gap: 24rpx;
}

.goods-state {
    background-color: #E8FFEA;
    color: rgb(0, 180, 42);
    font-size: 12px;
    font-weight: 400;
    line-height: 140%;
    letter-spacing: 0;
    text-align: center;
    padding: 4rpx 8rpx;
}

.goods-st {
    color: rgb(29, 33, 41);
    font-size: 26rpx;
    font-weight: 700;
    line-height: 140%;
    letter-spacing: 0;
    text-align: left;
}

.goods-st-info {
    color: rgb(78, 89, 105);
    font-size: 22rpx;
    font-weight: 400;
    line-height: 140%;
    letter-spacing: 0;
    text-align: left;
    margin-top: 12rpx;
}
</style>
