a
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="活动名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动名称"
          clearable
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item label="活动状态" prop="online">
        <el-select
          v-model="queryParams.online"
          placeholder="请选择活动状态"
          style="width: 180px"
        >
          <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>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="活动名称" align="center" prop="title" />
      <el-table-column
        label="活动介绍"
        align="center"
        prop="intro"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="活动详情"
        align="center"
        prop="content"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column label="活动海报" align="center" prop="img">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.img"
            style="width: 60px; height: 60px"
            :src="scope.row.img"
            :preview-src-list="[scope.row.img]"
          >
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="浏览量" align="center" prop="hits" />

      <el-table-column label="开始时间" align="center" prop="startTime">
        <template slot-scope="scope">
          {{
            scope.row.startTime
              ? $moment(scope.row.startTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
          }}
        </template>
      </el-table-column>

      <el-table-column label="结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          {{
            scope.row.endTime
              ? $moment(scope.row.endTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
          }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="使用代金卷" align="center" key="useVoucher">
        <template slot-scope="scope">
          {{ scope.row.useVoucher == 0 ? "否" : "是" }}
        </template>
      </el-table-column> -->

      <el-table-column label="活动状态" align="center" key="online" width="80">
        <template slot-scope="scope">
          {{
            scope.row.online == 0 ? "未开始" : scope.row.online == 1 ? "进行中" : "已结束"
          }}
        </template>
      </el-table-column>
      <el-table-column label="是否参与" align="center">
        <template slot-scope="scope">
          <el-tag>
            {{ scope.row.isJoin == 0 ? "未参与" : scope.row.isJoin == 1 ? "已参与" : "" }}
          </el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark"> </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          {{
            scope.row.createTime
              ? $moment(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
          }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isJoin == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleParticipate(scope.row)"
            v-hasPermi="['active:add']"
            >参与活动</el-button
          >
          <el-button
            v-if="scope.row.isJoin == 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="ParticipateEdit(scope.row)"
            v-hasPermi="['active:edit']"
            >编辑商品</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['active:detail']"
            >活动详情</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"
    />

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog title="详情" :visible.sync="detailOpen" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="活动名称" prop="title">
              {{ form.title }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动介绍" prop="intro">
              {{ form.intro }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动详情" prop="content">
              {{ form.content }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动时间" prop="dateRange">
              {{
                form.startTime
                  ? $moment(form.startTime).format("YYYY-MM-DD HH:mm:ss")
                  : ""
              }}-
              {{
                form.endTime ? $moment(form.endTime).format("YYYY-MM-DD HH:mm:ss") : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动海报">
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <!-- 添加或修改用户配置对话框 -->
    <el-dialog title="参与活动" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="适用商品" prop="title">
              <el-radio v-model="shop.type" :label="0">全部商品</el-radio>
              <el-radio v-model="shop.type" :label="1">指定商品</el-radio>
              <span v-if="shop.type == 1" style="color: #134dd1b3; cursor: pointer"
                >选择商品</span
              >
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="shop.type == 1">
            <el-table :data="shopList">
              <el-table-column label="商品名称" align="center" prop="title" />
              <el-table-column label="商品图片" align="center" prop="title" />
              <el-table-column label="商品单价" align="center" prop="title" />
              <el-table-column label="库存" align="center" prop="title" />
              <el-table-column label="起批量" align="center" prop="title" />
              <el-table-column label="商品价格" align="center" prop="title" />
              <el-table-column label="操作" align="center" prop="title" />
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { promotionList, promotionSku } from "@/api/active/index";
import moment from "moment";
export default {
  name: "Config",
  //   dicts: ["sys_yes_no"],
  data() {
    return {
      // 遮罩层
      loading: true,
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        online: "",
        title: "",
      },
      // 表单参数
      form: {},
      shop: {
        type: 0,
        promotionId: "",
      },
      // 表单校验
      rules: {
        configName: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
        configKey: [{ required: true, message: "参数键名不能为空", trigger: "blur" }],
        configValue: [{ required: true, message: "参数键值不能为空", trigger: "blur" }],
      },
      detailOpen: false,
      imageUrl: "",
      open: false,
      shopList: [],
      ParticipateId: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    ParticipateEdit(record) {
      this.ParticipateId = record.id;
      this.open = true;
    },
    handleParticipate(record) {
      this.ParticipateId = record.id;
      this.open = true;
    },
    handleDetail(record) {
      this.form = record;
      this.imageUrl = record.img;
      this.form.dateRange = [
        moment(record.startTime).format("yyyy-MM-DD hh:mm:ss"),
        moment(record.endTime).format("yyyy-MM-DD hh:mm:ss"),
      ];
      this.detailOpen = true;
    },

    /** 查询参数列表 */
    getList() {
      this.loading = true;
      promotionList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.configList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.shop.promotionId = this.ParticipateId;
          promotionSku(this.shop).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "参与成功",
              });
              this.open = false;
              this.getList();
            }
          });
        }
      });
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
</style>
<style scoped lang="scss">
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>
