<template>
  <div class="progress-page">
    <div class="canvas">
      <header class="topbar"><div class="topbar-center"><div class="model-title">XXX特大桥主体模型.rvt</div></div></header>

      <section class="model-stage">
        <ModelPlaceholder />
      </section>

      <section class="module-topbar">
        <div class="module-topbar-panel">
          <div class="field field-time">
            <select class="select" v-model="timeStatMode"><option value="cutoff">日期截止统计</option></select>
            <input class="date-input" type="date" v-model="cutoffDateDraft" />
            <button class="btn btn-sm btn-accent" type="button" @click="applyCutoffDate">查询</button>
            <div class="kpi kpi-progress">整体完成百分比：<span class="kpi-number">{{ formatPercentValue(overallPercent, 2) }}</span></div>
          </div>

          <div class="field field-percent">
            <span class="field-label">进度百分比</span>
            <label class="chip chip-status"><input type="checkbox" :checked="percentFilters.all" @change="togglePercentFilter('all', $event.target.checked)" /> 全部</label>
            <label class="chip chip-status chip-gray"><input type="checkbox" :checked="percentFilters.p0" @change="togglePercentFilter('p0', $event.target.checked)" /> 0%</label>
            <label class="chip chip-status chip-red"><input type="checkbox" :checked="percentFilters.p0_50" @change="togglePercentFilter('p0_50', $event.target.checked)" /> 0%-50%</label>
            <label class="chip chip-status chip-blue"><input type="checkbox" :checked="percentFilters.p50_100" @change="togglePercentFilter('p50_100', $event.target.checked)" /> 50%-100%</label>
            <label class="chip chip-status chip-green"><input type="checkbox" :checked="percentFilters.p100" @change="togglePercentFilter('p100', $event.target.checked)" /> 100%</label>
          </div>
        </div>
      </section>

      <aside class="sidepanel" :class="{ 'is-collapsed': sideCollapsed }">
        <header class="sidepanel-header">
          <div class="sidepanel-title">工程部位</div>
          <button class="iconbtn" type="button" @click="sideCollapsed = !sideCollapsed">{{ sideCollapsed ? "▸" : "▾" }}</button>
        </header>
        <div class="sidepanel-body" v-show="!sideCollapsed">
          <div class="tree">
            <div class="tree-item" :class="{ 'is-active': row.leaf && row.id === selectedStructureId }" :style="{ paddingLeft: `${10 + row.level * 14}px` }" v-for="row in visibleTreeRows" :key="row.id" @click="onTreeRowClick(row)">
              <button class="tree-caret" type="button" :class="{ 'is-leaf': row.leaf }" :disabled="row.leaf" @click.stop="toggleTreeExpand(row)">{{ row.leaf ? "•" : row.open ? "▾" : "▸" }}</button>
              <span class="tree-bullet"></span>
              <span class="tree-text">{{ row.name }}</span>
            </div>
          </div>
        </div>
      </aside>

      <section class="bottompanel" :class="{ 'is-collapsed': bottomCollapsed }">
        <header class="bottompanel-header"><div class="tabs"><button class="tab is-on" type="button">项目日进度明细</button></div></header>
        <button class="iconbtn bottompanel-toggle" type="button" @click="bottomCollapsed = !bottomCollapsed">{{ bottomCollapsed ? "▴" : "▾" }}</button>
        <div class="bottompanel-body" v-show="!bottomCollapsed">
          <table class="table">
            <thead>
              <tr>
<!--                <th>序号</th><th>全名称</th><th>清单编号</th><th>清单名称</th><th>单位</th><th>合同数量</th><th>合同金额</th><th>填报日期</th><th>完成数量</th><th>完成金额</th><th>累计完成数量</th>-->
                <th>序号</th><th>全名称</th><th>清单编号</th><th>清单名称</th><th>单位</th><th>合同数量</th><th>合同金额</th><th>合同金额不含税</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="r in progressRows" :key="r.no">
                <td>{{ r.no }}</td><td>{{ r.fullName }}</td><td>{{ r.workCode }}</td><td>{{ r.name }}</td><td>{{ r.workUnit }}</td><td>{{ formatNumber(r.meteringNum,2) }}</td><td>{{ formatMoney(r.meteringAmt) }}</td><td>{{ formatMoney(r.meteringNotaxAmt) }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </section>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted, reactive, ref } from "vue";
