<template>
  <div class="dashboard-container">
    <main class="dashboard-main">
      <!-- 页面标题和筛选 -->
      <div class="dashboard-header">
        <div class="header-title">
          <!-- <h2 class="title">资产仪表盘</h2>
          <p class="subtitle">实时监控资产状态和管理效率</p> -->
        </div>
        <div class="header-filters">
          <a-tree-select
            v-model:value="selectedDepartment" show-search style="width: 300px"
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }" placeholder="请选择部门" allow-clear
            tree-default-expand-all :tree-data="departmentOptions" tree-node-filter-prop="label"
            @change="handleDepartmentChange"
          />
        </div>
      </div>

      <!-- 资产总览卡片 -->
      <a-row :gutter="[20, 20]" class="overview-cards">
        <a-col :xs="24" :sm="12" :lg="6">
          <a-card class="stat-card" hoverable>
            <div class="card-content">
              <div class="card-info">
                <p class="card-label">资产总数量</p>
                <h3 class="card-value">{{ assetStats.assetQuantity }}</h3>
              </div>
              <div class="card-icon primary-icon">
                <AppstoreOutlined />
              </div>
            </div>
            <div class="card-footer">
              <span v-if="assetStats.assetNumMonthOnMonth === 'N/A'" class="trend-down">N/A</span>
              <span v-else :class="assetStats.assetNumMonthOnMonthUp ? 'trend-up' : 'trend-down'">
                <component :is="assetStats.assetNumMonthOnMonthUp ? ArrowDownOutlined : ArrowUpOutlined" />
                {{ assetStats.assetNumMonthOnMonth }}
              </span>
              <span class="trend-label">较上月</span>
            </div>
          </a-card>
        </a-col>

        <a-col :xs="24" :sm="12" :lg="6">
          <a-card class="stat-card" hoverable>
            <div class="card-content">
              <div class="card-info">
                <p class="card-label">在用资产</p>
                <h3 class="card-value">{{ assetStats.useAssetQuantity }}</h3>
              </div>
              <div class="card-icon success-icon">
                <CheckCircleOutlined />
              </div>
            </div>
            <div class="card-footer">
              <span v-if="assetStats.useAssetNumMonthOnMonth === 'N/A'" class="trend-down">N/A</span>
              <span v-else :class="assetStats.useAssetNumMonthOnMonthUp ? 'trend-up' : 'trend-down'">
                <component :is="assetStats.useAssetNumMonthOnMonthUp ? ArrowDownOutlined : ArrowUpOutlined" />
                {{ assetStats.useAssetNumMonthOnMonth }}
              </span>
              <span class="trend-label">较上月</span>
            </div>
          </a-card>
        </a-col>

        <a-col :xs="24" :sm="12" :lg="6">
          <a-card class="stat-card" hoverable>
            <div class="card-content">
              <div class="card-info">
                <p class="card-label">报废资产</p>
                <h3 class="card-value">{{ assetStats.scrapAssetQuantity }}</h3>
              </div>
              <div class="card-icon warning-icon">
                <ExclamationCircleOutlined />
              </div>
            </div>
            <div class="card-footer">
              <span v-if="assetStats.scrapAssetNumMonthOnMonth === 'N/A'" class="trend-down">N/A</span>
              <span v-else :class="assetStats.scrapAssetNumMonthOnMonthUp ? 'trend-up' : 'trend-down'">
                <component :is="assetStats.scrapAssetNumMonthOnMonthUp ? ArrowDownOutlined : ArrowUpOutlined" />
                {{ assetStats.scrapAssetNumMonthOnMonth }}
              </span>
              <span class="trend-label">较上月</span>
            </div>
          </a-card>
        </a-col>

        <a-col :xs="24" :sm="12" :lg="6">
          <a-card class="stat-card" hoverable>
            <div class="card-content">
              <div class="card-info">
                <p class="card-label">本月盘点单</p>
                <h3 class="card-value">{{ assetStats.checkDocumentQuantity }}</h3>
              </div>
              <div class="card-icon secondary-icon">
                <FileTextOutlined />
              </div>
            </div>
            <div class="card-footer">
              <span v-if="assetStats.checkDocumentNumMonthOnMonth === 'N/A'" class="trend-down">N/A</span>
              <span v-else :class="assetStats.checkDocumentNumMonthOnMonthUp ? 'trend-up' : 'trend-down'">
                <component :is="assetStats.checkDocumentNumMonthOnMonthUp ? ArrowDownOutlined : ArrowUpOutlined" />
                {{ assetStats.checkDocumentNumMonthOnMonth }}
              </span>
              <span class="trend-label">较上月</span>
            </div>
          </a-card>
        </a-col>
      </a-row>

      <!-- 资产趋势变化和搜索栏 -->
      <a-row :gutter="[24, 24]" class="trend-search-section">
        <a-col :xs="24" :lg="24">
          <a-card class="search-card">
            <h3 class="chart-title">资产搜索</h3>
            <div>
              <!-- <label class="form-label">资产名称/编号</label> -->
              <a-input-search
                v-model:value="searchQuery" placeholder="请输入资产名称、编号、责任人、保管人" enter-button="搜索"
                size="large" @search="searchAssets" @press-enter="searchAssets"
              />
            </div>
          </a-card>
        </a-col>
      </a-row>
      <a-row :gutter="[24, 24]" class="trend-search-section">
        <a-col :xs="24" :lg="24">
          <a-card class="chart-card">
            <div class="chart-header">
              <h3 class="chart-title">资产总数变化趋势</h3>
              <a-radio-group v-model:value="activeTrendTab" button-style="solid" size="small">
                <a-radio-button value="quarter">近3月</a-radio-button>
                <a-radio-button value="month">近6月</a-radio-button>
                <a-radio-button value="year">近12月</a-radio-button>
              </a-radio-group>
            </div>
            <div ref="trendChartRef" class="chart-container" />
          </a-card>
        </a-col>
      </a-row>

      <!-- 新增的统计图表区域 -->
      <a-row :gutter="[24, 24]" class="charts-section">
        <a-col :xs="24" :lg="8">
          <a-card class="chart-card">
            <div class="chart-header">
              <h3 class="chart-title">部门资产分布</h3>
              <!-- <a href="#" class="link-primary">查看详情 <RightOutlined /></a> -->
            </div>
            <div ref="departmentChartRef" class="chart-container" />
          </a-card>
        </a-col>

        <a-col :xs="24" :lg="8">
          <a-card class="chart-card">
            <div class="chart-header">
              <h3 class="chart-title">资产分类统计</h3>
              <!-- <a href="#" class="link-primary">查看详情 <RightOutlined /></a> -->
            </div>
            <div ref="categoryChartRef" class="chart-container" />
          </a-card>
        </a-col>

        <a-col :xs="24" :lg="8">
          <a-card class="chart-card">
            <div class="chart-header">
              <h3 class="chart-title">报废资产分析</h3>
              <!-- <a href="#" class="link-primary">查看详情 <RightOutlined /></a> -->
            </div>
            <div ref="scrapChartRef" class="chart-container" />
          </a-card>
        </a-col>
      </a-row>

      <!-- 报废资产和盘点单展示 -->
      <a-row :gutter="[24, 24]" class="list-section">
        <a-col :xs="24" :lg="12">
          <a-card class="list-card">
            <div class="list-header">
              <h3 class="chart-title">报废资产预警</h3>
              <!-- <a href="#" class="link-primary">查看全部 <RightOutlined /></a> -->
            </div>
            <div class="list-content">
              <a-empty v-if="!scrapWarningAssets || scrapWarningAssets.length === 0" />
              <template v-else>
                <div v-for="asset in scrapWarningAssets" :key="asset.assetInstanceId" class="list-item">
                  <div class="item-main">
                    <div class="item-info">
                      <h4 class="item-title">{{ asset.assetInstanceName }} - {{ asset.assetInstanceNo }}</h4>
                      <p class="item-desc">使用年限(月): {{ asset.estimateUsedMonth }}月 | 原值: {{ asset.orginalValue }}</p>
                    </div>
                    <a-tag
                      :color="asset.scrapLevelName === '超期未报废' ? 'error' : asset.scrapLevelName === '待报废' ? 'warning' : 'default'"
                    >
                      {{ asset.scrapLevelName }}
                    </a-tag>
                  </div>
                  <div class="item-footer">
                    <span class="item-date">预计报废日期: {{ asset.willScrapDate }}</span>
                    <!-- <a-button type="link" size="small">处理</a-button> -->
                  </div>
                </div>
              </template>
            </div>
          </a-card>
        </a-col>

        <a-col :xs="24" :lg="12">
          <a-card class="list-card">
            <div class="list-header">
              <h3 class="chart-title">最新盘点单</h3>
              <!-- <a href="#" class="link-primary">查看全部 <RightOutlined /></a> -->
            </div>
            <div class="list-content">
              <a-empty v-if="!recentInventories || recentInventories.length === 0" />
              <template v-else>
                <div v-for="inventory in recentInventories" :key="inventory.assetInventoryId" class="list-item">
                  <div class="item-main">
                    <div class="item-info">
                      <h4 class="item-title">{{ inventory.inventoryName }}</h4>
                      <p class="item-desc">盘点单号: {{ inventory.documentNo }} | 资产数量: {{ inventory.assetQuantity }}</p>
                    </div>
                    <a-tag :color="inventory.processed === '已完成' ? 'success' : 'processing'">
                      {{ inventory.processed }}
                    </a-tag>
                  </div>
                  <div class="item-footer">
                    <div>
                      <span class="item-date">计划开始时间: {{ inventory.inventoryStartDate }} </span><br />
                      <span class="item-date">计划结束时间: {{ inventory.inventoryEndDate }}</span>
                    </div>
                    <a-button type="link" size="small" @click="openDetail(inventory.assetInventoryId)">查看详情</a-button>
                  </div>
                </div>
              </template>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </main>

    <!-- 页脚 -->
    <!-- <footer class="dashboard-footer">
      <p>© 2023 资产管理系统 - 上海联物信息科技有限公司</p>
    </footer> -->
  </div>
