<template>
  <el-main class="flex flex-col gap-5px">
    <manage-breadcrumb />
    <el-card body-class="flex">
      <el-button type="success">新建社团</el-button>
      <el-button type="danger" :disabled="!selectedClubs.length">批量删除</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
      <search-input
        v-model="searchClubName"
        class="m-l-auto"
        ref="searchInput"
        placeholder="请输入社团名称"
        @submit="refresh"
        @reset="refresh"
      />
    </el-card>
    <el-card v-loading="refreshing" element-loading-text="正在加载" class="min-h-100px">
      <paged-wrapper
        pagination-class="w-min"
        :handle-request="getClubList"
        :num="20"
        :converter="(raw) => raw.map(toClubRender)"
        @load-complete="refreshing = false"
        v-slot="{ data }"
        ref="clubList"
      >
        <el-table :data="data" @selection-change="onSelect">
          <el-table-column type="selection" :selectable="({ deleting }: ClubRender) => !deleting" />
          <el-table-column prop="id" label="序号" />
          <el-table-column prop="name" label="社团名称" />
          <el-table-column label="社团封面" v-slot="{ row }">
            <club-avatar :avatar="(row as ClubRender).avatar" :size="50">
              <div class="text-black text-2.5 lh-4">
                暂无
                <br />
                图片
              </div>
            </club-avatar>
          </el-table-column>
          <el-table-column label="社团介绍" v-slot="{ row }">
            <el-button
              type="primary"
              plain
              :disabled="(row as ClubRender).deleting"
              @click="onClubClick(row)"
            >
              查看介绍
            </el-button>
          </el-table-column>
          <el-table-column label="操作" v-slot="{ row }" :min-width="100">
            <div class="club-action-outer">
              <el-button
                type="primary"
                plain
                :disabled="(row as ClubRender).deleting"
                @click="
                  $router.push({
                    name: 'ManageClubMember',
                    params: { id: (row as ClubRender).id },
                  })
                "
              >
                管理成员
              </el-button>
              <el-button
                type="warning"
                plain
                :disabled="(row as ClubRender).deleting"
                @click="
                  $router.push({
                    name: 'ManageClubEdit',
                    params: {
                      id: (row as ClubRender).id,
                    },
                  })
                "
              >
                编辑
              </el-button>
              <el-button
                type="danger"
                :loading="(row as ClubRender).deleting"
                @click="onDeleteButtonClick(row)"
              >
                删除
              </el-button>
            </div>
          </el-table-column>
        </el-table>
      </paged-wrapper>
    </el-card>
    <club-detail-dialog v-model="showClubDialog" :club="showingClub" />
  </el-main>
</template>
<style lang="scss" scoped>
.club-action-outer {
  @apply flex flex-wrap gap-10px;
  .el-button {
    margin: 0;
  }
}
</style>
<script lang="ts">
type ClubRender = Club & {
  deleting: boolean;
};
const toClubRender = (raw: Club): ClubRender => ({ ...raw, deleting: false });
</script>
<script setup lang="ts">
import { getClubList } from '@/api';
import ClubAvatar from '@/components/club/ClubAvatar.vue';
import ClubDetailDialog from '@/components/club/ClubDetailDialog.vue';
import ManageBreadcrumb from '@/components/manage/ManageBreadcrumb.vue';
import PagedWrapper from '@/components/PagedWrapper.vue';
import SearchInput from '@/components/SearchInput.vue';
import { type Club } from '@/schemas/response';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref, useTemplateRef } from 'vue';
const showingClub = ref<ClubRender>();
const showClubDialog = ref(false);
const searchClubName = ref('');
const searchInputRef = useTemplateRef('searchInput');
const clubListRef = useTemplateRef('clubList');
const refreshing = ref(true);
const selectedClubs = ref<ClubRender[]>([]);
function refresh() {
  refreshing.value = true;
  clubListRef.value!.refresh();
}
function onClubClick(club: ClubRender) {
  showingClub.value = club;
  showClubDialog.value = true;
}
function onSelect(clubs: ClubRender[]) {
  selectedClubs.value = clubs;
}
function onDeleteButtonClick(club: ClubRender) {
  ElMessageBox.confirm('是否删除该社团，此操作无法恢复！', '警告', {
    type: 'warning',
    confirmButtonText: '删除',
    cancelButtonText: '取消',
  }).then(
    async () => {
      // TODO 删除社团
      club.deleting = true;
      try {
        if (!(await clubListRef.value!.refresh())) {
          return;
        }
        ElMessage.success('删除成功');
      } finally {
        club.deleting = false;
      }
    },
    () => {},
  );
}
</script>
