<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.itemName" clearable size="mini" class="filter-item"
        style="width: 200px;"
        placeholder="请输入奖品名称"
      />
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <br />
      <el-button
        size="mini" type="primary" icon="el-icon-plus"
        @click="$refs.EditModal && $refs.EditModal.handleOpen({ id: '' })"
      >
        添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column align="center" width="100" label="ID" prop="id" fixed="left" />
        <el-table-column align="center" width="150" label="奖品名称" prop="prizeName" fixed="left" show-overflow-tooltip />
        <el-table-column align="center" min-width="120" label="关联抽奖活动ID" prop="lotteryId">
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.lotteryId }}</span>
              <el-button
                v-if="row.lotteryId"
                style="margin-left: 14px;" type="warning" size="mini"
                @click="$refs.DetailModal1 && $refs.DetailModal1.handleOpen({ id: row.lotteryId })"
              >
                详情
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="奖品类型" prop="prizeType">
          <template slot-scope="{ row }">
            <el-tag v-if="row.prizeType === -1" effect="plain">谢谢参与</el-tag>
            <el-tag v-else-if="row.prizeType === 1" effect="plain">普通奖品</el-tag>
            <el-tag v-else-if="row.prizeType === 2" effect="plain">唯一性奖品</el-tag>
            <el-tag v-else-if="row.prizeType === 3" effect="plain">代金券</el-tag>
            <el-tag v-else-if="row.prizeType === 4" effect="plain">消费金</el-tag>
            <el-tag v-else-if="row.prizeType === 5" effect="plain">现金余额</el-tag>
            <el-tag v-else-if="row.prizeType === 6" effect="plain">优惠券</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" min-width="100" label="num字段" prop="prizeNum" />
        <el-table-column align="center" width="120" label="总库存" prop="totalStock" />
        <el-table-column align="center" width="120" label="可用库存" prop="validStock" />
        <el-table-column align="center" min-width="150" label="备注" prop="remark" show-overflow-tooltip />
        <el-table-column align="center" label="操作" width="220" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
            <el-button size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 新增编辑 -->
    <EditModal ref="EditModal" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
    <!-- 活动详情 -->
    <DetailModal1 ref="DetailModal1" />
  </div>
</template>

<script>
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import DetailModal1 from '../lotteryActivities/components/DetailModal'
import { getPageLotteryPrize, deleteByIdLotteryPrize } from '@/api/lotteryManagement/LotteryPrizes'

export default {
  name: 'LotteryPrizes',
  components: {
    EditModal,
    DetailModal,
    DetailModal1
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        topic: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getPageLotteryPrize(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleEdit(row) {
      this.$refs.EditModal && this.$refs.EditModal.handleOpen(row)
    },
    handleDelete(row) {
      this.$confirm('确定删除此项？')
        .then(async () => {
          await deleteByIdLotteryPrize({ id: row.id })
          this.$message({ message: '删除成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => { })
    }
  }
}
</script>

<style lang="less" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