import ModelPlaceholder from "../../components/model-placeholder/index.vue";
import { progressApi } from "../../service/api/progress.js";

const structures = ref([]);
const elementTree = ref([]);
const progressData = ref([]);
const sideCollapsed = ref(false);
const bottomCollapsed = ref(false);
const selectedStructureId = ref("");
const expanded = ref(new Set());
const loading = ref(false);

const timeStatMode = ref("cutoff");
const baselineDate = ref(todayISODate());
const baselineOverallPercent = ref(0);
const cutoffDate = ref(todayISODate());
const cutoffDateDraft = ref(cutoffDate.value);

const percentFilters = reactive({ all: true, p0: false, p0_50: false, p50_100: false, p100: false });

const visibleTreeRows = computed(() => {
  const rows = [];
  const walk = (node, level) => {
    const leaf = node.isLeaf === true;
    const open = expanded.value.has(node.id);
    rows.push({ id: node.id, name: node.name, level, leaf, open, createDate: node.createDate });
    if (!leaf && open && node.children) {
      node.children.forEach((c) => walk(c, level + 1));
    }
  };
  elementTree.value.forEach((n) => walk(n, 0));
  return rows;
});

const selectedStructureName = computed(() => {
  const findById = (nodes) => {
    for (const n of nodes) {
      if (n.id === selectedStructureId.value) return n.name;
      if (n.children) {
        const found = findById(n.children);
        if (found) return found;
      }
    }
  };
  return findById(elementTree.value) || "";
});

const selectedStructureNode = computed(() => {
  return findNode(elementTree.value, selectedStructureId.value);
});

const overallPercent = computed(() => {
  if (!selectedStructureNode.value) return 0;
  return 0;
});

const progressRows = computed(() => {
  if (!selectedStructureId.value) return [];
  return progressData.value.map((item, index) => ({
    no: index + 1,
    fullName: item.fullName || "",
    workCode: item.workCode || "",
    name: item.name || "",
    workUnit: item.workUnit || "-",
    meteringAmt: item.meteringAmt || 0,
    meteringNotaxAmt: item.meteringNotaxAmt || 0,
    meteringNum: item.meteringNum || 0,
    doneQty: item.doneQty || 0,
    doneAmt: item.doneAmt || 0,
    cumDoneQty: item.cumDoneQty || 0,
  }));
});

async function loadWbsTree(parentId) {
  loading.value = true;
  try {
    const data = await progressApi.getTree(parentId);
    console.log(7777,data)
    if (!parentId) {
      elementTree.value = data.map((item) => ({
        ...item,
        children: item.isLeaf ? [] : [],
        isLeaf: !!item.isLeaf,
      }));
      if (data.length > 0) {
        const firstLeaf = findFirstLeaf(elementTree.value);
        if (firstLeaf) selectedStructureId.value = firstLeaf.id;
      }
    } else {
      updateChildren(elementTree.value, parentId, data);
    }
  } catch (e) {
    console.error("加载WBS树失败:", e);
  } finally {
    loading.value = false;
  }
}

function findFirstLeaf(nodes) {
  for (const n of nodes) {
    if (n.isLeaf) return n;
    if (n.children && n.children.length > 0) {
      const found = findFirstLeaf(n.children);
      if (found) return found;
    }
  }
}

function updateChildren(nodes, parentId, children) {
  for (const n of nodes) {
    if (n.id === parentId) {
      n.children = children.map((item) => ({
        ...item,
        children: item.isLeaf ? [] : [],
        isLeaf: !!item.isLeaf,
      }));
      return true;
    }
    if (n.children && updateChildren(n.children, parentId, children)) return true;
  }
  return false;
}

