<template>
  <div class="page">
    <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <el-form-item prop="awardsName">
        <el-input size="small" v-model="searchForm.awardsName" placeholder="奖项名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="bg-white top">
      <vxe-toolbar   :refresh="{query: refreshList}" custom>
        <template #buttons>
          <el-button v-if="hasPermission('lucky_draw:events:add')" icon="el-icon-plus" type="primary" size="small"  @click="add()">新建</el-button>
          <el-button v-if="hasPermission('lucky_draw:events:del')" icon="el-icon-delete" type="danger"  size="small"  @click="del()" :disabled="$refs.luckyTable && $refs.luckyTable.getCheckboxRecords().length === 0" plain>删除</el-button>
        </template>
      </vxe-toolbar>
      <div style="height: calc(100% - 80px);">
        <vxe-table
          border="inner"
          auto-resize
          resizable
          height="auto"
          :loading="loading"
          size="small"
          ref="luckyTable"
          show-header-overflow
          show-overflow
          highlight-hover-row
          :print-config="{}"
          :import-config="{}"
          :export-config="{}"
          :sort-config="{remote:true}"
          :data="dataList"
          :expand-config="{iconOpen: 'el-icon-caret-top', iconClose: 'el-icon-caret-right', accordion: true}"
        >
          <vxe-column type="seq" width="60" title="序号"></vxe-column>
          <vxe-column type="checkbox"  width="40px"></vxe-column>
          <vxe-column min-width="150" title="奖项名称" type="expand" field="awardsName" align="left">
            <template #default="{ row, rowIndex }">
              <span>{{ row.awardsName }}</span>
            </template>
            <template #content="{ row, rowIndex }">
              <div class="expand-wrapper">
                <vxe-table
                  border
                  :data="row.luckyDrawMembersDtos">
                  <vxe-column field="name" title="姓名" align="center"></vxe-column>
                  <vxe-column field="officeName" title="部门" align="center"></vxe-column>
                  <vxe-column field="phone" title="手机号" align="center"></vxe-column>
                </vxe-table>
              </div>
            </template>
          </vxe-column>
          <vxe-column min-width="180" title="奖品名称" field="prizeName" align="left"></vxe-column>
          <vxe-column min-width="180" title="奖品数量" field="number" align="left"></vxe-column>
<!--          <vxe-column width="90" title="状态" field="switchFlag" align="center">-->
<!--            <template slot-scope="scope">-->
<!--              <span v-if="scope.row.switchFlag === '1'" style="color: #67C23A">已开启</span>-->
<!--              <span v-if="scope.row.switchFlag === '0'" style="color: #F56C6C">已关闭</span>-->
<!--            </template>-->
<!--          </vxe-column>-->
          <vxe-column title="操作" width="200" fixed="right" align="center">
            <template slot-scope="scope">
              <el-button v-if="hasPermission('lucky_draw:events:edit')" type="text" size="small" @click="luckyDraw(scope.row.eventsId,scope.row.number,scope.row.id)">抽奖</el-button>
<!--              <el-button v-if="hasPermission('lucky_draw:events:del')" type="text" size="small" @click="del(scope.row.id)">删除</el-button>-->

            </template>
          </vxe-column>
        </vxe-table>
        <vxe-pager
          background
          size="small"
          :current-page="tablePage.currentPage"
          :page-size="tablePage.pageSize"
          :total="tablePage.total"
          :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="currentChangeHandle">
        </vxe-pager>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <LuckyDrawEventsForm  ref="luckyDrawEventsForm" @refreshDataList="refreshList"></LuckyDrawEventsForm>
    </div>
  </div>
</template>

<script>
  import LuckyDrawEventsForm from './LuckyDrawEventsForm'
  import LuckyDrawEventsService from '@/api/luckyDraw/LuckyDrawEventsService'
  import LuckyDrawInfoService from '@/api/luckyDraw/LuckyDrawInfoService'

  export default {
    data () {
      return {
        searchForm: {
          id: '',
          awardsName: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false
      }
    },
    components: {
      LuckyDrawEventsForm
    },
    luckyDrawEventsService: null,
    luckyDrawInfoService: null,
    created () {
      this.luckyDrawEventsService = new LuckyDrawEventsService()
      this.luckyDrawInfoService = new LuckyDrawInfoService()
    },
    activated () {
      this.refreshList()
    },
    watch: {
    },
    methods: {
      // 获取数据列表
      refreshList () {
        this.searchForm.id = this.$route.query.id
        console.log('this.$route.query.id', this.$route.query.id)
        this.loading = true
        this.luckyDrawInfoService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then(({data}) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
      },
      // findByEventId () {
      //   let id = this.$route.query.id
      //   this.luckyDrawEventsService.findByEventId(id).then(({data}) => {
      //     this.dataList = data.records
      //   })
      // },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 新建
      add () {
        this.$refs.luckyDrawEventsForm.init('add')
      },
      // 修改
      // edit (id) {
      //   id = id || this.$refs.luckyTable.getCheckboxRecords().map(item => {
      //     return item.id
      //   })[0]
      //   this.$refs.luckyDrawEventsForm.init('edit', id)
      // },
      // 抽奖
      luckyDraw (id, number, awardId) {
        let param = {id: id, number: number, awardId: awardId}
        this.luckyDrawInfoService.save(param)
        this.refreshList()
      },
      // 查看
      view (id) {
        this.$refs.luckyDrawEventsForm.init('view', id)
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
      // 删除
      // del (id) {
      //   let ids = id || this.$refs.luckyTable.getCheckboxRecords().map(item => {
      //     return item.id
      //   }).join(',')
      //   this.$confirm(`确定删除所选项吗?`, '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     this.loading = true
      //     this.luckyDrawEventsService.delete(ids).then(({data}) => {
      //       this.$message.success(data)
      //       this.refreshList()
      //       this.loading = false
      //     })
      //   })
      // }
    }
  }
</script>
<style lang="scss">
  .el-card__body {
    overflow: auto;
  }
</style>
<style>
  .expand-wrapper {
    padding: 10px;
  }
</style>
