<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <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">
        <el-button
            type="danger"
            plain
            icon="Upload"
            @click="uploadData.open=true"
            v-hasPermi="['common:file:add']"
        >上传附件
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['common:file:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="附件名称" align="center" prop="fileName"/>
      <el-table-column label="附件大小" align="center" prop="fileSize"/>
      <el-table-column label="备注" align="center" prop="remarks"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['common:file:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['common:file:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
    <el-dialog title="上传附件" v-model="uploadData.open" width="800px" top="5vh" append-to-body>
      <el-upload
          multiple
          :action="uploadFileUrl"
          :before-upload="handleBeforeUpload"
          :file-list="fileList"
          :on-error="handleUploadError"
          :on-exceed="handleExceed"
          :on-success="handleUploadSuccess"
          :show-file-list="false"
          :headers="headers"
          class="upload-file-uploader"
          ref="fileUpload"
      >
        <!-- 上传按钮 -->
        <el-button type="primary">选取文件</el-button>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script setup name="File">
import {delFile, listFile} from "@/api/common/file";
import {getToken} from "@/utils/auth.js";

const route = useRoute();
const {proxy} = getCurrentInstance();

const fileList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const uploadFileUrl = ref(import.meta.env.VITE_APP_BASE_API + "/common/upload"); // 上传文件服务器地址
const headers = ref({Authorization: "Bearer " + getToken()});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fileUrl: null,
    fileName: null,
    fileSize: null,
    linkId: null,
    linkTable: null,
    status: null,
    createrCode: null,
    createdate: null,
    updaterCode: null,
    updatedate: null,
    deptId: null,
    remarks: null
  },
  rules: {
    fileUrl: [
      {required: true, message: "附件地址不能为空", trigger: "blur"}
    ],
  },
  uploadData: {
    open: false,
  }
});

const {queryParams, form, rules, uploadData} = toRefs(data);

/** 查询附件列表 */
function getList() {
  const linkId = route.params && route.params.linkId;
  const linkTable = route.params && route.params.linkTable;
  loading.value = true;
  queryParams.linkTable = linkTable;
  queryParams.linkId = linkId;
  listFile(queryParams.value).then(response => {
    fileList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

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


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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

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

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

function handleRemove(file, uploadFiles) {
  console.log(file, uploadFiles)
}

function handlePreview(uploadFile) {
  console.log(uploadFile)
}

function handleExceed(files, uploadFiles) {
  proxy.$modal.warning(
      `The limit is 3, you selected ${files.length} files this time, add up to ${
          files.length + uploadFiles.length
      } totally`
  )
}

function beforeRemove(uploadFile, uploadFiles) {
  return proxy.$modal.confirm(
      `Cancel the transfer of ${uploadFile.name} ?`
  ).then(
      () => true,
      () => false
  )
}


getList();
</script>