function findNode(nodes, id) {
  for (const n of nodes) {
    if (n.id === id) return n;
    if (n.children) {
      const found = findNode(n.children, id);
      if (found) return found;
    }
  }
  return null;
}

onMounted(() => {
  loadWbsTree();
});

function todayISODate() {
  return new Date().toISOString().slice(0, 10);
}

function daysBetweenISO(a, b) {
  const da = new Date(`${a}T00:00:00`);
  const db = new Date(`${b}T00:00:00`);
  return Math.round((db.getTime() - da.getTime()) / 86400000);
}

function clamp(value, min, max) {
  return Math.min(max, Math.max(min, value));
}

function formatPercentValue(value, digits = 2) {
  return `${Number(value).toFixed(digits)}%`;
}

function formatMoney(value) {
  if (value == null || Number.isNaN(value)) return "--";
  const abs = Math.abs(value);
  const sign = value < 0 ? "-" : "";
  if (abs >= 1e8) return `${sign}${(abs / 1e8).toFixed(2)}亿`;
  if (abs >= 1e4) return `${sign}${(abs / 1e4).toFixed(2)}万`;
  return `${sign}${abs.toFixed(2)}元`;
}

function formatNumber(value, digits = 0) {
  return Number(value).toLocaleString("zh-CN", { minimumFractionDigits: digits, maximumFractionDigits: digits });
}

function applyCutoffDate() {
  cutoffDate.value = cutoffDateDraft.value || cutoffDate.value || todayISODate();
}

function applyAllExclusive(key, checked) {
  if (key === "all") {
    percentFilters.all = checked;
    if (checked) {
      percentFilters.p0 = false;
      percentFilters.p0_50 = false;
      percentFilters.p50_100 = false;
      percentFilters.p100 = false;
    }
    return;
  }
  percentFilters[key] = checked;
  if (checked) percentFilters.all = false;
  if (!percentFilters.p0 && !percentFilters.p0_50 && !percentFilters.p50_100 && !percentFilters.p100) percentFilters.all = true;
}

function togglePercentFilter(key, checked) { applyAllExclusive(key, checked); }

function toggleTreeExpand(row) {
  if (row.leaf) return;
  if (expanded.value.has(row.id)) {
    expanded.value.delete(row.id);
  } else {
    expanded.value.add(row.id);
    const node = findNode(elementTree.value, row.id);
    if (node && (!node.children || node.children.length === 0)) {
      loadWbsTree(row.id);
    }
  }
}

function onTreeRowClick(row) {
  console.log(8888,row)
  if (row.leaf) {
    selectedStructureId.value = row.id;
    loadProgressData(row.id,row.createDate);
  } else {
    toggleTreeExpand(row);
  }
}

