<template>
  <div class='m-container'>
    <h2>
      <div class="squrt"></div>VIP分部活动列表
    </h2>
    <div class="m-core">
      <div @click="openActive('create')"
           v-permission="'/branchActiveOperationAdd'"
           class='newBand'>新建</div>
      <!-- 搜索类型 -->
      <el-form :inline="true"
               class="searchForm"
               @submit.native.prevent
               v-model="searchForm">
        <el-form-item>
          <el-input type="text"
                    v-model="searchForm.search"
                    @keyup.enter.native='getList'
                    placeholder="分部活动名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.rewardMode"
                     clearable
                     filterable
                     placeholder="分部活动类型">
            <el-option label="累计奖励"
                       value="PER"></el-option>
            <el-option label="阶梯奖励"
                       value="STAIR"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.teacherId"
                     clearable
                     filterable
                     placeholder="适用老师">
            <el-option v-for="item in teacherList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchForm.vipGroupCategoryId"
                     filterable
                     clearable
                     placeholder="课程类型">
            <el-option v-for="item in vipGroupCategoryList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getList"
                     type="danger">搜索</el-button>
        </el-form-item>
      </el-form>
      <!-- 列表 -->
      <div class="tableWrap">
        <el-table :data='tableList'
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column align='center'
                           prop='name'
                           label="分部活动名">
          </el-table-column>
          <el-table-column align='center'
                           prop='rewardMode'
                           label="分部活动类型">
            <template slot-scope="scope">
              {{ scope.row.rewardMode == 'PER' ? '累计奖励' : '阶梯奖励' }}
            </template>
          </el-table-column>
          <el-table-column align='center'
                           prop='teacherNames'
                           label="适用老师">
          </el-table-column>
          <el-table-column align='center'
                           prop='vipGroupCategoryNameList'
                           label="课程类型">
          </el-table-column>
          <el-table-column align='center'
                           label="操作"
                           width='100'>
            <template slot-scope="scope">
              <el-button @click="openActive('update', scope.row)"
                         v-permission="'/branchActiveOperationUpdate'"
                         type="text">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="pageInfo.total"
                    :page.sync="pageInfo.page"
                    :limit.sync="pageInfo.limit"
                    :page-sizes="pageInfo.page_size"
                    @pagination="getList" />
      </div>
    </div>

  </div>
</template>
<script>
import pagination from '@/components/Pagination/index'
import { courseScheduleRewards } from '@/api/systemManage'
import { vipGroupCategory } from "@/api/vipSeting"
import { getTeacher } from '@/api/buildTeam'
import store from '@/store'
export default {
  name: 'branchActive',
  components: { pagination },
  data () {
    return {
      searchForm: {
        search: null,
        rewardMode: null,
        vipGroupCategoryId: null,
        teacherId: null,
      },
      organId: null,
      searchLsit: [],
      tableList: [],
      vipGroupCategoryList: [],
      teacherList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },

    }
  },
  created () {
    if (this.$route.query.searchForm) {
      this.$route.query.searchForm instanceof Object ? this.searchForm = this.$route.query.searchForm : this.searchForm = JSON.parse(this.$route.query.searchForm);
    }
    if (this.$route.query.rules) {
      this.$route.query.rules instanceof Object ? this.pageInfo = this.$route.query.rules : this.pageInfo = JSON.parse(this.$route.query.rules);
    }

  },
  mounted () {
    this.getList() // 获取列表数据
    this.__init()
  },
  methods: {
    async __init () {
      let vipGroupCategoryList = await vipGroupCategory({ page: 1, rows: 9999 })
      if (vipGroupCategoryList.code == 200) {
        vipGroupCategoryList.data.forEach(item => {
          this.vipGroupCategoryList.push({
            label: item.name,
            value: item.id
          })
        })
      }

      let teacherList = await getTeacher()
      if (teacherList.code == 200) {
        teacherList.data.forEach(item => {
          this.teacherList.push({
            label: item.realName,
            value: item.id
          })
        })
      }
    },
    getList () {
      let params = this.searchForm
      params.rows = this.pageInfo.limit
      params.page = this.pageInfo.page
      courseScheduleRewards(params).then(res => {
        if (res.code == 200 && res.data) {
          this.tableList = res.data.rows
          this.pageInfo.total = res.data.total
        }
      })
    },
    openActive (type, row) {
      let pageInfo = JSON.stringify(this.pageInfo)
      let searchForm = JSON.stringify(this.searchForm)
      let params = {
        path: '/vipClassSet/branchActiveOperation',
        query: {
          type: type,
          searchForm,
          pageInfo
        }
      }
      if (row) {
        params.query.id = row.id
      }
      this.$router.push(params)
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      this.$refs[formName].clearValidate()
    }
  }
}
</script>
<style lang="scss">
.el-button--primary {
  background: #14928a;
  border-color: #14928a;
  color: #fff;
  &:hover,
  &:active,
  &:focus {
    background: #14928a;
    border-color: #14928a;
    color: #fff;
  }
}
.el-vue-search-box-container {
  position: absolute !important;
  left: 10px;
  margin-top: 10px;
  z-index: 99999 !important;
}
</style>