<template>
   
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAddShareCode()"
        style="margin-left: 20px"
        >添加</el-button
      >
    </el-card>
    <div class="table-container">
      <el-table
        ref="roleTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="创建人" show-overflow-tooltip align="center">
          <template slot-scope="scope">{{ scope.row.createName }}</template>
        </el-table-column>
        <el-table-column label="创建时间" show-overflow-tooltip align="center">
          <template slot-scope="scope">{{ scope.row.createTime.substring(0,10) }}</template>
        </el-table-column>
        <el-table-column label="邀请码" show-overflow-tooltip align="center">
          <template slot-scope="scope">{{ scope.row.shareCode }}</template>
        </el-table-column>
        <el-table-column label="使用人" show-overflow-tooltip align="center">
          <template slot-scope="scope">{{
            scope.row.member || "未使用"
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="160" fixed="right" align="center">
          <template slot-scope="scope">
            <el-row>
              <!-- <el-button size="mini"
                       type="text"
                       @click="handleUpdateShareCode(scope.$index, scope.row)">
              编辑
            </el-button> -->
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.$index, scope.row)"
                >删除
              </el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container" style="maring-bottom: 30px">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNo"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getShareCodeList,
  createShareCode,
  getShareCode,
  deleteShareCode,
} from "@/api/share";
import { formatDate } from "@/utils/date";
//   import {companycreate,companydelete,companyupdate} from '@/api/permissions'

const defaultListQuery = {
  pageNo: 1,
  pageSize: 10,
};

export default {
  name: "roleList",
  data() {
    return {
      listQuery: Object.assign(
        { compId: parseInt(this.$route.query.id) },
        defaultListQuery
      ),
      list: null,
      total: null,
      listLoading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleAddShareCode() {
      this.$confirm("是否要生成邀请码?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        createShareCode(this.$route.query.id,this.$route.query.compName).then((response) => {
          this.$message({
            type: "success",
            message: "生成成功!",
          });
          this.getList();
        });
      });
    },
    handleDelete(index, row) {
      this.$confirm("是否要删除该邀请码?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // let codeId = [];
        // codeId.push(row.id);
        // let params = new URLSearchParams();
        // params.append("codeId", codeId);
        // console.log("del-params", params);
        // return false;
        deleteShareCode(row.id).then((response) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        });
      });
    },
    getList() {
      this.listLoading = true;
      getShareCodeList(this.listQuery)
        .then((response) => {
          console.log("response", JSON.parse(JSON.stringify(response)));
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        })
        .catch((err) => {
          this.listLoading = false;
          // this.$message({
          //     type: 'info',
          //     message: err
          // });
          console.log(err);
        });
    },
  },
};
</script>
<style>
</style>


