<template>
  <div class="full-container-p24">
    <div style="display: flex; justify-content: space-between">
      <el-select
        v-model="type"
        class="m-2"
        placeholder="全部文件"
        @change="getUFileList"
      >
        <el-option
          v-for="item in options"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-button @click="showUploadModal" type="primary">上传文件</el-button>
      <el-dialog v-model="visable" title="上传文件" style="width: 420px">
        <div class="df aic">
          <div style="width: 80px">文件类型:</div>
          <el-select
            v-model="currentFileType"
            class="m-2"
            placeholder="请选择文件类型"
          >
            <el-option
              v-for="item in options"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            />
          </el-select>
        </div>
        <div class="mt20" v-if="currentFileType">
          <el-upload
            ref="uploadRef"
            v-model:file-list="fileList"
            :action="store.state.baseurl + '/ufile/upload'"
            :on-remove="handleRemove"
            :data="{
              type: currentFileType,
              pCode: 0,
              cCode: 0,
              aCode: 0,
            }"
            :limit="1"
            :on-success="uploadSuccess"
            :auto-upload="false"
          >
            <el-button :disabled="fileList.length" type="primary">
              选择文件
            </el-button>
          </el-upload>
        </div>

        <div class="df jcfe mt40">
          <el-button @click="uploadUFile" type="primary">上传</el-button>
        </div>
      </el-dialog>
    </div>
    <div style="margin-top: 24px">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          min-width="80"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="typeName"
          label="文件类型"
          min-width="160"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="fileName"
          label="文件名称"
          min-width="160"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="createTime"
          label="上传时间"
          min-width="120"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" min-width="180" align="center">
          <template v-slot="scope">
            <el-button
              @click="downloadUFIle(scope.row)"
              type="primary"
              size="small"
            >
              下载
            </el-button>
            <el-button
              @click="deleteUFIle(scope.row)"
              type="danger"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="width: 100%; text-align: right; margin-top: 43px">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, h, reactive, toRefs, onMounted } from "vue";
import { ElNotification, ElMessageBox, ElMessage } from "element-plus";
import store from "../../store";
import router from "../../router";
import md5 from "md5";
import api from "../../apis/fetch";

let currentPage = ref(1);
let type = ref("");
let tableData = ref([]);
let total = ref(0);
async function getUFileList(page) {
  currentPage.value = page || currentPage.value;
  let res = await api.getUFileList({
    currentPage: currentPage.value,
    size: 10,
    type: type.value || 0,
  });
  if (res.data.status == 0) {
    tableData.value = res.data.result;
    total.value = res.data.total;
  } else {
    tableData.value = [];
    total.value = 0;
  }
}

let upLoading = ref(false);
let currentIndex = ref(-1);

function pageChange(e) {
  currentPage.value = e;
  getUFileList();
}
let options = ref([]);

async function getUFileTypeSelect() {
  let res = await api.getUFileTypeSelect();
  options.value = res.data.result;
}
let currentFileType = ref("");

function downloadUFIle(item) {
  let a = document.createElement("a");
  a.setAttribute("href", item.downloadUrl);
  a.click();
}
function deleteUFIle(item) {
  ElMessageBox.confirm(`确认删除: ${item.fileName}?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      let res = await api.deleteUFile({
        id: item.id,
      });
      if (res.data.status == 0) {
        ElMessage({
          type: "success",
          message: "删除成功",
        });
        getUFileList();
      }
    })
    .catch((e) => {
      console.log(e);
    });
}
let uploadRef = ref(null);
function uploadUFile(e) {
  if (!currentFileType.value) {
    ElMessage({
      message: "请选择文件类型并上传文件",
      type: "warning",
    });
    return;
  }
  if (fileList.value.length == 0) {
    ElMessage({
      message: "请上传文件",
      type: "warning",
    });
    return;
  }
  uploadRef.value.submit();
}
let visable = ref(false);
function showUploadModal() {
  visable.value = true;
}

let fileList = ref([]);
function uploadSuccess(e) {
  console.log(e);
  if (e.status == 0) {
    ElMessage({
      message: "上传成功!",
      type: "success",
    });
    visable.value = false;
    currentFileType.value = "";
    fileList.value = [];
    getUFileList();
  } else {
    ElMessage({
      message: e.msg,
      type: "error",
    });
  }
}
onMounted(() => {
  getUFileList();
  getUFileTypeSelect();
});
</script>
<style scoped>
.seach-btn {
  display: inline-block;
  width: 60px;
  height: 38px;
  background: #0094fe;
  border-radius: 2px;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  text-align: center;
  line-height: 38px;
  margin-left: 10px;
  cursor: pointer;
}

.cargo-owner-add {
  width: 80px;
  height: 32px;
  border-radius: 2px;
  border: 1px solid #0094fe;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #0094fe;
  line-height: 32px;
  text-align: center;
  cursor: pointer;
}

:deep().el-dialog {
  width: 560px;
  padding: 20px 50px;
  border-radius: 6px;
}

:deep() .el-dialog__title {
  font-size: 18px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #0094fe;
}
</style>
