<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({ jobId: '' })"
      >
        添加
      </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="itemName" 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" width="120" label="奖项等级" prop="level" />
        <el-table-column align="center" width="120" label="奖项概率" prop="percent" />
        <el-table-column align="center" min-width="120" label="关联抽奖奖品ID" prop="prizeId">
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.prizeId }}</span>
              <el-button
                v-if="row.prizeId"
                style="margin-left: 14px;" type="warning" size="mini"
                @click="$refs.DetailModal2 && $refs.DetailModal2.handleOpen({ id: row.prizeId })"
              >
                详情
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否默认" prop="defaultItem">
          <template slot-scope="{ row }">
            <el-tag v-if="row.defaultItem === 0" effect="plain">不是</el-tag>
            <el-tag v-else-if="row.defaultItem === 1" effect="plain">是</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="150" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="操作" width="300" 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" />
    <!-- 奖品详情 -->
    <DetailModal2 ref="DetailModal2" />
  </div>
</template>

<script>
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import DetailModal1 from '../lotteryActivities/components/DetailModal'
import DetailModal2 from '../LotteryPrizes/components/DetailModal'
import { getPageLotteryItem, deleteByIdLotteryItem } from '@/api/lotteryManagement/LotteryAwards'

export default {
  name: 'LotteryAwards',
  components: {
    EditModal,
    DetailModal,
    DetailModal1,
    DetailModal2
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        itemName: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getPageLotteryItem(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 deleteByIdLotteryItem({ 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>