</template>

<script setup>
import * as echarts from 'echarts';
import { Uuid } from 'pc-component-v3';
import { ref, onMounted, onUnmounted, nextTick, watch } from 'vue';
import {
  AppstoreOutlined,
  CheckCircleOutlined,
  ExclamationCircleOutlined,
  FileTextOutlined,
  ArrowUpOutlined,
  ArrowDownOutlined,
  SearchOutlined,
  RightOutlined,
} from '@ant-design/icons-vue';
import {
  queryOrgList, queryAssetTrend, queryDetailsById, queryOrgAsset, queryAssetType,
  queryScrapAsset, queryScrapWarning, queryInventory,
} from '../api/dashboard';
import { message } from 'ant-design-vue';
import Common from '../common/Common';
import { useRouter } from 'vue-router';

const router = useRouter();
// 筛选条件
const selectedDepartment = ref('');
const searchQuery = ref('');
const activeTrendTab = ref('year');

// 图表引用
const trendChartRef = ref(null);
const departmentChartRef = ref(null);
const categoryChartRef = ref(null);
const scrapChartRef = ref(null);

// 图表实例
let trendChartInstance = null;
let departmentChartInstance = null;
let categoryChartInstance = null;
let scrapChartInstance = null;

// 资产统计信息
const assetStats = ref({
  assetQuantity: 0,
  assetNumMonthOnMonth: '',
  assetNumMonthOnMonthUp: false,
  useAssetQuantity: 0,
  useAssetNumMonthOnMonth: '',
  useAssetNumMonthOnMonthUp: false,
  scrapAssetQuantity: 0,
  scrapAssetNumMonthOnMonth: '',
  scrapAssetNumMonthOnMonthUp: false,
  checkDocumentQuantity: 0,
  checkDocumentNumMonthOnMonth: '',
  checkDocumentNumMonthOnMonthUp: false,
});

