<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <span class="filter-item">
        商家类型：
        <span v-if="storeDetails.platformShopCheck.shopType === 1">商城</span>
        <span v-else-if="storeDetails.platformShopCheck.shopType === 2">商圈</span>
        <span v-else>--</span>
      </span>
      <span class="filter-item" style="margin-left:10px;">
        所属区域ID：{{ storeDetails.platformShopCheck.areaId || '--' }}
        <span v-if="storeDetails.shopAdress">（{{ storeDetails.shopAdress }}）</span>
      </span>
      <el-radio-group
        v-model="listQuery.type" class="filter-item" style="margin-left:10px;"
        size="mini" @input="handleSearch"
      >
        <el-radio-button :label="1">全部</el-radio-button>
        <el-radio-button :label="2">我参与的</el-radio-button>
      </el-radio-group>
      <el-radio-group
        v-model="listQuery.signType" class="filter-item" style="margin-left:10px;" size="mini"
        @input="handleSearch"
      >
        <el-radio-button :label="1">平台优惠券</el-radio-button>
        <el-radio-button :label="2">平台秒杀</el-radio-button>
        <el-radio-button :label="3">平台限时折扣</el-radio-button>
        <el-radio-button :label="5">消费金活动</el-radio-button>
        <el-radio-button :label="6">组合活动</el-radio-button>
      </el-radio-group>
      <el-input
        v-model="listQuery.activityName" clearable size="mini" class="filter-item"
        maxlength="15"
        style="width: 200px;margin-left:10px;" placeholder="请输入活动名称"
      />
      <el-select
        v-model="listQuery.state" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请选择活动状态"
      >
        <el-option label="报名未开始" value="0" />
        <el-option label="报名进行中" value="1" />
        <el-option label="活动待开始" value="2" />
        <el-option label="活动进行中" value="3" />
        <el-option label="活动已结束" value="4" />
      </el-select>
      <el-select
        v-model="listQuery.examineState" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请选择审核状态"
      >
        <el-option label="待审核" value="0" />
        <el-option label="报名成功" value="1" />
        <el-option label="报名失败" value="2" />
      </el-select>
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <el-button size="mini" type="info" class="filter-item" @click="handleReset">
        重置
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column prop="activityName" label="活动名称" show-overflow-tooltip />
        <el-table-column prop="bondMoney" label="活动保证金" show-overflow-tooltip />
        <el-table-column prop="signTime" label="报名时间" width="170" />
        <el-table-column prop="activityTime" label="活动时间" width="170" />
        <el-table-column label="活动优惠" show-overflow-tooltip>
          <template slot-scope="scope">
            <div v-if="scope.row.discountProgramme == 1">满减</div>
            <div v-if="scope.row.discountProgramme == 2">优惠券</div>
            <div>{{ scope.row.details }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="activityIntroduce" label="活动简介" show-overflow-tooltip />
        <el-table-column label="活动状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.state == 0">报名未开始</span>
            <span v-if="scope.row.state == 1">报名进行中</span>
            <span v-if="scope.row.state == 2">活动待开始</span>
            <span v-if="scope.row.state == 3">活动进行中</span>
            <span v-if="scope.row.state == 4">活动已结束</span>
          </template>
        </el-table-column>
        <el-table-column label="报名状态" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.signState == 0">待审核</span>
            <span v-if="scope.row.signState == 1">报名成功</span>
            <span v-if="scope.row.signState == 2">报名失败</span>
            <span v-if="scope.row.signState == null">未报名</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="260" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <div v-if="!listLoading">
              <el-button
                v-if="row.operation === 1" type="primary" size="mini"
                @click="$refs.SignActivity && $refs.SignActivity.handleOpen(row)"
              >
                立即报名
              </el-button>
              <el-button
                v-if="row.operation === 2" type="success" size="mini" disabled
              >
                立即报名
              </el-button>
              <el-button
                v-if="row.operation === 3" type="warning" size="mini"
                @click="$refs.ActivityDetail && $refs.ActivityDetail.handleOpen(row)"
              >
                活动数据
              </el-button>
              <el-button
                v-if="row.operation === 4" type="primary" size="mini"
                @click="$refs.SignActivity && $refs.SignActivity.handleOpen(row)"
              >
                重新报名
              </el-button>
              <el-button
                v-if="row.operation === 5" type="primary" size="mini"
                @click="$refs.SignActivity && $refs.SignActivity.handleOpen(row)"
              >
                继续报名
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 活动报名 -->
    <SignActivity ref="SignActivity" @success="getList" />
    <!-- 查看活动详情 -->
    <ActivityDetail ref="ActivityDetail" @success="getList" />
  </div>
</template>

<script>
import SignActivity from './components/SignActivity'
import ActivityDetail from './components/ActivityDetail'

import { shopSysGetById } from '@/api/shopSys'
import { activityGetAll } from '@/api/active'
export default {
  name: 'Active',
  components: {
    SignActivity,
    ActivityDetail
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 10,
        type: 1, // 查询类型 1-全部 2-我参与的
        activityName: '', // 活动名称
        state: '',
        examineState: '',
        signType: 1,
        regionId: ''
      },
      storeDetails: {
        platformShopCheck: {}
      }
    }
  },
  async created() {
    const res = await shopSysGetById({})
    this.storeDetails = { ...res.data, platformShopCheck: res.data.platformShopCheck || { areaId: '', shopType: '' } }
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await activityGetAll({ ...this.listQuery, regionId: this.storeDetails.platformShopCheck.shopType === 1 ? null : this.storeDetails.platformShopCheck.areaId })
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = { page: 1, pageSize: 10, type: 1, activityName: '', state: '', examineState: '', signType: 1 }
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
