<template>
  <div style="padding: 20px">
    <div>
      <div class="header">
        <h3>分享奖励</h3>
        <el-button type="primary" size="small" @click="addInfo">添加</el-button>
      </div>

      <span style="color: orange"
        >*选择店铺商品,设置分享奖励比例,已在发布时设置分享奖励商品,无法在此设置，可取商品管理编辑</span
      >
    </div>
    <div>
      <el-row
        style="
          margin-top: 20px;
          display: flex;
          flex-direction: row;
          flex-wrap: wrap;
          min-height: 250px;
        "
      >
        <el-col :span="12" v-for="(item, index) in list" :key="index">
          <el-card class="box-card" style="width: 90%">
            <div
              style="
                display: flex;
                flex-direction: row;
                align-items: center;
                justify-content: space-around;
              "
            >
              {{ item.shareName }}
              <span>比例:{{ item.proportion }}%</span>
              <span>已选择商品:{{ item.shareSpuNum }}个</span>
              <div>
                <el-button type="text" @click="edit(item)">编辑</el-button>
                <el-button type="text" @click="remove(item)" style="color: red"
                  >删除</el-button
                >
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { shareRewardList, shareRewardDelete } from "@/api/manage/share";
export default {
  data() {
    return {
      list: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      shareRewardList().then((res) => {
        if (res.code == 200) {
          this.list = res.data;
        }
      });
    },
    edit(item) {
      let index = this.list.length;
      this.$router.replace({ path: "/store/manage/shareReward", query: { index, item } });
    },
    remove(item) {
      this.$confirm("是否确认删除该分享规则", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          shareRewardDelete(item).then((res) => {
            if (res.code == 200) {
              this.$message.success("删除成功！");
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addInfo() {
      let index = this.list.length;
      this.$router.replace({ path: "/store/manage/shareReward", query: { index } });
    },
  },
};
</script>

<style scoped>
.header {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}
</style>