// 下拉选项
const departmentOptions = ref([]);

// 报废资产预警数据
const scrapWarningAssets = ref([]);

// 盘点单数据
const recentInventories = ref([]);

// 搜索资产方法
const searchAssets = () => {
  localStorage.setItem('##searchStr##', searchQuery.value);
  router.push('/eam/inventoryAssetInstanceSearch');
};

// 获取部门列表
const getOrgList = () => {
  queryOrgList().then(
    res => {
      if (res.errorCode == 0) {
        if (res.datas && res.datas.length > 0) {
          // 添加"全部部门"选项作为第一个节点
          const allDepartmentsOption = {
            label: '全部部门',
            value: '',
            children: [],
          };
          departmentOptions.value = [allDepartmentsOption, ...transformData(res.datas)];
        } else {
          departmentOptions.value = [{
            label: '全部部门',
            value: '',
            children: [],
          }];
        }
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
    },
  );
};

// 处理部门选择变化
const handleDepartmentChange = value => {
  // 如果清除选择，确保值为''（全部部门）
  if (value === undefined || value === null) {
    selectedDepartment.value = '';
  }
  
  // 重新查询所有相关接口
  refreshAllData();
};

// 刷新所有数据的方法
const refreshAllData = () => {
  getDetailsById();
  getAssetTrend();
  getOrgAsset();
  getAssetType();
  getScrapAsset();
  getScrapWarning();
  getInventory();
};

// 根据部门查询资产和盘点详情
const getDetailsById = () => {
  // 传入当前选中的部门ID（可能是空字符串表示全部部门）
  queryDetailsById(selectedDepartment.value).then(
    res => {
      if (res.errorCode === 0) {
        assetStats.value = res.data;
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
    },
  );
};

// 部门资产分布
const getOrgAsset = () => {
  // 如果选择了具体部门，传入部门ID；否则传入空字符串
  const orgId = selectedDepartment.value || '';
  queryOrgAsset(orgId).then(
    res => {
      if (res.errorCode == 0) {
        updateDepartmentChart(res.results);
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
    },
  );
};

// 资产分类统计
const getAssetType = () => {
  // 传入当前选中的部门ID
  queryAssetType(selectedDepartment.value).then(
    res => {
      if (res.errorCode == 0) {
        updateCategoryChart(res.results);
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
    },
  );
};

// 报废资产分析
const getScrapAsset = () => {
  // 传入当前选中的部门ID
  queryScrapAsset(selectedDepartment.value).then(
    res => {
      if (res.errorCode == 0) {
        if (res.datas && Array.isArray(res.datas)) {
          updateScrapChart(res.datas);
        } else {
          updateScrapChart([]);
        }
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
      updateScrapChart([]);
    },
  );
};

// 查询报废资产预警
const getScrapWarning = () => {
  // 传入当前选中的部门ID
  queryScrapWarning(selectedDepartment.value).then(
    res => {
      if (res.errorCode == 0) {
        console.log('查询报废资产预警:', res.datas);
        if (res.datas && Array.isArray(res.datas)) {
          scrapWarningAssets.value = res.datas;
        } else {
          scrapWarningAssets.value = [];
        }
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
    },
  );
};

// 最新盘点单查询
const getInventory = () => {
  // 传入当前选中的部门ID
  queryInventory(selectedDepartment.value).then(
    res => {
      if (res.errorCode == 0) {
        if (res.results && Array.isArray(res.results)) {
          recentInventories.value = res.results;
        } else {
          recentInventories.value = [];
        }
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
    },
  );
};

const openDetail =id => {
  let url = Common.getRedirectUrl('#/desktop/window1/window-read/view/041101/0/' + id +
          '?currPage=1&currIndex=1&totalCount=1&uuid=' + Uuid.createUUID());
  window.open(url);
};

// 初始化图表
const initCharts = () => {
  // 资产趋势图表
  if (trendChartRef.value) {
    trendChartInstance = echarts.init(trendChartRef.value);
    trendChartInstance.setOption({
      tooltip: {
        trigger: 'axis',
        axisPointer: { type: 'cross' },
      },
      legend: {
        data: ['资产总数', '未删减资产', '报废资产', '减少资产', '丢失资产'],
        top: 0,
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '资产总数',
          type: 'line',
          smooth: true,
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          itemStyle: { color: '#165DFF' },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(22, 93, 255, 0.3)' },
              { offset: 1, color: 'rgba(22, 93, 255, 0.05)' },
            ]),
          },
        },
        {
          name: '未删减资产',
          type: 'line',
          smooth: true,
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          itemStyle: { color: '#00B42A' },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(0, 180, 42, 0.2)' },
              { offset: 1, color: 'rgba(0, 180, 42, 0.05)' },
            ]),
          },
        },
        {
          name: '报废资产',
          type: 'line',
          smooth: true,
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          itemStyle: { color: '#F53F3F' },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(245, 63, 63, 0.2)' },
              { offset: 1, color: 'rgba(245, 63, 63, 0.05)' },
            ]),
          },
        },
        {
          name: '减少资产',
          type: 'line',
          smooth: true,
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          itemStyle: { color: '#FF7D00' },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(255, 125, 0, 0.2)' },
              { offset: 1, color: 'rgba(255, 125, 0, 0.05)' },
            ]),
          },
        },
        {
          name: '丢失资产',
          type: 'line',
          smooth: true,
          data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
          itemStyle: { color: '#86909C' },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: 'rgba(134, 144, 156, 0.2)' },
              { offset: 1, color: 'rgba(134, 144, 156, 0.05)' },
            ]),
          },
        },
      ],
    });
  }

  // 部门资产分布图表
  if (departmentChartRef.value) {
    departmentChartInstance = echarts.init(departmentChartRef.value);
    departmentChartInstance.setOption({
      tooltip: {
        trigger: 'axis',
        axisPointer: { type: 'shadow' },
      },
      legend: {
        orient: 'horizontal',
        left: 'center',
        top: 'top',
        icon: 'circle',
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis: {
        type: 'category',
        data: [],
      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '资产数量',
          type: 'bar',
          data: [],
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#165DFF' },
              { offset: 1, color: '#36CBCB' },
            ]),
          },
          barWidth: '60%',
        },
      ],
    });
  }

  // 资产分类统计图表
  if (categoryChartRef.value) {
    categoryChartInstance = echarts.init(categoryChartRef.value);
    categoryChartInstance.setOption({
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)',
      },
      legend: {
        orient: 'horizontal',
        left: 'center',
        top: 'top',
        icon: 'circle',
      },
      series: [
        {
          name: '资产分类',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2,
          },
          label: {
            show: false,
            position: 'center',
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 20,
              fontWeight: 'bold',
            },
          },
          labelLine: {
            show: false,
          },
          data: [
            { value: 0, name: '暂无数据', itemStyle: { color: '#165DFF' } },
          ],
        },
      ],
    });
  }

  // 报废资产统计图表
  if (scrapChartRef.value) {
    scrapChartInstance = echarts.init(scrapChartRef.value);
    scrapChartInstance.setOption({
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)',
      },
      legend: {
        orient: 'horizontal',
        left: 'center',
        top: 'top',
        icon: 'circle',
      },
      series: [
        {
          name: '报废资产',
          type: 'pie',
          radius: [30, 110],
          center: ['50%', '50%'],
          roseType: 'area',
          itemStyle: {
            borderRadius: 8,
          },
          data: [
            { value: 0, name: '暂无数据', itemStyle: { color: '#F53F3F' } },
          ],
        },
      ],
    });
  }
};

