广州矛调粤政易端
xusd
7 days ago d27794814b69d18aeb8ee96a46cae91d5613570c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import React, { useState, useEffect } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { Input, Toast } from 'dingtalk-design-mobile';
import { SearchOutlined } from 'dd-icons';
import NavBarPage from '../../../components/NavBarPage';
import caseTypeSelect from '../../../utils/caseTypeSelect';
import './index.less';
 
const DisputeTypePicker = () => {
  const history = useHistory();
  const location = useLocation();
  const [searchValue, setSearchValue] = useState('');
  const [selectedCategory, setSelectedCategory] = useState(''); // 初始设为空字符串
  const [searchResults, setSearchResults] = useState([]);
  const [showSearchResults, setShowSearchResults] = useState(false);
  const searchCategoryId = 'search-results-category'; // 搜索结果分类的唯一ID
 
  // 组件初始化时设置默认选中的分类
  useEffect(() => {
    try {
      if (caseTypeSelect && Array.isArray(caseTypeSelect.caseTypeSelect) && caseTypeSelect.caseTypeSelect.length > 0) {
        // 默认选中第一个分类
        setSelectedCategory(caseTypeSelect.caseTypeSelect[0].value);
      }
    } catch (error) {
      console.error('初始化分类选择失败:', error);
    }
  }, []);
 
  // 处理返回按钮
  const handleBack = () => {
    history.goBack();
  };
 
  // 高亮搜索关键词
  const highlightText = (text, keyword) => {
    if (!keyword.trim()) return text;
    
    const parts = text.split(new RegExp(`(${keyword})`, 'gi'));
    return (
      <>
        {parts.map((part, index) => 
          part.toLowerCase() === keyword.toLowerCase() ? 
            <span key={index} className="highlight-text">{part}</span> : 
            part
        )}
      </>
    );
  };
 
  // 处理搜索
  const handleSearch = () => {
    if (!searchValue.trim()) {
      setShowSearchResults(false);
      return;
    }
 
    const results = [];
    caseTypeSelect.caseTypeSelect.forEach(category => {
      if (category && Array.isArray(category.children) && category.children.length > 0) {
        category.children.forEach(item => {
          if (item && item.label && item.label.includes(searchValue)) {
            results.push({
              ...item,
              categoryName: category.label || '未分类',
              categoryValue: category.value
            });
          }
        });
      }
    });
 
    setSearchResults(results);
    setShowSearchResults(true);
    
    // 不管是否有搜索结果,只要执行了搜索,都选中"搜索结果"分类
    setSelectedCategory(searchCategoryId);
  };
 
  // 处理搜索输入变化
  const handleSearchInputChange = (value) => {
    setSearchValue(value);
    if (!value) {
      // 当搜索框清空时,如果之前在搜索结果页面,则切换到第一个分类
      if (selectedCategory === searchCategoryId && caseTypeSelect.caseTypeSelect.length > 0) {
        setSelectedCategory(caseTypeSelect.caseTypeSelect[0].value);
      }
      setShowSearchResults(false);
    }
  };
  
  // 处理分类选择
  const handleCategorySelect = (categoryValue) => {
    setSelectedCategory(categoryValue);
    // 如果选择了非搜索结果分类,则清除搜索状态和搜索框内容
    if (categoryValue !== searchCategoryId) {
      setShowSearchResults(false);
      setSearchValue(''); // 清空搜索框内容
    }
  };
 
  // 处理类型选择
  const handleTypeSelect = (type) => {
    // 检查type是否包含必要的属性
    if (!type || !type.value || !type.label) {
      Toast.fail({
        content: '选择的类型数据不完整',
        duration: 2,
      });
      return;
    }
    
    // 构造完整的选择信息,包括一级案由和二级案件
    const selectedTypeInfo = {
      // 二级案件信息
      value: type.value,  // 二级案件的value,对应表单中的caseType
      label: type.label,  // 二级案件的label,对应表单中的caseTypeName
      
      // 一级案由信息
      caseTypeFirst: type.categoryValue,  // 一级案由的value
      caseTypeFirstName: type.categoryName // 一级案由的label
    };
    
    console.log('将要保存的纠纷类型信息:', selectedTypeInfo);
    
    // 将选择的完整纠纷类型信息存储到 sessionStorage
    try {
      sessionStorage.setItem('selectedDisputeType', JSON.stringify(selectedTypeInfo));
      
      // 同时更新caseDes_form_cache缓存
      const formCacheKey = 'caseDes_form_cache';
      const existingCache = sessionStorage.getItem(formCacheKey);
      let formCache = {};
      
      if (existingCache) {
        try {
          formCache = JSON.parse(existingCache);
        } catch (parseError) {
          console.error('解析表单缓存失败:', parseError);
          // 继续使用空对象
        }
      }
      
      // 更新caseDes_form_cache中的纠纷类型相关字段
      const updatedCache = {
        ...formCache,
        // 确保使用统一的字段名,与BasicInfoForm组件中使用的保持一致
        caseType: selectedTypeInfo.value,           // 二级案件value
        caseTypeName: selectedTypeInfo.label,       // 二级案件label
        caseTypeFirst: selectedTypeInfo.caseTypeFirst, // 一级案由value
        caseTypeFirstName: selectedTypeInfo.caseTypeFirstName // 一级案由label
      };
      
      // 保存更新后的缓存
      sessionStorage.setItem(formCacheKey, JSON.stringify(updatedCache));
      console.log('成功更新表单缓存中的纠纷类型数据:', {
        caseType: updatedCache.caseType,
        caseTypeName: updatedCache.caseTypeName,
        caseTypeFirst: updatedCache.caseTypeFirst,
        caseTypeFirstName: updatedCache.caseTypeFirstName
      });
      
      Toast.success({
        content: `已选择${selectedTypeInfo.caseTypeFirstName}/${type.label}`,
        duration: 1,
      });
      
      // 返回上一页
      history.goBack();
    } catch (error) {
      console.error('保存选择数据失败:', error);
      Toast.fail({
        content: '保存选择失败,请重试',
        duration: 2,
      });
    }
  };
 
  // 渲染左侧分类导航
  const renderCategoryNav = () => {
    const categories = [...caseTypeSelect.caseTypeSelect];
    
    // 只要执行过搜索操作就添加"搜索结果"分类,不管是否有搜索结果
    const searchResultsCategory = showSearchResults ? 
      [{
        value: searchCategoryId,
        label: '搜索结果'
      }] : [];
    
    return (
      <div className="category-nav">
        {[...searchResultsCategory, ...categories].map(category => (
          <div 
            key={category.value}
            className={`category-nav-item ${selectedCategory === category.value ? 'active' : ''}`}
            onClick={() => handleCategorySelect(category.value)}
          >
            {category.label}
          </div>
        ))}
      </div>
    );
  };
 
  // 渲染右侧类型列表
  const renderTypeList = () => {
    // 如果当前选中的是搜索结果分类,则显示搜索结果
    if (selectedCategory === searchCategoryId && showSearchResults) {
      return renderSearchResults();
    }
    
    const currentCategory = caseTypeSelect.caseTypeSelect.find(item => item.value === selectedCategory);
    
    if (!currentCategory) return null;
    
    // 确保children存在且是一个数组
    const children = Array.isArray(currentCategory.children) ? currentCategory.children : [];
    
    return (
      <div className="type-list">
        <div className="type-category-title">{currentCategory.label}</div>
        <div className="type-items">
          {children.length > 0 ? (
            children.map((type, index) => {
              // 为每个类型项添加一级案由信息
              const typeWithCategory = {
                ...type,
                categoryName: currentCategory.label,
                categoryValue: currentCategory.value
              };
              
              return (
                <div 
                  key={type.value || `type-${Math.random()}`}
                  className="type-item"
                  onClick={() => handleTypeSelect(typeWithCategory)}
                >
                  <div className="type-item-icon">
                    <div className="inner-icon"></div>
                  </div>
                  <div className="type-item-label">{type.label}</div>
                </div>
              );
            })
          ) : (
            <div className="no-items">暂无数据</div>
          )}
        </div>
      </div>
    );
  };
 
  // 渲染搜索结果
  const renderSearchResults = () => {
    if (searchResults.length === 0) {
      // 返回空内容,但保留容器
      return <div className="search-results"></div>;
    }
 
    // 按一级案由分组
    const groupedResults = searchResults.reduce((acc, type) => {
      const categoryItem = caseTypeSelect.caseTypeSelect.find(cat => cat.value === type.categoryValue);
      const categoryName = categoryItem ? categoryItem.label : '未分类';
      
      if (!acc[categoryName]) {
        acc[categoryName] = {
          categoryValue: type.categoryValue,
          items: []
        };
      }
      
      acc[categoryName].items.push(type);
      return acc;
    }, {});
 
    return (
      <div className="search-results">
        {Object.entries(groupedResults).map(([categoryName, group]) => (
          <div key={categoryName} className="search-category">
            <div className="type-category-title">{categoryName}</div>
            <div className="type-items">
              {group.items.map((type) => {
                // 为搜索结果添加一级案由信息
                const typeWithCategory = {
                  ...type,
                  categoryName: categoryName,
                  categoryValue: group.categoryValue
                };
                
                return (
                  <div
                    key={type.value}
                    className="type-item"
                    onClick={() => handleTypeSelect(typeWithCategory)}
                  >
                    <div className="type-item-icon">
                      <div className="inner-icon"></div>
                    </div>
                    <div className="type-item-label">
                      {highlightText(type.label, searchValue)}
                    </div>
                  </div>
                );
              })}
            </div>
          </div>
        ))}
      </div>
    );
  };
 
  return (
    <div className="dispute-type-picker">
      <NavBarPage 
        leftContentVisible={true} 
        title="选择纠纷类型" 
        leftContentFunc={handleBack} 
      />
      
      <div className="main-container">
        {/* 搜索区域 */}
        <div className="search-container">
          <div className="search-context">
            <div className="search-icon">
              <SearchOutlined />
            </div>
            <Input
              placeholder="搜索纠纷类型"
              value={searchValue}
              onChange={(value) => {
                handleSearchInputChange(value);
              }}
              onKeyPress={(e) => {
                if (e.key === 'Enter') {
                  handleSearch();
                }
              }}
            />
            <div className="search-button" onClick={handleSearch}>
              <p className="search-split">|</p>
              <p className="search-text">查询</p>
            </div>
          </div>
        </div>
 
        {/* 类型选择区域 - 不再使用showSearchResults控制显示,而是通过选中的category类型控制 */}
        <div className="category-container">
          {renderCategoryNav()}
          {renderTypeList()}
        </div>
      </div>
    </div>
  );
};
 
export default DisputeTypePicker;