<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <div class="flex-item flex-h">
          <el-button size="mini" type="success" @click="redirection('addActivities')">新增活动</el-button>
        </div>
        <ul>
          <li>
            <el-select v-model="CaseId" placeholder="请选择案场">
              <el-option
                key=""
                label="不限案场"
                value="">
              </el-option>
              <el-option
                v-for="item in cases"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId">
              </el-option>
            </el-select>
          </li>
        </ul>
        <el-button
          size="mini"
          type="primary" @click="clickList">搜索</el-button>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="CaseName"
            label="案场"
            width="180">
          </el-table-column>
          <el-table-column
            prop="ActivityName"
            label="活动名称">
          </el-table-column>
          <!-- <el-table-column
            prop="ActivityType"
            label="活动类型">
            <template slot-scope="scope">
              <span>{{getActivitName(scope.row.ActivityType)}}</span>
            </template>
          </el-table-column> -->
          <el-table-column
            prop="giftLabel"
            label="卡券名称">
            <template slot-scope="scope">
              <span>{{getGiftLabel(scope.row.ResourceDesc)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="address"
            label="赠送数量">
            <template slot-scope="scope">
              <span>{{getNum(scope.row.ResourceDesc)}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="Status"
            label="状态">
            <template slot-scope="scope">
              <span>{{scope.row.Status === 1 ? '启用' : '停用'}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="address"
            label="操作"
            width="200">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.Status === 0"
                size="mini"
                type="danger"
                @click="delet(scope.row.ActivityId)">删除</el-button>
              <el-button
                v-if="scope.row.Status === 1"
                size="mini"
                type="warning"
                @click="disable(scope.row.ActivityId)">停用</el-button>
              <el-button
                v-else
                size="mini"
                type="success"
                @click="normal(scope.row.ActivityId)">启用</el-button>
            </template>
          </el-table-column>
        </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage2"
      :page-size="Number(value)"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      options: [{ // 下拉框数数据,分页的每页多少
        value: '5',
        label: '5'
      }, {
        value: '10',
        label: '10'
      }, {
        value: '20',
        label: '20'
      }],
      value: '10', // 一页多少数据
      tableData: [],
      postData: { // 表格搜索条件
        caseid: '', // 案场id
        page: 1, // 当前页码
        pageSize: 10, // 请求数据量
      },
      currentPage2: 1,
      total: 0 // 数据条数
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      clientUrl: x => x.app.clientUrl,
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    getNum (desc) {
      if (desc === '') {
        return 0
      }
      desc = desc.replace(/\n/g, '')
      const descjson = JSON.parse(desc)
      return descjson.giftNum
    },
    getActivitName (desc) { // 根据活动类型编号,获取活动名称
      if (desc === '1' || desc === 'reigste') {
        return '注册'
      }
      return '未知'
    },
    getGiftLabel (desc) {
      if (desc === '' || desc === null) {
        return '未知卡券'
      }
      desc = desc.replace(/\n/g, '')
      return JSON.parse(desc).giftLabel
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.$data.postData.page = val
      this.getList()
    },
    redirection (pathName) { // 重定向
      this.$router.push({ name: pathName })
    },
    getList () { // 获取数据列表
      this.$ajax(this.$api.marketingActivities.getMarketingList.url, {
        method: this.$api.marketingActivities.getMarketingList.method,
        queryData: { ...this.postData }
      }).then(res => {
        this.tableData = res.list
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    disable (ActivityId) { // 停用
      this.$ajax(this.$api.marketingActivities.delMarketingDisable.url, {
        method: this.$api.marketingActivities.delMarketingDisable.method,
        urlData: { activityId: ActivityId }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.getList()
      })
    },
    normal (ActivityId) { // 启用
      this.$ajax(this.$api.marketingActivities.delMarketingNormal.url, {
        method: this.$api.marketingActivities.delMarketingNormal.method,
        urlData: { activityId: ActivityId }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.getList()
      })
    },
    delet (ActivityId) { // 删除
      this.$ajax(this.$api.marketingActivities.delMarketing.url, {
        method: this.$api.marketingActivities.delMarketing.method,
        urlData: { activityId: ActivityId }
      }).then(res => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.getList()
      })
    },
    clickList () { // 点击查询按钮
      this.$data.postData.page = 1
      this.getList()
    }
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
@import "page.scss";
</style>
