<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="false" v-show="showSearch" label-width="68px">
            <el-form-item label="文件名称" prop="fileName">
                <el-input v-model="queryParams.fileName" placeholder="请输入文件名称" clearable @keyup.enter="handleQuery"
                    @blur="handleQuery" style="display:inline-block" />
            </el-form-item>

            <el-form-item label="文件类型" prop="fileType">
                <el-radio-group v-model="queryParams.fileType" clearable @change="handleQuery">
                    <el-radio-button v-for="item in fileTypeOptions" :key="item.value" :label="item.value"
                        :value="item.value" name="type">{{ item.label }}</el-radio-button>
                </el-radio-group>
                <!-- <el-select v-model="queryParams.fileType" placeholder="请选择文件类型" clearable @change="handleQuery">
                    <el-option v-for="item in fileTypeOptions" :key="item.value" :label="item.label"
                        :value="item.value" />
                </el-select> -->
            </el-form-item>
            <!-- <el-form-item label="文件大小" prop="fileSize">
                <el-input v-model="queryParams.fileSize" placeholder="请输入文件大小" clearable @keyup.enter="handleQuery"
                     />
            </el-form-item> -->





            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索
                </el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5" v-hasPermi="['business:workFileinfo:add']">
                <!-- <file-upload v-model="form.filePath" @handleUploadSuccess="handleUploadSuccess" :limit="1"
                    :isShowTip="false" :showFileList="false" :plain="true" label="上传报表" /> -->
                <el-button type="primary" plain icon="UploadFilled" @click="handleAdd">上传报表
                </el-button>
            </el-col>
            <!-- <el-col :span="1.5" v-hasPermi="['business:workFileinfo:edit']">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate">修改
                </el-button>
            </el-col> -->
            <el-col :span="1.5" v-hasPermi="['business:workFileinfo:remove']">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete">删除
                </el-button>
            </el-col>
            <!-- <el-col :span="1.5" v-hasPermi="['business:workFileinfo:export']">
                <el-button type="warning" plain icon="Download" @click="handleExport">导出
                </el-button>
            </el-col> -->
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>


        <el-card shadow="never">
            <el-table ref="tableRef" v-loading="loading" :data="workFileInfoList" @selection-change="handleSelectionChange"
                :default-sort="defaultSort" @sort-change="handleSortChange">
                <el-table-column type="selection" width="55" align="center" v-hasPermi="['business:workFileinfo:remove']" />
                <el-table-column label="序号" align="center" type="index" width="50" />
                <el-table-column label="文件编号" align="center" prop="fileId" width="150" />
                <el-table-column label="" width="30px">
                    <template #default="scope">
                        <svg-icon :icon-class="`weizhiwenjian`" style="font-size:20px;" v-if="scope.row.fileType == null" />
                        <svg-icon :icon-class="`txt`" style="font-size:20px;"
                            v-else-if="scope.row.fileType.toLowerCase() == `txt`" />
                        <svg-icon :icon-class="`excel (2)`" style="font-size:20px;"
                            v-else-if="(scope.row.fileType).toLowerCase().indexOf(`xls`) != -1" />
                        <svg-icon :icon-class="`PPT`" style="font-size:20px;"
                            v-else-if="(scope.row.fileType).toLowerCase().indexOf(`ppt`) != -1" />
                        <svg-icon :icon-class="`word`" style="font-size:20px;"
                            v-else-if="(scope.row.fileType).toLowerCase().indexOf(`doc`) != -1" />
                        <svg-icon :icon-class="`PDF (2)`" style="font-size:20px;"
                            v-else-if="scope.row.fileType.toLowerCase() == `pdf`" />
                        <svg-icon v-else :icon-class="`weizhiwenjian`" style="font-size:20px;" />
                    </template>
                </el-table-column>
                <el-table-column label="文件名称" align="center" prop="fileName" show-overflow-tooltip>
                    <template #default="scope">
                        <el-link @click="handleDownload(scope.row)">{{ scope.row.fileName }}</el-link>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="文件路径" align="center" prop="filePath" /> -->
                <el-table-column label="文件类型" align="center" prop="fileType" />
                <el-table-column label="文件大小" align="center" prop="fileSize">
                    <template #default="scope">
                        {{ ((scope.row.fileSize || 0) / 1024 / 1024).toFixed(2) }}MB
                    </template>
                </el-table-column>
                <el-table-column label="上传人" align="center" prop="createName" />
                <el-table-column label="上传时间" align="center" prop="createTime" width="180" sortable="custom"
                    :sort-orders="['descending', 'ascending']">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
                    <template #default="scope">
                        <!-- <el-button link icon="View" @click="handleView(scope.row)"
                        v-hasPermi="['business:workFileinfo:query']">详细
                    </el-button> -->
                        <el-button link icon="Edit" type="success" @click="handleUpdate(scope.row)"
                            v-hasPermi="['business:workFileinfo:edit']">修改
                        </el-button>
                        <el-button link icon="Delete" type="danger" @click="handleDelete(scope.row)"
                            v-hasPermi="['business:workFileinfo:remove']">删除
                        </el-button>
                        <el-button link icon="Download" type="primary" @click="handleDownload(scope.row)">下载
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或上传维保报表对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="workFileInfoRef" :model="form" :rules="rules" label-width="80px">
                <!-- <el-form-item label="文件名称" prop="fileName">
                    <el-input v-model="form.fileName" placeholder="请输入文件名称" />
                </el-form-item> -->
                <el-form-item label="文件路径">
                    <!--  -->
                    <file-upload v-model="form.filePath" :limit="1" @handleUploadSuccess="handleUploadSuccess"
                        listType="picture" style="width: 100%;" backUp />
                </el-form-item>
                <!-- <el-form-item label="文件大小" prop="fileSize">
                    <el-input v-model="form.fileSize" placeholder="请输入文件大小" />
                </el-form-item> -->
                <el-form-item label="备注" prop="remark" style="width:100%">
                    <el-input maxlength="300" v-model="form.remark" type="textarea" :rows="5" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="WorkFileInfo">
