<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="activeList">
      <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="bgImg">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.bgImg"
            style="width: 60px; height: 60px"
            :src="scope.row.bgImg"
            :preview-src-list="[scope.row.bgImg]"
          >
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="活动标签" align="center" prop="bgImg">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.tag > 0">{{
            scope.row.tag == 1
              ? "年货节"
              : scope.row.tag == 9
              ? "清库专区"
              : scope.row.tag == 8
              ? "集采预售"
              : scope.row.tag == 7
              ? "团批秒杀"
              : scope.row.tag == 6
              ? "新店福利"
              : ""
          }}</el-tag>
        </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" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isJoin == 0"
            size="mini"
            type="text"
            @click="handleParticipate(scope.row)"
            v-hasPermi="['active:add']"
            >参与活动</el-button
          >
          <el-button
            v-if="scope.row.isJoin == 1"
            size="mini"
            type="text"
            @click="ParticipateEdit(scope.row)"
            v-hasPermi="['active:edit']"
            >编辑商品</el-button
          >
          <el-button
            size="mini"
            type="text"
            @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.pageNo"
      :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="120px">
        <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="content">
              <el-tag type="success" v-if="form.tag > 0">{{
                form.tag == 1
                  ? "年货节"
                  : form.tag == 2
                  ? "6.18"
                  : form.tag == 3
                  ? "国庆大促"
                  : form.tag == 4
                  ? "双十一"
                  : form.tag == 5
                  ? "双十二"
                  : ""
              }}</el-tag>
            </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")
                  : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动结束时间" prop="dateRange">
              {{
                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="form.img" :src="form.img" class="avatar" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动背景图">
              <img v-if="form.bgImg" :src="form.bgImg" 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="1200px"
      append-to-body
      :destroy-on-close="true"
      @close="open = false"
    >
      <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"
                @click="selectSpu"
                >选择商品</span
              >
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <div style="margin-bottom: 10px; font-size: 18px">已选择商品</div>
            <el-table :data="shopList">
              <el-table-column label="商品名称" align="center" prop="title" />
              <el-table-column label="商品图片" align="center">
                <template slot-scope="scope">
                  <el-image
                    v-if="scope.row.pic"
                    style="width: 60px; height: 60px"
                    :src="scope.row.pic"
                    :preview-src-list="[scope.row.pic]"
                  >
                  </el-image>
                </template>
              </el-table-column>
              <el-table-column label="商品价格" align="center">
                <template slot-scope="scope">
                  {{ scope.row.salePrice }}元/ {{ scope.row.unit }}
                </template>
              </el-table-column>
              <el-table-column label="库存" align="center" prop="title">
                <template slot-scope="scope">
                  {{ scope.row.totalStocks }}/ {{ scope.row.unit }}
                </template>
              </el-table-column>
              <el-table-column label="起批量" align="center">
                <template slot-scope="scope">
                  {{ scope.row.lowestPurchase }}{{ scope.row.unit }}
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    style="color: red"
                    type="text"
                    @click="shopRemove(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="shopQuery.total > 0"
              :total="shopQuery.total"
              :page.sync="shopQuery.pageNo"
              :limit.sync="shopQuery.pageSize"
              @pagination="getShopList"
            />
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
      title="选择商品"
      :visible.sync="shopOpen"
      direction="rtl"
      :before-close="shopOpen == false"
      size="50%"
    >
      <productList @multipleTable="multipleTable" ref="product"></productList>
    </el-drawer>
  </div>
</template>

<script>
import { promotionList, promotionSku, spuList, spuRemove } from "@/api/active/index";
import productList from "../product/list/allProduct.vue";
import moment from "moment";
export default {
  components: { productList },
  name: "Config",
  //   dicts: ["sys_yes_no"],
  data() {
    return {
      // 遮罩层
      loading: true,
      total: 0,
      // 弹出层标题
      title: "",
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        online: "",
        title: "",
      },
      // 表单参数
      form: {},
      shop: {
        type: 0,
        promotionId: "",
        skuList: [],
      },
      // 表单校验
      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: [],
      activeList: [],
      ParticipateId: "",
      shopQuery: {
        pageSize: 5,
        pageNo: 1,
        title: "",
        total: 0,
      },
      shopOpen: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selectSpu() {
      this.shopOpen = true;
      this.$nextTick(() => {
        console.log(this.$refs.product);
        this.$refs.product.handRef("active", 0);
      });
    },
    multipleTable(val) {
      let list = [];
      for (let index = 0; index < val.length; index++) {
        if (val[index].scope == 3) {
          this.$message.error("同城秒送商品不能添加到活动！");
          return;
        }
        list.push({
          goodsInfo: val[index].goodsInfo,
          spuId: val[index].id,
        });
      }
      this.shop.skuList = list;
      this.shop.promotionId = this.ParticipateId;
      promotionSku(this.shop).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "添加成功",
          });
          this.shopOpen = false;
          this.getShopList();
        }
      });
      this.shopOpen = false;
    },
    shopRemove(record) {
      this.$confirm("是否把此商品移除该活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          spuRemove(record.id).then((res) => {
            console.log("12312", res);
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "移除成功",
              });
              this.getShopList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    ParticipateEdit(record) {
      this.ParticipateId = record.id;
      this.getShopList();
    },
    getShopList() {
      this.shopQuery.promotionId = this.ParticipateId;
      spuList(this.shopQuery).then((response) => {
        this.shopList = response.data.records;
        this.shopQuery.total = response.data.total;
        this.loading = false;
        this.open = true;
      });
    },

    handleParticipate(record) {
      this.ParticipateId = record.id;

      this.getShopList();
    },
    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.activeList = 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 () {
      // if (this.shop.skuList.length < 1) {
      this.$message({
        type: "success",
        message: "保存成功",
      });
      this.open = false;
      // } else {
      //   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>
