<template>
  <div class="page">
    <LayoutTable :data="tableData" :config="tableConfig"> </LayoutTable>
    <!-- 构建工程信息导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据
          <el-link
            type="primary"
            style="font-size: 14px"
            @click="downloadTemplate"
            >下载模板</el-link
          >
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import table from "@/assets/mixins/table";
export default {
  components: {},
  mixins: [table],
  created() {},
  methods: {
    /** 下载工程操作 */
    toDownload(row) {
      this.isExport = true;
      this.$download(
        "/project/filedownload",
        {filepath:row.projectPath},
        { type: "zip" }
      )
        .then(() => (this.isExport = false))
        .catch((error) => {
          this.isExport = false;
          this.$message.error(error.message);
        });
    },

    /** 导入 */
    handleImport() {
      this.upload.title = "构建工程信息导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    downloadTemplate() {
      this.isExport = true;
      this.$download("/project/downloadTemplate", {}, { type: "excel" })
        .then(() => (this.isExport = false))
        .catch((error) => {
          this.isExport = false;
          this.$message.error(error.message);
        });
    },
    /** 文件上传中处理 */
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    /** 文件上传成功处理 */
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getData();
    },
    /** 提交上传文件 */
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出Excel */
    doExport() {
      this.isExport = true;
      this.$download(
        "/project/exportExcel",
        {
          "query.name": this.$route.query["query.name"],
          "query.projectPath": this.$route.query["query.projectPath"],
          "query.version": this.$route.query["query.version"],
        },
        { type: "excel" }
      )
        .then(() => (this.isExport = false))
        .catch((error) => {
          this.isExport = false;
          this.$message.error(error.message);
        });
    },
  },
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（构建工程信息导入）
        open: false,
        // 弹出层标题（构建工程信息导入）
        title: "导入构建工程信息数据",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 上传的地址
        url: "/m/project/importData",
      },
      isExport: false,
      config: {
        search: [
          {
            name: "name",
            type: "text",
            label: "工程名称",
            fuzzy: true,
          },
          {
            name: "projectPath",
            type: "text",
            label: "工程地址",
            fuzzy: true,
          },
          {
            name: "version",
            type: "text",
            label: "工程版本",
            fuzzy: true,
          },
        ],
        columns: [
          { type: "selection", width: 60 },
          { label: "工程名称", prop: "name" },
          { label: "工程版本", prop: "version" },
          { label: "备注", prop: "remark" },
          {
            label: "创建时间",
            prop: "createTime",
            formatter: this.formatterDate,
          },
          { label: "创建用户", prop: "createUser" },
          {
            label: "操作",
            width: 180,
            formatter: (row) => {
              return (
                <div>
                  <el-button
                    type="text"
                    icon="el-icon-download"
                    size="mini"
                    onClick={() => {
                      this.toDownload(row);
                    }}
                  >
                    下载
                  </el-button>
                </div>
              );
            },
          },
        ],
      },
    };
  },
};
</script>