<template>
  <div>
    <el-dialog
      title="任务奖励"
      width="1000px"
      top="20px"
      :visible.sync="dialogVisible"
      @close="diaClose"
      :close-on-click-modal="true"
    >
      <div class="">
        <el-button type="primary" @click="openAdd">添加奖励</el-button>
        <div class="zhanwei"></div>
        <el-table :border="true" :data="tableData">
          <el-table-column label="奖励名称" prop="name"></el-table-column>
          <el-table-column label="奖励类型">
            <template slot-scope="scope">
              <span>{{ returnTypeName(scope.row.type) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="奖励图片">
            <template slot-scope="scope">
              <img
            style="width: 50px; height: 50px"
            v-if="scope.row.img"
            :src="scope.row.img"
          />
            </template>
          </el-table-column>
          <el-table-column label="数量">
            <template slot-scope="scope">
              <span>{{ scope.row.number }}</span>
            </template>
          </el-table-column>
          <el-table-column label="剩余数量">
            <template slot-scope="scope">
              <span>{{ scope.row.surplusNumber }}</span>
            </template>
          </el-table-column>
          <el-table-column label="总数量">
            <template slot-scope="scope">
              <span>{{ scope.row.totalNumber }}</span>
            </template>
          </el-table-column>
          <el-table-column label="编辑">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="openAdd(scope.row)"
                >编辑</el-button
              >
              <el-button type="text" size="mini" @click="del(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="limit"
            :current-page="page"
            layout="total,sizes, prev, pager, next,jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </el-dialog>
    <add-gift ref="addGift" @refresh="init"></add-gift>
  </div>
</template>
  
  <script>
import $api from "@/api/gift.js";
import addGift from "./pop-add-gift.vue";
export default {
  components: { addGift },
  data() {
    return {
      dialogVisible: false,
      total: 0,
      tableData: [],
      page: 1,
      limit: 10,
      renwu: {},
    };
  },
  methods: {
    open(item) {
      this.renwu = item;
      this.init();
      this.dialogVisible = true;
    },
    diaClose() {
      this.dialogVisible = false;
    },
    async showsChange(e, item) {
      console.log(e);
      const res = await $api.update({ ...item, shows: e });
      if (res.data.code == 0) {
        this.$message.success("修改成功");
        this.init();
      } else {
        this.$message.error(res.data.msg || "修改失败");
      }
    },
    handleSizeChange() {
      this.page = 1;
      this.init();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.init();
    },
    returnJumpTypeName(type) {
      const $types = {
        1: "内部路径",
        2: "外部路径",
        3: "内部tabbar页面",
      };
      return $types[type] ? $types[type] : "";
    },
    returnTypeName(type) {
      const $types = {
        1: "金币",
        2: "现金红包",
        3: "虚拟物品",
        9: "大转盘抽奖次数",
      };
      return $types[type] ? $types[type] : "";
    },
    //数据初始化
    async init() {
      const { id } = this.renwu;
      const { data } = await $api.getList({
        taskId: id,
        page: this.page,
        limit: this.limit,
      });
      this.tableData = data.data.records;
      this.total = data.data.totalCount;
    },
    del(item) {
      this.$confirm("是否删除该任务?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          $api.del(item.id).then((res) => {
            const data = res.data;
            if (data.code == 0) {
              this.$message.success("删除成功");
              this.init();
            } else {
              this.$message.error(data.msg || "删除失败");
            }
          });
        })
        .catch(() => {});
    },
    openAdd(item) {
      this.$refs.addGift.open(item, this.renwu);
    },
  },
  mounted() {},
};
</script>
  <style scoped lang="scss">
.zhanwei {
  height: 20px;
}
.pagination {
  margin-top: 20px;
  display: flex;
}
</style>