<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { AgGridVue } from 'ag-grid-vue3'
import type { ColDef, FirstDataRenderedEvent, GridApi, GridReadyEvent, GridSizeChangedEvent } from 'ag-grid-community'
import localforage from 'localforage'
import { majorList } from '@/sql'
import {  myTheme ,gridOptions} from '@/lib/diyAgGridOptions'
import { decimalAggSum, sumByNumber } from '@/lib/decimal'
import { formatThousands } from '@/lib/numberFormat'

import { AG_GRID_LOCALE_CN } from '@ag-grid-community/locale';
// 精简的边框配置（细线条+浅灰色，弱化分割线视觉）

interface DictLeaf {
  id: string
  code: string
  name: string
}

interface DictGroup {
  id: string
  code: string
  name: string
  children: DictLeaf[]
}

interface DetailRow {
  id: string
  groupCode: string
  groupName: string
  majorCode: string
  majorName: string
  amount: number | null
  landArea: number | null
  path: string[]
}

interface XmInfoState {
  projectName: string
  detailRows: DetailRow[]
}

const props = defineProps<{
  contractId: string
}>()
const DB_KEY = computed(() => `ht-info-v3-${props.contractId}`)
const XM_DB_KEY = 'xm-info-v3'

const detailRows = ref<DetailRow[]>([])
const gridApi = ref<GridApi<DetailRow> | null>(null)

type majorLite = { code: string; name: string }
const serviceEntries = Object.entries(majorList as Record<string, majorLite>)
  .sort((a, b) => Number(a[0]) - Number(b[0]))
  .filter((entry): entry is [string, majorLite] => {
    const item = entry[1]
    return Boolean(item?.code && item?.name)
  })

const detailDict: DictGroup[] = (() => {
  const groupMap = new Map<string, DictGroup>()
  const groupOrder: string[] = []
  const codeLookup = new Map(serviceEntries.map(([key, item]) => [item.code, { id: key, code: item.code, name: item.name }]))

  for (const [key, item] of serviceEntries) {
    const code = item.code
    const isGroup = !code.includes('-')
    if (isGroup) {
      if (!groupMap.has(code)) groupOrder.push(code)
      groupMap.set(code, {
        id: key,
        code,
        name: item.name,
        children: []
      })
      continue
    }

    const parentCode = code.split('-')[0]
    if (!groupMap.has(parentCode)) {
      const parent = codeLookup.get(parentCode)
      if (!groupOrder.includes(parentCode)) groupOrder.push(parentCode)
      groupMap.set(parentCode, {
        id: parent?.id || `group-${parentCode}`,
        code: parentCode,
        name: parent?.name || parentCode,
        children: []
      })
    }

    groupMap.get(parentCode)!.children.push({
      id: key,
      code,
      name: item.name
    })
  }

  return groupOrder.map(code => groupMap.get(code)).filter((group): group is DictGroup => Boolean(group))
})()

const idLabelMap = new Map<string, string>()
for (const group of detailDict) {
  idLabelMap.set(group.id, `${group.code} ${group.name}`)
  for (const child of group.children) {
    idLabelMap.set(child.id, `${child.code} ${child.name}`)
  }
}

const buildDefaultRows = (): DetailRow[] => {
  const rows: DetailRow[] = []
  for (const group of detailDict) {
    for (const child of group.children) {
      rows.push({
        id: child.id,
        groupCode: group.code,
        groupName: group.name,
        majorCode: child.code,
        majorName: child.name,
        amount: null,
        landArea: null,
        path: [group.id, child.id]
      })
    }
  }
  return rows
}

const mergeWithDictRows = (rowsFromDb: DetailRow[] | undefined): DetailRow[] => {
  const dbValueMap = new Map<string, DetailRow>()
  for (const row of rowsFromDb || []) {
    dbValueMap.set(row.id, row)
  }

  return buildDefaultRows().map(row => {
    const fromDb = dbValueMap.get(row.id)
    if (!fromDb) return row

    return {
      ...row,
      amount: typeof fromDb.amount === 'number' ? fromDb.amount : null,
      landArea: typeof fromDb.landArea === 'number' ? fromDb.landArea : null
    }
  })
}

