<template>
    <div class="system-user-container layout-padding">
        <el-row :gutter="15" class="home-card-one mb10">
            <el-col :span="24">
                <el-card shadow="hover" class="mb10">
                    <el-radio-group v-model="state.areaCode" @change="getTableData">
                        <el-radio label="">潍坊市</el-radio>
                        <el-radio v-for="(item, index) in state.county" :key="index" :label="'' + item.value + ''">{{
                            item.label
                        }}</el-radio>
                    </el-radio-group>
                </el-card>
            </el-col>
        </el-row>
        <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-option label="申请退回" value="3" />
                </el-select>
                <el-button @click="getTableData" size="default" type="primary" class="ml10">
                    <el-icon>
                        <ele-Search />
                    </el-icon>
                    查询
                </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="areaName" 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="businessState" label="业务状态">
                    <template #default="scope">
                        <el-tag v-if="scope.row.businessState == 1" class="ml-2">申请中</el-tag>
                        <el-tag v-if="scope.row.businessState == 2" class="ml-2" type="success">申请成功</el-tag>
                        <el-tag v-if="scope.row.businessState == 3" class="ml-2" type="danger">申请退回</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" label="添加时间">
                    <template #default="scope">
                        {{ proxy?.$method.formatDate(scope.row.createTime) }}
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="上报备注" show-overflow-tooltip> </el-table-column>
                <el-table-column prop="approvalRemark" label="审批备注" show-overflow-tooltip> </el-table-column>
                <!-- <el-table-column prop="approvalRemark" label="操" show-overflow-tooltip> </el-table-column> -->
                <el-table-column label="操作" show-overflow-tooltip width="100" fixed="right">
                    <template #default="scope">
                        <el-button size="small" @click="editshow(scope.row)" text type="primary">审核</el-button>
                    </template>
                </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 } from "vue";
import { ElMessageBox, ElMessage, ElNotification } from "element-plus";
import { getApi } from "/@/api/district/examine";
const disDialog = defineAsyncComponent(() => import("/@/views/examine/dialog.vue"));
import { useCurrentInstance } from "/@/method/get";
const { proxy } = useCurrentInstance();
import { getAll } from "/@/api/sys/index";


// 定义变量内容
const disDialogRef = ref();
// 定义变量内容
const state = reactive<any>({
    date: undefined,
    businessState: '',
    areaCode: '',
    county: [],
    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('该类型无法打开')
    }
};

function editshow(e: any) {
    if (e.businessState != 1) return ElMessage.error('本数据已审核完成！')
    disDialogRef.value.openDialog(e);
}

// 初始化表格数据
const getTableData = async () => {
    state.tableData.loading = true;
    // 项目
    const data = await getApi().listApi({
        page: state.tableData.param.pageNum,
        limit: state.tableData.param.pageSize,
        areaCode: state.areaCode,
        businessState: state.businessState,
        type: state.type
    });
    state.tableData.data = data.data;
    state.tableData.total = data.count;
    state.tableData.loading = false;
    let items = await getAll().codeApi();
    state.county = items.data;
};
// 分页改变
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>
  