<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.composeName" clearable size="mini" class="filter-item"
        style="width: 200px;"
        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-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>
      <br />
      <el-button
        size="mini" type="primary" icon="el-icon-plus"
        @click="$refs.EditModal && $refs.EditModal.handleOpen({ composeId: '' })"
      >
        新建活动
      </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="composeName" label="活动名称" width="120" show-overflow-tooltip />
        <el-table-column prop="content" label="活动内容" align="center" />
        <el-table-column prop="shops" label="参与商家数" align="center" />
        <el-table-column prop="products" label="商品数量" align="center" />
        <el-table-column label="报名时间" align="center" width="140" prop="signTime">
        </el-table-column>
        <el-table-column label="活动起止时间" align="center" width="140" prop="time">
        </el-table-column>
        <el-table-column label="活动状态" align="center" width="120">
          <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 prop="createTime" label="创建时间" width="220" /> -->
        <el-table-column align="center" label="操作" width="280" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
            <el-button v-if="row.state != 4" size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button v-if="row.state != 4" type="danger" size="mini" @click="handleStop(row)">
              结束
            </el-button>
            <el-button v-if="row.state == 4" type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </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>

    <!-- 新增编辑 -->
    <EditModal ref="EditModal" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import { getPlatformComposePage, deletePlatformComposeActivity, updatePlatformComposeStop } from '@/api/active/combinationActivities'

export default {
  name: 'CombinationActivities',
  components: {
    EditModal,
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        composeName: '',
        state: '',
        page: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getPlatformComposePage(this.listQuery)
        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, composeName: '', state: '' }
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleEdit(row) {
      this.$refs.EditModal && this.$refs.EditModal.handleOpen(row)
    },
    handleStop(row) {
      this.$confirm('确定停止此项？')
        .then(async () => {
          await updatePlatformComposeStop({ composeId: row.composeId })
          this.$message({ message: '停止成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    },
    handleDelete(row) {
      this.$confirm('确定删除此项？')
        .then(async () => {
          await deletePlatformComposeActivity({ id: row.composeId })
          this.$message({ message: '删除成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    }
  }
}
</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>