// 处理部门资产分布数据并更新图表
const updateDepartmentChart = data => {
  if (!data || !Array.isArray(data) || data.length === 0) {
    return;
  }

  // 提取x轴数据（部门名称）
  const xAxisData = data.map(item => item.org_name);

  // 提取y轴数据（资产数量）
  const seriesData = data.map(item => item.asset_count);

  if (departmentChartInstance) {
    departmentChartInstance.setOption({
      xAxis: {
        data: xAxisData,
      },
      series: [
        {
          name: '资产数量',
          data: seriesData,
        },
      ],
    });
  }
};

// 处理资产分类数据并更新图表
const updateCategoryChart = data => {
  if (!data || !Array.isArray(data) || data.length === 0) {
    return;
  }

  // 定义颜色数组，确保有足够的颜色
  const colors = ['#7B61FF', '#00B42A', '#FF7D00', '#F53F3F', '#165DFF', '#36CBCB', '#86909C', '#FF5722'];

  // 处理数据，添加颜色
  const chartData = data.map((item, index) => {
    return {
      value: item.value,
      name: item.name,
      itemStyle: {
        color: colors[index % colors.length],
      },
    };
  });

  if (categoryChartInstance) {
    categoryChartInstance.setOption({
      series: [
        {
          name: '资产分类',
          data: chartData,
        },
      ],
    });
  }
};

