<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleAllDelete"
            >删除</el-button
          >
        </el-col> -->
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        >新增</el-button
      >
    </el-row>
    <el-table ref="tables" v-loading="loading" :data="list">
      <el-table-column
        label="活动名称"
        align="center"
        width="200"
        prop="activityName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="活动描述"
        align="center"
        width="200"
        prop="description"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="开始时间"
        align="center"
        width="200"
        prop="startTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="结束时间"
        align="center"
        width="200"
        prop="endTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加活动" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="活动名称" required>
              <el-input
                v-model="form.activityName"
                type="input"
                placeholder="请输入活动名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="活动描述" required>
              <el-input
                v-model="form.description"
                type="input"
                placeholder="请输入活动描述"
              ></el-input>
            </el-form-item>
            <el-form-item label="活动类型">
              <el-select style="width: 100%;" v-model="form.activityType" placeholder="请选择活动类型">
                <el-option v-for="dict in activityTypeList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="折扣/积分奖励" required>
              <el-input
                v-model="form.activityValue"
                type="number"
                placeholder="请输入折扣/积分奖励"
              ></el-input>
            </el-form-item>
            <el-form-item label="开始时间" required>
                <el-date-picker
                    v-model="form.time"
                    type="datetimerange"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                    </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onAdd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, deleteList, updateList } from "@/api/promotion/index";
import { getDict } from "@/api/common/index";

export default {
  name: "report",
  data() {
    return {
    activityTypeList:[],
      open: false,
      editOpen: false,
      form: {
        time:''
      },
      // 遮罩层
      loading: true,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        ipaddr: undefined,
        userName: undefined,
        status: undefined,
      },
      editForm: {},
    };
  },
  created() {
    this.getList();
    this.onGetDict()
  },
  methods: {

    // 新增
    handleAdd() {
      this.open = true;
    },
    cancel() {
      this.form = {};
      this.open = false;
    },
    onAdd() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
            const params = JSON.parse(JSON.stringify(this.form))
            params.startTime = params.time[0]
            params.endTime = params.time[1]
          add(this.addDateRange({ ...params })).then((response) => {
            this.$modal.msgSuccess("添加成功");
            this.getList();
            this.cancel();
          });
        }
      });
    },

    // 获取数据字典
    onGetDict() {
        getDict(this.addDateRange({type:'member_promotion_type'})).then((response) => {
            this.activityTypeList = response.data
      });
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.list = response.rows;
        // this.total = response.total;
        this.loading = false;
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return deleteList(row.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal
        .confirm("是否确认清空所有登录日志数据项？")
        .then(function () {
          return cleanLogininfor();
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        })
        .catch(() => {});
    },
  },
};
</script>