import { listWorkFileInfo, getWorkFileInfo, delWorkFileInfo, addWorkFileInfo, updateWorkFileInfo } from "@/api/business/workFileInfo";
const baseURL = import.meta.env.VITE_APP_BASE_API

const { proxy, ctx } = getCurrentInstance();
const workFileInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const defaultSort = ref({ prop: "createTime", order: "descending" });

const fileTypeOptions = ref([

    // {
    //     label:"xls(x)",
    //     value:"xls",
    // },
    // {
    //     label:"ppt(x)",
    //     value:"ppt",
    // },
    // {
    //     label:"doc(X)",
    //     value:"doc",
    // },
    {
        label: "所有",
        value: null,
    }, {
        label: "xls",
        value: "xls1",
    }, {
        label: "xlsx",
        value: "xlsx",
    }, {
        label: "ppt",
        value: "ppt",
    }, {
        label: "pptx",
        value: "pptx",
    }, {
        label: "doc",
        value: "doc",
    }, {
        label: "docx",
        value: "docx",
    }, {
        label: "pdf",
        value: "pdf",
    }, {
        label: "txt",
        value: "txt",
    },

])

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
        filePath: null,
        fileType: null,
        fileSize: null
    },
    rules: {
    }
});

const { queryParams, form, rules } = toRefs(data);
const baseUrl = import.meta.env.VITE_APP_BASE_API;

/** 查询文件信息列表 */
function getList() {
    loading.value = true;
    queryParams.value.orderByColumn = queryParams.value.orderByColumn || defaultSort.value.prop;
    queryParams.value.isAsc = queryParams.value.isAsc || defaultSort.value.order;
    listWorkFileInfo(queryParams.value).then(response => {
        workFileInfoList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

function addList() {
    loading.value = true;
    listWorkFileInfo(queryParams.value)
        .then(response => {
            workFileInfoList.value.push(...response.rows);
            total.value = response.total;
            if (workFileInfoList.value.length >= total.value) finished.value = true;
            loading.value = false;
        })
        .catch(response => {
            loading.value = false;
        });
}

// 下拉刷新
function handlePullDownRefresh() {
    queryParams.value.pageNum = 1;
    workFileInfoList.value = [];
    addList();
}

// 滚动到底部的事件
function handleReachBottom() {
    queryParams.value.pageNum++;
    addList();
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        fileId: null,
        fileName: null,
        filePath: null,
        fileType: null,
        createBy: null,
        createTime: null,
        fileSize: null
    }
        ;
    proxy.resetForm("workFileInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    // console.log(queryParams.value)
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    queryParams.value.pageNum = 1;
    proxy.$refs["tableRef"].sort(defaultSort.value.prop, defaultSort.value.order);
    // handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.fileId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "上传维保报表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const fileId =
        row.fileId || ids.value
    getWorkFileInfo(fileId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "上传维保报表";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["workFileInfoRef"].validate(valid => {
        if (valid) {
            if (form.value.fileId != null) {
                updateWorkFileInfo(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addWorkFileInfo(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const fileIds = row.fileId || ids.value;
    console.log(fileIds)
    proxy.$modal.confirm('是否确认删除文件信息编号为"' + fileIds + '"的数据项？').then(function () {
        return delWorkFileInfo(fileIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    downloadWorkFileInfo(queryParams.value)
    // proxy.download('workFileInfo/workFileInfo/export', {
    //   ...queryParams.value
    // }, `workFileInfo_${new Date().getTime()}.xlsx`)
}

/** 详细按钮操作 */
function handleView(row) {
}

function handleUploadSuccess(file) {
    // console.log(file)
    form.value.fileName = file.response.originalFilename
    // form.value.filePath = file.response.raw.name
    form.value.fileSize = file.raw.size
    let fileTypeList = file.raw.name.split(".")
    form.value.fileType = fileTypeList[fileTypeList.length - 1]
    // addWorkFileInfo(form.value).then(response => {
    //     proxy.$modal.msgSuccess("新增成功");
    //     open.value = false;
    //     getList();
    //     form.value = {}
    // });
}

function handleDownload(row) {
    // proxy.$download.resource(row.filePath);
    window.open(baseURL + row.filePath)
}


/** 排序触发事件 */
function handleSortChange(column, prop, order) {
   queryParams.value.orderByColumn = column.prop;
   queryParams.value.isAsc = column.order;
   getList();
}

getList();
</script>