// 处理报废资产数据并更新图表
const updateScrapChart = data => {
  if (!data || !Array.isArray(data) || data.length === 0) {
    // 如果没有数据，显示"暂无数据"
    if (scrapChartInstance) {
      scrapChartInstance.setOption({
        legend: {
          data: ['暂无数据'],
        },
        series: [
          {
            name: '报废资产',
            data: [
              { value: 1, name: '暂无数据', itemStyle: { color: '#F53F3F' } },
            ],
          },
        ],
      });
    }
    return;
  }

  // 定义颜色数组，为不同的报废状态分配不同颜色
  const colors = ['#F53F3F', '#FF7D00', '#165DFF', '#86909C', '#00B42A', '#7B61FF', '#36CBCB', '#FF5722'];

  // 处理接口返回的数据，将categoryName映射为name，quantity映射为value
  // 确保所有数据项都显示，包括值为0的项
  const chartData = data.map((item, index) => {
    return {
      value: item.quantity,
      name: item.categoryName,
      itemStyle: {
        color: colors[index % colors.length],
      },
    };
  });

  // 提取所有分类名称用于图例显示
  const legendData = data.map(item => item.categoryName);

  if (scrapChartInstance) {
    scrapChartInstance.setOption({
      legend: {
        data: legendData,
      },
      series: [
        {
          name: '报废资产',
          data: chartData,
          // 确保值为0的数据项也能显示标签
          label: {
            show: true,
            formatter: function (params) {
              // 即使值为0也显示标签
              return params.name + ': ' + params.value;
            },
          },
          // 启用emphasis效果，提升用户体验
          emphasis: {
            scale: true,
          },
        },
      ],
    });
  }
};

