<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() {},
  methods: {
    /** 申报 */
    toApply(row) {
      this.$router.push({
        path: "/doc/baseinfo/index",
        query: {
          docId: row.id,
          templateId: row.docTemplateEntity.id,
        },
      });
    },
    /** 模板配置 */
    toTemplate(row) {
      this.$router.push({
        path: "/doc/template/list",
        query: {
          docId: row.id,
        },
      });
    },
    /** 导入 */
    handleImport() {
      this.upload.title = "doc基础信息导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    downloadTemplate() {
      this.isExport = true;
      this.$download("/doc/baseinfo/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/baseinfo/exportExcel",
        {
          "query.name": this.$route.query["query.name"],
          "query.samplePath": this.$route.query["query.samplePath"],
        },
        { type: "excel" }
      )
        .then(() => (this.isExport = false))
        .catch((error) => {
          this.isExport = false;
          this.$message.error(error.message);
        });
    },
    renderTable(obj) {
      let tableData = [];
      tableData.push(obj);
      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}
           width={options.width}
        ></el-table-column>
      );
    },
  },
  data() {
    return {
      formConf: {
        fields: [
          {
            __config__: {
              label: "单行文本",
              labelWidth: null,
              showLabel: true,
              changeTag: true,
              tag: "el-input",
              tagIcon: "input",
              required: true,
              layout: "colFormItem",
              span: 24,
              document: "https://element.eleme.cn/#/zh-CN/component/input",
              regList: [
                {
                  pattern: "/^1(3|4|5|7|8|9)\\d{9}$/",
                  message: "手机号格式错误",
                },
              ],
            },
            __slot__: {
              prepend: "",
              append: "",
            },
            __vModel__: "mobile",
            placeholder: "请输入手机号",
            style: {
              width: "100%",
            },
            clearable: true,
            "prefix-icon": "el-icon-mobile",
            "suffix-icon": "",
            maxlength: 11,
            "show-word-limit": true,
            readonly: false,
            disabled: false,
          },
          {
            __config__: {
              label: "日期范围",
              tag: "el-date-picker",
              tagIcon: "date-range",
              defaultValue: null,
              span: 24,
              showLabel: true,
              labelWidth: null,
              required: true,
              layout: "colFormItem",
              regList: [],
              changeTag: true,
              document:
                "https://element.eleme.cn/#/zh-CN/component/date-picker",
              formId: 101,
              renderKey: 1585980082729,
            },
            style: {
              width: "100%",
            },
            type: "daterange",
            "range-separator": "至",
            "start-placeholder": "开始日期",
            "end-placeholder": "结束日期",
            disabled: false,
            clearable: true,
            format: "yyyy-MM-dd",
            "value-format": "yyyy-MM-dd",
            readonly: false,
            __vModel__: "field101",
          },
          {
            __config__: {
              layout: "rowFormItem",
              tagIcon: "row",
              label: "行容器",
              layoutTree: true,
              children: [
                {
                  __config__: {
                    label: "评分",
                    tag: "el-rate",
                    tagIcon: "rate",
                    defaultValue: 0,
                    span: 24,
                    showLabel: true,
                    labelWidth: null,
                    layout: "colFormItem",
                    required: true,
                    regList: [],
                    changeTag: true,
                    document: "https://element.eleme.cn/#/zh-CN/component/rate",
                    formId: 102,
                    renderKey: 1586839671259,
                  },
                  style: {},
                  max: 5,
                  "allow-half": false,
                  "show-text": false,
                  "show-score": false,
                  disabled: false,
                  __vModel__: "field102",
                },
              ],
              document: "https://element.eleme.cn/#/zh-CN/component/layout",
              formId: 101,
              span: 24,
              renderKey: 1586839668999,
              componentName: "row101",
              gutter: 15,
            },
            type: "default",
            justify: "start",
            align: "top",
          },
          {
            __config__: {
              label: "按钮",
              showLabel: true,
              changeTag: true,
              labelWidth: null,
              tag: "el-button",
              tagIcon: "button",
              span: 24,
              layout: "colFormItem",
              document: "https://element.eleme.cn/#/zh-CN/component/button",
              renderKey: 1594288459289,
            },
            __slot__: {
              default: "测试按钮1",
            },
            type: "primary",
            icon: "el-icon-search",
            round: false,
            size: "medium",
            plain: false,
            circle: false,
            disabled: false,
            on: {
              click: "clickTestButton1",
            },
          },
        ],
        __methods__: {
          clickTestButton1() {
            console.log(
              `%c【测试按钮1】点击事件里可以访问当前表单：
                1) formModel='formData', 所以this.formData可以拿到当前表单的model
                2) formRef='elForm', 所以this.$refs.elForm可以拿到当前表单的ref(vue组件)
              `,
              "color:#409EFF;font-size: 15px"
            );
            console.log("表单的Model：", this.formData);
            console.log("表单的ref：", this.$refs.elForm);
          },
        },
        formRef: "elForm",
        formModel: "formData",
        size: "small",
        labelPosition: "right",
        labelWidth: 100,
        formRules: "rules",
        gutter: 15,
        disabled: false,
        span: 24,
        formBtns: true,
        unFocusedComponentBorder: false,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（doc基础信息导入）
        open: false,
        // 弹出层标题（doc基础信息导入）
        title: "导入doc基础信息数据",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 上传的地址
        url: "/m/doc/baseinfo/importData",
      },
      isExport: false,
      columnSet: [
        { prop: "name", label: "模板名称" },
        { prop: "templatePath", label: "模板地址" },
        { prop: "remark", label: "备注" },
        { prop: "createTime", label: "创建时间"},
        { prop: "createUser", label: "创建用户" },
      ],
      config: {
        search: [
          {
            name: "name",
            type: "text",
            label: "业务名称",
            fuzzy: true,
          },
          {
            name: "samplePath",
            type: "text",
            label: "样表地址",
            fuzzy: true,
          },
        ],
        columns: [
          { type: "selection", width: 60 },

          { label: "业务名称", prop: "name" },
          { label: "样表地址", prop: "samplePath" },
          { label: "备注", prop: "remark" },

          {
            label: "创建时间",
            prop: "createTime",
            formatter: this.formatterDate,
          },
          { label: "创建用户", prop: "createUser" },
          {
            label: "doc模板",
            width: 120,
            prop: "subColumns",
            formatter: (row) => {
              let widthsize = this.columnSet.length * 150;
              return (
                <el-popover placement="right" width={widthsize} trigger="click">
                  {this.renderTable(row.docTemplateEntity)}
                  <el-button type="text" slot="reference">
                    详细
                  </el-button>
                </el-popover>
              );
            },
          },
          {
            label: "操作",
            width: 320,
            formatter: (row) => {
              return (
                <div>
                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                    size="mini"
                    onClick={() => {
                      this.toEdit(row);
                    }}
                  >
                    编辑
                  </el-button>

                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                    size="mini"
                    onClick={() => {
                      this.toTemplate(row);
                    }}
                  >
                    文档模板
                  </el-button>

                  <el-button
                    type="primary"
                    icon="el-icon-edit"
                    size="mini"
                    onClick={() => {
                      this.toApply(row);
                    }}
                  >
                    文档申报
                  </el-button>
                </div>
              );
            },
          },
        ],
      },
    };
  },
};
</script>