<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="'activityUserMapper/add'"
        @click="addStudent"
        >添加学员</el-button
      >
      <save-form
        :inline="true"
        ref="searchForm"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="search">
          <el-input
            class="search"
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="
              (e) => {
                e.target.blur();
                $refs.searchForm.save();
                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="organName"
            label="分部"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="userId"
            label="学员编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="username"
            label="学员姓名"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="phone"
            label="手机号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="付费剩余课时数/总课时数"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.userId">
                {{ scope.row.subCourseNum + "" + "/" + scope.row.totalCourseNum + "" }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="赠送剩余课时数/总课时数"
          >
            <template slot-scope="scope">
              <div v-if="scope.row.userId">
                {{
                  scope.row.subGiveCourseNum +
                  "" +
                  "/" +
                  scope.row.totalGiveCourseNum +
                  ""
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="studentId"
            label="操作"
            width="260px"
          >
            <template slot-scope="scope">
              <div>
                <el-button
                  type="text"
                  v-permission="'activityUserMapper/add'"
                  @click="addCount(scope.row)"
                  >添加资格</el-button
                >
                <el-button
                  type="text"
                  v-permission="'activityUserMapper/cut'"
                  @click="reduceCount(scope.row)"
                  >减少资格</el-button
                >
                <el-button
                  type="text"
                  v-permission="'activityUserMapperAdjustLog/queryPage'"
                  @click="lookInfo(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"
      v-if="addStudentVisible"
    >
      <addStudentSenior
        :organList="organList"
        :hasGive="hasGive"
        ref="addStudentSenior"
        @getList="getList"
        @close="addStudentVisible = false"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="addStudentVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitStudent">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加资格"
      :visible.sync="addSeniorVisible"
      width="800px"
      v-if="addSeniorVisible"
    >
      <addSenior
        :hasGive="hasGive"
        :activeRow="activeRow"
        @getList="getList"
        ref="addSenior"
        @close="addSeniorVisible = false"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="addSeniorVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSeniorSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="减少资格"
      :visible.sync="reduceVisible"
      width="1000px"
      v-if="reduceVisible"
    >
      <reduceSenior
        :activeRow="activeRow"
        :courseType="courseType"
        :giveCourseType="giveCourseType"
        @getList="getList"
      />
      <span slot="footer" class="dialog-footer">
        <!-- <el-button @click="addStudentVisible = false">取 消</el-button> -->
        <el-button type="primary" @click="reduceVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="资格记录"
      :visible.sync="infoVisible"
      width="1000px"
      v-if="infoVisible"
    >
      <seniorInfo :activeRow="activeRow" />
      <!-- <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="infoVisible = 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,
  getActivityUserMapperList,
} from "@/api/vipSeting";
import addSenior from "./modals/addSenior.vue";
import reduceSenior from "./modals/reduceSenior.vue";
import addStudentSenior from "./modals/addStudentSenior.vue";
import seniorInfo from "./modals/seniorInfo";
export default {
  components: {
    pagination,
    addSenior,
    reduceSenior,
    addStudentSenior,
    seniorInfo,
  },
  data() {
    return {
      searchForm: {
        search: null,
        organId: null,
      },
      title: "",
      tableList: [{}],
      organList: [],
      activeRow: null,
      hasGive: false,
      addStudentVisible: false,
      addSeniorVisible: false,
      reduceVisible: false,
      infoVisible: false,
      courseType: "",
      giveCourseType: "",
      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);
        }
      }
      this.getList();
    },
    async getList() {
      try {
        const res = await getActivityUserMapperList({
          ...this.searchForm,
          page: this.rules.page,
          rows: this.rules.limit,
          activityId: this.$route.query.id,
        });
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$refs.searchForm.resetFields();
      this.search();
    },
    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"
      );
      this.courseType = data.courseType;
      this.giveCourseType = data.giveCourseType;
      console.log(this.courseType, this.giveCourseType);
      // selects.branchs
    },
    addCount(row) {
      this.activeRow = row;
      this.addSeniorVisible = true;
    },
    reduceCount(row) {
      this.activeRow = row;
      this.reduceVisible = true;
    },
    addStudent() {
      this.addStudentVisible = true;
    },
    submitStudent() {
      this.$refs.addStudentSenior.submit();
    },
    addSeniorSubmit() {
      this.$refs.addSenior.submit();
    },
    lookInfo(row) {
      this.activeRow = row;
      this.infoVisible = true;
    },
  },
};
</script>
<style lang='scss' scoped>
.m-container {
  .search {
    ::v-deep .el-input__inner {
      width: 200px !important;
    }
  }
}
</style>

