<template>
  <div class="page">
    <LayoutTable :data="tableData" :config="tableConfig">
      <el-button
        slot="table-head-left2"
        style="margin-left: 10px"
        icon="el-icon-tickets"
        size="mini"
        @click="handleImport"
        >导入</el-button
      >
      <el-button
        slot="table-head-left2"
        style="margin-left: 10px"
        icon="el-icon-tickets"
        size="mini"
        @click="doExport"
        :disabled="isExport"
        >导出</el-button
      >
    </LayoutTable>
    <!-- doc模板导入对话框 -->
    <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() {
    const { docId } = this.$route.query;
    this.config.addQuery = { docId: docId };
  },
  methods: {
    /**解析模板 */
    toParseXml(row) {
      this.loading = true;

      let form = Object.assign({}, row);
      this.$post("/doc/template/parseXmltoJson", form)
        .then((res) => {
          console.log("res", res);
          if (res && res.code && res.code == 1) {
            this.$message.success("解析模板成功！");
            this.getData();
          }
        })
        .catch((error) => {
          this.$message.error(error.message);
        });
    },
    /**表单配置 */
    toParseForm(row) {
     this.$router.push({
        path: "/template/form",
        query: {
            formId:row.docFormEntity.id,
          templateId: row.id,
        },
      });

    },
    /** 导入 */
    handleImport() {
      this.upload.title = "doc模板导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    downloadTemplate() {
      this.isExport = true;
      this.$download("/doc/template/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();
    },
    doExport() {
      this.isExport = true;
      this.$download(
        "/doc/template/exportExcel",
        {
          "query.name": this.$route.query["query.name"],
          "query.docId": this.$route.query["query.docId"],
          "query.templatePath": this.$route.query["query.templatePath"],
        },
        { type: "excel" }
      )
        .then(() => (this.isExport = false))
        .catch((error) => {
          this.isExport = false;
          this.$message.error(error.message);
        });
    },
    renderTable(tableData) {
      return (
        <el-table stripe data={tableData} class="total-table">
          {this.columnSet.map((item) => this.renderTableColumn(item))}
        </el-table>
      );
    },
    renderTableColumn(options) {
      return (
        <el-table-column
          prop={options.prop}
          label={options.label}
        ></el-table-column>
      );
    },
  },
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（doc模板导入）
        open: false,
        // 弹出层标题（doc模板导入）
        title: "导入doc模板数据",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 上传的地址
        url: "/m/doc/template/importData",
      },
      isExport: false,
      columnSet: [
        { prop: "id", label: "ID,主键，自增长" },
        { prop: "templateId", label: "模板ID" },
        { prop: "formName", label: "表单名称" },
        { prop: "formContent", label: "表单内容" },
        { prop: "remark", label: "备注" },
        { prop: "updateTime", label: "变更时间" },
        { prop: "updateUser", label: "变更用户,登录名" },
        { prop: "createTime", label: "创建时间" },
        { prop: "createUser", label: "创建用户,登录名" },
      ],
      config: {
        search: [
          {
            name: "name",
            type: "text",
            label: "模板名称",
            fuzzy: true,
          },
          {
            name: "docId",
            type: "text",
            label: "doc主键ID",
            fuzzy: true,
          },
          {
            name: "templatePath",
            type: "text",
            label: "模板地址",
            fuzzy: true,
          },
        ],
        columns: [
          { type: "selection", width: 60 },

          { label: "模板名称", prop: "name" },

          { label: "模板地址", prop: "templatePath" },
          { label: "备注", prop: "remark" },

          {
            label: "创建时间",
            prop: "createTime",
            formatter: this.formatterDate,
          },
          { label: "创建用户", prop: "createUser" },

          {
            label: "操作",
            width: 320,
            formatter: (row) => {
              return (
                <div>
                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                
                    size="mini"
                    onClick={() => {
                      this.toParseXml(row);
                    }}
                  >
                    模板解析
                  </el-button>

                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                
                    size="mini"
                    onClick={() => {
                      this.toParseForm(row);
                    }}
                  >
                    表单配置
                  </el-button>
                </div>
              );
            },
          },
        ],
      },
    };
  },
};
</script>