<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="输入活动ID/活动名称搜索"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="活动时间" prop="times">
        <el-date-picker
          v-model="queryParams.times"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="redChange"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动状态" prop="activityStatus">
        <el-select
          v-model="queryParams.activityStatus"
          placeholder="请选择活动状态"
          style="width: 180px"
        >
          <el-option label="全部" value=""> </el-option>
          <el-option label="未开始" value="0"> </el-option>
          <el-option label="进行中" value="1"> </el-option>
          <el-option label="已结束" value="2"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div style="margin: 10px 0px">
      <el-button type="primary" size="mini" @click="hanleAdd">添加</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="title" />
      <el-table-column label="参与活动资格" align="center">
        <template slot-scope="scope">
          {{
            scope.row.joinType == 0
              ? "所有人"
              : scope.row.joinType == 1
              ? "固定时间下单"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="活动时间" align="center">
        <template slot-scope="scope">
          {{
            scope.row.startTime
              ? $moment(scope.row.startTime).format("YYYY-MM-DD") +
                "-" +
                $moment(scope.row.endTime).format("YYYY-MM-DD")
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          {{
            scope.row.activityStatus == 0
              ? "未开始"
              : scope.row.activityStatus == 1
              ? "进行中"
              : scope.row.activityStatus == 2
              ? "已结束"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLook(scope.row)"
            >查看</el-button
          >
          <el-button size="mini" type="text" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            style="color: red"
            @click="handleRemove(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import moment from "moment";
import { queryPage, storeRaffleRemove } from "@/api/marketing/lottery";
export default {
  name: "Config",
  data() {
    return {
      loading: true,
      total: 0,
      configList: [],
      title: "",
      open: false,
      queryParams: {
        key: "",
        times: [],
        endTime: "",
        startTime: "",
        activityStatus: "",
        pageNo: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      drawer: false,
      userid: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    redChange(val) {
      this.queryParams.startTime = moment(val[0]).valueOf();
      this.queryParams.endTime = moment(val[1]).valueOf();
    },
    hanleAdd() {
      this.$router.push({
        path: "/marketing/marketing/lottery/new/index",
      });
    },

    handleRemove(record) {
      this.$confirm("是否确认删除该奖励活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          storeRaffleRemove(record.id).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleEdit(record) {
      this.$router.push({
        path: "/marketing/marketing/lottery/new/edit",
        query: {
          id: record.id,
        },
      });
    },
    handleLook(record) {
      this.$router.push({
        path: "/marketing/marketing/lottery/new/detail",
        query: {
          id: record.id,
        },
      });
    },

    /** 查询参数列表 */
    getList() {
      this.loading = true;
      queryPage(this.queryParams).then((response) => {
        this.configList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.endTime = "";
      this.queryParams.startTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style scoped lang="scss"></style>