// 将部门数据转为树形选择数据
const transformData = data => {
  if (!data || !Array.isArray(data)) {
    return [];
  }

  const result = [];
  
  data.forEach(item => {
    // 如果当前节点的isClient为true，则不包含该节点，但处理其子节点
    if (item.isClient === true) {
      // 递归处理子节点并添加到结果中
      if (item.childrenDatas && Array.isArray(item.childrenDatas) && item.childrenDatas.length > 0) {
        const childNodes = transformData(item.childrenDatas);
        result.push(...childNodes);
      }
    } else {
      // 正常包含该节点
      const transformed = {
        label: item.name,
        value: item.id,
      };

      // 如果有子节点，递归处理
      if (item.childrenDatas && Array.isArray(item.childrenDatas) && item.childrenDatas.length > 0) {
        transformed.children = transformData(item.childrenDatas);
      }

      result.push(transformed);
    }
  });

  return result;
};

// 获取指定月份前几个月的开始和结束日期
const getDateRange = monthsBack => {
  const now = new Date();
  const endDate = new Date(now.getFullYear(), now.getMonth() + 1, 0); // 当月最后一天
  const startDate = new Date(endDate.getFullYear(), endDate.getMonth() - monthsBack + 1, 1); // 开始月第一天

  const formatDate = date => {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  };

  return {
    start: `${formatDate(startDate)} 00:00:00`,
    end: `${formatDate(endDate)} 23:59:59`,
  };
};

