import React, { useState, useEffect } from 'react';
import ResultModal from './components/ResultModal';
import ViewModal from './components/ViewModal';
import BlacklistApproval from './components/BlacklistApproval';
import ExitApproval from './components/ExitApproval';
import CategoryLibraryApproval from './components/CategoryLibraryApproval';
import CategoryLibrarySupplierApproval from './components/CategoryLibrarySupplierApproval';
import SupplierCategoryAccessApproval from './components/SupplierCategoryAccessApproval';
import SupplierInfoChangeApproval from './components/SupplierInfoChangeApproval';
import { refreshDictCache } from '@/servers/api/login';
import { encryptWithRsa } from '@/utils/encryptWithRsa';


const ViewReviewPage: React.FC = () => {
  const [modalVisible, setModalVisible] = useState<boolean>(false);
  const [modalRecord, setModalRecord] = useState<{ id: string } | null>(null);
  const [type, setType] = useState<string>(''); // 空串即可

  useEffect(() => {
    const params = new URLSearchParams(window.location.search);
    const base64 = params.get('code');
    if (!base64) return;
    const b64 = base64.replace(/-/g, '+').replace(/_/g, '/');
    try {
      // 解码
      const decodedStr = atob(b64);
      const query = /%[0-9A-F]{2}/i.test(decodedStr) ? decodeURIComponent(decodedStr) : decodedStr;
      const p2 = new URLSearchParams(query);
      const id = p2.get('id') ?? '';
      const code = p2.get('code') ?? '';
      const userId = p2.get('userId') ?? '';
      if (!id) return;
      setType(code); // code 现在一定是 string
      // 初始化字典
      if (!sessionStorage.getItem('dict')) {
        refreshDictCache().then((res) => {
          if (res?.code === 200) {
            sessionStorage.setItem('dict', JSON.stringify(res.data));
          }
        });
      }
      // 只有在 userId 存在时再加密保存
      if (userId) {
        sessionStorage.setItem('userId', encryptWithRsa(userId));
      }
      setModalRecord({ id });
      setModalVisible(true);
    } catch (err) {
      // atob 或者 URLSearchParams 解析失败
      console.error('解析 code 失败：', err);
    }
  }, []);

  /**
   * SUPPLIER_ACCESS_APPROVAL("supplierAccessApproval", "供应商准入审批"),
   * SUPPLIER_CATEGORY_ACCESS_APPROVAL("supplierCategoryAccessApproval", "供应商品类准入审批"),
   * SUPPLIER_INFO_CHANGE_APPROVAL("supplierInfoChangeApproval", "供应商信息变更审批"),
   * BLACKLIST_APPROVAL("blacklistApproval", "黑名单审批"),
   * EXIT_APPROVAL("exitApproval", "退出审批"),
   * CATEGORY_LIBRARY_APPROVAL("categoryLibraryApproval", "品类库审批"),
   * CATEGORY_LIBRARY_SUPPLIER_APPROVAL("categoryLibrarySupplierApproval","品类库供应商入库审批"),
   * EVALUATION_APPROVAL("evaluationApproval", "评价审批");
   */

  return (
    <div style={{ padding: '20px' }}>
      {/* 供应商准入审批 */}
      {['supplierAccessApproval'].includes(type) && (
        <>
          <ViewModal
            visible={modalVisible}
            record={modalRecord ?? undefined}
          />
          <ResultModal
            visible={modalVisible}
            record={modalRecord ?? undefined}
          />
        </>
      )}
      {/* 供应商品类准入审批 */}
      {['supplierCategoryAccessApproval'].includes(type) && (
        <>
          <SupplierCategoryAccessApproval
            visible={modalVisible}
            record={modalRecord ?? undefined}
          />
        </>
      )}
      {/* 供应商信息变更审批*/}
      {['supplierInfoChangeApproval'].includes(type) && (
        <>
          <SupplierInfoChangeApproval
            visible={modalVisible}
            record={modalRecord}
          />
        </>
      )}
      {/* 黑名单审批*/}
      {['blacklistApproval'].includes(type) && (
        <>
          <BlacklistApproval
            visible={modalVisible}
            record={modalRecord}
          />
        </>
      )}
      {/* 退出审批 ----*/}
      {['exitApproval'].includes(type) && (
        <>
          <ExitApproval
            visible={modalVisible}
            record={modalRecord}
          />
        </>
      )}
      {/* 品类库审批 */}
      {['categoryLibraryApproval'].includes(type) && (
        <>
          <CategoryLibraryApproval
            visible={modalVisible}
            record={modalRecord}
          />
        </>
      )}
      {/* 品类库供应商入库审批 */}
      {['categoryLibrarySupplierApproval'].includes(type) && (
        <>
          <CategoryLibrarySupplierApproval
            visible={modalVisible}
            record={modalRecord}
          />
        </>
      )}
      {/* 评价审批 */}
      {['evaluationApproval'].includes(type) && (
        <>
          {/* <EvaluationApproval
            visible={modalVisible}
            record={modalRecord}
          /> */}
        </>
      )}



    </div>
  );
};

export default ViewReviewPage;
