<template>
    <div class="system-user-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <div class="system-user-search mb15">
                <el-select v-model="state.type" size="default" class="m-2" placeholder="请选择类型">
                    <el-option label="全部" value="" />

                    <el-option label="市场运行报告" value="1" />
                    <el-option label="数据日报" value="2" />
                    <el-option label="数据月报" value="3" />
                    <el-option label="建设部报表" value="4" />
                    <el-option label="省厅报表" value="5" />
                </el-select>
                <el-select size="default" style="margin: 0 10px;" v-model="state.businessState" class="m-2"
                    placeholder="请选择状态">
                    <el-option label="全部" value="" />

                    <el-option label="启用" value="1" />
                    <el-option label="停用" value="2" />
                </el-select>
                <el-button @click="getTableData" size="default" type="primary" class="ml10">
                    <el-icon>
                        <ele-Search />
                    </el-icon>
                    查询
                </el-button>
                <el-button @click="upshow" size="default" type="primary" class="ml10">添加</el-button>
            </div>
            <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" border stripe
                :cell-style="{ textAlign: 'center' }" :header-cell-style="{ 'text-align': 'center' }">
                <el-table-column prop="version" label="版本号"></el-table-column>
                <el-table-column prop="type" label="报表类型">
                    <template #default="scope">
                        <span v-if="scope.row.type == 1">市场运行报告</span>
                        <span v-if="scope.row.type == 2">数据日报</span>
                        <span v-if="scope.row.type == 3">数据月报</span>
                        <span v-if="scope.row.type == 4">建设部报表</span>
                        <span v-if="scope.row.type == 5">省厅报表</span>
                    </template>
                </el-table-column>
                <el-table-column prop="excelUrl" label="文件">
                    <template #default="scope">
                        <el-link type="primary" @click="download(scope.row)">点击下载/查看</el-link>
                    </template>
                </el-table-column>
                <el-table-column prop="state" label="状态">
                    <template #default="scope">
                        <el-switch @change="putswitch($event, scope.row)" v-model="scope.row.states" width="60"
                            inline-prompt active-text="启用" inactive-text="禁用" />
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" show-overflow-tooltip> </el-table-column>
            </el-table>
            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum" background
                v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="state.tableData.total">
            </el-pagination>
        </el-card>
        <disDialog ref="disDialogRef" @refresh="getTableData()" />

    </div>
</template>
  
<script setup lang="ts" name="reportrundata">
import { defineAsyncComponent, reactive, onMounted, ref, computed } from "vue";
import { ElMessageBox, ElMessage, ElNotification } from "element-plus";
import { getApi } from "/@/api/district/setting";
const disDialog = defineAsyncComponent(() => import("/@/views/report/setting/dialog.vue"));
import { useCurrentInstance } from "/@/method/get";
const { proxy } = useCurrentInstance();
let num = 0;

// 定义变量内容
const disDialogRef = ref();
// 定义变量内容
const state = reactive<any>({
    date: undefined,
    businessState: '',
    type: '',
    tableData: {
        data: [],
        total: 0,
        loading: false,
        param: {
            pageNum: 1,
            pageSize: 10,
        },
    },
});

const download = async (row: any) => {
    let name = proxy?.$method.atchFileSuffixType(row.excelUrl)
    if (name == 'excel') {
        let urls = row.excelUrl.slice(1)
        let item = await getApi().downloadUrl(urls);
        let download: any = await getApi().download(item);
        let blob = new Blob([download], { type: "application/octet-stream;" });
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.href = url;
        let reg1 = new RegExp("_upload_")
        link.download = `${row.excelUrl.replace(reg1, '')}`;
        document.body.appendChild(link);
        link.click();

    } else if (name == 'pdf') {
        let pdf = await getApi().pdflook(row.excelUrl);
        window.open(pdf)
    } else {
        ElMessage.error('该类型无法打开')
    }
};


async function putswitch(e: any, row: any) {
    if (num >= 1) {
        let item = await getApi().stateApi({ id: row.id, state: e == true ? 1 : 2, type: row.type });
        num++
        getTableData()
    }
}


function upshow() {
    disDialogRef.value.openDialog();
}

// 初始化表格数据
const getTableData = async () => {
    state.tableData.loading = true;
    // 项目
    const data = await getApi().listApi({
        page: state.tableData.param.pageNum,
        limit: state.tableData.param.pageSize,
        businessState: state.businessState,
        type: state.type
    });
    let _data: any = []

    data.data.forEach((res: any) => {
        res.states = res.state == 1 ? true : false
        _data.push(res)
    });

    state.tableData.data = _data;
    state.tableData.total = data.count;
    state.tableData.loading = false;
    num++
};
// 分页改变
const onHandleSizeChange = (val: number) => {
    state.tableData.param.pageSize = val;
    getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.tableData.param.pageNum = val;
    getTableData();
};
// 页面加载时
onMounted(() => {
    getTableData();
});
</script>
  
<style scoped lang="scss">
.wgt {
    font-weight: bold;
}

.system-user-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}
</style>
  