// ViewReviewPage.tsx
import React, { useState, useEffect } from 'react';
import { useLocation } from 'umi'; // 或者 'react-router-dom'
import ResultModal from './components/ResultModal';
import ViewModal from './components/ViewModal';
import { refreshDictCache } from '@/servers/api/login';
import { encryptWithRsa } from '@/utils/encryptWithRsa'


const ViewReviewPage: React.FC = () => {
  const [modalVisible, setModalVisible] = useState(false); // 控制弹窗
  const [modalRecord, setModalRecord] = useState<any>(null);
 

  useEffect(() => {
    const params = new URLSearchParams(location.search);
    const base64 = params.get('code');
    console.log(base64);

    if (!base64) return;
    // 解码
    const decodedStr = atob(base64);
    // 再次转成参数
    const p2 = new URLSearchParams(decodedStr);
    if (p2.get('id')) { 
      if (!sessionStorage.getItem('dict')) {
        refreshDictCache().then((res) => {
          if (res.code == 200) {
            sessionStorage.setItem('dict', JSON.stringify(res.data))
          }
        })
      }
      sessionStorage.setItem('userId',  encryptWithRsa(p2.get('userId')))
      setModalRecord({ id: p2.get('id') });  
      setModalVisible(true);
    }
  }, []);

 
  return (
    <div style={{ padding: '20px' }}>
      {/* 其他页面内容  */}
      <ViewModal
        visible={modalVisible}
        record={modalRecord}
        onCancel={() => setModalVisible(false)}
      />
      <ResultModal
        visible={modalVisible}
        record={modalRecord}
        onCancel={() => setModalVisible(false)}
      />
    </div>
  );
};

export default ViewReviewPage;
