<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'
import pageIcon from '@/assets/images/systemConfig/systemParamPage.png'

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">
    <div class="app-container__body">
      <IconTitle :icon="pageIcon" title="系统备份" />
      <div class="box">
        <div class="updata">
          <img src="@/assets/images/systemConfig/systemBackups.png" alt="" />
          <div style="margin-top: 30px;">
            <el-button icon="Upload" :loading="uploadLoading" style="position: relative;width: 115px;height: 40px;background: rgba(33, 103, 217, 1);color: #fff;">
              <span>数据库导入</span>
              <div class="uploadBut">
                <CustomUpload v-if="!uploadLoading" @file-change="handleFileChange" />
              </div>
            </el-button>
            <el-button icon="Download" style="width: 115px;height: 40px;background: rgba(11, 136, 251, 1);color: #fff;margin-left: 32px" :loading="uploadLoading" @click="exportSqlFunc">数据库导出</el-button>
          </div>
          <div class="title-header">注意：您可以将配置库信息导出，也可以随时通过导入功能初始化配置库。</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.app-container__body {
  .box {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
  }
  .updata {
    margin-top: 10%;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    img{
      width: 640px;
    }
    .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 {
      margin-top: 31px;
      font-size: 14px;
      line-height: 22px;
      color: rgba(253, 84, 81, 1);
    }
  }
}
</style>