<template>
    <a-modal :width="800" title="设备异常反馈" :maskClosable="false" :closable="false" :okButtonProps="{ hidden: true }"
        cancelText="关闭" v-model:visible="visible" @cancel="onClickClose">
        <a-spin :spinning="state.loading">
            <a-form :colon="false" :label-col="labelCol" :wrapper-col="wrapperCol">
                <a-form-item label='异常描述'>
                    {{ state.info.logs_info || '--' }}
                </a-form-item>
                <a-form-item label='发生时间'>
                    {{ state.info.happen_time || '--' }}
                </a-form-item>
                <a-form-item label='联系电话'>
                    {{ state.info.contact_number || '--' }}
                </a-form-item>
                <a-form-item label='联系邮箱'>
                    {{ state.info.contact_email || '--' }}
                </a-form-item>
            </a-form>
            <div class="log-list">
                <div class="log-list-item borderRight">
                    <div class="log-list-item-title">
                        机场日志
                    </div>
                    <div class="log-list-item-content">
                        <div class="log-list-item-content-cell" v-for="item in state.info.airportLogList"
                            :key="item.boot_index">
                            <div>
                                <div>
                                    {{ getDateTime(item.start_time) }}
                                    <span style="margin: 0 5px;">
                                        -
                                    </span>
                                    {{ getDateTime(item.end_time) }}
                                </div>
                            </div>
                            <div>
                                {{ getLogSize(item.size) }} G
                            </div>
                        </div>
                    </div>
                </div>
                <div class="log-list-item borderLeft">
                    <div class="log-list-item-title">
                        飞行器日志
                    </div>
                    <div class="log-list-item-content">
                        <div class="log-list-item-content-cell" v-for="item in state.info.droneLogList"
                            :key="item.boot_index">
                            <div>
                                <div>
                                    {{ getDateTime(item.start_time) }}
                                    <span style="margin: 0 5px;">
                                        -
                                    </span>
                                    {{ getDateTime(item.end_time) }}
                                </div>
                            </div>
                            <div>
                                {{ getLogSize(item.size) }} G
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </a-spin>
    </a-modal>
</template>

<script lang="ts" setup>
import { reactive, onMounted } from 'vue';
import { apis } from '/@/api/custom';
import moment from 'moment';

interface Props {
    currentId: string,
    visible: boolean,
    onClickClose: () => void,
};

const props = withDefaults(defineProps<Props>(), {

});

interface State {
    loading: boolean,
    info: {
        logs_info: string,
        happen_time: string,
        contact_number: string,
        contact_email: string,
        airportLogList: {
            boot_index: number,
            end_time: number,
            size: number,
            start_time: number,
        }[],
        droneLogList: {
            boot_index: number,
            end_time: number,
            size: number,
            start_time: number,
        }[],
    }
}

const state: State = reactive({
    loading: false,
    info: {
        logs_info: '',
        happen_time: '',
        contact_number: '',
        contact_email: '',
        airportLogList: [],
        droneLogList: [],
    }
})

const labelCol = { style: { width: '100px', marginRight: '30px' } };

const wrapperCol = { span: 18 };

const fetchDetail = async () => {
    state.loading = true;
    try {
        const res = await apis.fetchDeviceLogDetail({ logsId: props.currentId });
        if (res.code !== 0) {
            return;
        }
        const info = res.data;
        const list = info.list || [];
        const data = {
            logs_info: info.logs_info,
            happen_time: moment(info.happen_time).format('YYYY-MM-DD HH:mm:ss'),
            contact_number: info.contact_number,
            contact_email: info.contact_email,
            airportLogList: [],
            droneLogList: [],
        }
        const airportLog = list.find((item: any) => item.module === '3');
        if (airportLog) {
            data.airportLogList = airportLog.list;
        }
        const droneLog = list.find((item: any) => item.module === '0');
        if (droneLog) {
            data.droneLogList = droneLog.list;
        }
        state.info = data;
    } catch (error) {
        console.error(error);
    } finally {
        state.loading = false;
    }
}

onMounted(async () => {
    await fetchDetail();
})

const getDateTime = (timestamp: number) => {
    const date = moment.unix(timestamp).format('YYYY-MM-DD HH:mm');
    return date;
}

const getLogSize = (size: number) => {
    // 将字节转换为GB
    const gbSize = size / (1024 * 1024 * 1024);
    // 保留一位小数
    return parseFloat(gbSize.toFixed(1));
};
</script>

<style lang="scss" scoped>
.log-list {
    border-top: 1px solid #f0f0f0;
    display: flex;
    justify-content: space-between;

    .borderRight {
        border-right: 1px solid #f0f0f0;
    }

    .borderLeft {
        border-left: 1px solid #f0f0f0;
    }

    &-item {
        width: 48%;
        padding: 10px;
        margin-top: 20px;

        &-title {
            font-weight: bold;
        }

        &-content {
            width: 100%;
            max-height: 300px;
            padding: 0 10px;
            overflow-y: auto;

            &-cell {
                display: flex;
                justify-content: space-between;
                align-items: center;
                margin-top: 10px;
            }
        }
    }
}
</style>