<template>
  <div class="payreturnPage">
    <!-- 搜索 -->
    <div class="formSearch">
      <!-- 搜索条件 -->
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="俱乐部名称">
          <el-input clearable v-model="query.name" placeholder="请输入活动名称" />
        </el-form-item>

        <el-form-item label="俱乐部区域编码">
          <el-cascader clearable style="width: 100%" v-model="query.areaCode" :props="regionProps" placeholder="请选择区域"></el-cascader>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" plain @click="search">查询</el-button>
          <el-button plain @click="clear">重置</el-button>
          <el-button type="primary" plain @click="$refs.clubModalRef && $refs.clubModalRef.show()">新增俱乐部</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table ref="multipleTable" :data="tableData" border :header-cell-style="{ background: '#EEF3FF', color: '#333333' }" tooltip-effect="dark" style="width: 100%">
        <el-table-column align="center" type="index" label="#" width="55"></el-table-column>
        <el-table-column prop="name" label="俱乐部名称" align="center" width="220" />

        <el-table-column prop="maxNumber" label="最大人数" align="center">
          <template slot-scope="scope">
            <el-tag size="mini">{{ scope.row.maxNumber || '未设置' }} 人</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="currentNumber" label="当前人数" align="center">
          <template slot-scope="scope">
            <el-tag type="success" size="mini">{{ scope.row.currentNumber || '0' }} 人</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="areaCode" label="俱乐部区域编码" align="center">
          <template slot-scope="scope">
            <span v-if="!scope.row.areaCode">---</span>
            <el-tag type="info" size="mini">{{ scope.row.areaCode == 0 ? '全国' : scope.row.areaCode }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="createTime" label="俱乐部创建时间" align="center" width="220" />
        <el-table-column width="280" label="操作" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="btnList">
              <el-button type="text" @click="$refs.clubModalRef && $refs.clubModalRef.show(scope.row)">编辑</el-button>
              <el-button type="text" @click="$refs.detailModalRef && $refs.detailModalRef.show(scope.row.id)">详情</el-button>
              <el-button type="text" style="color: #f56c6c" @click="handleDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          :current-page="query.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <DetailModal ref="detailModalRef"></DetailModal>
    <ClubModal @success="getAll" ref="clubModalRef"></ClubModal>
  </div>
</template>

<script>
import { getClubListApi, deleteClubApi } from '@/api/rm-bank'
import ClubModal from './components/ClubModal.vue'
import DetailModal from './components/DetailModal.vue'
import { getProvinceList, getChildAreaList } from '@/api/address'

export default {
  components: {
    DetailModal,
    ClubModal
  },
  data() {
    return {
      query: {
        page: 1,
        pageSize: 10,
        name: undefined,
        areaCode: []
      },
      total: 1,
      tableData: [],
      regionProps: {
        checkStrictly: true,
        lazy: true,
        label: 'name',
        value: 'id',
        lazyLoad(node, resolve) {
          const { level, value } = node
          if (level === 0) {
            resolve([{ id: 0, parentId: 0, name: '全中国', shortName: '全国', longitude: '108.55', latitude: '34.32', level: 1, sort: 1, status: true }])
          } else if (level === 1) {
            getProvinceList().then((res) => {
              resolve(res.data)
            })
          } else if (level != 0 && level != 1) {
            getChildAreaList(value).then((res) => {
              resolve(
                res.data.map((item) => {
                  item.leaf = level === 4
                  return item
                })
              )
            })
          }
        }
      }
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    async getAll() {
      const queryParams = JSON.parse(JSON.stringify(this.query))
      const { name, areaCode } = queryParams
      if (name) {
        queryParams['club.name'] = name
      }
      if (areaCode.length) {
        queryParams['club.areaCode'] = areaCode[areaCode.length - 1]
      }

      delete queryParams.name
      delete queryParams.areaCode

      const res = await getClubListApi(queryParams)
      this.tableData = res.data.list
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.query.pageSize = val
      this.getAll()
    },
    handleCurrentChange(val) {
      this.query.page = val
      this.getAll()
    },
    search() {
      this.total = 1
      this.query.page = 1
      this.getAll()
    },
    clear() {
      this.query = {
        page: 1,
        pageSize: 10,
        name: '',
        areaCode: []
      }
      this.getAll()
    },

    // 删除
    async handleDelete(row) {
      this.$confirm(`是否删除【${row.name}】这个俱乐部?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await deleteClubApi({ id: row.id })
          if (res.code == '200') {
            this.$message.success('删除成功')
            this.getAll()
          }
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.payreturnPage {
  padding: 30px;

  .tableBox {
    .fenye {
      margin: 20px;
    }
  }
}
</style>
