// 评价结果详情
import React, { useState, useEffect } from 'react';
import { Card, Form, Button, Descriptions, Divider, Row, Col, Tabs, message } from 'antd';
import { ArrowLeftOutlined } from '@ant-design/icons';
import { history, useLocation } from 'umi';
import { EvaluateLevel, EvaluateLevelText } from '@/dicts/supplierTemplateDict';
import GeneralEvaluation from './components/GeneralEvaluation';
import TechnicalEvaluation from './components/TechnicalEvaluation';
import styles from './supplierEvaluateResult.less';

const { TabPane } = Tabs;

// 组件使用默认的接口定义

const SupplierEvaluateResultByZb: React.FC = () => {
  const location = useLocation<{ record: any }>();
  const [loading, setLoading] = useState<boolean>(false);
  const [scoreRecord, setScoreRecord] = useState<any>(null);
  const [evaluationData, setEvaluationData] = useState<{
    general: any[];
    technical: any[];
  }>({
    general: [],
    technical: [],
  });

  // 模拟获取评价详情数据
  const fetchEvaluationData = (record: any) => {
    setLoading(true);
    try {
      // 模拟API请求
      setTimeout(() => {
        // 模拟通用评价数据
        const generalData = [
          {
            id: '1',
            firstIndex: '质量管理',
            firstDescription: '供应商质量管理体系评价',
            secondIndex: '质量管理体系认证',
            secondDescription: '是否通过ISO9001认证',
            score: 9,
            weight: 0.1,
            weightedScore: 0.9,
            comment: '已通过ISO9001认证，证书有效',
          },
          {
            id: '2',
            firstIndex: '质量管理',
            firstDescription: '供应商质量管理体系评价',
            secondIndex: '质量控制流程',
            secondDescription: '是否建立完善的质量控制流程',
            score: 8,
            weight: 0.1,
            weightedScore: 0.8,
            comment: '质量控制流程完善，但执行记录不够完整',
          },
          {
            id: '3',
            firstIndex: '交付能力',
            firstDescription: '供应商交付能力评价',
            secondIndex: '准时交付率',
            secondDescription: '过去一年准时交付率评价',
            score: 9.5,
            weight: 0.15,
            weightedScore: 1.425,
            comment: '准时交付率98%，表现优秀',
          },
        ];

        // 模拟技术评价数据
        const technicalData = [
          {
            id: '1',
            firstIndex: '技术能力',
            firstDescription: '供应商技术研发能力评价',
            secondIndex: '研发投入',
            secondDescription: '研发投入占营业额比例',
            score: 8.5,
            weight: 0.1,
            weightedScore: 0.85,
            comment: '研发投入占营业额5%，符合行业平均水平',
          },
          {
            id: '2',
            firstIndex: '技术能力',
            firstDescription: '供应商技术研发能力评价',
            secondIndex: '专利数量',
            secondDescription: '拥有专利数量评价',
            score: 9,
            weight: 0.05,
            weightedScore: 0.45,
            comment: '拥有15项有效专利，其中发明专利5项',
          },
          {
            id: '3',
            firstIndex: '设备设施',
            firstDescription: '供应商设备设施评价',
            secondIndex: '设备先进性',
            secondDescription: '生产设备先进程度评价',
            score: 8,
            weight: 0.1,
            weightedScore: 0.8,
            comment: '主要生产设备较为先进，但部分设备需要更新',
          },
        ];

        setEvaluationData({
          general: generalData,
          technical: technicalData,
        });
        setLoading(false);
      }, 500);
    } catch (error) {
      console.error('获取评价详情数据失败:', error);
      message.error('获取评价详情数据失败');
      setLoading(false);
    }
  };

  // 获取上级页面传递的数据
  useEffect(() => {
    if (location.state?.record) {
      setScoreRecord(location.state.record);
      // 模拟获取评价详情数据
      fetchEvaluationData(location.state.record);
    }
  }, [location]);

  // 返回上一页
  const handleBack = () => {
    history.goBack();
  };

  // 计算总得分
  const calculateTotalScore = () => {
    let totalScore = 0;
    let totalWeight = 0;

    // 计算通用评价得分
    evaluationData.general.forEach((item: any) => {
      totalScore += item.weightedScore;
      totalWeight += item.weight;
    });

    // 计算技术评价得分
    evaluationData.technical.forEach((item: any) => {
      totalScore += item.weightedScore;
      totalWeight += item.weight;
    });

    // 如果权重总和不为0，则计算加权平均分
    if (totalWeight > 0) {
      return (totalScore / totalWeight).toFixed(2);
    }

    return '0.00';
  };

  // 获取评价等级
  const getEvaluateLevel = (score: number) => {
    if (score >= 90) {
      return EvaluateLevelText[EvaluateLevel.EXCELLENT];
    } else if (score >= 80) {
      return EvaluateLevelText[EvaluateLevel.GOOD];
    } else if (score >= 70) {
      return EvaluateLevelText[EvaluateLevel.AVERAGE];
    } else {
      return EvaluateLevelText[EvaluateLevel.POOR];
    }
  };

  if (!scoreRecord) {
    return <div className="common-container">加载中...</div>;
  }

  return (
    <div className="common-container">
      <div className="action-row">
        <Button type="link" icon={<ArrowLeftOutlined />} onClick={handleBack}>
          返回
        </Button>
      </div>

      <Card title="评价基本信息" bordered={false} className={styles.infoCard}>
        <Descriptions column={3}>
          <Descriptions.Item label="供应商名称">{scoreRecord.supplierName}</Descriptions.Item>
          <Descriptions.Item label="品类">{scoreRecord.category}</Descriptions.Item>
          <Descriptions.Item label="评价单位">{scoreRecord.evaluateUnit}</Descriptions.Item>
          <Descriptions.Item label="评价人员">{scoreRecord.evaluator}</Descriptions.Item>
          <Descriptions.Item label="评价时间">{scoreRecord.evaluateTime}</Descriptions.Item>
          <Descriptions.Item label="评价得分">{scoreRecord.score}</Descriptions.Item>
          <Descriptions.Item label="评价等级">
            {getEvaluateLevel(scoreRecord.score)}
          </Descriptions.Item>
        </Descriptions>
      </Card>

      <Card title="评价详情" bordered={false} className={styles.detailCard}>
        <GeneralEvaluation supplierName={scoreRecord.supplierName} />
      </Card>
    </div>
  );
};

export default SupplierEvaluateResultByZb;
