<script setup name="SystemBackups">
import { onMounted, ref, toRefs, onBeforeUnmount } from 'vue'
import { ElMessage } from "element-plus";
import CustomUpload from "@/components/CustomUpload/index.vue";
import { importSql, exportSql } from '@/api/systemConfig/systemBackups.js'

const uploadLoading = ref(false);

// 上传回调
const handleFileChange = (file) => {
  uploadLoading.value = true;
  const formData = new FormData();
  formData.append('fileUpload', file.raw);
  importSql(formData).then(res => {
    const { flag } = res
    if (flag) {
      uploadLoading.value = false;
      ElMessage.success('导入成功');
    }
    uploadLoading.value = false;
  }).catch(err => {
    uploadLoading.value = false;
  })
}

// 下载数据库
const exportSqlFunc = async () => {
  exportSql({}).then(response => {
    const blob = new Blob([response]);
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = `表.sql`
    link.click()
    URL.revokeObjectURL(link.href)
    ElMessage.success('导出成功')
    uploadLoading.value = false;
  }).catch(err => {
    uploadLoading.value = false;
  })
}


</script>

<template>
  <div class="app-container scroller">
    <PageTitle>
      <template #title>系统备份</template>
    </PageTitle>
    <div class="app-container__body">
      <div class="updata">
        <div class="left">
          <img src="@/assets/images/systemConfig/systemBackups.png" alt=""
            style="vertical-align: middle; padding-right: 10px;" />
        </div>
        <div class="right">
          <div class="title-header">系统备份</div>
          <div style="margin-top: 30px;">
            <el-button icon="Upload" :loading="uploadLoading" style="position: relative;">
              <span>数据库导入</span>
              <div class="uploadBut">
                <CustomUpload v-if="!uploadLoading" @file-change="handleFileChange" />
              </div>
            </el-button>
            <el-button icon="Download" :loading="uploadLoading" @click="exportSqlFunc">数据库导出</el-button>
          </div>
          <div class="title-header">恢复配置库</div>
          <div>
            <span class="title-item">注意：</span>
            <span>您可以将配置库信息导出，也可以随时通过导入功能初始化配置库。</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.app-container__body {
  align-items: center;
  .updata {
    margin-top: 10%;
    width: 700px;
    display: flex;
    .right {
      padding-left: 20px;
      .uploadBut {
        position: absolute;
        top: 0;
        right: 0;
        width: 100%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
        opacity: 0;
      }
      .title-header {
        height: 84px;
        line-height: 40px;
        font-weight: 700;
        font-size: 18px;
        color: #666;
        position: relative;
        top: 40px;
      }
      .title-item {
        font-weight: 700;
        line-height: 30px;
        color: #666;
      }
    }
  }
}
</style>