<template>
  <div class="app-container">
    <div>
      <el-form
        ref="form"
        :model="activationCode"
        :rules="rulesActivationCode"
        label-width="80px"
      >
        <el-form-item label="租户信息" prop="tenantCode">
          <el-select
            v-model="activationCode.tenantCode"
            placeholder="请选择"
            filterable
          >
            <el-option
              v-for="item in tenantList"
              :key="item.tenantId"
              :label="item.tenantName"
              :value="item.tenantId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="充值时间"
          prop="tenantExpirationDate"
          style="width: 300px"
        >
          <el-input
            v-model="activationCode.tenantExpirationDate"
            placeholder="请输入充值天数"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="crateCode">生成激活码</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <!-- 激活码生成成功-->
      <el-dialog
        :title="title"
        :visible.sync="activationCodeForm"
        width="500px"
        append-to-body
      >
        <span v-html="actCode"></span>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="copyToClipboard"
            >一键复制</el-button
          >
          <el-button @click="activationCodeForm = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
    <div style="margin-top: 30px">
      <!--搜索条件表单-->
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="登录地址" prop="ipAddress">
          <el-input
            v-model="queryParams.ipAddress"
            placeholder="请输入登录地址"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作人员" prop="operator">
          <el-input
            v-model="queryParams.operator"
            placeholder="请输入操作人员"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </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-table
        ref="tables"
        v-loading="loading"
        :data="list"
        @selection-change="handleSelectionChange"
        :default-sort="defaultSort"
        @sort-change="handleSortChange"
        style="margin-top: 20px"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="日志编号" align="center" prop="logId" />
        <el-table-column label="操作人员" align="center" prop="operator" />
        <el-table-column
          label="操作ip地址"
          align="center"
          prop="ipAddress"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="操作日期"
          align="center"
          prop="generationTime"
          sortable="custom"
          :sort-orders="['descending', 'ascending']"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.generationTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="note">
        </el-table-column>
      </el-table>
      <!--分页器-->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getCodeLogList"
      />
    </div>
  </div>
</template>

<script>
import { listTenant, createTenantCode } from "@/api/system/tenant";
import {
  list,
  delActivationLog,
  cleanActivationLog,
} from "@/api/monitor/activationcode";

import Clipboard from "clipboard";

export default {
  name: "TenantCode",
  data() {
    return {
      options: [],
      //表单校验
      rulesActivationCode: {
        tenantCode: [
          { required: true, message: "未选择租户信息", trigger: "change" },
        ],
        tenantExpirationDate: [
          { required: true, message: "租户到期时间未设置", trigger: "blur" },
        ],
      },
      activationCode: {},
      //租户信息表格数据
      tenantList: [],
      activationCodeForm: false,
      actCode: "",
      title: "激活码生成成功",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: "operTime", order: "descending" },
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operator: undefined,
        ipAddress: undefined,
        isEnablePaging:false,
      },
    };
  },
  computed: {},
  created() {
    this.getList();
    this.getCodeLogList();
  },
  methods: {
    //查询租户信息列表
    getList() {
      listTenant({ isEnablePaging: false }).then((response) => {
        this.tenantList = response.rows;
      });
    },
    //创建激活码
    crateCode() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          createTenantCode(this.activationCode).then((response) => {
            this.activationCodeForm = true;
            this.actCode = response.msg;
          });
        } else {
          return false;
        }
      });
    },
    //激活码复制操作
    copyToClipboard() {
      //创建一个新 Clipboard 实例，将目标元素和复制成功时的回调传递给它
      const clipboard = new Clipboard("button", {
        text: () => this.actCode, //在这里替换为你要复制的文本
      });
      clipboard.on("success", () => {
        this.activationCodeForm = false;
        this.$message({
          showClose: true,
          message: "文本已成功复制到剪贴板",
          type: "success",
        });
        clipboard.destroy(); //清除 Clipboard 实例
      });
      clipboard.on("error", () => {
        console.error("复制失败");
        clipboard.destroy(); //清除 Clipboard 实例
      });
      //触发按钮点击事件,开始复制操作
      clipboard.onClick({
        action: "copy",
      });
    },
    //取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //激活码日志信息
    getCodeLogList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order);
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.logId);
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getCodeLogList();
    },
  },
};
</script>
