<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="name">
        <el-input size="small" v-model="searchForm.name" 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="jp-table top" style="">
      <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% - 90px);">
        <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="name" align="left">
            <template #default="{ row, rowIndex }">
              <span>{{ row.name }}</span>
            </template>
            <template #content="{ row, rowIndex }">
              <div class="expand-wrapper">
                <vxe-table
                  border
                  :data="row.luckyDrawAwardsDTOList">
                  <vxe-column field="name" title="奖项名称" align="center"></vxe-column>
                  <vxe-column field="number" title="奖品数量" align="center"></vxe-column>
                  <vxe-column field="prizeName" title="奖品名称" align="center"></vxe-column>
                </vxe-table>
              </div>
            </template>
          </vxe-column>
          <vxe-column min-width="180" title="活动详情" field="detail" align="left"></vxe-column>
          <vxe-column width="90" title="状态" field="switchFlag" align="center">
            <template #default="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 #default="scope">
              <el-button v-if="hasPermission('lucky_draw:events:edit')" text type="primary" size="small" @click="edit(scope.row.id)">修改</el-button>
              <el-button v-if="hasPermission('lucky_draw:events:edit')" text type="primary" size="small" @click="changeSwitch(scope.row)">
                {{scope.row.switchFlag === '1' ? '关闭活动' : '开启活动'}}
              </el-button>
              <!--<el-button v-if="hasPermission('lucky_draw:events:edit')" text type="primary" size="small" @click="luckyDraw(scope.row.id)">抽奖</el-button>-->
              <el-button v-if="hasPermission('lucky_draw:events:del')" text type="primary" 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'

  export default {
    data () {
      return {
        searchForm: {
          name: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        loading: false
      }
    },
    components: {
      LuckyDrawEventsForm
    },
    luckyDrawEventsService: null,
    created () {
      this.luckyDrawEventsService = new LuckyDrawEventsService()
    },
    activated () {
      this.refreshList()
    },
    watch: {
    },
    methods: {
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.luckyDrawEventsService.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
        })
      },
      // 当前页
      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)
      },
      // 查看
      view (id) {
        this.$refs.luckyDrawEventsForm.init('view', id)
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },
      changeSwitch (row) {
        if (row.switchFlag === '0') {
          this.$confirm(`确定开启此活动吗?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.luckyDrawEventsService.openEvent(row.id).then((data) => {
              this.$message.success(data)
              this.refreshList()
            })
          }).catch(() => {
            this.refreshList()
          })
        } else if (row.switchFlag === '1') {
          this.$confirm(`确定关闭此活动吗?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.luckyDrawEventsService.closeEvent(row.id).then((data) => {
              this.$message.success(data)
              this.refreshList()
            })
          }).catch(() => {
            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
          })
        })
      },
      luckyDraw (id) {
        console.log('id', id)
        this.$router.push({path: './LuckyDrawInfoList', query: {id: id}})
      }
    }
  }
</script>
<style lang="scss">
  .el-card__body {
    overflow: auto;
  }
</style>
<style>
  .expand-wrapper {
    padding: 10px;
  }
</style>