// 处理资产趋势数据并更新图表
const updateTrendChart = data => {
  if (!data || !Array.isArray(data) || data.length === 0) {
    return;
  }

  // 按月份排序（确保按时间顺序）
  const sortedData = [...data].sort((a, b) => {
    return new Date(a.year, a.month - 1) - new Date(b.year, b.month - 1);
  });

  // 提取x轴数据（月份字符串）
  const xAxisData = sortedData.map(item => item.month_str);

  // 提取各系列数据
  const totalAssetsData = sortedData.map(item => item.total_assets);
  const totalCreatedData = sortedData.map(item => item.total_created);
  const totalScrappedData = sortedData.map(item => item.total_scrapped);
  const totalDecreasedData = sortedData.map(item => item.total_decreased);
  const totalLostData = sortedData.map(item => item.total_lost);

  if (trendChartInstance) {
    trendChartInstance.setOption({
      xAxis: {
        data: xAxisData,
      },
      series: [
        { name: '资产总数', data: totalAssetsData },
        { name: '未删减资产', data: totalCreatedData },
        { name: '报废资产', data: totalScrappedData },
        { name: '减少资产', data: totalDecreasedData },
        { name: '丢失资产', data: totalLostData },
      ],
    });
  }
};

// 根据时间范围查询资产趋势
const getAssetTrend = () => {
  let monthsBack = 12; // 默认近12个月
  if (activeTrendTab.value === 'quarter') {
    monthsBack = 3;
  } else if (activeTrendTab.value === 'month') {
    monthsBack = 6;
  } else if (activeTrendTab.value === 'year') {
    monthsBack = 12;
  }

  const { start, end } = getDateRange(monthsBack);

  queryAssetTrend(start, end, selectedDepartment.value).then(
    res => {
      if (res.errorCode == 0) {
        if (res.results && Array.isArray(res.results)) {
          updateTrendChart(res.results);
        }
      } else {
        message.error(res.errorMessage);
      }
    },
    errorData => {
      Common.processException(errorData);
    },
  );
};

// 监听时间范围变化
watch(activeTrendTab, () => {
  getAssetTrend();
});

// 组件挂载后初始化图表
onMounted(() => {
  nextTick(() => {
    initCharts();
    getOrgList();
    // 初始化时查询所有数据
    refreshAllData();
    
    // 添加窗口resize事件监听，实现图表响应式
    window.addEventListener('resize', handleResize);
  });
});

// 组件卸载时清理
onUnmounted(() => {
  window.removeEventListener('resize', handleResize);

  // 销毁图表实例
  if (trendChartInstance) trendChartInstance.dispose();
  if (departmentChartInstance) departmentChartInstance.dispose();
  if (categoryChartInstance) categoryChartInstance.dispose();
  if (scrapChartInstance) scrapChartInstance.dispose();
});

// 处理窗口大小变化
const handleResize = () => {
  if (trendChartInstance) trendChartInstance.resize();
  if (departmentChartInstance) departmentChartInstance.resize();
  if (categoryChartInstance) categoryChartInstance.resize();
  if (scrapChartInstance) scrapChartInstance.resize();
};
</script>

<style scoped>
/* 主容器 */
.dashboard-container {
  min-height: 100vh;
  background-color: #f9f9f9;
  border-radius: 8px;
}

.dashboard-main {
  margin: 10px;
  padding: 20px;
}

/* 页面标题 */
.dashboard-header {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-bottom: 12px;
}