const columnDefs: ColDef<DetailRow>[] = [
 
  {
    headerName: '造价金额(万元)',
    field: 'amount',
    headerClass: 'ag-right-aligned-header',
    minWidth: 100,
      flex: 1, // 核心：开启弹性布局，自动占满剩余空间

    editable: params => !params.node?.group && !params.node?.rowPinned,
    cellClass: params => (!params.node?.group && !params.node?.rowPinned ? 'ag-right-aligned-cell editable-cell-line' : 'ag-right-aligned-cell'),
    cellClassRules: {
      'editable-cell-empty': params =>
        !params.node?.group && !params.node?.rowPinned && (params.value == null || params.value === '')
    },
    aggFunc: decimalAggSum,
    valueParser: params => {
      if (params.newValue === '' || params.newValue == null) return null
      const v = Number(params.newValue)
      return Number.isFinite(v) ? v : null
    },
    valueFormatter: params => {
      if (!params.node?.group && !params.node?.rowPinned && (params.value == null || params.value === '')) {
        return '点击输入'
      }
      if (params.value == null) return ''
      return formatThousands(params.value)
    }
  },
  {
    headerName: '用地面积(亩)',
    field: 'landArea',
    minWidth: 100,
      flex: 1, // 核心：开启弹性布局，自动占满剩余空间

    editable: params => !params.node?.group && !params.node?.rowPinned,
    cellClass: params => (!params.node?.group && !params.node?.rowPinned ? 'editable-cell-line' : ''),
    cellClassRules: {
      'editable-cell-empty': params =>
        !params.node?.group && !params.node?.rowPinned && (params.value == null || params.value === '')
    },
    aggFunc: decimalAggSum,
    valueParser: params => {
      if (params.newValue === '' || params.newValue == null) return null
      const v = Number(params.newValue)
      return Number.isFinite(v) ? v : null
    },
    valueFormatter: params => {
      if (!params.node?.group && !params.node?.rowPinned && (params.value == null || params.value === '')) {
        return '点击输入'
      }
      if (params.value == null) return ''
      return String(Number(params.value))
    }
  }
]

const autoGroupColumnDef: ColDef = {
  headerName: '专业编码以及工程专业名称',
  minWidth: 200,
  maxWidth: 300,
    flex:2, // 核心：开启弹性布局，自动占满剩余空间
wrapText: true,
    autoHeight: true,
    // cellStyle: { whiteSpace: 'normal', lineHeight: '1.4' },
  cellRendererParams: {
    suppressCount: true
  },
  valueFormatter: params => {
    if (params.node?.rowPinned) {
      return '总合计'
    }
    const nodeId = String(params.value || '')
    return idLabelMap.get(nodeId) || nodeId
  },
  tooltipValueGetter: params => {
    if (params.node?.rowPinned) return '总合计'
    const nodeId = String(params.value || '')
    return idLabelMap.get(nodeId) || nodeId
  }
}

const totalAmount = computed(() => sumByNumber(detailRows.value, row => row.amount))

const totalLandArea = computed(() => sumByNumber(detailRows.value, row => row.landArea))
const pinnedTopRowData = computed(() => [
  {
    id: 'pinned-total-row',
    groupCode: '',
    groupName: '',
    majorCode: '',
    majorName: '',
    amount: totalAmount.value,
    landArea: totalLandArea.value,
    path: ['TOTAL']
  }
])



const saveToIndexedDB = async () => {
  try {
    const payload = {
  detailRows: JSON.parse(JSON.stringify(detailRows.value)) 
    }
    console.log('Saving to IndexedDB:', payload)
    await localforage.setItem(DB_KEY.value, payload)
  } catch (error) {
    console.error('saveToIndexedDB failed:', error)
  }
}

