<template>
  <div>
    <div class="prize" v-for="(item, index) in awardsList" :key="index">
        <div class="prize-top">
          <div>奖级名称：{{ item.name }}</div>
          <div>奖级：{{ item.sort }}</div>
          <div>
            奖级数量：
            <el-input-number
              v-model="item.quantity"
              controls-position="right"
              @change="handleChangeAll($event, item)"
              :min="0"
              size="small"
            ></el-input-number>
          </div>
        </div>
        <div class="prize-table">
          <el-table :data="item.prizeList" class="el-table">
            <el-table-column label="奖品图片">
              <template slot-scope="scope">
                <el-image
                  style="width: 70px; height: 70px"
                  :src="scope.row.picUrl"
                  :preview-src-list="[scope.row.picUrl]"
                >
                </el-image>
              </template>
            </el-table-column>
            <el-table-column label="奖品名称" prop="title" />
            <el-table-column label="奖品类型">
              <template slot-scope="scope">
                <div v-if="scope.row.prizeType == 'goods'">商品</div>
                <div v-if="scope.row.prizeType == 'coupon'">券</div>
                <div v-if="scope.row.prizeType == 'coin'">盲豆</div>
              </template>
            </el-table-column>
            <!-- <el-table-column label="奖品数量" prop="storeName">
              <template slot-scope="scope">
                <div>
                  <el-input-number
                    v-model="scope.row.quantity"
                    controls-position="right"
                    @change="handleChange($event, index)"
                    :min="1"
                    size="small"
                  ></el-input-number>
                </div>
              </template>
            </el-table-column> -->
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="handleDel(scope.$index, item)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="prize-btn">
          <el-dropdown @command="handleCommand($event, index)">
            <el-button type="primary" size="small">
              添加奖品<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="goods">商品</el-dropdown-item>
              <el-dropdown-item command="coupon">券</el-dropdown-item>
              <el-dropdown-item command="coin">盲豆</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
  </div>
</template>
<script>
export default {
  name: "AwardList",
  props: {
    value: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      prizeIndex: null, // 奖级下标
      // 奖级列表
      awardsList: [
        {
          name: "永恒",
          sort: 1,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "王者",
          sort: 2,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "星耀",
          sort: 3,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "铂金",
          sort: 4,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "钻石",
          sort: 5,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "至尊",
          sort: 6,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "传说",
          sort: 7,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "史诗",
          sort: 8,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "稀有",
          sort: 9,
          quantity: 0,
          prizeList: [],
        },
        {
          name: "尊贵",
          sort: 10,
          quantity: 0,
          prizeList: [],
        },
      ],
    };
  },
  methods: {
    // 添加奖品
    add(type, item) {
      if(type == 1) {
        this.awardsList[this.prizeIndex].prizeList = this.awardsList[this.prizeIndex].prizeList.concat(item);
      } else if (type == 2) {
        this.awardsList[this.prizeIndex].prizeList.push(item);
      }
      this.$emit('input', this.awardsList)
      this.$emit('close')
    },

    // 选择奖品种类
    handleCommand(e, index) {
      this.prizeIndex = index;
      if (e == "goods") {
        this.goodsTableVisible = true;
      } else if (e == "coupon") {
        this.couponTableVisible = true;
      } else if (e == "coin") {
        this.coinTableVisible = true;
      }
      this.$emit('handleCommand', e)
    },
    // 改变奖级数量
    handleChangeAll(e, item) {
      this.$set(item, "quantity", e);
      this.$forceUpdate();
      // this.getQuantity();
    },

    // 改变奖品数量
    handleChange(e, index) {
      this.prizeIndex = index;
      this.$forceUpdate();
      // this.getQuantity();
    },

    // 奖级商品删除
    handleDel(index, item) {
      this.prizeIndex = item.sort - 1;
      let list = item.prizeList;
      list.splice(index, 1);
      this.$set(item, "prizeList", list);
      // this.getQuantity();
    },

    // 计算奖级数量
    getQuantity() {
      let num = 0;
      this.awardsList[this.prizeIndex].prizeList.forEach((item) => {
        num += item.quantity;
      });
      this.awardsList[this.prizeIndex].quantity = num;
    },
  },
};
</script>
<style lang="scss" scoped>
.prize {
  width: 1000px;
  margin-bottom: 50px;
  background: #f9f9f9;
  border: 1px solid #bbbbbb;
  font-size: 14px;
  &-top {
    padding: 10px 20px;
    margin-bottom: 10px;
    display: flex;
    align-content: center;
    justify-content: space-around;
    border-bottom: 1px solid #bbbbbb;
    div {
      line-height: 36px;
    }
  }
  &-btn {
    border-top: 1px solid #bbbbbb;
    padding: 10px;
  }
}
</style>