.header-title .title {
  font-size: clamp(1.5rem, 3vw, 2rem);
  font-weight: bold;
  margin: 0;
  color: #1d2129;
}

.header-title .subtitle {
  color: #86909c;
  margin: 4px 0 0;
}

.header-filters {
  margin-top: 16px;
}

/* 统计卡片 */
.overview-cards {
  margin-bottom: 24px;
}

.stat-card {
  border-radius: 12px;
  border: 1px solid #e5e6eb;
  transition: all 0.3s ease;
}

.stat-card:hover {
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.1);
  border-color: #d0d3d9;
}

.card-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.card-info .card-label {
  color: #86909c;
  font-size: 14px;
  margin: 0 0 4px;
}

.card-info .card-value {
  font-size: 24px;
  font-weight: bold;
  margin: 0;
  color: #1d2129;
}

.card-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.primary-icon {
  background-color: rgba(22, 93, 255, 0.1);
  color: #165dff;
}

.success-icon {
  background-color: rgba(0, 180, 42, 0.1);
  color: #00b42a;
}

.warning-icon {
  background-color: rgba(255, 125, 0, 0.1);
  color: #ff7d00;
}

.secondary-icon {
  background-color: rgba(54, 203, 203, 0.1);
  color: #36cbcb;
}

.card-footer {
  display: flex;
  align-items: center;
  margin-top: 16px;
}

.trend-up {
  color: #00b42a;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.trend-down {
  color: #f53f3f;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.trend-label {
  color: #c9cdd4;
  font-size: 14px;
  margin-left: 8px;
}

/* 趋势和搜索区域 */
.trend-search-section {
  margin-bottom: 24px;
}

.chart-card,
.search-card {
  border-radius: 12px;
  border: 1px solid #e5e6eb;
  height: 100%;
}

.search-card {
  display: flex;
  flex-direction: column;
}

.search-card .ant-card-body {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.chart-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
  color: #1d2129;
}

.chart-container {
  position: relative;
  height: 340px;
}

.form-label {
  display: block;
  font-size: 14px;
  color: #86909c;
  margin-bottom: 4px;
}

.link-primary {
  color: #165dff;
  font-size: 14px;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 4px;
}

.link-primary:hover {
  text-decoration: underline;
}

/* 图表区域 */
.charts-section {
  margin-bottom: 24px;
}

.charts-section .chart-card {
  border: 1px solid #e5e6eb;
}

/* 列表区域 */
.list-section {
  margin-bottom: 24px;
}

.list-card {
  border-radius: 12px;
  border: 1px solid #e5e6eb;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.list-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
  height: 800px;
  max-height: 800px;
  overflow: auto;
}

.list-item {
  padding: 16px;
  border: 1px solid #e5e6eb;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.list-item:hover {
  background-color: #f7f8fa;
}

.item-main {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.item-info {
  flex: 1;
}

.item-title {
  font-size: 16px;
  font-weight: 500;
  margin: 0 0 4px;
  color: #1d2129;
}

.item-desc {
  font-size: 14px;
  color: #86909c;
  margin: 0;
}

.item-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
}

.item-date {
  font-size: 14px;
  color: #86909c;
}

/* 页脚 */
.dashboard-footer {
  background-color: #fff;
  border-top: 1px solid #e5e6eb;
  padding: 16px 0;
  margin-top: 40px;
}

.dashboard-footer p {
  text-align: center;
  font-size: 14px;
  color: #86909c;
  margin: 0;
}

/* 响应式布局 */
@media (min-width: 768px) {
  .dashboard-header {
    flex-direction: row;
    align-items: center;
  }

  .header-filters {
    margin-top: 0;
  }
}

@media (max-width: 1024px) {
  .chart-container {
    height: 250px;
  }
}

@media (max-width: 768px) {
  .dashboard-main {
    padding: 80px 12px 32px;
  }

  .chart-container {
    height: 220px;
  }
}
</style>