const loadFromIndexedDB = async () => {
  try {
    const data = await localforage.getItem<XmInfoState>(DB_KEY.value)
    if (data) {
      detailRows.value = mergeWithDictRows(data.detailRows)
      return
    }

    // 首次创建合同段时，默认继承项目规模信息（同一套专业字典，按 id 对齐）
    const xmData = await localforage.getItem<XmInfoState>(XM_DB_KEY)
    if (xmData?.detailRows) {
      detailRows.value = mergeWithDictRows(xmData.detailRows)
      return
    }

    detailRows.value = buildDefaultRows()
  } catch (error) {
    console.error('loadFromIndexedDB failed:', error)
    detailRows.value = buildDefaultRows()
  }
}

let persistTimer: ReturnType<typeof setTimeout> | null = null




let gridPersistTimer: ReturnType<typeof setTimeout> | null = null
const handleCellValueChanged = () => {
  if (gridPersistTimer) clearTimeout(gridPersistTimer)
  gridPersistTimer = setTimeout(() => {
    void saveToIndexedDB()
  }, 1000)
}


onMounted(async () => {
  await loadFromIndexedDB()
})

onBeforeUnmount(() => {
  if (persistTimer) clearTimeout(persistTimer)
  if (gridPersistTimer) clearTimeout(gridPersistTimer)
  void saveToIndexedDB()
})
const processCellForClipboard = (params:any) => {
  if (Array.isArray(params.value)) {
    return JSON.stringify(params.value); // 数组转字符串复制
  }
  return params.value;
};

const processCellFromClipboard = (params:any) => {
  try {
    const parsed = JSON.parse(params.value);
    if (Array.isArray(parsed)) return parsed;
  } catch (e) {
    // 解析失败时返回原始值，无需额外处理
  }
  return params.value;
};

const fitColumnsToGrid = () => {
  if (!gridApi.value) return
  requestAnimationFrame(() => {
    gridApi.value?.sizeColumnsToFit({ defaultMinWidth: 80 })
  })
}

const handleGridReady = (event: GridReadyEvent<DetailRow>) => {
  gridApi.value = event.api
  fitColumnsToGrid()
}

const handleGridSizeChanged = (_event: GridSizeChangedEvent<DetailRow>) => {
  fitColumnsToGrid()
}

const handleFirstDataRendered = (_event: FirstDataRenderedEvent<DetailRow>) => {
  fitColumnsToGrid()
}
</script>

<template>
  <div class="h-full min-h-0 min-w-0 flex flex-col">
  

    <div class="rounded-lg border bg-card xmMx flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
      <div class="flex items-center justify-between border-b px-4 py-3">
        <h3 class="text-sm font-semibold text-foreground">合同规模明细</h3>
        <div class="text-xs text-muted-foreground">导入导出</div>
      </div>

      <div class="ag-theme-quartz h-full min-h-0 min-w-0 w-full flex-1 overflow-hidden">
         <AgGridVue
    :style="{ height: '100%' }"
    :rowData="detailRows"
    :pinnedTopRowData="pinnedTopRowData"
    :columnDefs="columnDefs"
    :autoGroupColumnDef="autoGroupColumnDef"
    :gridOptions="gridOptions"
    :theme="myTheme"
    @cell-value-changed="handleCellValueChanged"
    :suppressColumnVirtualisation="true"
    :suppressRowVirtualisation="true"
    :cellSelection="{ handle: { mode: 'range' } }"
    :enableClipboard="true"
    :localeText="AG_GRID_LOCALE_CN"
    :tooltipShowDelay="500"
        :headerHeight="50"
    :suppressHorizontalScroll="true"
    :processCellForClipboard="processCellForClipboard"
    :processCellFromClipboard="processCellFromClipboard"
    :undoRedoCellEditing="true"
    :undoRedoCellEditingLimit="20"
    @grid-ready="handleGridReady"
    @grid-size-changed="handleGridSizeChanged"
    @first-data-rendered="handleFirstDataRendered"

  />
      </div>
    </div>
  </div>
</template>
