<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="模板" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="规则" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入规则"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="historyName">
        <el-input
          v-model="queryParams.historyName"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导入导出" prop="identifyingCode">
        <el-input
          v-model="queryParams.identifyingCode"
          placeholder="请输入导入导出标识"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:historydata:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="historydataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="模板" align="center" prop="templateName" />
        <el-table-column label="规则" align="center" prop="roleName" />
        <el-table-column label="标题" align="center" prop="historyName" />
        <el-table-column label="导入导出" align="center" prop="identifyingCode">
            <template slot-scope="scope">
                <span v-if="scope.row.identifyingCode==0">导入</span>
                <span v-if="scope.row.identifyingCode==1">导出</span>
            </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetails(scope.row)"
            v-hasPermi="['system:historydata']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:historydata:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

      <div id="luckysheet" :style="{ position: positionValue, top: '0', left: '0', right: '0', bottom: '0' }"></div>

  </div>
</template>

<style>
#luckysheet_info_detail_title {
    display: none;
}

#luckysheet_info_detail_update {
    display: none;
}

#luckysheet_info_detail_save {
    display: none;
}

.luckysheet-share-logo {
    display: none;
}
</style>

<script>
import { listHistorydata, getHistorydata, delHistorydata, addHistorydata, updateHistorydata, exportHistorydata } from "@/api/system/historydata";
import Editor from '@/components/Editor';
import $ from 'jquery';
import XLSX from 'xlsx';
import LuckyExcel from 'luckyexcel';
import { Loading } from 'element-ui';
export default {
  name: "Historydata",
  components: {
    Editor,
  },
  data() {
    return {
        positionValue: 'static',

        // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //  历史数据表格数据
      historydataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        importUuid: null,
        templateId: null,
        exportUuid: null,
        roleId: null,
        historyName: null,
        historyContent: null,
        identifyingCode: null,
        status: null,
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
      this.positionValue = "static";

  },
    mounted() {

        window.handleReturnButtonClick = this.handleReturnButtonClick;

    },
  methods: {
    /** 查询 历史数据列表 */
    getList() {
      this.loading = true;
      listHistorydata(this.queryParams).then(response => {
        this.historydataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        importUuid: null,
        templateId: null,
        exportUuid: null,
        roleId: null,
        historyName: null,
        historyContent: null,
        identifyingCode: null,
        status: 0,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加 历史数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHistorydata(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改 历史数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHistorydata(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHistorydata(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDetails(row) {

        this.positionValue = 'absolute';
        luckysheet.destroy();
        luckysheet.create({
            container: "luckysheet", // Luckysheet 的容器元素 ID
            title: row.historyName, // Excel 文件名
            data: JSON.parse(row.historyContent), // Excel 数据
            showtoolbar: false, //是否第二列显示工具栏
            showinfobar: true, //是否显示顶部名称栏
            showsheetbar: false, //是否显示底部表格名称区域
            pointEdit: false, //是否是编辑器插入表格模式
            pointEditUpdate: null, //编辑器表格更新函数
            allowEdit: false,//作用：是否允许前台编辑
            functionButton: '<button id="exportButton" class="btn btn-primary" style=" padding:3px 6px; font-size: 16px;width: 100px;height: 27px; margin-right: 85px;" onclick="handleReturnButtonClick()">返回</button>',

        });

    },
      /**详情返回按钮 */
      handleReturnButtonClick() {
          this.positionValue = 'static';
          luckysheet.destroy();
      },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除 历史数据编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delHistorydata(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有 历史数据数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportHistorydata(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
