<script setup>
import { onMounted, ref, reactive } from "vue";
import sHeader from "@/components/SimpleHeader";
import { getBuyConfList } from "@/service/buyOrSell";
import { showFailToast } from "vant";
import { getLoginUser, styleUrl } from "@/common/js/utils";
import dateUtil from "@/utils/dateUtil";
import { useRouter } from 'vue-router'
import { useTradeStore } from '@/stores/trade';

const router = useRouter()

const user = getLoginUser();
const loading = ref(false); // 加载状态
const error = ref(false); // 错误状态
const finished = ref(false); // 结束翻页状态
const buyConfList = ref([]); // 列表集合
const buyConfTotal = ref(0); // 列表总数
let searchParams = reactive({
    current: 1, // 当前页，默认第一页
    size: 10, // 页大小，默认10条
});

// 滚动加载
const onLoad = () => {
    if (!finished.value) {
        getList();
    }
};

// 查询列表
const searchGetList = () => {
    buyConfList.value = [];
    searchParams.current = 1;
    getList();
};

// 获取新人账号注册审批列表数据
const getList = async () => {

    console.log("你好");
    const { data } = await getBuyConfList(
        // Object.assign({}, searchParams)
    );

    // const { data } = await getBuyConfList();
    if (data.code === "00000") {
        console.log("data.data >>>", data.data);
        // 列表值叠加
        buyConfList.value = buyConfList.value.concat(
            data.data.records
        );
        buyConfTotal.value = data.data.total;
        if (buyConfList.value.length === data.data.total) {
            finished.value = true;
        }
        loading.value = false;
        console.log("buyConfList >>>", buyConfList.value);
    } else {
        showFailToast(data.message);
    }
};

const showDateTime = (date) => {
    return date
        ? dateUtil.formateDate(new Date(date), "yyyy-MM-dd hh:mm:ss")
        : "";
};

const showStatusText = (status) => {
    if (status === '0') {
        return "未确认";
    }
    if (status === '1') {
        return "已确认";
    }
    if (status === '2') {
        return "特殊取消";
    }
    return "未知";
};

// const userStore = useUserStore();
const tradeStore = useTradeStore();
// 去审核账号
const toBuy = (item) => {
    // userStore.setUserInfo(item);
    tradeStore.setTradeItem(item);
    router.push({ name: 'taskAccount' });
};

// 是否有操作的权限
const isOper = ref(true);
// 初始化页面获取列表
onMounted(async () => {
    // 加载样式
    styleUrl('taskAccountList');
    if (user) {
        searchGetList();

        // 如果不是admin账号，不能有操作权限
        if (user.userName != 'admin') {
            isOper.value = false;
        }
    }
});
</script>

<template>
    <!-- 买家付款确认列表 -->
    <div class="taskAccountList flex-col">
        <s-header name="买家付款确认" :noback="false"></s-header>

        <div class="taskAccountBox flex-col">
            <van-list v-model:loading="loading" v-model:error="error" :error-text="$t('common.reqFailClkReload')"
                :finished="finished" :finished-text="$t('common.noMoreTxt')" offset="300" :immediate-check="false"
                @load="onLoad">
                <div class="searchRow flex-row justify-between">
                    <div class="flex-col">
                        <div class="flex-row justify-between bd3">
                            <!-- 图标 -->
                            <!-- <div class="flex-col outer4 proportionIcon"></div> -->
                            <span class="flex-col txt2">{{ $t("taskMessage.total")
                                }}<span class="discountNumber">{{ buyConfTotal }}</span>{{
                                    $t("taskMessage.recordsInTotal") }}</span>
                        </div>
                    </div>
                    <!-- <div class="l-flex-RC"> -->
                    <!-- <div @click="reviewedClk" class="label3 o-mr-30">
                        {{ $t("taskMessage.toViewAppro") }}
                    </div> -->
                    <!-- </div> -->
                </div>
                <!-- 买入列表 -->
                <!-- 卖家用户名sellerUserName，单据凭证certificate，创建时间createTime，买入价格price，状态status -->
                <div class="listBox">
                    <div v-for="(item) in buyConfList" :key="item.id" class="listItem">
                        <div class="itemBox">
                            <div class="itemRow">
                                <span class="itemTitle">卖家用户名:&nbsp;</span>{{
                                    item.sellerUserName }}
                            </div>
                            <div class="itemRow">
                                <span class="itemTitle">单据凭证:&nbsp;</span>{{
                                    item.certificate }}
                            </div>
                            <div class="itemRow">
                                <span class="itemTitle">创建时间:&nbsp;</span>{{
                                    item.createTime }}
                            </div>
                            <div class="itemRow">
                                <span class="itemTitle">买入价格:&nbsp;</span>{{
                                    item.price }}
                            </div>

                            <!-- 状态，0未确认，1已确认，2特殊取消 -->
                            <div class="itemRow" v-if="item.status === '0'">
                                <span class="itemTitle">状态:&nbsp;</span><span style="color: red">{{
                                    showItemType(item.status)
                                }}</span>
                            </div>
                            <!-- 卖家确认时间 -->
                            <div class="itemRow" v-if="item.status === '1'">
                                <span class="itemTitle">卖家确认时间:&nbsp;</span>{{
                                    showDateTime(item.sellerConfirmTime)
                                }}
                            </div>
                            <!-- 去付款 -->
                            <div v-if="item.status === '0' && isOper" class="itemRow"
                                style="display: flex; justify-content: flex-end">
                                <van-button span="5" round type="primary" :style="{
                                    height: '2em',
                                    padding: '0 1.2em',
                                    margin: '0 1em',
                                    backgroundColor: 'rgb(200, 55, 95 / 80%)', // 红色背景，不透明度为100%
                                    color: '#fff', // 文字颜色为白色
                                    boxShadow: '0 2px 8px rgba(255, 55, 95, 0.3)', // 添加阴影效果
                                    transition: 'all 0.3s ease' // 添加过渡效果，使变化更平滑
                                }" @click="toBuy(item)">
                                    付款
                                </van-button>
                            </div>
                            <div class="itemRow" style="display: flex; justify-content: flex-end">
                                <!-- 已确认 -->
                                <span v-if="item.status === '1'" style="color: #4fc08d">{{
                                    showStatusText(item.status)
                                    }}</span>
                                <!-- 特殊取消 -->
                                <span v-if="item.status === '2'" style="color: #ff0000">{{
                                    showStatusText(item.status)
                                    }}</span>
                            </div>
                        </div>
                    </div>
                </div>
            </van-list>
        </div>

    </div>
</template>



<style lang="less" scoped>
@import "../../common/style/common.less";
</style>