<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="激活码" prop="vipCode">
        <el-input
          v-model="queryParams.vipCode"
          placeholder="请输入激活码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="激活状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择激活状态"
          clearable
          size="small"
        >
          <el-option
            v-for="item in keyOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          size="small"
          style="width: 200px"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="激活用户id"
        prop="activationUserId"
        label-width="90px"
      >
        <el-input
          v-model="queryParams.activationUserId"
          placeholder="请输入激活用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          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="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vip:code:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="codeList"
      :row-class-name="tableRowClassName"
    >
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column
        label="激活码"
        align="center"
        prop="vipCode"
        width="200px"
      >
        <template slot-scope="scope">
          <span style="margin-right:5px">{{ scope.row.vipCode }}</span>
          <el-button type="primary" round @click="copyFun(scope.row.vipCode)"
            >复制</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="激活状态"
        align="center"
        prop="status"
        :formatter="keyOptionsFormatter"
      />
      <el-table-column
        label="激活时间"
        align="center"
        prop="activationTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activationTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="激活用户id"
        align="center"
        prop="activationUserId"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vip:code:edit']"
            round
            >作废</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"
    />

    <!-- 新增激活码对话框 -->
    <el-dialog
      title="新增激活码"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <div class="jihuoma">
        激活码: <span style="color:red">{{ jihuoma }} </span>
        <el-button type="primary" round @click="copyFun(jihuoma)"
          >复制激活码</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCode, updateCode, addNewCode } from "@/api/vip/code";

export default {
  name: "Code",
  data() {
    return {
      keyOptions: [
        {
          value: "0",
          label: "未激活"
        },
        {
          value: "1",
          label: "已激活"
        },
        {
          value: "2",
          label: "已作废"
        }
      ],
      keyOptionsFormatter: row => {
        return this.keyOptions[row.status].label;
      },
      jihuoma: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 激活码表格数据
      codeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vipCode: null,
        status: null,
        activationTime: null,
        activationUserId: null,
        startTime: null,
        endTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({ row }) {
      if (row.status === 2) {
        return "warning-row";
      } else if (row.status === 0) {
        return "success-row";
      }
      return "";
    },
    copyFun(ElementObj) {
      var inputElement = document.createElement("input"); //获取要赋值的input的元素
      inputElement.value = ElementObj; //给input框赋值
      // inputElement.style.display = "none"; //不占位隐藏
      document.body.appendChild(inputElement); //加入BODY
      inputElement.select(); //选中input框的内容
      document.execCommand("Copy"); // 执行浏览器复制命令
      inputElement.remove(); //删除元素
      this.msgSuccess("复制成功");
    },
    /** 查询激活码列表 */
    getList() {
      this.loading = true;
      listCode(this.queryParams).then(response => {
        this.codeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vipCode: null,
        createTime: null,
        updateTime: null,
        status: 0,
        activationTime: null,
        activationUserId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.jihuoma = "获取失败";
      let { data } = await addNewCode();
      this.jihuoma = data;
      this.open = true;
      this.getList();
    },
    /** 作废按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$confirm("是否确认作废激活码: " + row.vipCode, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return updateCode({ id: row.id, status: 2 });
        })
        .then(() => {
          this.getList();
          this.msgSuccess("作废成功");
        });
    }
  }
};
</script>

<style>
.jihuoma {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 30px;
}
.el-table .warning-row {
  background: rgb(228, 213, 187);
}

.el-table .success-row {
  background: #cde6bf;
}
</style>
