<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="输入主题名称关键字搜索"
          clearable
          style="width: 240px"
        @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="活动时间" prop="danages">
        <el-date-picker
          v-model="queryParams.danages"
          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>
        <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" prop="discountRule">
        <template slot-scope="scope">
          满{{ scope.row.fullAmount }}元,减{{ scope.row.returnAmount }}元
        </template>
      </el-table-column>
      <el-table-column label="活动时间" align="center">
        <template slot-scope="scope">
          {{
            scope.row.orderStartTime
              ? $moment(scope.row.orderStartTime).format("YYYY-MM-DD")
              : ""
          }}
          --
          {{
            scope.row.orderEndTime
              ? $moment(scope.row.orderEndTime).format("YYYY-MM-DD")
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="下单商品价格" align="center" prop="spuLength" />

      <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"
            style="color: red"
            type="text"
            @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, activityRemove } from "@/api/marketing/activity";
export default {
  data() {
    return {
      loading: true,
      total: 0,
      configList: [],
      title: "",
      open: false,
      queryParams: {
        key: "",
        danages: [],
        pageNo: 1,
        pageSize: 10,
        orderEndTime: "",
        orderStartTime: "",
        publishType: 0,
        type: 2,
      },
      // 表单参数
      form: {},
      // 表单校验
      drawer: false,
      userid: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    redChange(val) {
      this.queryParams.orderStartTime = moment(val[0]).valueOf();
      this.queryParams.orderEndTime = moment(val[1]).valueOf();
    },

    hanleAdd() {
      this.$router.push({
        path: "/marketing/activity/add",
      });
    },

    handleLook(record) {
      this.$router.push({
        path: "/marketing/activity/detail",
        query: {
          id: record.id,
        },
      });
    },
    handleEdit(record) {
      this.$router.push({
        path: "/marketing/activity/edit",
        query: {
          id: record.id,
        },
      });
    },
    handleRemove(record) {
      this.$confirm("是否确认删除满减活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          activityRemove({ removeIds: record.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    /** 查询参数列表 */
    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.danages = [];
      this.queryParams.orderStartTime = "";
      this.queryParams.orderEndTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style scoped lang="scss"></style>
