<template>
  <div>
    <div class="prize" v-for="(item, index) in awardsList" :key="index">
        <div class="prize-top">
          <div>奖级：{{ item.name }}</div>
          <div>名额：
            <el-input-number
              v-model="item.quantity"
              controls-position="right"
              @change="handleChangeAllTwo($event, item)"
              :min="0"
              size="small"
              :disabled="item.isEdit"
            ></el-input-number>
          </div>
          <div>
            内部名额：
            <el-input-number
              v-model="item.insideNum"
              controls-position="right"
              @change="handleChangeAll($event, item)"
              :min="0"
              size="small"
              :disabled="item.isEdit"
            ></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" >
              <template slot-scope="scope">
                <div v-if="scope.row.prizeType == 'coin' && scope.row.value >= 0">盲豆 x{{scope.row.value}}</div>
                <div v-else>{{scope.row.title}}</div>
              </template>
            </el-table-column>
            <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 == 'coupon_pkg'">券包</div>
                <div v-if="scope.row.prizeType == 'coin'">盲豆</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="coupon_pkg">券包</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: [],
          insideNum: 0,
        },
        {
          name: "二等奖",
          sort: 2,
          quantity: 0,
          prizeList: [],
          insideNum: 0,
        },
        {
          name: "三等奖",
          sort: 3,
          quantity: 0,
          prizeList: [],
          insideNum: 0,
        },
        {
          name: "四等奖",
          sort: 4,
          quantity: 0,
          prizeList: [],
          insideNum: 0,
        },
        {
          name: "五等奖",
          sort: 5,
          quantity: 0,//名额数量
          prizeList: [],//列表
          insideNum: 0,//内定数量
        }
      ],
    };
  },
  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);
      } else if (type == 3) {
        this.awardsList = item
      }
      let filterArr = this.awardsList.filter((item) => {
        return item.prizeList.length != 0;
      });
      this.$emit('input', filterArr)
      this.$emit('close')
    },
    // 选择奖品种类
    handleCommand(e, index) {
      this.prizeIndex = index;
      this.$emit('handleCommand', e)
    },

    // 改变奖级数量
    handleChangeAll(e, item) {
      this.$set(item, "insideNum", e);
      this.$forceUpdate();
    },
    handleChangeAllTwo(e, item){
      this.$set(item, "quantity", e);
      this.$forceUpdate();
    },

    // 改变奖品数量
    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: 100%;
  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>
