<script lang="ts" setup>
import { OperationMeetingApi } from '@/api/pms/meeting'
import type { DeptOption, OperationMeetingListItem } from './types'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'
import MeetingForm from './meeting-form.vue'
import dayjs from 'dayjs'

const loading = ref(false)
const list = ref<OperationMeetingListItem[]>([])
const visible = ref(false)
const currentMeetingSeries = ref('')
const currentYear = ref('')

const { ZmTable, ZmTableColumn } = useTableComponents<OperationMeetingListItem>()

const deptOptions = ref<DeptOption[]>([])

async function getDeptOptions() {
  const deptList = await OperationMeetingApi.getDeptList()
  deptOptions.value = deptList
    .filter((item) => {
      return item.type === '1'
    })
    .map((item) => ({
      label: item.name,
      value: item.id as number
    }))
}

async function getList() {
  loading.value = true
  try {
    const res = await OperationMeetingApi.getIntegratedMeetingList()
    list.value = res || []
  } finally {
    loading.value = false
  }
}

function handleQuery() {
  getList()
}

function resetQuery() {
  getList()
}

function getMeetingYear(row: Pick<OperationMeetingListItem, 'meetingDate'>) {
  const meetingDate = dayjs(row.meetingDate)

  return meetingDate.isValid() ? meetingDate.format('YYYY') : ''
}

function handleView(row: OperationMeetingListItem) {
  currentMeetingSeries.value = row.meetingSeries || ''
  currentYear.value = getMeetingYear(row)
  visible.value = true
}

function formatMeetingDate(row: Pick<OperationMeetingListItem, 'meetingDate'>) {
  return row.meetingDate ? dayjs(row.meetingDate).format('YYYY-MM-DD') : '-'
}

onMounted(() => {
  getDeptOptions()
  getList()
})
</script>

<template>
  <div
    class="operation-meeting-page min-w-0 overflow-x-hidden grid grid-rows-[auto_1fr] gap-4 h-[calc(100vh-20px-var(--top-tool-height)-var(--tags-view-height)-var(--app-footer-height))]">
    <el-form
      size="default"
      label-position="left"
      class="operation-meeting-query min-w-0 overflow-hidden rounded-lg bg-white p-4 shadow dark:bg-[#1d1e1f]">
      <div class="min-w-0 flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-end">
        <div
          class="operation-meeting-query__actions min-w-0 flex flex-col gap-3 sm:flex-row lg:shrink-0">
          <el-button type="primary" class="!ml-0 w-full sm:w-auto" @click="handleQuery">
            搜索
          </el-button>
          <el-button class="!ml-0 w-full sm:w-auto" @click="resetQuery">重置</el-button>
        </div>
      </div>
    </el-form>

    <div
      class="operation-meeting-data-panel bg-white dark:bg-[#1d1e1f] shadow rounded-lg flex flex-col p-4 min-h-0">
      <div class="flex-1 min-h-0 relative">
        <el-auto-resizer class="operation-meeting-table-view absolute">
          <template #default="{ width, height }">
            <zm-table
              :data="list"
              :loading="loading"
              :width="width"
              :max-height="height"
              :height="height"
              show-border>
              <ZmTableColumn label="会议期次" prop="meetingSeries" width="180" />
              <ZmTableColumn label="公司名称" prop="companyName" :min-width="220" />
              <ZmTableColumn
                label="会议日期"
                prop="meetingDate"
                cover-formatter
                :real-value="formatMeetingDate"
                :min-width="160" />
              <ZmTableColumn label="操作" width="100" fixed="right">
                <template #default="{ row }">
                  <el-button size="default" link type="success" @click="handleView(row)">
                    查看
                  </el-button>
                </template>
              </ZmTableColumn>
            </zm-table>
          </template>
        </el-auto-resizer>

        <div v-loading="loading" class="operation-meeting-card-view">
          <template v-if="list.length">
            <article v-for="item in list" :key="item.id" class="operation-meeting-card">
              <div class="operation-meeting-card__header">
                <div class="operation-meeting-card__title">
                  <span>会议期次</span>
                  <strong>{{ item.meetingSeries || '-' }}</strong>
                </div>
              </div>

              <div class="operation-meeting-card__content">
                <div class="operation-meeting-card__field">
                  <span>公司名称</span>
                  <strong>{{ item.companyName || '-' }}</strong>
                </div>
                <div class="operation-meeting-card__field">
                  <span>会议日期</span>
                  <strong>{{ formatMeetingDate(item) }}</strong>
                </div>
              </div>

              <div class="operation-meeting-card__actions">
                <el-button size="default" link type="success" @click="handleView(item)">
                  查看
                </el-button>
              </div>
            </article>
          </template>
          <el-empty v-else description="暂无数据" :image-size="80" />
        </div>
      </div>
    </div>
  </div>

  <meeting-form
    v-model:visible="visible"
    mode="summary"
    type="view"
    :meeting-series="currentMeetingSeries"
    :year="currentYear"
    :dept-options="deptOptions"
    @update:visible="visible = $event" />
</template>

<style scoped lang="scss">
.operation-meeting-query__actions {
  width: min(100%, 428px);
  max-width: 100%;
}

:deep(.operation-meeting-query .el-button) {
  max-width: 100%;
  min-width: 0;
}

.operation-meeting-table-view {
  display: block;
}

.operation-meeting-card-view {
  display: none;
}

.operation-meeting-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  padding-bottom: 4px;
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 8px;
  box-shadow: 0 2px 8px rgb(15 23 42 / 6%);
}

.operation-meeting-card__header,
.operation-meeting-card__actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.operation-meeting-card__title {
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 4px;
}

.operation-meeting-card__title span,
.operation-meeting-card__field span {
  font-size: 12px;
  line-height: 1.4;
  color: var(--el-text-color-secondary);
}

.operation-meeting-card__title strong {
  overflow: hidden;
  font-size: 16px;
  line-height: 1.35;
  color: var(--el-text-color-primary);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.operation-meeting-card__content {
  display: grid;
  gap: 10px;
}

.operation-meeting-card__field {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 12px;
  align-items: start;
}

.operation-meeting-card__field strong {
  min-width: 0;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.5;
  color: var(--el-text-color-primary);
  overflow-wrap: anywhere;
}

.operation-meeting-card__actions {
  justify-content: flex-end;
  padding-top: 2px;
  border-top: 1px solid var(--el-border-color-lighter);
}

@media (width >= 640px) {
  .operation-meeting-query__actions {
    width: auto;
    max-width: none;
  }
}

@media (width < 768px) {
  .operation-meeting-page {
    height: auto;
    min-height: calc(
      100vh - 20px - var(--top-tool-height) - var(--tags-view-height) - var(--app-footer-height)
    );
    grid-template-rows: auto 1fr;
    gap: 12px;
  }

  .operation-meeting-data-panel {
    min-height: 280px;
    padding: 12px;
  }

  .operation-meeting-table-view {
    display: none;
  }

  .operation-meeting-card-view {
    display: flex;
    min-height: 180px;
    flex: 1;
    flex-direction: column;
    gap: 12px;
    overflow-y: auto;
  }
}
</style>
