<template>
    <div class="view" v-loading="load">
        <module-view :propConfig="config" ref="view" @pagination="getList" @onRefresh="getList" />
    </div>

</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import { dsCallbackLog, IResponeData, ICallbackLog, orderTimeList } from '@/api/omsOrder'
@Component
export default class BackCallLog extends Vue {
    config: any = {
        attr: {
            calculateH: true
        },
        tool: {
            tools: {
                // search:true,
                refresh: true
            }
        },
        table: {
            attr: {
                size: 'mini',
                seq: true,
                align: 'center',
                triggerRowCheck: 'row'
            },
            columns: [{
                title: '订单编号',
                field: 'waterOrderNo', //未知，问全工。2025-8-12
                ellipsis: true,
            }, {
                title: '订单创建时间',
                field: 'orderTime',
                ellipsis: true,
            }, {
                title: '聚水潭拉取时间',
                field: 'getOrderTime',
                ellipsis: true,
            }, {
                title: '推送仓库时间',
                field: 'pushWarehouseTime',
                ellipsis: true,
                component: 'textChange',
                compConfig: {
                    attr: {
                        data: [{
                            label: '未能获取WMS(极智嘉)数据',
                            value: '未能获取WMS(极智嘉)数据',
                            color: '#E99D42'
                        }]
                    }
                }
            }, {
                title: '仓库响应时间',
                field: 'warehouseResponseTime',
                ellipsis: true,
                component: 'textChange',
                compConfig: {
                    attr: {
                        data: [{
                            label: '未能获取WMS(极智嘉)数据',
                            value: '未能获取WMS(极智嘉)数据',
                            color: '#E99D42'
                        }]
                    }
                }
            }, {
                title: '完成耗时(秒)',
                field: 'finishTime',
                ellipsis: true,
            }]
        }
    }
    time: any;
    timeNum = 0;
    data: Array<any> = []
    load = false;


    getList() {
        if (!this.$refs.view) {
            if (this.timeNum > 5) {
                return
            }
            this.timeNum++;
            setTimeout(() => {
                this.getList()
            }, 500)
            return
        }
        let query: any = (this.$refs.view as any).getQuery();
        query.pageNum = query.pageNo;
        delete query.pageNo;
        this.load = true;
        orderTimeList(query).then((res: any) => {
            this.load = false;
            //this.checkTableData(res.rows as ICallbackLog[]);
            this.checkFinishTime(res.rows as ICallbackLog[])
            res.rows.forEach((item: ICallbackLog) => {
                if (!item.pushWarehouseTime) {
                    item.pushWarehouseTime = '未能获取WMS(极智嘉)数据';
                }
                if (!item.warehouseResponseTime) {
                    item.warehouseResponseTime = '未能获取WMS(极智嘉)数据';
                }
            });
            (this.$refs.view as any).setTableValue(res.rows);
            //(this.$refs.view as any).setTableValue(res.rows);
            let page = {
                pageNum: 1,// res.data.current, //当前页
                pageSize: 20,//res.data.size, //每页条数
                total: res.total //总条数
            };
            (this.$refs.view as any).setPage(page)
        }).catch(() => {
            this.load = false;
        });

    }

    //计算耗时
    checkFinishTime(data: ICallbackLog[]): ICallbackLog[] {
        //pushWarehouseTime-warehouseResponseTime
        //小于1000毫秒显示单位为毫秒，大于1000毫秒小于60秒的显示秒，大于60秒的显示分钟
        data.forEach((item: ICallbackLog) => {
            if (!item.pushWarehouseTime || !item.warehouseResponseTime) {
                item.finishTime = '-'
                return
            };

            const start = new Date(item.pushWarehouseTime).getTime();
            const end = new Date(item.warehouseResponseTime).getTime();
            const diff = end - start;

            if (diff < 1000) {
                item.finishTime = `${diff}毫秒`;
            } else if (diff < 60000) {
                item.finishTime = `${Math.floor(diff / 1000)}秒`;
            } else {
                item.finishTime = `${Math.floor(diff / 60000)}分`;
            }
        })
        return data
    }

    mounted() {
        this.$nextTick(() => {
            this.getList()
        })
    }
}
</script>

<style lang="scss" scoped>
.view {
    width: 100%;
    height: 100%;
    overflow-y: hidden;

    ::v-deep .def-color {
        color: #E99D42;
    }
}
</style>