<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
      <el-form-item label="会员卡名称:" prop="cardName" >
        <el-input
          v-model="queryParams.cardName"
          placeholder="请输入会员卡名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开售:" prop="sale">
        <el-switch
          v-model="queryParams.sale"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item style="float: right">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['sc:course:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新设会员卡
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['sc:course:update']"
          type="primary"
          icon="el-icon-edit-outline"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改会员卡
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['sc:course:delete']"
          v-loading="loadingChange"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除会员卡
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" :border="true" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="cardName" width="120" align="center" label="会员卡名称" />
      <el-table-column prop="courseTypes" width="100" header-align="center" label="课程类别" >
        <template slot-scope="scope" >
          {{scope.row.courseTypesName}}
        </template>
      </el-table-column>
      <el-table-column prop="restrictedCourses" width="180" header-align="center"  label="限制课程">
        <template slot-scope="scope" >
          <div v-if="scope.row.restrictedCourses=='1'">
            全部允许<br>
          </div>
          <div v-if="scope.row.restrictedCourses=='2'">
            <div>
              <ul>
                <li v-for="item in scope.row.courses" :key="item.id">
                  <span style="font-weight: bold">{{ item.courseType }}</span>
                  - {{item.courseNames}}
                </li>
              </ul>
            </div>

          </div>
        </template>
      </el-table-column>
      <el-table-column prop="restrictedTeacher" width="150" header-align="center" label="限制教练" >
        <template slot-scope="scope" >
          {{scope.row.restrictedTeacher=='1'?'全部允许':scope.row.teachersName}}
        </template>
      </el-table-column>
      <el-table-column prop="restrictedNum" align="center" width="150" label="次数限制" >
        <template slot-scope="scope" >
          {{ scope.row.restrictedNum=='1'?'不限次数':'限制次数'}}<br>
          <div v-if="scope.row.restrictedNum=='2'">
            {{scope.row.bookTime}}内只能使用{{scope.row.bookNum}}次
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="totalFee" width="190" header-align="center"  label="每节课扣次或费用">
        <template slot-scope="scope" >
          <div v-if="scope.row.chargeType=='default'">
<!--            默认<br>-->
            扣{{scope.row.defaultNum}}次，或扣{{scope.row.defaultFee}}元
          </div>
          <div v-if="scope.row.chargeType=='customize'">
            <div>
              <ul>
                <li v-for="item in scope.row.feeModeHourList" :key="item.courseTypeId">
                  <span style="font-weight: bold">{{ item.courseType }}</span>
                  - 扣{{item.cnt}}次，或{{item.totalFee}}元
                </li>
              </ul>
            </div>

          </div>
        </template>
      </el-table-column>
      <el-table-column prop="count" width="180" header-align="center" label="总天数/次数/时长" >
        <template slot-scope="scope" >
          总天数/有效期：{{ scope.row.days?scope.row.days+'天':'未定义'}}<br>
          课程总次数：{{ scope.row.count?scope.row.count+'次':'未定义'}}<br>
          应付金额：{{ scope.row.totalFee?scope.row.totalFee+'元':'未定义'}}
        </template>
      </el-table-column>
<!--      :show-overflow-tooltip="true"-->
      <el-table-column prop="description" width="120" header-align="center" label="会员卡简介" :show-overflow-tooltip="true" />
      <el-table-column prop="conditions" width="120" header-align="center" label="会员卡条款" :show-overflow-tooltip="true"/>
      <el-table-column prop="createTime" width="100" align="center" label="创建时间">
        <template slot-scope="scope">{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</template>
      </el-table-column>
      <el-table-column prop="sale" align="center" label="开售">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.sale"
            active-value="1"
            inactive-value="0"
            @change="handleSaleChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column width="100" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['sc:course:update']"
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleUpdate(scope.row)"
          >变更信息
          </el-button>
          <el-button
            v-hasPermi="['sc:course:delete']"
            v-loading="loadingChange"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除会员卡
          </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"
    />

    <add-member-cart-types ref="addCourseComponents" @success="getList" />
   </div>
</template>

<script>
import {
  listMemberCard,
  getMemberCard,
  delMemberCard,
  editSale
} from '@/api/school/sc/memberCardType'
import addMemberCartTypes from '@/components/sc/memberCardTypes/addMemberCardTypes.vue'

export default {
  name: 'Course',
  components: {
    addMemberCartTypes
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingChange: false,
      loadingSelect: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格树数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardName: undefined,
        sale: '1'
      },
      // 会员卡类型
      courseTypeOptions: [],

      // 开课校区
      campusOptions: [],
      // 收费模式
      chargeTypeOptions: [{
        chargeType: 'hour',
        chargeTypeName: '按次数'
      }, {
        chargeType: 'date',
        chargeTypeName: '按时间'
      }, {
        chargeType: 'cycle',
        chargeTypeName: '按周期'
      }],
      notifyInstance: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listMemberCard(this.queryParams).then(response => {
        this.dataList = response.data.rows
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.$refs.addCourseComponents.init()
      this.$refs.addCourseComponents.reset()
      this.$refs.addCourseComponents.open = true
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cardTypeId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getMemberCard(row.cardTypeId || this.ids).then(response => {
        this.$refs.addCourseComponents.init()
        this.$refs.addCourseComponents.form = response.data
        this.$refs.addCourseComponents.open = true
        this.$refs.addCourseComponents.title = '修改会员卡信息'
        this.$refs.addCourseComponents.initFeeModeDate(response.data)
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const that = this
      const id = row.cardTypeId || this.ids
      this.$confirm('是否确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loadingChange = true
        return delMemberCard(id)
      }).then((response) => {
        that.loadingChange = false
        if (response.respCode === '0000') {
          this.getList()
          this.msgSuccess('删除成功')
        } else {
          this.msgError(response.respMsg)
        }
      }).catch(function() {
        that.loadingChange = false
      })
    },
    chargeNameByCodes(chargeNames) {
      if (chargeNames) {
        const chargeNameArray = []
        chargeNames.split(',').forEach((value, index, array) => {
          if (value === 'hour') {
            chargeNameArray.push('按次数')
          } else if (value === 'date') {
            chargeNameArray.push('按时间')
          } else if (value === 'cycle') {
            chargeNameArray.push('按周期')
          }
        })
        return chargeNameArray.toString()
      } else {
        return '-'
      }
    },
    // 用户状态修改
    handleSaleChange(row) {
      const text = row.sale === '1' ? '开售' : '停售'
      this.$confirm('确认要"' + text + '""' + row.cardName + '"会员卡吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return editSale({cardTypeId:row.cardTypeId, sale:row.sale})
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.sale = row.sale === '0' ? '1' : '0'
      })
    }
  }
}
</script>
