<template>
    <div class="app-container">
        <el-table
            ref="dataTableRef"
            v-loading="loading"
            :data="state.tableData"
            highlight-current-row
            border
            ><el-table-column type="index" width="50" align="center" />

            <el-table-column
                align="center"
                prop="os"
                label="手机型号"
                width="250"
            />
            <el-table-column
                align="center"
                prop="createTime"
                label="创建时间"
                width="150"
                show-overflow-tooltip
            />
            <el-table-column
                align="center"
                prop="error"
                label="错误描述"
                show-overflow-tooltip
            />
            <el-table-column
                align="center"
                prop="error"
                label="操作"
                width="100"
            >
                <template #default="scope">
                    <el-button
                        size="small"
                        type="primary"
                        @click="download(scope.row)"
                        >导出</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <pagination
            v-if="state.total > 0"
            :total="state.total"
            v-model:page="state.queryParams.pageNo"
            v-model:limit="state.queryParams.pageSize"
            @pagination="handleQuery"
        />
    </div>
</template>
<script lang="ts">
export default {
    name: "applog",
};
</script>
<script lang="ts" setup>
import { onMounted, reactive, ref, toRefs } from "vue";
import { memberApplog } from "@/api/member/applog";
import { ApplogItem, ApplogQueryParam } from "@/types/api/member/applog";

const state = reactive({
    loading: true,
    tableData: [] as ApplogItem[],
    queryParams: {
        pageNo: 1,
        pageSize: 15,
        account: "",
    } as ApplogQueryParam,
    total: 0,
});

const { loading, tableData, queryParams } = toRefs(state);
function handleQuery() {
    state.loading = true;
    memberApplog(state.queryParams).then(({ data }) => {
        state.tableData = data.content;
        state.total = data.totalElements;
        state.loading = false;
    });
}

function download(row: any) {
    console.log(row);
    var blob = new Blob([row.createTime + "\r\n\r\n", row.error], {
        type: "text/plain",
    });
    var anchor = document.createElement("a");
    anchor.download = row.os + ".txt";
    anchor.href = window.URL.createObjectURL(blob);
    anchor.target = "_blank";
    anchor.style.display = "none"; // just to be safe!
    document.body.appendChild(anchor);
    anchor.click();
    document.body.removeChild(anchor);
}

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

<style lang="less" scoped></style>
