<script setup>
import {defineEmits, reactive, ref, watch, onMounted, nextTick} from "vue";
import XLink from "../../../components/XLink.vue";
import XUpload from "../../../components/XUpload.vue";
import XInput from "../../../components/XInput.vue";
import Api from "../../../api/index.js";
import {showToast} from "../../../utils/uils.js";
import dayjs from "dayjs";
import XCountdown from "../../../components/XCountdown.vue";
import XImage from "../../../components/XImage.vue";
import Resubmit from "./Resubmit.vue";
import ReplyMessageModal from "./replyMessageModal.vue";

const emits = defineEmits(['success']);
const {data} = defineProps({
    data: {
        type: Object,
        default: null,
    }
});

const content = reactive([]);

const current = ref(0);

const success = async () => {
    const {msg} = await Api.system.addTaskBackfill({
        id: data.children.id,
        cid: current.value + 1,
        content: content.flat(),
        type: data.task_content[current.value].is_image,
    });
    showToast(msg);
    emits('success');
}

watch(
    () => [data, current],
    () => {
        if (data.children.back[current.value]?.content_arr) {
            setTimeout(() => {
                content.length = 0;
                content.push(...data.children.back[current.value].content_arr.map(v => [v]));
            }, 100);
        }
    },
    {deep: true, immediate: true}
)

const initContent = () => {
    content.length = 0;
    data.task_content[current.value].data.forEach(v => {
        content.push([]);
    })
}

onMounted(() => {
    initContent();
})
</script>

<template>
    <!--我的回填-->
    <view class="!flex gap-[24rpx] !mb-[28rpx]">
        <view
            v-for="(item,index) in data.task_content"
            @click="() => {current=index;initContent()}"
            :class="['tab-item', index===current?'cur':'']">
            回填{{ index + 1 }}
        </view>
    </view>

    <view class="block">
        <view class="block-title">
            回填指引:
        </view>
        <view class="block-info">
            <x-image :src="data.task_content[current].ts_images_arr[0]"
                     :list="data.task_content[current].ts_images_arr">
                <x-link>查看指引</x-link>
            </x-image>
        </view>
    </view>

    <view class="block">
        <view class="block-title">
            回填时间:
        </view>
        <view class="block-info">
            {{ data.children.back[current].time }}
        </view>
    </view>

    <view class="block">
        <view class="block-title">
            倒计时:
        </view>
        <view class="block-info">
            <template v-if="data.children?.back[current].status === -3">
                {{ data.children?.back[current].back_time }}后开始回填
            </template>
            <template v-else-if="data.children?.back[current].status === -2">
                <x-countdown :time="data.children?.back[current].back_time">
                </x-countdown>
            </template>
            <template v-else>
                /
            </template>
        </view>
    </view>

    <view class="block" v-for="(v, index) in data.task_content[current].data">
        <view class="block-title">
            {{ v.name }}:
        </view>
        <view class="block-info">
            <x-upload
                :del="data.children.back[current]?.status === -2"
                :single="true"
                v-model:files="content[index]"
                v-if="v.is_image === 1">
            </x-upload>
            <x-input v-else v-model:model-value="content[index]" height="64rpx" placeholder="请输入内容"
                     :disabled="data.children.back[current]?.status !== -2"></x-input>
        </view>
    </view>

    <view class="py-[32rpx]" v-html="data.children.back[current]?.text">
    </view>

    <template
        v-if="!data.children.back[current] || data.children.back[current]?.operate === 0 || data.children.back[current]?.operate === 3">
        <tui-button @click="success"
                    :disabled="data.children.back[current]?.status === 0 || data.children.back[current]?.status === -1 || data.children.back[current]?.status === 1">
            {{
                data.children.back[current]?.status === 0 || data.children.back[current]?.status === -1 || data.children.back[current]?.status === 1 ? '已提交' : '提交'
            }}
        </tui-button>
    </template>
    <template v-else>
        <view class="!flex !w-full gap-[30rpx]">
            <view class="!w-[50%]">
                <tui-button disabled>已提交</tui-button>
            </view>
            <view class="!w-[50%]" v-if="data.children.back[current]?.operate === 1">
                <resubmit :data="data" :current="current" @success="emits('success')">
                    <tui-button>重新提交</tui-button>
                </resubmit>
            </view>
            <view class="!w-[50%]" v-if="data.children.back[current]?.operate === 2">
                <reply-message-modal :data="data" @success="emits('success')" :backId="data.children.back[current].id"
                                     :pattern="1">
                    <tui-button>回复</tui-button>
                </reply-message-modal>
            </view>
        </view>
    </template>
</template>

<style scoped lang="scss">
.tab-item {
    padding: 12rpx 26rpx;
    border-radius: 9999rpx;
    background-color: #F7F8FA;
    font-size: 24rpx;
    color: #4E5969;
    transition: 500ms;
}

.cur {
    background-color: #E8F3FF;
    color: #165DFF;
}

.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;
    }
}
</style>
