<template>
  <div class="app-container">
    <el-form :inline="true" :model="search" :size="ELsize">
      <el-form-item label="活动名称">
        <el-input
          v-model="search.name"
          placeholder="活动名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="活动状态">
        <el-select v-model="search.activeState" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          :size="ELsize"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table :data="dataList" row-key="Id" border style="width: 100%">
      <el-table-column prop="name" align="center" label="活动名称">
      </el-table-column>
      <el-table-column prop="goodsNum" align="center" label="活动商品">
      </el-table-column>
      <el-table-column align="center" label="活动时间">
        <template slot-scope="scope">
          <div>开始时间： {{ scope.row.startTime }}</div>
          <div>结束时间：{{ scope.row.endTime }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="activeState"
        align="center"
        :formatter="activeState"
        label="活动状态"
      >
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.activeState === 0"
            >未开始</el-tag
          >
          <el-tag type="success" v-else-if="scope.row.activeState == 1"
            >进行中</el-tag
          >
          <el-tag type="danger" v-else>已结束</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
            v-if="scope.row.activeState == 0"
            >修改</el-button
          >
          <el-button type="text" size="small" @click="goProduct(scope.row.id)">
            管理商品</el-button
          >

          <el-button
            type="text"
            size="small"
            class="c-error"
            @click="deleteHandle(scope.row.id)"
            v-if="scope.row.activeState != 1"
            >删除</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"
    />
    <!-- 弹窗, 新增 / 修改 -->
    <GroupBookingAddUpdate
      ref="groupBookingAddUpdateRef"
      @updataList="updataList"
    ></GroupBookingAddUpdate>
  </div>
</template>

<script>
import {
  groupBookingActivityPage_api,
  groupBookingActivityDel_api,
} from "@/api/marketing/groupBooking.js";
import getListApi from "@/mixins/getListApi";
import GroupBookingAddUpdate from "./components/GroupBookingAddUpdate";

export default {
  mixins: [getListApi],
  data() {
    return {
      listURL: groupBookingActivityPage_api,
      showGroupMember: false,
      options: [
        {
          label: "未开始",
          value: 0,
        },
        {
          label: "进行中",
          value: 1,
        },
        {
          label: "已结束",
          value: 2,
        },
      ],
    };
  },
  components: {
    GroupBookingAddUpdate,
  },
  mounted() {},
  methods: {
    getMember(id) {
      this.showGroupMember = true;
      this.$nextTick(() => {
        this.$refs.groupMember.init(id);
      });
    },
    updataList(e) {
      if (e == 1) {
        this.getList();
      }
    },
    // 活动状态
    activeState(row, column) {
      let status = row.activeState;
      if (status === 0) {
        return "未开始";
      } else if (status === 1) {
        return "进行中";
      } else {
        return "已结束";
      }
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.$nextTick(() => {
        this.$refs.groupBookingAddUpdateRef.init(id);
      });
    },
    // 拼团管理商品页
    goProduct(id) {
      this.$router.push({
        path: "/marketing/groupBooking/gbProduct",
        query: { id: id },
      });
    },

    // 删除
    deleteHandle(id) {
      this.$confirm(`确定要删除该条数据?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          groupBookingActivityDel_api(id).then((res) => {
            if (res && res.code == 200) {
              this.$message.success("删除成功");
              this.getList();
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
