<script lang="ts" setup>
import { QhseMonthReportSummaryApi, QhseMonthReportApi } from '@/api/pms/qhse'
import { useTableComponents } from '@/components/ZmTable/useTableComponents'
import QhseMonthReportPreviewDrawer from './preview-drawer.vue'
import type { QhseMonthReportItem, QhseMonthReportListItem } from './types'
import dayjs from 'dayjs'

const loading = ref(false)
const list = ref<QhseMonthReportListItem[]>([])
const total = ref(0)
const visible = ref(false)

const queryFormRef = ref()
const queryParams = reactive({
  yearMonths: ''
})
const pagination = reactive({
  pageNo: 1,
  pageSize: 10
})

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

async function getList() {
  loading.value = true
  try {
    const res = await QhseMonthReportSummaryApi.getQhseMonthReportSummaryList({
      ...queryParams,
      pageNo: pagination.pageNo,
      pageSize: pagination.pageSize
    })
    list.value = res?.list || []
    total.value = res?.total || 0
  } finally {
    loading.value = false
  }
}

function handleQuery() {
  pagination.pageNo = 1
  getList()
}

function resetQuery() {
  queryFormRef.value?.resetFields()
  pagination.pageNo = 1
  getList()
}

let yearMonths = ref<string | undefined>('')
async function handleView(row: QhseMonthReportItem) {
  yearMonths.value = row.yearMonths
  visible.value = true
}

function handleSizeChange(pageSize: number) {
  pagination.pageSize = pageSize
  pagination.pageNo = 1
  getList()
}

function handleCurrentChange(pageNo: number) {
  pagination.pageNo = pageNo
  getList()
}

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

<template>
  <div
    class="report-summary-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
      ref="queryFormRef"
      :model="queryParams"
      inline
      label-position="left"
      class="report-summary-query min-w-0 overflow-hidden rounded-lg bg-white p-4 shadow dark:bg-[#1d1e1f]">
      <el-form-item label="月报期次" prop="yearMonths">
        <el-date-picker
          v-model="queryParams.yearMonths"
          type="month"
          value-format="YYYY-MM"
          class="w-full!" />
      </el-form-item>
      <el-form-item class="report-summary-query__actions">
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div
      class="report-summary-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="report-summary-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="yearMonths" />
              <ZmTableColumn label="月报标题" prop="title" />
              <!-- <ZmTableColumn
                label="创建日期"
                prop="createTime"
                cover-formatter
                :real-value="({ createTime }) => formatDate(createTime)"
                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="report-summary-card-view">
          <template v-if="list.length">
            <article v-for="item in list" :key="item.id" class="report-summary-card">
              <div class="report-summary-card__title">
                <span>月报标题</span>
                <strong>{{ item.title || '-' }}</strong>
              </div>
              <div class="report-summary-card__field">
                <span>月报期次</span>
                <strong>{{ item.yearMonths || '-' }}</strong>
              </div>

              <div class="report-summary-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 class="mt-4 flex justify-end">
        <el-pagination
          v-model:current-page="pagination.pageNo"
          v-model:page-size="pagination.pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>

  <QhseMonthReportPreviewDrawer
    v-model:visible="visible"
    :yearMonths="yearMonths"
    @update:visible="visible = $event" />
</template>

<style scoped lang="scss">
.report-summary-query {
  :deep(.el-form-item) {
    margin-bottom: 0;
  }
}

.report-summary-query__actions {
  :deep(.el-form-item__content) {
    gap: 12px;
  }
}

.report-summary-table-view {
  display: block;
}

.report-summary-card-view {
  display: none;
}

.report-summary-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  background: var(--el-bg-color);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 10px;
}

.report-summary-card__title,
.report-summary-card__field {
  display: grid;
  gap: 4px;
}

.report-summary-card__title span,
.report-summary-card__field span {
  font-size: 12px;
  color: var(--el-text-color-secondary);
}

.report-summary-card__title strong,
.report-summary-card__field strong {
  font-size: 14px;
  line-height: 1.5;
  color: var(--el-text-color-primary);
  overflow-wrap: anywhere;
}

.report-summary-card__actions {
  display: flex;
  justify-content: flex-end;
  padding-top: 4px;
  border-top: 1px solid var(--el-border-color-lighter);
}

@media (width < 768px) {
  .report-summary-page {
    height: auto;
    min-height: calc(
      100vh - 20px - var(--top-tool-height) - var(--tags-view-height) - var(--app-footer-height)
    );
    gap: 12px;
  }

  .report-summary-data-panel {
    min-height: 280px;
    padding: 12px;
  }

  .report-summary-table-view {
    display: none;
  }

  .report-summary-card-view {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 12px;
    overflow-y: auto;
  }
}
</style>
