<template>
  <div class="m-container">
    <h2>
      <el-page-header @back="onCancel" :content="title"></el-page-header>
    </h2>
    <div class="m-core">
      <el-button
        type="primary"
        style="margin-bottom: 20px"
        v-permission="'/vipNewActive'"
        @click="addStudent"
        >添加学员</el-button
      >
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item>
          <el-input
            class="search"
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="学员名称、编号、手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="organId">
          <el-select
            class="multiple"
            filterable
            style="width: 180px !important"
            v-model.trim="searchForm.organId"
            clearable
            placeholder="请选择分部"
          >
            <el-option
              v-for="(item, index) in organList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="studentId"
            label="分部"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="学员编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="学员姓名"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="手机号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="付费课已使用/剩余资格"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="赠课已使用/剩余资格"
          ></el-table-column>
          <el-table-column align="center" prop="studentId" label="操作">
            <template slot-scope="scope">
              <div>
                <el-button
                  type="text"
                  v-permission="'vipGroupActivity/update'"
                  @click="reduceCount(scope.row)"
                  >减少资格</el-button
                >
                <el-button
                  type="text"
                  v-permission="'vipGroupActivity/update'"
                  @click="addCount(scope.row)"
                  >添加资格</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
    <el-dialog
      title="添加学员"
      :visible.sync="addStudentVisible"
      width="1000px"
    >
      <addStudentSenior :organList="organList" :hasGive="hasGive" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="addStudentVisible = false">取 消</el-button>
        <el-button type="primary" @click="addStudentVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog title="添加资格" :visible.sync="addSeniorVisible" width="800px">
      <addSenior :hasGive="hasGive" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="addSeniorVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSeniorVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { getVipGroupActivity } from "@/api/vipSeting";
import addSenior from "./modals/addSenior.vue";
import reduceSenior from "./modals/reduceSenior.vue";
import addStudentSenior from "./modals/addStudentSenior.vue";
export default {
  components: { pagination, addSenior, reduceSenior, addStudentSenior },
  data() {
    return {
      searchForm: {
        search: null,
        organId: null,
      },
      title: "",
      tableList: [{}],
      organList: [],
      hasGive: false,
      addStudentVisible: false,
      addSeniorVisible: false,
      reduceVisible:false,
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  async mounted() {
    // 获取分部
    // 获取分部
    await this.$store.dispatch("setBranchs");
    this.init();
  },
  methods: {
    async init() {
      if (this.$route.query.id) {
        // this.baseForm.id = this.$route.query.id;
        try {
          const rusult = await getVipGroupActivity({
            id: this.$route.query.id,
          });
          this.formatDetail(rusult.data);
        } catch (e) {
          console.log(e);
        }
      }
    },
    getList() {},
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {},
    onCancel() {
      this.$store.dispatch("delVisitedViews", this.$route);
      this.$router.push({
        path: "/vipActiveManager/vipActiveList",
        query: { rules: this.rules, searchForm: this.searchForm },
      });
    },
    formatDetail(data) {
      this.title = data.name;
      let organIds = data.organId.split(",").map((organ) => {
        return Number(organ);
      });
      this.organList = [];
      this.selects.branchs.forEach((organ) => {
        if (organIds.indexOf(organ.id) != -1) {
          this.organList.push(organ);
        }
      });
      this.hasGive = Boolean(
        data.giveCourseType && data.giveCourseType != "MEMBER"
      );
      console.log(this.hasGive);
      // selects.branchs
    },
    addCount() {
      this.addSeniorVisible = true;
    },
    reduceCount() {
      this.reduceVisible = true
    },
    addStudent() {
      this.addStudentVisible = true;
    },
  },
};
</script>
<style lang='scss' scoped>
.m-container {
  .search {
    /deep/.el-input__inner {
      width: 200px !important;
    }
  }
}
</style>

