<script setup>
import {defineEmits, reactive, ref, watch} 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,
        type: data.task_content[current.value].is_image,
    });
    showToast(msg);
    emits('success');
}

watch(
    () => [data, current],
    () => {
        if (data.children.back[current.value]?.content_arr) {
            content.push(...data.children.back[current.value].content_arr);
        }
    },
    {deep: true, immediate: true}
)
</script>

<template>
    <!--我的回填-->
    <view class="!flex gap-[24rpx] !mb-[28rpx]">
        <view
            v-for="(item,index) in data.task_content"
            @click="current=index"
            :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">
            {{ dayjs(data.task_content[current].start_time).format('YYYY-MM-DD HH:mm') }} 至
            {{ dayjs(data.task_content[current].end_time).format('YYYY-MM-DD HH:mm') }}
        </view>
    </view>

    <view class="block">
        <view class="block-title">
            倒计时:
        </view>
        <view class="block-info">
            <view v-if="data.children.back[current]">/</view>
            <x-countdown v-else :time="dayjs(data.task_content[current].end_time)">
            </x-countdown>
        </view>
    </view>

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

    <template v-if="data.children.back[current]">
        <view class="text-[#165DFF] test-24r py-[32rpx]" v-if="data.children.back[current].status === 1">
            {{ dayjs(data.children.createtime * 1000).format('YYYY-MM-DD HH:mm') }}已提交，审核通过
        </view>
        <template v-if="data.children.back[current].status === 2">
            <view class="text-[#165DFF] test-24r py-[32rpx]" v-if="data.children.back[current].operate === 1">
                <view>{{ dayjs(data.children.createtime * 1000).format('YYYY-MM-DD HH:mm') }}已提交，审核拒绝</view>
                <view>请请点击审核沟通，查看修改建议</view>
                <view>
                    并于{{ dayjs(data.children.back[current].end_time * 1000).format('YYYY-MM-DD HH:mm') }}前点击下方重新提交本次回填
                </view>
            </view>
            <view class="text-[#165DFF] test-24r py-[32rpx]" v-if="data.children.back[current].operate === 2">
                <view>{{ dayjs(data.children.createtime * 1000).format('YYYY-MM-DD HH:mm') }}已提交，审核拒绝</view>
                <view>请请点击审核沟通，查看修改建议</view>
                <view>
                    并于{{
                        dayjs(data.children.back[current].end_time * 1000).format('YYYY-MM-DD HH:mm')
                    }}前点击下方回复，按照要求进行回复
                </view>
            </view>
        </template>
        <view class="text-[#165DFF] test-24r py-[32rpx]"
              v-if="data.children.back[current].status === 0">
            {{ dayjs(data.children.createtime * 1000).format('YYYY-MM-DD HH:mm') }}已提交，审核中
        </view>
        <view class="text-[#165DFF] test-24r py-[32rpx]"
              v-if="data.children.back[current].status === -1">
            {{ dayjs(data.children.createtime * 1000).format('YYYY-MM-DD HH:mm') }}已提交，审核失败
        </view>
    </template>

    <template
        v-if="data.children.back[current].status === 0 || data.children.back[current].status === -1">
        <tui-button @click="success" :disabled="data.children.back[current]">
            {{
                data.children.back[current].status === 0 || 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">
                    <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>