async function loadProgressData(positionId,time) {
  loading.value = true;
  try {
    const data = await progressApi.getPjProgress({
      projectId: "12e3c0eb186243869d94e214363ba083",
      positionIds: positionId,
      period: time,
    });
    progressData.value = data;
    console.log("进度数据:", data);
  } catch (e) {
    console.error("加载进度明细失败:", e);
    progressData.value = [];
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.progress-page { min-height: 100vh; font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", sans-serif; }
.canvas { position: relative; min-height: 100vh; overflow: hidden; border-radius: 24px; border: 1px solid rgba(98,191,206,.34); background: radial-gradient(circle at 50% 30%, #bcc9d6 0%, #b2c3d2 60%, #a8b8c9 100%); box-shadow: inset 0 0 0 8px rgba(214,230,241,.55); }
.topbar { position: absolute; inset: 0 0 auto; height: 120px; z-index: 30; }
.topbar-center { position: absolute; left: 50%; transform: translateX(-50%); top: 16px; }
.model-title { font-size: 20px; letter-spacing: 1px; font-weight: 700; color: rgba(255,128,52,.95); text-shadow: 0 2px 10px rgba(0,0,0,.25); }
.model-stage { position: absolute; inset: 0; }

.module-topbar { position: absolute; left: 50%; top: 20px; transform: translateX(-50%); width: min(800px, calc(100% - 180px)); z-index: 99; }
.module-topbar-panel { border-radius: 18px; border: 1px solid rgba(83,214,206,.26); background: radial-gradient(420px 120px at 18% 0%, rgba(83,214,206,.22), transparent 70%), linear-gradient(180deg, rgba(18,32,40,.94), rgba(15,25,32,.88)); box-shadow: 0 20px 56px rgba(0,0,0,.28), 0 0 0 1px rgba(83,214,206,.12) inset; padding: 10px 14px; display: grid; gap: 10px; }
.field { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.field-time { flex-wrap: nowrap; }
.field-percent { flex-wrap: nowrap; }
.select, .date-input { height: 34px; border-radius: 10px; border: 1px solid rgba(255,255,255,.14); background: rgba(0,0,0,.18); color: rgba(255,255,255,.9); padding: 0 10px; font-size: 13px; font-weight: 700; }
.date-input { width: 150px; }
.btn { appearance: none; border: 1px solid rgba(255,255,255,.1); background: rgba(255,255,255,.08); color: rgba(255,255,255,.88); border-radius: 12px; padding: 8px 10px; cursor: pointer; }
.btn-sm { padding: 6px 10px; border-radius: 999px; font-size: 12px; font-weight: 800; }
.btn-accent { border-color: rgba(83,214,206,.22); background: rgba(43,191,178,.22); }

.kpi { margin-left: auto; font-size: 13px; font-weight: 800; color: rgba(236,248,251,.86); white-space: nowrap; }
.kpi-number { font-weight: 900; color: rgba(83,214,206,.95); margin-left: 4px; }

.field-label { font-weight: 900; color: rgba(244,252,255,.92); font-size: 14px; white-space: nowrap; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px; border: 1px solid rgba(255,255,255,.12); background: rgba(0,0,0,.2); color: rgba(255,255,255,.88); font-weight: 800; font-size: 13px; white-space: nowrap; }
.chip input { width: 13px; height: 13px; margin: 0; accent-color: rgba(64,224,208,.92); }
.chip-status::before { content: ""; width: 10px; height: 10px; border-radius: 3px; border: 1px solid rgba(255,255,255,.2); background: transparent; }
.chip-status.chip-gray::before { border-color: rgba(110,125,150,.45); background: rgba(110,125,150,.24); }
.chip-status.chip-red::before { border-color: rgba(217,54,62,.4); background: rgba(217,54,62,.22); }
.chip-status.chip-blue::before { border-color: rgba(21,108,255,.4); background: rgba(21,108,255,.22); }
.chip-status.chip-green::before { border-color: rgba(47,143,47,.4); background: rgba(47,143,47,.22); }

.sidepanel { position: absolute; left: 16px; top: 175px; width: 320px; bottom: 100px; border-radius: 18px; border: 1px solid rgba(83,214,206,.24); background: radial-gradient(260px 140px at 8% 0%, rgba(63,203,191,.16), transparent 62%) padding-box, linear-gradient(180deg, rgba(20,31,37,.92), rgba(15,23,29,.88)) padding-box; box-shadow: 0 22px 60px rgba(0,0,0,.34), 0 0 0 1px rgba(83,214,206,.12) inset; overflow: hidden; z-index: 25; }
.sidepanel.is-collapsed { width: 64px; }
.sidepanel-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 10px 12px; border-bottom: 1px solid rgba(83,214,206,.16); background: radial-gradient(360px 100px at 10% 0%, rgba(83,214,206,.24), transparent 68%), linear-gradient(180deg, rgba(28,134,122,.84), rgba(15,60,74,.62)); }
.sidepanel-title { font-size: 14px; font-weight: 900; color: rgba(207,247,242,.96); }
.iconbtn { width: 30px; height: 30px; border-radius: 10px; border: 1px solid rgba(255,255,255,.12); background: rgba(255,255,255,.42); color: rgba(11,27,58,.86); font-weight: 900; cursor: pointer; }
.sidepanel-body { height: calc(100% - 52px); overflow: auto; padding: 8px; }

.tree { display: grid; gap: 8px; }
.tree-item { display: grid; grid-template-columns: 20px 10px 1fr; align-items: center; gap: 8px; min-height: 40px; border-radius: 14px; border: 1px solid rgba(154,186,198,.22); background: linear-gradient(180deg, rgba(44,58,68,.58), rgba(37,49,58,.54)); cursor: pointer; }
.tree-item:hover { border-color: rgba(83,214,206,.32); background: linear-gradient(180deg, rgba(51,67,78,.66), rgba(43,57,67,.62)); }
.tree-item.is-active { border-color: rgba(132,188,255,.3); background: linear-gradient(180deg, rgba(112,146,198,.34), rgba(95,128,180,.28)); }
.tree-caret { border: 0; background: transparent; color: rgba(203,230,236,.88); cursor: pointer; font-size: 14px; }
.tree-caret.is-leaf { cursor: default; }
.tree-bullet { width: 10px; height: 10px; border-radius: 50%; background: rgba(83,214,206,.85); }
.tree-text { font-size: 13px; font-weight: 800; color: rgba(222,238,244,.9); }

.bottompanel { position: absolute; left: 370px; right: 16px; bottom: 100px; height: 390px; border-radius: 18px; border: 1px solid rgba(83,214,206,.24); background: radial-gradient(420px 180px at 14% 0%, rgba(83,214,206,.2), transparent 66%) padding-box, radial-gradient(520px 220px at 82% 0%, rgba(40,156,228,.14), transparent 70%) padding-box, linear-gradient(180deg, rgba(18,33,40,.94), rgba(14,24,31,.9)) padding-box; box-shadow: 0 24px 70px rgba(0,0,0,.36), 0 0 0 1px rgba(83,214,206,.14) inset; overflow: hidden; z-index: 26; }
.bottompanel-header { padding: 12px 52px 12px 12px; border-bottom: 1px solid rgba(83,214,206,.28); background: radial-gradient(380px 120px at 18% 0%, rgba(83,214,206,.28), transparent 70%), linear-gradient(180deg, rgba(25,137,124,.86), rgba(16,66,82,.64)); }
.bottompanel-toggle { position: absolute; right: 12px; top: 12px; }
.tabs { display: flex; gap: 10px; }
.tab { border: 1px solid rgba(255,255,255,.12); background: rgba(255,255,255,.08); color: rgba(214,235,241,.9); border-radius: 999px; padding: 8px 16px; cursor: pointer; font-weight: 900; font-size: 12px; }
.tab.is-on { border-color: rgba(83,214,206,.32); background: rgba(83,214,206,.24); }
.bottompanel.is-collapsed { height: 70px; }
.bottompanel-body { padding: 0 12px 12px; overflow: auto; height: calc(100% - 66px); }

.table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 14px; }
.table th, .table td { text-align: left; padding: 12px 14px; border-bottom: 1px solid rgba(178,206,216,.16); }
.table th { color: rgba(219,240,246,.86); font-weight: 900; background: linear-gradient(90deg, rgba(12,38,52,.92), rgba(16,47,61,.86)); position: sticky; top: 0; z-index: 2; }
.table td { color: rgba(222,238,244,.86); background: linear-gradient(90deg, rgba(37,51,61,.42), rgba(31,45,54,.36)); }
.table tbody tr:hover td { background: linear-gradient(90deg, rgba(69,102,130,.36), rgba(56,86,113,.32)); }

@media (max-width: 1300px) {
  .module-topbar { width: calc(100% - 32px); }
  .field-time, .field-percent { flex-wrap: wrap; }
  .kpi { margin-left: 0; }
  .sidepanel { width: 280px; }
  .bottompanel { left: 330px; }
}
</style>
