<!-- 用采数据列表 -->
<template>
    <div class="default-main" :style="height">
        <div class="title">
            用采数据列表
            <back-component />
        </div>
        <TableHeader :showReset="false" ref="TableHeaderRef" showCustomColumn>
            <template #select>
                <el-form-item label="筛选数据">
                    <el-input v-model="tableStore.table.params.searchValue" clearable placeholder="请输入关键字" maxlength="32" show-word-limit/>
                </el-form-item>
            </template>
            <template #operation>
                <el-button type="primary" icon="el-icon-Plus" @click="dialogVisible = true">新增</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef"></Table>
        <!-- 详情 -->
        <completenessDetails ref="completenessDetailsRef" />

        <el-dialog v-model="dialogVisible" draggable title="上传数据" width="500" :before-close="handleClose">
            <el-upload
                ref="upload"
                action=""
                v-model:file-list="fileList"
                accept=".xlsx,.xls"
                :auto-upload="false"
                :on-change="choose"
                :limit="2"
            >
                <el-button type="primary" class="ml10" icon="el-icon-Upload">上传文件</el-button>
            </el-upload>
            <template #footer>
                <el-button @click="handleClose">取消</el-button>
                <el-button type="primary" @click="submitupload" :loading="loading">确认</el-button>
            </template>
        </el-dialog>
    </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { mainHeight } from '@/utils/layout'
import TableStore from '@/utils/tableStore'
import TableHeader from '@/components/table/header/index.vue'
import Table from '@/components/table/index.vue'
import BackComponent from '@/components/icon/back/index.vue'
import completenessDetails from './completenessDetails.vue'
import { genFileId, ElMessage } from 'element-plus'
import { uploadUserData, deleteUserDataByIds } from '@/api/advance-boot/division'
import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus'
defineOptions({
    name: 'division/aListOfLoadData'
})
const loading = ref(false)
const height = mainHeight(20)
const completenessDetailsRef = ref()
const dialogVisible = ref(false)
const upload = ref<UploadInstance>()
const fileList = ref([])
const tableStore = new TableStore({
    url: '/advance-boot/responsibility/userDataList',
    method: 'POST',
    publicHeight: 52,
    column: [
        { title: '序号', field: 'index', width: 80, formatter: (row: any) => {
            return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
        }
        },
        { title: '表名', field: 'name' },
        { title: '起始时间', field: 'startTime' },
        { title: '截止时间', field: 'endTime' },
        { title: '更新时间', field: 'updateTime' },

        {
             title: '操作',fixed: 'right',
            width: '180',
            render: 'buttons',
            buttons: [
                {
                    name: 'edit',
                    title: '完整性详情',
                    type: 'primary',
                    icon: 'el-icon-Plus',
                    disabled: row => {
                        return row.integrity == 1
                    },
                    render: 'basicButton',
                    click: row => {
                        completenessDetailsRef.value.open(row.id)
                    }
                },
                {
                    title: '删除',
                    type: 'danger',
                    icon: 'el-icon-Delete',
                    render: 'confirmButton',

                    popconfirm: {
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        confirmButtonType: 'danger',
                        title: '确定删除吗？'
                    },
                    click: row => {
                        deleteUserDataByIds([row.id]).then(() => {
                            ElMessage.success('删除成功')
                            tableStore.index()
                        })
                    }
                }
            ]
        }
    ],
    loadCallback: () => {}
})
const handleClose = () => {
    fileList.value = []
    dialogVisible.value = false
}
// 上传
const choose = (e: any) => {
    upload.value!.clearFiles()
    setTimeout(() => {
        if (e.name.includes('.xls')) {
            fileList.value = [e]
        } else {
            ElMessage.warning('请上传Excel文件！')
        }
    }, 0)
}

// 上传
const submitupload = () => {
    if (fileList.value.length == 0) {
        ElMessage.warning('请上传文件！')
        return
    }
    ElMessage.info('上传中,请稍等...')
    const formData = new FormData()
    formData.append('file', fileList.value[0].raw)
    loading.value = true
    uploadUserData(formData)
        .then(res => {
            ElMessage.success('上传成功')
            loading.value = false
            handleClose()
            tableStore.index()
        })
        .catch(err => {
            loading.value = false
        })
}
provide('tableStore', tableStore)
tableStore.table.params.searchValue = ''
onMounted(() => {
    tableStore.index()
})
</script>
<style lang="scss" scoped>
.title {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    font-size: 16px;
    font-weight: 550;
}

:deep(.upload-demo) {
    display: flex;

    .el-upload-list__item-info {
        width: 300px;
    }
}
</style>
