add_member.html 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070
  1. {% extends "layout.html" %}
  2. {% block title %}{{ '编辑' if member else '录入' }}成员 - 家谱管理系统{% endblock %}
  3. {% block extra_css %}
  4. <style>
  5. .split-container { display: flex; height: calc(100vh - 100px); overflow: hidden; }
  6. .form-panel { flex: 1.2; padding: 20px; overflow-y: auto; border-right: 1px solid #dee2e6; }
  7. .image-panel { flex: 0.8; padding: 20px; background: #f8f9fa; display: flex; flex-direction: column; }
  8. .image-viewer { flex: 1; border: 1px solid #ccc; background: white; overflow: hidden; text-align: center; position: relative; }
  9. .image-viewer img { max-width: 100%; height: auto; transition: transform 0.2s, filter 0.2s; transform-origin: top left; cursor: grab; }
  10. .image-viewer img.dragging { cursor: grabbing; }
  11. /* 放大镜样式 */
  12. .magnifier-glass {
  13. position: absolute;
  14. border: 3px solid #000;
  15. border-radius: 50%;
  16. cursor: none;
  17. width: 150px;
  18. height: 150px;
  19. box-shadow: 0 0 10px rgba(0,0,0,0.5);
  20. display: none;
  21. z-index: 1000;
  22. background-repeat: no-repeat;
  23. background-color: white;
  24. pointer-events: none;
  25. }
  26. /* Image Viewer & Dragging */
  27. .image-viewer {
  28. flex: 1;
  29. border: 1px solid #ccc;
  30. background: #f0f0f0;
  31. overflow: hidden;
  32. text-align: center;
  33. position: relative;
  34. cursor: grab;
  35. user-select: none;
  36. }
  37. .image-viewer:active {
  38. cursor: grabbing;
  39. }
  40. .image-wrapper {
  41. display: inline-block;
  42. transition: transform 0.2s ease-out;
  43. transform-origin: center center;
  44. position: absolute;
  45. /* Initial centering will be handled by JS or CSS translate */
  46. top: 50%;
  47. left: 50%;
  48. transform: translate(-50%, -50%);
  49. }
  50. .image-wrapper img {
  51. max-width: 100%;
  52. max-height: 100vh;
  53. display: block;
  54. pointer-events: none;
  55. user-select: none;
  56. transition: filter 0.2s;
  57. }
  58. .image-toolbar {
  59. background: #e9ecef;
  60. padding: 5px 10px;
  61. border-bottom: 1px solid #dee2e6;
  62. display: flex;
  63. gap: 10px;
  64. align-items: center;
  65. flex-wrap: wrap;
  66. }
  67. .image-toolbar .btn-group-xs > .btn, .image-toolbar .btn-sm {
  68. padding: 0.25rem 0.5rem;
  69. font-size: 0.875rem;
  70. }
  71. .filter-controls { display: flex; align-items: center; gap: 5px; font-size: 0.8rem; }
  72. .filter-controls input[type=range] { width: 80px; }
  73. .page-nav { margin-bottom: 10px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
  74. #imageTabNav .nav-link { cursor: pointer; font-size: 0.9rem; padding: 0.35rem 0.75rem; }
  75. .reference-empty-state { color: #6c757d; padding: 40px 20px; text-align: center; }
  76. .reference-empty-state i { font-size: 2.5rem; display: block; margin-bottom: 10px; }
  77. .section-title { border-left: 4px solid #0d6efd; padding-left: 10px; margin: 25px 0 15px; font-weight: bold; color: #333; }
  78. .father-lineage-hint {
  79. background-color: #f8f9fa;
  80. border-left: 4px solid #17a2b8;
  81. padding: 8px 12px;
  82. border-radius: 4px;
  83. font-size: 14px;
  84. margin-top: 8px;
  85. margin-bottom: 12px;
  86. }
  87. .father-lineage-hint .text-info {
  88. color: #17a2b8;
  89. }
  90. </style>
  91. {% endblock %}
  92. {% block content %}
  93. <div class="split-container">
  94. <!-- 左侧:录入/编辑表单 -->
  95. <div class="form-panel">
  96. <div class="card shadow-sm mb-4">
  97. <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
  98. <h5 class="mb-0">{{ '编辑成员信息' if member else '录入新成员' }}</h5>
  99. <a href="{{ url_for('members') }}" class="btn btn-sm btn-light">返回列表</a>
  100. </div>
  101. <div class="card-body">
  102. <form method="POST">
  103. <input type="hidden" name="source_record_id" value="{{ source_record_id or '' }}">
  104. <input type="hidden" name="source_index" value="">
  105. <input type="hidden" name="reference_oss_url" id="referenceOssUrl" value="{{ member.reference_oss_url if member and member.reference_oss_url else '' }}">
  106. <input type="hidden" name="reference_file_name" id="referenceFileName" value="{{ member.reference_file_name if member and member.reference_file_name else '' }}">
  107. <input type="hidden" name="delete_reference" id="deleteReference" value="0">
  108. <div class="section-title">核心信息 (必填)</div>
  109. <div class="row g-3 mb-4">
  110. <div class="col-md-6">
  111. <label class="form-label">姓名(繁体) <span class="text-danger">*</span></label>
  112. <input type="text" name="name" id="nameInput" class="form-control" required value="{{ member.name if member else '' }}">
  113. <div id="nameCheckResult" class="mt-2"></div>
  114. </div>
  115. <div class="col-md-6">
  116. <label class="form-label">姓名(简体)</label>
  117. <input type="text" name="simplified_name" class="form-control" value="{{ member.simplified_name if member else '' }}">
  118. </div>
  119. <div class="col-md-12">
  120. <label class="form-label">族谱原文(繁体)</label>
  121. <input type="text" name="genealogy_original_traditional" class="form-control" value="{{ member.genealogy_original_traditional if member and member.genealogy_original_traditional and member.genealogy_original_traditional != 'None' else '' }}" placeholder="录入族谱原文中的繁体姓名及相关信息">
  122. </div>
  123. <div class="col-md-12">
  124. <label class="form-label">族谱原文(简体)</label>
  125. <input type="text" name="genealogy_original_simplified" class="form-control" value="{{ member.genealogy_original_simplified if member and member.genealogy_original_simplified and member.genealogy_original_simplified != 'None' else '' }}" placeholder="录入族谱原文中的简体姓名及相关信息">
  126. </div>
  127. <div class="col-md-6">
  128. <label class="form-label">性别 <span class="text-danger">*</span></label>
  129. <select name="sex" class="form-select" required>
  130. <option value="1" {{ 'selected' if member and member.sex == 1 else '' }}>男</option>
  131. <option value="2" {{ 'selected' if member and member.sex == 2 else '' }}>女</option>
  132. </select>
  133. </div>
  134. <div class="col-md-6">
  135. <label class="form-label">出生日期 <span class="text-danger">*</span></label>
  136. <div class="input-group has-validation">
  137. {% set birthday_val = member.birthday_date if member and member.birthday_date != '未知' else '' %}
  138. <input type="date" name="birthday" class="form-control" required value="{{ birthday_val }}" onchange="validateAge()">
  139. <div class="input-group-text bg-white">
  140. <input class="form-check-input mt-0" type="checkbox" id="birthdayUnknown" onchange="toggleBirthdayUnknown()" {{ 'checked' if member and member.birthday_date == '未知' else '' }}>
  141. <label class="form-check-label ms-1 small user-select-none" for="birthdayUnknown">不详</label>
  142. </div>
  143. <div class="invalid-feedback" id="ageFeedback"></div>
  144. </div>
  145. </div>
  146. <div class="col-md-8">
  147. <label class="form-label">世系世代</label>
  148. <div id="lineage-generations-container" class="d-flex flex-wrap align-items-center gap-2">
  149. {% if member and member.name_word_generation %}
  150. {% set generations = member.name_word_generation.split(';') %}
  151. {% for gen in generations %}
  152. {% if gen.strip() %}
  153. <span class="lineage-tag badge bg-primary bg-opacity-10 text-primary border border-primary border-opacity-25 px-3 py-2 rounded-pill d-inline-flex align-items-center" style="font-size: 0.85rem;">
  154. {{ gen.strip() }}
  155. <button type="button" class="btn-close ms-2 remove-lineage" style="font-size: 0.55rem; filter: none; opacity: 0.6;" aria-label="删除"></button>
  156. <input type="hidden" name="lineage_generations[]" value="{{ gen.strip() }}">
  157. </span>
  158. {% endif %}
  159. {% endfor %}
  160. {% endif %}
  161. <div id="lineage-input-form" class="d-none">
  162. <div class="input-group input-group-sm" style="width: 220px;">
  163. <input type="text" id="lineage-input" class="form-control" placeholder="如:衢州第二十九代" maxlength="20">
  164. <button type="button" id="confirm-lineage" class="btn btn-success"><i class="bi bi-check-lg"></i></button>
  165. <button type="button" id="cancel-lineage" class="btn btn-outline-secondary"><i class="bi bi-x-lg"></i></button>
  166. </div>
  167. </div>
  168. <button type="button" id="add-lineage" class="btn btn-outline-primary btn-sm rounded-pill px-3 py-1">
  169. <i class="bi bi-plus-lg me-1"></i>添加
  170. </button>
  171. </div>
  172. </div>
  173. <div class="col-md-4">
  174. <label class="form-label">堂内排行</label>
  175. <input type="text" name="family_rank" class="form-control" value="{{ member.family_rank if member else '' }}">
  176. </div>
  177. </div>
  178. <div class="section-title">状态信息</div>
  179. <div class="row g-3 mb-4">
  180. <div class="col-md-6">
  181. <label class="form-label">是否过世</label>
  182. <select name="is_pass_away" class="form-select">
  183. <option value="0" {{ 'selected' if member and member.is_pass_away == 0 else '' }}>健在</option>
  184. <option value="1" {{ 'selected' if member and member.is_pass_away == 1 else '' }}>已故</option>
  185. <option value="2" {{ 'selected' if member and member.is_pass_away == 2 else '' }}>未知</option>
  186. </select>
  187. </div>
  188. <div class="col-md-6">
  189. <label class="form-label">婚姻状况</label>
  190. <select name="marital_status" class="form-select">
  191. <option value="0" {{ 'selected' if member and member.marital_status == 0 else '' }}>未知</option>
  192. <option value="1" {{ 'selected' if member and member.marital_status == 1 else '' }}>未婚</option>
  193. <option value="2" {{ 'selected' if member and member.marital_status == 2 else '' }}>已婚</option>
  194. <option value="3" {{ 'selected' if member and member.marital_status == 3 else '' }}>离异/丧偶</option>
  195. </select>
  196. </div>
  197. </div>
  198. <div class="section-title">关系录入 (选择关联成员及关系)</div>
  199. <div id="relations-container">
  200. <!-- Existing relations will be added here dynamically -->
  201. {% if relations and relations|length > 0 %}
  202. {% for rel in relations %}
  203. <div class="row g-3 mb-1 relation-row" data-index="{{ loop.index0 }}">
  204. <div class="col-md-4">
  205. <label class="form-label">关联成员</label>
  206. <div class="input-group">
  207. <input type="text" class="form-control related-member-display" placeholder="点击选择关联成员" readonly value="{{ selected_member_names[loop.index0] if selected_member_names and selected_member_names[loop.index0] else '' }}">
  208. <input type="hidden" name="relations[{{ loop.index0 }}][parent_mid]" class="related_mid" value="{{ rel.parent_mid }}">
  209. <button type="button" class="btn btn-outline-primary select-member-btn" data-index="{{ loop.index0 }}" data-bs-toggle="modal" data-bs-target="#memberSelectModal">
  210. <i class="bi bi-search"></i>
  211. </button>
  212. </div>
  213. </div>
  214. <div class="col-md-3">
  215. <label class="form-label">关系类型</label>
  216. <select name="relations[{{ loop.index0 }}][relation_type]" class="form-select relation-type">
  217. <option value="">-- 请选择 --</option>
  218. <option value="1" {{ 'selected' if rel.relation_type == 1 else '' }}>父子 (关联人为父)</option>
  219. <option value="2" {{ 'selected' if rel.relation_type == 2 else '' }}>母子 (关联人为母)</option>
  220. <option value="10" {{ 'selected' if rel.relation_type == 10 else '' }}>夫妻</option>
  221. <option value="11" {{ 'selected' if rel.relation_type == 11 else '' }}>兄弟</option>
  222. <option value="12" {{ 'selected' if rel.relation_type == 12 else '' }}>姐妹</option>
  223. </select>
  224. </div>
  225. <div class="col-md-3">
  226. <label class="form-label">子类型</label>
  227. <select name="relations[{{ loop.index0 }}][sub_relation_type]" class="form-select sub-relation-type">
  228. <option value="0" {{ 'selected' if rel.sub_relation_type == 0 else '' }}>亲生/正妻</option>
  229. <option value="1" {{ 'selected' if rel.sub_relation_type == 1 else '' }}>养父</option>
  230. <option value="2" {{ 'selected' if rel.sub_relation_type == 2 else '' }}>出继(亲生父母)</option>
  231. <option value="3" {{ 'selected' if rel.sub_relation_type == 3 else '' }}>入继(养父母)</option>
  232. <option value="10" {{ 'selected' if rel.sub_relation_type == 10 else '' }}>妾</option>
  233. <option value="11" {{ 'selected' if rel.sub_relation_type == 11 else '' }}>外室</option>
  234. </select>
  235. </div>
  236. <div class="col-md-2 d-flex align-items-end">
  237. <button type="button" class="btn btn-danger w-100 remove-relation-btn" {% if relations|length <= 1 %}style="display: none;"{% endif %}>
  238. <i class="bi bi-trash"></i>
  239. </button>
  240. </div>
  241. <div class="col-md-2 child-order-wrapper" style="display: {{ 'block' if rel.relation_type in [1,2] else 'none' }};">
  242. <label class="form-label">第几子</label>
  243. <input type="number" name="relations[{{ loop.index0 }}][child_order]" class="form-control child-order-input"
  244. min="1" placeholder="排行(选填)" value="{{ rel.child_order if rel.child_order else '' }}">
  245. </div>
  246. </div>
  247. {% endfor %}
  248. {% else %}
  249. <div class="row g-3 mb-1 relation-row" data-index="0">
  250. <div class="col-md-4">
  251. <label class="form-label">关联成员</label>
  252. <div class="input-group">
  253. <input type="text" class="form-control related-member-display" placeholder="点击选择关联成员" readonly>
  254. <input type="hidden" name="relations[0][parent_mid]" class="related_mid">
  255. <button type="button" class="btn btn-outline-primary select-member-btn" data-index="0" data-bs-toggle="modal" data-bs-target="#memberSelectModal">
  256. <i class="bi bi-search"></i>
  257. </button>
  258. </div>
  259. </div>
  260. <div class="col-md-3">
  261. <label class="form-label">关系类型</label>
  262. <select name="relations[0][relation_type]" class="form-select relation-type">
  263. <option value="">-- 请选择 --</option>
  264. <option value="1">父子 (关联人为父)</option>
  265. <option value="2">母子 (关联人为母)</option>
  266. <option value="10">夫妻</option>
  267. <option value="11">兄弟</option>
  268. <option value="12">姐妹</option>
  269. </select>
  270. </div>
  271. <div class="col-md-3">
  272. <label class="form-label">子类型</label>
  273. <select name="relations[0][sub_relation_type]" class="form-select sub-relation-type">
  274. <option value="0">亲生/正妻</option>
  275. <option value="1">养父</option>
  276. <option value="2">出继(亲生父母)</option>
  277. <option value="3">入继(养父母)</option>
  278. <option value="10">妾</option>
  279. <option value="11">外室</option>
  280. </select>
  281. </div>
  282. <div class="col-md-2 d-flex align-items-end">
  283. <button type="button" class="btn btn-danger w-100 remove-relation-btn" style="display: none;">
  284. <i class="bi bi-trash"></i>
  285. </button>
  286. </div>
  287. <div class="col-md-2 child-order-wrapper" style="display: none;">
  288. <label class="form-label">第几子</label>
  289. <input type="number" name="relations[0][child_order]" class="form-control child-order-input"
  290. min="1" placeholder="排行(选填)">
  291. </div>
  292. </div>
  293. {% endif %}
  294. </div>
  295. <div class="row mb-4">
  296. <div class="col-md-12">
  297. <button type="button" class="btn btn-outline-success" id="add-relation-btn">
  298. <i class="bi bi-plus-circle"></i> 添加关系
  299. </button>
  300. </div>
  301. </div>
  302. <div class="section-title">人员备注</div>
  303. <div class="row g-3 mb-4">
  304. <div class="col-md-12">
  305. <textarea name="notes" class="form-control" rows="2">{{ member.notes if member else '' }}</textarea>
  306. </div>
  307. </div>
  308. <div class="section-title">疑似错误标注</div>
  309. <div class="row g-3 mb-4">
  310. <div class="col-md-12">
  311. <textarea name="suspected_error" class="form-control" rows="2" onblur="this.value = this.value.trim()">
  312. {{ '' if not member or not member.suspected_error or member.suspected_error in ['None', 'none', 'NULL', 'null'] else member.suspected_error.strip() }}
  313. </textarea>
  314. <div class="form-text mt-1">请在此标注该成员信息中可能存在的错误,例如出生日期矛盾、关系冲突等</div>
  315. </div>
  316. </div>
  317. <!-- 悬浮的保存按钮,始终保持在一屏内或跟随页面底部 -->
  318. <div class="d-grid gap-2 mb-4 sticky-bottom bg-white py-2 border-top" style="z-index: 1020;">
  319. <button type="submit" class="btn btn-success btn-lg">
  320. <i class="bi bi-check-circle me-1"></i> {{ '保存修改' if member else '确认录入' }}
  321. </button>
  322. </div>
  323. <!-- 折叠的其他信息区 -->
  324. <div class="accordion" id="accordionExtraInfo">
  325. <div class="accordion-item border-0">
  326. <h2 class="accordion-header" id="headingExtra">
  327. <button class="accordion-button collapsed bg-light text-secondary rounded shadow-sm border" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExtra" aria-expanded="false" aria-controls="collapseExtra">
  328. <i class="bi bi-three-dots me-2"></i> 展开更多其他信息(谱系详情、联络、履历等)
  329. </button>
  330. </h2>
  331. <div id="collapseExtra" class="accordion-collapse collapse" aria-labelledby="headingExtra" data-bs-parent="#accordionExtraInfo">
  332. <div class="accordion-body px-0 pt-3 pb-0">
  333. <div class="section-title mt-0">谱系详情</div>
  334. <div class="row g-3 mb-4">
  335. <div class="col-md-4">
  336. <label class="form-label">曾用名</label>
  337. <input type="text" name="former_name" class="form-control" value="{{ member.former_name if member else '' }}">
  338. </div>
  339. <div class="col-md-4">
  340. <label class="form-label">幼名/乳名</label>
  341. <input type="text" name="childhood_name" class="form-control" value="{{ member.childhood_name if member else '' }}">
  342. </div>
  343. <div class="col-md-4">
  344. <label class="form-label">字辈</label>
  345. <input type="text" name="name_word" class="form-control" value="{{ member.name_word if member else '' }}">
  346. </div>
  347. <div class="col-md-6">
  348. <label class="form-label">名号/封号</label>
  349. <input type="text" name="name_title" class="form-control" value="{{ member.name_title if member else '' }}">
  350. </div>
  351. <div class="col-md-6">
  352. <label class="form-label">分房/堂号</label>
  353. <input type="text" name="branch_family_hall" class="form-control" value="{{ member.branch_family_hall if member else '' }}">
  354. </div>
  355. <div class="col-md-6">
  356. <label class="form-label">聚居地</label>
  357. <input type="text" name="cluster_place" class="form-control" value="{{ member.cluster_place if member else '' }}">
  358. </div>
  359. </div>
  360. <div class="section-title">联络信息</div>
  361. <div class="row g-3 mb-4">
  362. <div class="col-md-4">
  363. <label class="form-label">民族</label>
  364. <input type="text" name="nation" class="form-control" value="{{ member.nation if member else '' }}">
  365. </div>
  366. <div class="col-md-4">
  367. <label class="form-label">手机号</label>
  368. <input type="text" name="phone" class="form-control" value="{{ member.phone if member else '' }}">
  369. </div>
  370. <div class="col-md-4">
  371. <label class="form-label">微信号</label>
  372. <input type="text" name="wechat_account" class="form-control" value="{{ member.wechat_account if member else '' }}">
  373. </div>
  374. <div class="col-md-12">
  375. <label class="form-label">现居住址</label>
  376. <input type="text" name="residential_address" class="form-control" value="{{ member.residential_address if member else '' }}">
  377. </div>
  378. </div>
  379. <div class="section-title">个人履历</div>
  380. <div class="row g-3 mb-2">
  381. <div class="col-md-6">
  382. <label class="form-label">职业</label>
  383. <textarea name="occupation" class="form-control" rows="2">{{ member.occupation if member else '' }}</textarea>
  384. </div>
  385. <div class="col-md-6">
  386. <label class="form-label">教育背景</label>
  387. <textarea name="educational" class="form-control" rows="2">{{ member.educational if member else '' }}</textarea>
  388. </div>
  389. <div class="col-md-12">
  390. <label class="form-label">标签</label>
  391. <input type="text" name="tags" class="form-control" placeholder="例如:抗战老兵, 教师 (用逗号分隔)" value="{{ member.tags if member else '' }}">
  392. </div>
  393. <div class="col-md-12">
  394. <label class="form-label">个人成就</label>
  395. <textarea name="personal_achievements" class="form-control" rows="3">{{ member.personal_achievements if member else '' }}</textarea>
  396. </div>
  397. </div>
  398. </div>
  399. </div>
  400. </div>
  401. </div>
  402. </form>
  403. </div>
  404. </div>
  405. </div>
  406. <!-- AI 推理日志及结果面板 -->
  407. <div id="aiLogPanel" class="position-fixed bottom-0 end-0 p-3 bg-dark text-white shadow"
  408. style="display: none; width: 450px; max-height: 85vh; border-radius: 8px 0 0 0; z-index: 1050; opacity: 0.95; overflow-y: auto;">
  409. <!-- 吸顶头部与当前选中详情 -->
  410. <div class="sticky-top bg-dark pb-2" style="z-index: 1060; margin-top: -1rem; padding-top: 1rem; border-bottom: 1px solid #444;">
  411. <div class="d-flex justify-content-between align-items-center mb-2 pb-1">
  412. <span class="fw-bold"><i class="bi bi-robot"></i> AI 识别助手</span>
  413. <button class="btn btn-sm btn-outline-light py-0" onclick="closeAiLog()">×</button>
  414. </div>
  415. <!-- 当前选中详情 -->
  416. <div id="aiCurrentDetail" class="mt-2 p-2 bg-secondary bg-opacity-25 rounded border border-info shadow-sm" style="display:none; max-height: 250px; overflow-y: auto;">
  417. <div class="d-flex justify-content-between align-items-center mb-2 border-bottom border-secondary pb-1">
  418. <strong class="text-info"><i class="bi bi-info-circle"></i> 当前填充详情</strong>
  419. <button class="btn btn-sm btn-link text-muted py-0 text-decoration-none" onclick="document.getElementById('aiCurrentDetail').style.display='none'">×</button>
  420. </div>
  421. <div id="aiDetailContent" class="small text-light" style="word-break: break-all;"></div>
  422. </div>
  423. </div>
  424. <!-- 推理过程 -->
  425. <div class="mb-3 mt-3">
  426. <button class="btn btn-sm btn-link text-decoration-none text-light p-0 mb-1 d-flex align-items-center" type="button" data-bs-toggle="collapse" data-bs-target="#collapseReasoning">
  427. <i class="bi bi-cpu me-1"></i> 推理过程 <span class="badge bg-secondary ms-2" id="reasoningStatus">进行中...</span>
  428. </button>
  429. <div class="collapse show" id="collapseReasoning">
  430. <pre id="aiLogContent" class="text-success small mb-0 p-2 bg-black rounded border border-secondary" style="max-height: 200px; overflow-y: auto; white-space: pre-wrap; font-family: monospace; font-size: 0.8rem;"></pre>
  431. </div>
  432. </div>
  433. <!-- 识别结果列表 -->
  434. <div id="aiResultSection" style="display: none;">
  435. <div class="d-flex justify-content-between align-items-center mb-2">
  436. <h6 class="mb-0 text-info"><i class="bi bi-check-circle"></i> 识别结果 (<span id="resultCount">0</span>)</h6>
  437. <span class="small text-muted">点击下方条目填充</span>
  438. </div>
  439. <div id="aiResultList" class="d-flex flex-column gap-2">
  440. <!-- 结果项将动态插入 -->
  441. </div>
  442. </div>
  443. </div>
  444. <!-- 右侧:图片参考 -->
  445. <div class="image-panel">
  446. <ul class="nav nav-tabs mb-2" id="imageTabNav">
  447. <li class="nav-item">
  448. <button type="button" class="nav-link active" id="tab-scan" onclick="switchImageTab('scan')">
  449. <i class="bi bi-file-earmark-image"></i> 查看扫描件
  450. </button>
  451. </li>
  452. <li class="nav-item">
  453. <button type="button" class="nav-link" id="tab-reference" onclick="switchImageTab('reference')">
  454. <i class="bi bi-file-earmark-plus"></i> 查看参考件
  455. </button>
  456. </li>
  457. </ul>
  458. <div id="scanTabPanel">
  459. <div class="page-nav">
  460. <label class="fw-bold">扫描件参考:</label>
  461. <button id="aiBtn" onclick="recognizeImage()" class="btn btn-sm btn-info text-white ms-2 me-2">
  462. <i class="bi bi-magic"></i> AI 识别
  463. </button>
  464. <input type="number" id="pageInput" class="form-control form-control-sm" style="width: 70px;" placeholder="页码">
  465. <button type="button" onclick="gotoPage()" class="btn btn-sm btn-primary">跳转</button>
  466. <div class="ms-auto small text-muted">
  467. 当前: <span id="currentPage">1</span> / <span id="totalPages">{{ images|length }}</span>
  468. </div>
  469. </div>
  470. <div class="mb-2 small text-muted" id="imageMetadata" style="display: none;">
  471. <span class="me-2"><i class="bi bi-journal-text"></i> 版本名称: <span id="metaVersion">-</span></span>
  472. <span class="me-2"><i class="bi bi-archive"></i> 版本来源: <span id="metaSource">-</span></span>
  473. <span><i class="bi bi-person"></i> 提供人: <span id="metaPerson">-</span></span>
  474. </div>
  475. </div>
  476. <div id="referenceTabPanel" style="display: none;">
  477. <div class="page-nav">
  478. <label class="fw-bold">参考件:</label>
  479. <input type="file" id="referenceFileInput" accept="image/jpeg,image/png,image/gif,image/webp" style="display: none;">
  480. <button type="button" onclick="document.getElementById('referenceFileInput').click()" class="btn btn-sm btn-primary ms-2">
  481. <i class="bi bi-cloud-upload"></i> 上传参考件
  482. </button>
  483. <button type="button" id="deleteReferenceBtn" onclick="deleteReference()" class="btn btn-sm btn-outline-danger ms-2" style="display: none;">
  484. <i class="bi bi-trash"></i> 删除参考件
  485. </button>
  486. <span class="small text-muted ms-2">支持 Ctrl+V 粘贴图片</span>
  487. <span class="ms-auto small text-muted" id="referenceFileLabel"></span>
  488. </div>
  489. </div>
  490. <div class="image-toolbar rounded-top">
  491. <div class="btn-group btn-group-sm">
  492. <button type="button" class="btn btn-outline-secondary" onclick="rotateImage(-90)" title="左旋90°"><i class="bi bi-arrow-counterclockwise"></i></button>
  493. <button type="button" class="btn btn-outline-secondary" onclick="rotateImage(90)" title="右旋90°"><i class="bi bi-arrow-clockwise"></i></button>
  494. </div>
  495. <div class="filter-controls border-start border-end px-2 mx-1">
  496. <i class="bi bi-brightness-high" title="亮度"></i>
  497. <input type="range" min="50" max="150" value="100" oninput="updateImageFilter()" id="brightnessRange">
  498. <i class="bi bi-circle-half ms-2" title="对比度"></i>
  499. <input type="range" min="50" max="200" value="100" oninput="updateImageFilter()" id="contrastRange">
  500. <button class="btn btn-link btn-sm text-decoration-none py-0" onclick="resetFilters()">重置</button>
  501. </div>
  502. <div class="form-check form-switch ms-auto mb-0" title="开启后鼠标悬停图片可局部放大">
  503. <input class="form-check-input" type="checkbox" id="magnifierSwitch">
  504. <label class="form-check-label small" for="magnifierSwitch">🔍 放大镜</label>
  505. </div>
  506. </div>
  507. <div class="image-viewer shadow-inner" id="viewer">
  508. <div id="magnifier" class="magnifier-glass"></div>
  509. <div id="imageWrapper" class="image-wrapper">
  510. <img id="refImage" src="" alt="家谱图片" draggable="false" style="display: none;">
  511. <div id="scanEmptyState" class="reference-empty-state" style="display: none;">
  512. <i class="bi bi-image"></i>
  513. 暂无关联扫描件
  514. </div>
  515. <div id="referenceEmptyState" class="reference-empty-state" style="display: none;">
  516. <i class="bi bi-file-earmark-plus"></i>
  517. 暂无参考件,请上传或粘贴散页图片<br>
  518. <span class="small">点击「上传参考件」或按 Ctrl+V / ⌘+V 粘贴</span>
  519. </div>
  520. </div>
  521. </div>
  522. <div id="scanNavButtons" class="mt-2 d-flex justify-content-between">
  523. <button type="button" onclick="prevImage()" class="btn btn-sm btn-outline-secondary">上一张</button>
  524. <button type="button" onclick="nextImage()" class="btn btn-sm btn-outline-secondary">下一张</button>
  525. </div>
  526. </div>
  527. <!-- 成员选择弹窗 -->
  528. <div class="modal fade" id="memberSelectModal" tabindex="-1" aria-hidden="true">
  529. <div class="modal-dialog modal-lg">
  530. <div class="modal-content border-0 shadow-lg">
  531. <div class="modal-header bg-primary text-white">
  532. <h5 class="modal-title"><i class="bi bi-people me-2"></i>选择关联成员</h5>
  533. <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
  534. </div>
  535. <div class="modal-body">
  536. <div class="mb-4">
  537. <div class="input-group">
  538. <input type="text" id="member-search" class="form-control" placeholder="搜索成员姓名(支持繁体和简体)">
  539. <button type="button" class="btn btn-primary" onclick="searchMembers()">
  540. <i class="bi bi-search"></i> 搜索
  541. </button>
  542. </div>
  543. </div>
  544. <div id="member-list" class="list-group mb-4 max-h-96 overflow-y-auto">
  545. <!-- 成员列表将通过JavaScript动态生成 -->
  546. </div>
  547. <div class="d-flex justify-content-between align-items-center">
  548. <div class="text-muted small">共 <span id="total-members">0</span> 个成员</div>
  549. <nav>
  550. <ul class="pagination pagination-sm" style="max-width: 300px; overflow-x: auto; padding: 5px;">
  551. <li class="page-item disabled">
  552. <a class="page-link" href="#" onclick="changePage(0)">&laquo;</a>
  553. </li>
  554. <li class="page-item active">
  555. <a class="page-link" href="#" onclick="changePage(1)">1</a>
  556. </li>
  557. <li class="page-item">
  558. <a class="page-link" href="#" onclick="changePage(2)">&raquo;</a>
  559. </li>
  560. </ul>
  561. </nav>
  562. </div>
  563. </div>
  564. <div class="modal-footer">
  565. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
  566. </div>
  567. </div>
  568. </div>
  569. </div>
  570. </div>
  571. {% endblock %}
  572. {% block extra_js %}
  573. <script>
  574. let tomSelectInstance = null;
  575. let currentPage = 1;
  576. let totalPages = 1;
  577. let totalMembers = 0;
  578. let membersData = [];
  579. function toggleBirthdayUnknown() {
  580. const cb = document.getElementById('birthdayUnknown');
  581. const input = document.querySelector('input[name="birthday"]');
  582. if (!cb || !input) return;
  583. if (cb.checked) {
  584. input.value = '';
  585. input.disabled = true;
  586. input.required = false;
  587. input.classList.remove('is-invalid');
  588. const fb = document.getElementById('ageFeedback');
  589. if(fb) fb.textContent = '';
  590. } else {
  591. input.disabled = false;
  592. input.required = true;
  593. }
  594. }
  595. function validateAge() {
  596. const cb = document.getElementById('birthdayUnknown');
  597. if (cb && cb.checked) return;
  598. const birthdayInput = document.querySelector('input[name="birthday"]');
  599. const relatedSelect = document.querySelector('select[name="related_mid"]');
  600. const relationType = document.querySelector('select[name="relation_type"]');
  601. const feedback = document.getElementById('ageFeedback');
  602. if (!birthdayInput.value || !relatedSelect.value) {
  603. birthdayInput.classList.remove('is-invalid');
  604. return;
  605. }
  606. // Only check for Parent-Child relations (1: Father, 2: Mother)
  607. if (relationType.value !== '1' && relationType.value !== '2') return;
  608. // We need the parent's birthday. This is tricky as we only have the ID.
  609. // Option 1: Store parent birthdays in the select option dataset (easiest)
  610. // Option 2: Async fetch.
  611. const selectedOption = relatedSelect.options[relatedSelect.selectedIndex];
  612. const parentBirthdayTs = parseInt(selectedOption.dataset.birthday || '0');
  613. if (parentBirthdayTs > 0) {
  614. const childBirthday = new Date(birthdayInput.value).getTime() / 1000;
  615. if (childBirthday < parentBirthdayTs) {
  616. birthdayInput.classList.add('is-invalid');
  617. feedback.textContent = '警告:子女出生日期早于父母,请核对!';
  618. } else if (childBirthday - parentBirthdayTs < 12 * 365 * 24 * 3600) {
  619. // Warning if age gap < 12 years
  620. birthdayInput.classList.add('is-invalid');
  621. feedback.textContent = '警告:父母与子女年龄差小于12岁,请核对!';
  622. } else {
  623. birthdayInput.classList.remove('is-invalid');
  624. }
  625. }
  626. }
  627. // Call validation when relation changes too
  628. document.addEventListener('DOMContentLoaded', () => {
  629. const relatedSelect = document.querySelector('select[name="related_mid"]');
  630. if (relatedSelect) {
  631. relatedSelect.addEventListener('change', validateAge);
  632. if (typeof TomSelect !== 'undefined') {
  633. tomSelectInstance = new TomSelect(relatedSelect, {
  634. create: false,
  635. sortField: null,
  636. searchField: ['text'],
  637. render: {
  638. no_results: function(data, escape) {
  639. return '<div class="no-results">未找到匹配项</div>';
  640. }
  641. }
  642. });
  643. }
  644. }
  645. // Initialize birthday unknown state
  646. toggleBirthdayUnknown();
  647. });
  648. const images = [
  649. {% for img in images %}
  650. {
  651. id: {{ img.id }},
  652. url: "{{ img.oss_url }}",
  653. page: {{ img.page_number or 0 }},
  654. ai_status: {{ img.ai_status or 0 }},
  655. ai_content: {{ img.ai_content | tojson | safe if img.ai_content else 'null' }},
  656. genealogy_version: "{{ img.genealogy_version or '' }}",
  657. genealogy_source: "{{ img.genealogy_source or '' }}",
  658. upload_person: "{{ img.upload_person or '' }}"
  659. },
  660. {% endfor %}
  661. ];
  662. let currentIndex = 0;
  663. let activeImageTab = 'scan';
  664. let referenceImageUrl = "{{ member.reference_image_url if member and member.reference_image_url else '' }}";
  665. let referenceOssUrlRaw = "{{ member.reference_oss_url if member and member.reference_oss_url else '' }}";
  666. const memberIdForReference = {{ member.id if member else 'null' }};
  667. const hasScanSource = {{ 'true' if source_record_id else 'false' }};
  668. const hasReferenceInitial = {{ 'true' if (member and member.reference_oss_url) else 'false' }};
  669. // 初始化时根据source_record_id设置正确的扫描件
  670. {% if source_record_id %}
  671. // 在模板渲染时直接设置currentIndex
  672. currentIndex = images.findIndex(img => img.id == {{ source_record_id }});
  673. // 如果找不到,保持为0
  674. if (currentIndex === -1) {
  675. currentIndex = 0;
  676. }
  677. {% endif %}
  678. let currentParsedPeople = [];
  679. // Image State
  680. let imgRotation = 0;
  681. let imgBrightness = 100;
  682. let imgContrast = 100;
  683. // Dragging State
  684. let isDragging = false;
  685. let hasDragged = false;
  686. let startX = 0, startY = 0;
  687. let currentX = 0, currentY = 0; // Relative to center (offsets)
  688. // Zoom State
  689. let isZoomedIn = false;
  690. const ZOOM_LEVEL = 2.0;
  691. // Magnifier Logic
  692. const viewer = document.getElementById('viewer');
  693. const magnifier = document.getElementById('magnifier');
  694. const magnifierSwitch = document.getElementById('magnifierSwitch');
  695. const imageWrapper = document.getElementById('imageWrapper');
  696. // 页面加载完成后更新显示
  697. document.addEventListener('DOMContentLoaded', function() {
  698. activeImageTab = hasScanSource ? 'scan' : (hasReferenceInitial ? 'reference' : 'scan');
  699. switchImageTab(activeImageTab, true);
  700. document.getElementById('referenceFileInput').addEventListener('change', handleReferenceFileSelect);
  701. document.addEventListener('paste', handleReferencePaste);
  702. updateReferenceControls();
  703. });
  704. function isReferencePasteTarget(element) {
  705. if (!element) return false;
  706. const tag = element.tagName;
  707. if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return true;
  708. if (element.isContentEditable) return true;
  709. return !!element.closest('.form-panel');
  710. }
  711. function handleReferencePaste(event) {
  712. if (activeImageTab !== 'reference') return;
  713. if (isReferencePasteTarget(event.target)) return;
  714. const items = event.clipboardData && event.clipboardData.items;
  715. if (!items) return;
  716. for (const item of items) {
  717. if (!item.type.startsWith('image/')) continue;
  718. event.preventDefault();
  719. const blob = item.getAsFile();
  720. if (!blob) return;
  721. const ext = item.type === 'image/jpeg' ? 'jpg' : (item.type.split('/')[1] || 'png');
  722. const file = new File([blob], `paste_${Date.now()}.${ext}`, { type: item.type });
  723. uploadReferenceFile(file);
  724. return;
  725. }
  726. }
  727. function switchImageTab(tab, skipPersist) {
  728. activeImageTab = tab;
  729. document.getElementById('tab-scan').classList.toggle('active', tab === 'scan');
  730. document.getElementById('tab-reference').classList.toggle('active', tab === 'reference');
  731. document.getElementById('scanTabPanel').style.display = tab === 'scan' ? 'block' : 'none';
  732. document.getElementById('referenceTabPanel').style.display = tab === 'reference' ? 'block' : 'none';
  733. document.getElementById('scanNavButtons').style.display = tab === 'scan' ? 'flex' : 'none';
  734. if (tab === 'scan') {
  735. updateDisplay();
  736. } else {
  737. updateReferenceDisplay();
  738. }
  739. }
  740. function updateReferenceControls() {
  741. const deleteBtn = document.getElementById('deleteReferenceBtn');
  742. const fileLabel = document.getElementById('referenceFileLabel');
  743. const fileName = document.getElementById('referenceFileName').value;
  744. const hasRef = !!referenceImageUrl;
  745. deleteBtn.style.display = hasRef ? 'inline-block' : 'none';
  746. fileLabel.textContent = fileName ? `当前: ${fileName}` : '';
  747. }
  748. function updateReferenceDisplay() {
  749. const img = document.getElementById('refImage');
  750. const scanEmpty = document.getElementById('scanEmptyState');
  751. const refEmpty = document.getElementById('referenceEmptyState');
  752. const metaContainer = document.getElementById('imageMetadata');
  753. if (metaContainer) metaContainer.style.display = 'none';
  754. resetFilters();
  755. if (referenceImageUrl) {
  756. img.src = referenceImageUrl;
  757. img.style.display = 'block';
  758. refEmpty.style.display = 'none';
  759. scanEmpty.style.display = 'none';
  760. } else {
  761. img.style.display = 'none';
  762. img.removeAttribute('src');
  763. refEmpty.style.display = 'block';
  764. scanEmpty.style.display = 'none';
  765. }
  766. currentX = 0;
  767. currentY = 0;
  768. isZoomedIn = false;
  769. updateImageTransform();
  770. }
  771. async function handleReferenceFileSelect(event) {
  772. const file = event.target.files && event.target.files[0];
  773. event.target.value = '';
  774. if (file) await uploadReferenceFile(file);
  775. }
  776. async function uploadReferenceFile(file) {
  777. if (!file) return;
  778. if (!file.type.startsWith('image/')) {
  779. alert('请选择图片文件');
  780. return;
  781. }
  782. if (file.size > 10 * 1024 * 1024) {
  783. alert('图片大小不能超过 10MB');
  784. return;
  785. }
  786. const formData = new FormData();
  787. formData.append('file', file);
  788. const uploadBtn = document.querySelector('#referenceTabPanel .btn-primary');
  789. const originalHtml = uploadBtn.innerHTML;
  790. uploadBtn.disabled = true;
  791. uploadBtn.innerHTML = '<span class="spinner-border spinner-border-sm"></span> 上传中...';
  792. try {
  793. const url = memberIdForReference
  794. ? `/manager/api/member/${memberIdForReference}/reference`
  795. : '/manager/api/upload_reference';
  796. const resp = await fetch(url, { method: 'POST', body: formData });
  797. const result = await resp.json();
  798. if (!resp.ok || !result.success) {
  799. throw new Error(result.message || '上传失败');
  800. }
  801. referenceImageUrl = result.oss_url;
  802. referenceOssUrlRaw = result.oss_url_raw || result.oss_url;
  803. document.getElementById('referenceOssUrl').value = referenceOssUrlRaw;
  804. document.getElementById('referenceFileName').value = result.file_name || file.name;
  805. document.getElementById('deleteReference').value = '0';
  806. updateReferenceControls();
  807. if (activeImageTab === 'reference') {
  808. updateReferenceDisplay();
  809. }
  810. } catch (error) {
  811. alert(error.message || '上传失败,请稍后重试');
  812. } finally {
  813. uploadBtn.disabled = false;
  814. uploadBtn.innerHTML = originalHtml;
  815. }
  816. }
  817. async function deleteReference() {
  818. if (!confirm('确定要删除参考件吗?')) return;
  819. if (memberIdForReference) {
  820. try {
  821. const resp = await fetch(`/manager/api/member/${memberIdForReference}/reference`, { method: 'DELETE' });
  822. const result = await resp.json();
  823. if (!resp.ok || !result.success) {
  824. throw new Error(result.message || '删除失败');
  825. }
  826. } catch (error) {
  827. alert(error.message || '删除失败,请稍后重试');
  828. return;
  829. }
  830. }
  831. referenceImageUrl = '';
  832. referenceOssUrlRaw = '';
  833. document.getElementById('referenceOssUrl').value = '';
  834. document.getElementById('referenceFileName').value = '';
  835. document.getElementById('deleteReference').value = '1';
  836. updateReferenceControls();
  837. if (activeImageTab === 'reference') {
  838. updateReferenceDisplay();
  839. }
  840. }
  841. // Initialize Dragging and Zooming
  842. if (imageWrapper) {
  843. // Center initial position
  844. imageWrapper.style.left = '50%';
  845. imageWrapper.style.top = '50%';
  846. viewer.style.cursor = 'zoom-in';
  847. viewer.addEventListener('mousedown', (e) => {
  848. if (e.target.closest('.image-toolbar') || e.target.closest('.magnifier-glass')) return;
  849. isDragging = true;
  850. hasDragged = false;
  851. startX = e.clientX;
  852. startY = e.clientY;
  853. viewer.style.cursor = 'grabbing';
  854. e.preventDefault(); // Prevent text selection
  855. });
  856. window.addEventListener('mousemove', (e) => {
  857. if (!isDragging) return;
  858. const dx = e.clientX - startX;
  859. const dy = e.clientY - startY;
  860. // Threshold to consider it a drag
  861. if (Math.abs(dx) > 2 || Math.abs(dy) > 2) {
  862. hasDragged = true;
  863. }
  864. currentX += dx;
  865. currentY += dy;
  866. startX = e.clientX;
  867. startY = e.clientY;
  868. updateImageTransform();
  869. });
  870. window.addEventListener('mouseup', (e) => {
  871. if (isDragging) {
  872. isDragging = false;
  873. viewer.style.cursor = isZoomedIn ? 'grab' : 'zoom-in';
  874. // If it was a click (not a drag) and clicked inside the viewer
  875. if (!hasDragged && viewer.contains(e.target)) {
  876. toggleZoom();
  877. }
  878. }
  879. });
  880. }
  881. function toggleZoom() {
  882. isZoomedIn = !isZoomedIn;
  883. if (!isZoomedIn) {
  884. // Reset position when zooming out to center
  885. currentX = 0;
  886. currentY = 0;
  887. }
  888. updateImageTransform();
  889. // Update cursor immediately
  890. viewer.style.cursor = isZoomedIn ? 'grab' : 'zoom-in';
  891. }
  892. viewer.addEventListener('mousemove', function(e) {
  893. if (!magnifierSwitch.checked) {
  894. magnifier.style.display = 'none';
  895. return;
  896. }
  897. if (isDragging) {
  898. magnifier.style.display = 'none';
  899. return;
  900. }
  901. const img = document.getElementById('refImage');
  902. if (!img) return;
  903. // Calculate position relative to the image
  904. const rect = img.getBoundingClientRect();
  905. const x = e.clientX - rect.left;
  906. const y = e.clientY - rect.top;
  907. // Only show if inside image rect (approximate for rotated)
  908. if (x < 0 || x > rect.width || y < 0 || y > rect.height) {
  909. magnifier.style.display = 'none';
  910. return;
  911. }
  912. magnifier.style.display = 'block';
  913. // Position the glass near mouse
  914. const glassOffset = 20;
  915. const viewerRect = viewer.getBoundingClientRect();
  916. magnifier.style.left = (e.clientX - viewerRect.left + glassOffset) + 'px';
  917. magnifier.style.top = (e.clientY - viewerRect.top + glassOffset) + 'px';
  918. // Background logic (Zoom 2x)
  919. const zoom = 2.5;
  920. magnifier.style.backgroundImage = `url('${img.src}')`;
  921. magnifier.style.backgroundSize = `${rect.width * zoom}px ${rect.height * zoom}px`;
  922. // Simple version (imperfect for rotation)
  923. magnifier.style.backgroundPosition = `-${x * zoom - 75}px -${y * zoom - 75}px`;
  924. });
  925. // Image Manipulation
  926. function rotateImage(deg) {
  927. imgRotation = (imgRotation + deg) % 360;
  928. updateImageTransform();
  929. }
  930. function updateImageFilter() {
  931. imgBrightness = document.getElementById('brightnessRange').value;
  932. imgContrast = document.getElementById('contrastRange').value;
  933. applyImageFilters();
  934. }
  935. function resetFilters() {
  936. imgRotation = 0;
  937. imgBrightness = 100;
  938. imgContrast = 100;
  939. currentX = 0;
  940. currentY = 0;
  941. isZoomedIn = false;
  942. document.getElementById('brightnessRange').value = 100;
  943. document.getElementById('contrastRange').value = 100;
  944. updateImageTransform();
  945. applyImageFilters();
  946. }
  947. function updateImageTransform() {
  948. const wrapper = document.getElementById('imageWrapper');
  949. if (wrapper) {
  950. const scale = isZoomedIn ? ZOOM_LEVEL : 1;
  951. wrapper.style.transform = `translate(calc(-50% + ${currentX}px), calc(-50% + ${currentY}px)) rotate(${imgRotation}deg) scale(${scale})`;
  952. // Adjust cursor based on state
  953. if (!isDragging) {
  954. viewer.style.cursor = isZoomedIn ? 'grab' : 'zoom-in';
  955. }
  956. }
  957. }
  958. function applyImageFilters() {
  959. const img = document.getElementById('refImage');
  960. if (img) {
  961. img.style.filter = `brightness(${imgBrightness}%) contrast(${imgContrast}%)`;
  962. }
  963. }
  964. // Reuse applyImageStyles as alias for compatibility if called elsewhere
  965. function applyImageStyles() {
  966. updateImageTransform();
  967. applyImageFilters();
  968. }
  969. const fieldMapping = {
  970. name: '姓名(繁体)',
  971. simplified_name: '姓名(简体)',
  972. sex: '性别',
  973. birthday: '出生日期',
  974. father_name: '父亲姓名',
  975. spouse_name: '配偶姓名',
  976. generation: '堂内排行(代数)',
  977. name_word: '字辈',
  978. education: '学历/功名',
  979. title: '官职/称号',
  980. death_date: '逝世日期',
  981. note: '备注'
  982. };
  983. // --- Keyboard Shortcuts ---
  984. document.addEventListener('keydown', (e) => {
  985. // Ctrl/Cmd + Enter: Save
  986. if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
  987. e.preventDefault();
  988. const submitBtn = document.querySelector('form button[type="submit"]');
  989. if (submitBtn && !submitBtn.disabled) {
  990. submitBtn.click(); // Trigger form submit listener
  991. }
  992. }
  993. // Ctrl/Cmd + Right Arrow: Next Image
  994. if ((e.ctrlKey || e.metaKey) && e.key === 'ArrowRight') {
  995. e.preventDefault();
  996. nextImage();
  997. }
  998. // Ctrl/Cmd + Left Arrow: Prev Image
  999. if ((e.ctrlKey || e.metaKey) && e.key === 'ArrowLeft') {
  1000. e.preventDefault();
  1001. prevImage();
  1002. }
  1003. // Alt + 1: Auto Fill First person in list
  1004. if (e.altKey && e.key === '1') {
  1005. e.preventDefault();
  1006. // Try to find the first "fill" button that is not disabled/success
  1007. const firstBtn = document.querySelector('button[id^="btn-fill-"]:not(.btn-success)');
  1008. if (firstBtn) firstBtn.click();
  1009. }
  1010. });
  1011. // --- Multi-relation handling ---
  1012. let currentRelationIndex = 0;
  1013. document.addEventListener('DOMContentLoaded', function() {
  1014. // Set initial index based on existing rows
  1015. const relationRows = document.querySelectorAll('.relation-row');
  1016. currentRelationIndex = relationRows.length;
  1017. // Show remove button if there are multiple rows
  1018. updateRemoveButtons();
  1019. // Add click handler for add relation button
  1020. const addBtn = document.getElementById('add-relation-btn');
  1021. if (addBtn) {
  1022. addBtn.addEventListener('click', addRelationRow);
  1023. }
  1024. // Add click handlers for select member buttons
  1025. document.querySelectorAll('.select-member-btn').forEach(btn => {
  1026. btn.addEventListener('click', function() {
  1027. const index = parseInt(this.dataset.index);
  1028. window.currentRelationIndex = index;
  1029. });
  1030. });
  1031. // Add click handlers for remove buttons
  1032. document.querySelectorAll('.remove-relation-btn').forEach(btn => {
  1033. btn.addEventListener('click', removeRelationRow);
  1034. });
  1035. });
  1036. function addRelationRow() {
  1037. const container = document.getElementById('relations-container');
  1038. const newIndex = currentRelationIndex++;
  1039. // Automatically set currentRelationIndex to the new row
  1040. window.currentRelationIndex = newIndex;
  1041. const newRow = document.createElement('div');
  1042. newRow.className = 'row g-3 mb-3 relation-row';
  1043. newRow.dataset.index = newIndex;
  1044. newRow.innerHTML = `
  1045. <div class="col-md-4">
  1046. <label class="form-label">关联成员</label>
  1047. <div class="input-group">
  1048. <input type="text" class="form-control related-member-display" placeholder="点击选择关联成员" readonly>
  1049. <input type="hidden" name="relations[${newIndex}][parent_mid]" class="related_mid">
  1050. <button type="button" class="btn btn-outline-primary select-member-btn" data-index="${newIndex}" data-bs-toggle="modal" data-bs-target="#memberSelectModal">
  1051. <i class="bi bi-search"></i>
  1052. </button>
  1053. </div>
  1054. </div>
  1055. <div class="col-md-3">
  1056. <label class="form-label">关系类型</label>
  1057. <select name="relations[${newIndex}][relation_type]" class="form-select relation-type">
  1058. <option value="">-- 请选择 --</option>
  1059. <option value="1">父子 (关联人为父)</option>
  1060. <option value="2">母子 (关联人为母)</option>
  1061. <option value="10">夫妻</option>
  1062. <option value="11">兄弟</option>
  1063. <option value="12">姐妹</option>
  1064. </select>
  1065. </div>
  1066. <div class="col-md-3">
  1067. <label class="form-label">子类型</label>
  1068. <select name="relations[${newIndex}][sub_relation_type]" class="form-select sub-relation-type">
  1069. <option value="0">亲生/正妻</option>
  1070. <option value="1">养父</option>
  1071. <option value="2">出继(亲生父母)</option>
  1072. <option value="3">入继(养父母)</option>
  1073. <option value="10">妾</option>
  1074. <option value="11">外室</option>
  1075. </select>
  1076. </div>
  1077. <div class="col-md-2 d-flex align-items-end">
  1078. <button type="button" class="btn btn-danger w-100 remove-relation-btn">
  1079. <i class="bi bi-trash"></i>
  1080. </button>
  1081. </div>
  1082. <div class="col-md-2 child-order-wrapper" style="display: none;">
  1083. <label class="form-label">第几子</label>
  1084. <input type="number" name="relations[${newIndex}][child_order]" class="form-control child-order-input"
  1085. min="1" placeholder="排行(选填)">
  1086. </div>
  1087. `;
  1088. container.appendChild(newRow);
  1089. // Add click handlers for new buttons
  1090. const selectBtn = newRow.querySelector('.select-member-btn');
  1091. selectBtn.addEventListener('click', function() {
  1092. const index = parseInt(this.dataset.index);
  1093. window.currentRelationIndex = index;
  1094. });
  1095. const removeBtn = newRow.querySelector('.remove-relation-btn');
  1096. removeBtn.addEventListener('click', removeRelationRow);
  1097. // Show/hide 第几子 based on relation type
  1098. const relTypeSelect = newRow.querySelector('.relation-type');
  1099. relTypeSelect.addEventListener('change', function() {
  1100. toggleChildOrderField(this);
  1101. });
  1102. updateRemoveButtons();
  1103. }
  1104. function toggleChildOrderField(selectEl) {
  1105. const row = selectEl.closest('.relation-row');
  1106. if (!row) return;
  1107. const wrapper = row.querySelector('.child-order-wrapper');
  1108. if (!wrapper) return;
  1109. const val = selectEl.value;
  1110. if (val === '1' || val === '2') {
  1111. wrapper.style.display = 'block';
  1112. } else {
  1113. wrapper.style.display = 'none';
  1114. const input = wrapper.querySelector('.child-order-input');
  1115. if (input) input.value = '';
  1116. }
  1117. }
  1118. // Init toggle for pre-rendered rows
  1119. document.querySelectorAll('#relations-container .relation-type').forEach(function(sel) {
  1120. sel.addEventListener('change', function() { toggleChildOrderField(this); });
  1121. });
  1122. function removeRelationRow() {
  1123. const row = this.closest('.relation-row');
  1124. if (row) {
  1125. row.remove();
  1126. updateRemoveButtons();
  1127. }
  1128. }
  1129. function updateRemoveButtons() {
  1130. const rows = document.querySelectorAll('.relation-row');
  1131. rows.forEach((row, index) => {
  1132. const removeBtn = row.querySelector('.remove-relation-btn');
  1133. if (removeBtn) {
  1134. // Show remove button only if there's more than one row
  1135. removeBtn.style.display = rows.length > 1 ? 'block' : 'none';
  1136. }
  1137. // Update index attribute
  1138. row.dataset.index = index;
  1139. });
  1140. }
  1141. // Override selectMember to support multiple relations
  1142. window.selectMember = function(member) {
  1143. const index = window.currentRelationIndex || 0;
  1144. const rows = document.querySelectorAll('.relation-row');
  1145. if (rows[index]) {
  1146. const displayInput = rows[index].querySelector('.related-member-display');
  1147. const hiddenInput = rows[index].querySelector('.related_mid');
  1148. if (displayInput) {
  1149. let displayText = member.name;
  1150. if (member.simplified_name && member.simplified_name !== member.name) {
  1151. displayText += ` (${member.simplified_name})`;
  1152. }
  1153. if (member.name_word) {
  1154. displayText += ` · ${member.name_word}`;
  1155. }
  1156. displayInput.value = displayText;
  1157. }
  1158. if (hiddenInput) {
  1159. hiddenInput.value = member.id;
  1160. }
  1161. }
  1162. // Close modal
  1163. const modal = bootstrap.Modal.getInstance(document.getElementById('memberSelectModal'));
  1164. if (modal) {
  1165. modal.hide();
  1166. }
  1167. };
  1168. // --- AJAX Form Submission ---
  1169. document.addEventListener('DOMContentLoaded', () => {
  1170. const form = document.querySelector('form');
  1171. form.addEventListener('submit', async (e) => {
  1172. e.preventDefault();
  1173. // Collect form data
  1174. const formData = new FormData(form);
  1175. // Visual feedback on button
  1176. const submitBtn = form.querySelector('button[type="submit"]');
  1177. const originalBtnHtml = submitBtn.innerHTML;
  1178. submitBtn.disabled = true;
  1179. submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 保存中...';
  1180. // Create loading overlay
  1181. const loadingOverlay = document.createElement('div');
  1182. loadingOverlay.className = 'position-fixed inset-0 bg-black/50 flex items-center justify-center z-50';
  1183. loadingOverlay.innerHTML = `
  1184. <div class="text-center text-white">
  1185. <div class="spinner-border text-yellow-400" style="width: 3rem; height: 3rem;" role="status">
  1186. <span class="visually-hidden">Loading...</span>
  1187. </div>
  1188. <p class="mt-3 text-lg">正在保存,请稍候...</p>
  1189. </div>
  1190. `;
  1191. document.body.appendChild(loadingOverlay);
  1192. try {
  1193. // Determine target URL based on mode
  1194. const isEditMode = {{ 'true' if member else 'false' }};
  1195. const targetUrl = isEditMode ? window.location.href : '/manager/add_member';
  1196. console.log('[Add Member] Submitting to:', targetUrl);
  1197. const response = await fetch(targetUrl, {
  1198. method: 'POST',
  1199. body: formData,
  1200. headers: {
  1201. 'X-Requested-With': 'XMLHttpRequest'
  1202. },
  1203. credentials: 'include',
  1204. timeout: 60000 // 60秒超时
  1205. });
  1206. console.log('[Add Member] Response status:', response.status);
  1207. if (!response.ok) {
  1208. throw new Error(`HTTP error! status: ${response.status}`);
  1209. }
  1210. const result = await response.json();
  1211. console.log('[Add Member] Response data:', result);
  1212. if (result.success) {
  1213. // Remove loading overlay
  1214. loadingOverlay.remove();
  1215. // Success!
  1216. // 1. Show a toast or small alert
  1217. const toast = document.createElement('div');
  1218. toast.className = 'position-fixed bottom-0 start-50 translate-middle-x mb-4 p-4 bg-success text-white rounded-lg shadow-lg';
  1219. toast.style.zIndex = '2000';
  1220. toast.innerHTML = `<i class="bi bi-check-circle me-2"></i> ${result.message || '保存成功!'}`;
  1221. document.body.appendChild(toast);
  1222. setTimeout(() => toast.remove(), 3000);
  1223. // 2. Mark the AI list item as "Saved" if applicable
  1224. if (window.lastFilledIndex !== undefined) {
  1225. const btn = document.getElementById(`btn-fill-${window.lastFilledIndex}`);
  1226. if (btn) {
  1227. btn.className = 'btn btn-sm btn-success text-white ms-2 disabled';
  1228. btn.innerHTML = '<i class="bi bi-check-lg"></i> 已录入';
  1229. btn.onclick = null;
  1230. }
  1231. // Update local data state so it persists if we switch images/filters
  1232. if (window.lastFilledIndex !== undefined && currentParsedPeople[window.lastFilledIndex]) {
  1233. currentParsedPeople[window.lastFilledIndex].is_imported = true;
  1234. currentParsedPeople[window.lastFilledIndex].imported_member_id = result.member_id;
  1235. // Sync back to images array to persist across image switching
  1236. if (images[currentIndex]) {
  1237. images[currentIndex].ai_content = currentParsedPeople;
  1238. console.log('Updated images array:', images[currentIndex]);
  1239. }
  1240. }
  1241. }
  1242. // 3. Clear form (reset to defaults) or keep some fields?
  1243. // Usually for genealogy, Surname/Generation might be same, but let's clear for safety
  1244. // Resetting form but keeping "related_mid" might be useful for siblings?
  1245. // For now, simple reset.
  1246. // --- Update Local Matches before resetting form ---
  1247. // If we just saved a person, check if this person is the father/spouse of anyone else in the list
  1248. // and update their matches so 'fillForm' will work for them.
  1249. const savedName = formData.get('name'); // Traditional (Raw)
  1250. const savedSimplifiedName = formData.get('simplified_name'); // Simplified (Cleaned)
  1251. const savedId = result.member_id;
  1252. const savedSex = formData.get('sex'); // 1: Male, 2: Female
  1253. if (savedId) {
  1254. currentParsedPeople.forEach(p => {
  1255. if (!p.matches) p.matches = {};
  1256. // Check Father Match
  1257. // Try matching against Simplified Name (p.father_name is Simplified Cleaned)
  1258. // Or fallback to savedName if p.father_name happened to be Traditional (rare but possible)
  1259. if (p.father_name && (p.father_name === savedSimplifiedName || p.father_name === savedName)) {
  1260. // Assume simple match logic here (usually father is male)
  1261. if (savedSex === '1') {
  1262. if (!p.matches.father) p.matches.father = [];
  1263. // Add to matches if not exists
  1264. if (!p.matches.father.find(m => m.id === savedId)) {
  1265. p.matches.father.push({ id: savedId, name: savedName, sex: 1 }); // Mock DB object
  1266. }
  1267. }
  1268. }
  1269. // Check Spouse Match
  1270. if (p.spouse_name && (p.spouse_name === savedSimplifiedName || p.spouse_name === savedName)) {
  1271. // Spouse logic...
  1272. if (!p.matches.spouse) p.matches.spouse = [];
  1273. if (!p.matches.spouse.find(m => m.id === savedId)) {
  1274. p.matches.spouse.push({ id: savedId, name: savedName, sex: parseInt(savedSex) });
  1275. }
  1276. }
  1277. });
  1278. // Also, we need to add this new member to the <select> options for future manual selection!
  1279. // This is tricky because the select is rendered by Jinja2.
  1280. // We can append an option via JS.
  1281. const relatedSelect = document.querySelector('select[name="related_mid"]');
  1282. if (relatedSelect) {
  1283. const newOption = document.createElement('option');
  1284. newOption.value = savedId;
  1285. newOption.textContent = `${savedName} (ID: ${savedId})`;
  1286. // Add birthday data if available for validation
  1287. newOption.dataset.birthday = new Date(formData.get('birthday')).getTime() / 1000;
  1288. relatedSelect.add(newOption); // Add to end
  1289. }
  1290. }
  1291. // --- End Local Match Update ---
  1292. form.reset();
  1293. // Clear lineage generation tags
  1294. document.querySelectorAll('#lineage-generations-container .lineage-tag').forEach(t => t.remove());
  1295. const lgInput = document.getElementById('lineage-input-form');
  1296. if (lgInput) lgInput.classList.add('d-none');
  1297. const lgAdd = document.getElementById('add-lineage');
  1298. if (lgAdd) lgAdd.classList.remove('d-none');
  1299. form.querySelector('[name="personal_achievements"]').value = '';
  1300. form.querySelector('[name="notes"]').value = '';
  1301. form.querySelector('[name="tags"]').value = '';
  1302. form.querySelector('[name="family_rank"]').value = '';
  1303. // Update the source_record_id field to the current image's ID after reset
  1304. const sourceRecordIdField = document.querySelector('input[name="source_record_id"]');
  1305. if (sourceRecordIdField && images.length > 0) {
  1306. // If we're editing a member, keep the existing source_record_id
  1307. // Otherwise, use the current image's ID
  1308. const isEditMode = {{ 'true' if member else 'false' }};
  1309. if (!isEditMode) {
  1310. sourceRecordIdField.value = images[currentIndex].id;
  1311. }
  1312. }
  1313. // Close detail panel
  1314. document.getElementById('aiCurrentDetail').style.display = 'none';
  1315. // 4. Auto-Next Logic
  1316. // Find the next available person in the list to fill
  1317. if (window.lastFilledIndex !== undefined) {
  1318. const nextIndex = window.lastFilledIndex + 1;
  1319. if (currentParsedPeople[nextIndex]) {
  1320. // Automatically fill the next one!
  1321. fillForm(nextIndex);
  1322. // Scroll list to show the new active item if needed
  1323. const btn = document.getElementById(`btn-fill-${nextIndex}`);
  1324. if(btn) btn.scrollIntoView({ behavior: 'smooth', block: 'center' });
  1325. }
  1326. }
  1327. } else {
  1328. // Remove loading overlay
  1329. loadingOverlay.remove();
  1330. alert('保存失败: ' + result.message);
  1331. }
  1332. } catch (error) {
  1333. // Remove loading overlay
  1334. loadingOverlay.remove();
  1335. console.error('[Add Member] Error submitting form:', error);
  1336. alert('网络或服务器错误,请稍后重试: ' + error.message);
  1337. } finally {
  1338. submitBtn.disabled = false;
  1339. submitBtn.innerHTML = originalBtnHtml;
  1340. }
  1341. });
  1342. });
  1343. // --- End AJAX Form Submission ---
  1344. function updateDisplay() {
  1345. if (activeImageTab !== 'scan') return;
  1346. const img = document.getElementById('refImage');
  1347. const scanEmpty = document.getElementById('scanEmptyState');
  1348. const refEmpty = document.getElementById('referenceEmptyState');
  1349. const isEditMode = {{ 'true' if member else 'false' }};
  1350. const metaContainer = document.getElementById('imageMetadata');
  1351. const aiBtn = document.getElementById('aiBtn');
  1352. if (isEditMode && !hasScanSource) {
  1353. img.style.display = 'none';
  1354. img.removeAttribute('src');
  1355. scanEmpty.innerHTML = '<i class="bi bi-image"></i> 暂无关联扫描件';
  1356. scanEmpty.style.display = 'block';
  1357. refEmpty.style.display = 'none';
  1358. if (metaContainer) metaContainer.style.display = 'none';
  1359. if (aiBtn) {
  1360. aiBtn.innerHTML = '<i class="bi bi-magic"></i> AI 识别';
  1361. aiBtn.className = 'btn btn-sm btn-info text-white ms-2 me-2';
  1362. aiBtn.onclick = recognizeImage;
  1363. }
  1364. resetFilters();
  1365. return;
  1366. }
  1367. if (images.length > 0) {
  1368. const imageData = images[currentIndex];
  1369. img.src = imageData.url;
  1370. img.style.display = 'block';
  1371. scanEmpty.style.display = 'none';
  1372. refEmpty.style.display = 'none';
  1373. document.getElementById('currentPage').innerText = currentIndex + 1;
  1374. // Update metadata display
  1375. const metaContainer = document.getElementById('imageMetadata');
  1376. if (imageData.genealogy_version || imageData.genealogy_source || imageData.upload_person) {
  1377. metaContainer.style.display = 'block';
  1378. document.getElementById('metaVersion').innerText = imageData.genealogy_version || '未提供';
  1379. document.getElementById('metaSource').innerText = imageData.genealogy_source || '未提供';
  1380. document.getElementById('metaPerson').innerText = imageData.upload_person || '未提供';
  1381. } else {
  1382. metaContainer.style.display = 'none';
  1383. }
  1384. // Update the source_record_id hidden field in the form
  1385. const sourceRecordIdField = document.querySelector('input[name="source_record_id"]');
  1386. if (sourceRecordIdField) {
  1387. // If we're editing a member, keep the existing source_record_id
  1388. // Otherwise, use the current image's ID
  1389. const isEditMode = {{ 'true' if member else 'false' }};
  1390. if (!isEditMode) {
  1391. sourceRecordIdField.value = imageData.id;
  1392. }
  1393. }
  1394. // Reset image state on switch
  1395. resetFilters();
  1396. // AI Button Logic
  1397. const aiBtn = document.getElementById('aiBtn');
  1398. const aiPanel = document.getElementById('aiLogPanel');
  1399. const resultList = document.getElementById('aiResultList');
  1400. const resultCount = document.getElementById('resultCount');
  1401. // Hide panel when switching images to avoid confusion
  1402. if (aiPanel) aiPanel.style.display = 'none';
  1403. // Clear current data
  1404. currentParsedPeople = [];
  1405. if (resultCount) resultCount.innerText = '0';
  1406. if (resultList) resultList.innerHTML = '';
  1407. if (imageData.ai_status === 2 && imageData.ai_content) {
  1408. // Determine content
  1409. let content = imageData.ai_content;
  1410. // Parse if string (it might be a string if double encoded or stored as JSON string in DB)
  1411. if (typeof content === 'string') {
  1412. try { content = JSON.parse(content); } catch(e) { content = []; }
  1413. }
  1414. if (!Array.isArray(content) && content) content = [content];
  1415. if (content && content.length > 0) {
  1416. // Update Button to "View Results"
  1417. aiBtn.innerHTML = '<i class="bi bi-list-check"></i> 查看解析结果';
  1418. aiBtn.className = 'btn btn-sm btn-success text-white ms-2 me-2';
  1419. aiBtn.onclick = function() {
  1420. // Show panel with loading
  1421. if (aiPanel) aiPanel.style.display = 'block';
  1422. if (resultList) resultList.innerHTML = '<div class="text-center p-3"><div class="spinner-border text-primary" role="status"></div></div>';
  1423. // Process (small delay to allow UI update)
  1424. setTimeout(() => processAiData(content), 10);
  1425. };
  1426. return; // Done
  1427. }
  1428. }
  1429. // Default: Reset to "AI Recognition"
  1430. aiBtn.innerHTML = '<i class="bi bi-magic"></i> AI 识别';
  1431. aiBtn.className = 'btn btn-sm btn-info text-white ms-2 me-2';
  1432. aiBtn.onclick = recognizeImage;
  1433. } else {
  1434. img.style.display = 'none';
  1435. img.removeAttribute('src');
  1436. scanEmpty.innerHTML = '<i class="bi bi-image"></i> 暂无上传的家谱图片';
  1437. scanEmpty.style.display = 'block';
  1438. refEmpty.style.display = 'none';
  1439. }
  1440. }
  1441. function nextImage() {
  1442. if (images.length === 0) return;
  1443. const currentVersion = images[currentIndex].genealogy_version;
  1444. // 从当前索引开始,寻找下一个相同版本的图片
  1445. for (let i = currentIndex + 1; i < images.length; i++) {
  1446. if (images[i].genealogy_version === currentVersion) {
  1447. currentIndex = i;
  1448. updateDisplay();
  1449. return;
  1450. }
  1451. }
  1452. }
  1453. function prevImage() {
  1454. if (images.length === 0) return;
  1455. const currentVersion = images[currentIndex].genealogy_version;
  1456. // 从当前索引开始,寻找上一个相同版本的图片
  1457. for (let i = currentIndex - 1; i >= 0; i--) {
  1458. if (images[i].genealogy_version === currentVersion) {
  1459. currentIndex = i;
  1460. updateDisplay();
  1461. return;
  1462. }
  1463. }
  1464. }
  1465. function gotoPage() {
  1466. const val = document.getElementById('pageInput').value;
  1467. if (!val) return;
  1468. const page = parseInt(val);
  1469. if (images.length === 0) {
  1470. alert('未找到该页码对应的图片');
  1471. return;
  1472. }
  1473. const currentVersion = images[currentIndex].genealogy_version;
  1474. const index = images.findIndex(img => img.page === page && img.genealogy_version === currentVersion);
  1475. if (index !== -1) {
  1476. currentIndex = index;
  1477. updateDisplay();
  1478. } else {
  1479. alert('未找到该页码对应的图片');
  1480. }
  1481. }
  1482. function closeAiLog() {
  1483. document.getElementById('aiLogPanel').style.display = 'none';
  1484. }
  1485. function toggleAiPanel() {
  1486. const panel = document.getElementById('aiLogPanel');
  1487. if (panel.style.display === 'none') {
  1488. panel.style.display = 'block';
  1489. } else {
  1490. panel.style.display = 'none';
  1491. }
  1492. }
  1493. function updateAiButtonState(hasResults) {
  1494. const btn = document.getElementById('aiBtn');
  1495. if (!btn) return;
  1496. if (hasResults) {
  1497. btn.innerHTML = '<i class="bi bi-list-check"></i> 查看识别结果';
  1498. btn.onclick = toggleAiPanel;
  1499. btn.classList.remove('btn-info');
  1500. btn.classList.add('btn-success');
  1501. } else {
  1502. // Revert state if needed (usually on new image load if we clear data)
  1503. btn.innerHTML = '<i class="bi bi-magic"></i> AI 识别';
  1504. btn.onclick = recognizeImage;
  1505. btn.classList.remove('btn-success');
  1506. btn.classList.add('btn-info');
  1507. }
  1508. }
  1509. function fillForm(index) {
  1510. window.lastFilledIndex = index;
  1511. const person = currentParsedPeople[index];
  1512. if (!person) return;
  1513. const form = document.querySelector('form');
  1514. form.reset(); // Clear previous data first
  1515. if (tomSelectInstance) {
  1516. tomSelectInstance.clear();
  1517. }
  1518. // Set Source Index
  1519. const sourceIndexInput = form.querySelector('[name="source_index"]');
  1520. if (sourceIndexInput) {
  1521. sourceIndexInput.value = index;
  1522. console.log('Set source_index:', index);
  1523. window.lastFilledIndex = index;
  1524. console.log('Set lastFilledIndex:', index);
  1525. }
  1526. // 1. 姓名
  1527. if (person.name) {
  1528. const nameInput = form.querySelector('[name="name"]');
  1529. nameInput.value = person.name;
  1530. nameInput.dispatchEvent(new Event('input')); // 触发重名检测
  1531. }
  1532. if (person.simplified_name) {
  1533. const snInput = form.querySelector('[name="simplified_name"]');
  1534. if (snInput) snInput.value = person.simplified_name;
  1535. } else {
  1536. // Fallback: if no simplified_name explicitly, generate it
  1537. if (person.name) {
  1538. const snInput = form.querySelector('[name="simplified_name"]');
  1539. if (snInput) snInput.value = cleanName(person.name);
  1540. }
  1541. }
  1542. // 2. 性别
  1543. if (person.sex) {
  1544. const sexSelect = form.querySelector('[name="sex"]');
  1545. if (person.sex.includes('女')) sexSelect.value = '2';
  1546. else if (person.sex.includes('男')) sexSelect.value = '1';
  1547. }
  1548. // 3. 生日 & 自动推断过世
  1549. // Reset unknown toggle first
  1550. const birthdayUnknownCb = document.getElementById('birthdayUnknown');
  1551. if (birthdayUnknownCb) {
  1552. birthdayUnknownCb.checked = false;
  1553. toggleBirthdayUnknown();
  1554. }
  1555. let isDeceased = false;
  1556. let isDeceasedUnknown = true;
  1557. if (person.birthday) {
  1558. let dateVal = person.birthday;
  1559. // Handle partial dates like "1890年" or "1890年?月?日"
  1560. const partialYearMatch = dateVal.match(/^(\d{4})[^\d]*$/) || dateVal.match(/(\d{4})年\s*[??Xxx]\s*月/i);
  1561. if (partialYearMatch) {
  1562. dateVal = `${partialYearMatch[1]}-01-01`;
  1563. } else {
  1564. // 尝试标准化完整日期
  1565. const dateMatch = dateVal.match(/(\d{4})[-/年](\d{1,2})[-/月](\d{1,2})/);
  1566. if (dateMatch) {
  1567. const y = dateMatch[1];
  1568. const m = dateMatch[2].padStart(2, '0');
  1569. const d = dateMatch[3].padStart(2, '0');
  1570. dateVal = `${y}-${m}-${d}`;
  1571. }
  1572. }
  1573. // 只有当日期格式正确时才填充
  1574. if (/^\d{4}-\d{2}-\d{2}$/.test(dateVal)) {
  1575. form.querySelector('[name="birthday"]').value = dateVal;
  1576. // Auto "Is Deceased" Logic (e.g. older than 100 years from now)
  1577. const birthYear = parseInt(dateVal.substring(0, 4));
  1578. const currentYear = new Date().getFullYear();
  1579. if (currentYear - birthYear > 100) {
  1580. isDeceased = true;
  1581. }
  1582. isDeceasedUnknown = false;
  1583. } else {
  1584. // Parse failed, set to unknown
  1585. if (birthdayUnknownCb) {
  1586. birthdayUnknownCb.checked = true;
  1587. toggleBirthdayUnknown();
  1588. }
  1589. }
  1590. } else {
  1591. // No birthday found, automatically check unknown
  1592. if (birthdayUnknownCb) {
  1593. birthdayUnknownCb.checked = true;
  1594. toggleBirthdayUnknown();
  1595. }
  1596. }
  1597. // 当自己年龄不详时,通过父母年龄推断是否在世
  1598. if (isDeceasedUnknown && person.matches && person.matches.father && person.matches.father.length > 0) {
  1599. const father = person.matches.father[0];
  1600. if (father.birthday) {
  1601. const fatherBirthYear = new Date(father.birthday * 1000).getFullYear();
  1602. const currentYear = new Date().getFullYear();
  1603. // 假设如果父亲是120年前出生的,子女大概率也已超过100岁
  1604. if (currentYear - fatherBirthYear > 120) {
  1605. isDeceased = true;
  1606. isDeceasedUnknown = false;
  1607. }
  1608. }
  1609. }
  1610. // 已故状态
  1611. const passAwaySelect = form.querySelector('[name="is_pass_away"]');
  1612. if (passAwaySelect) {
  1613. // "殁", "葬", "卒" in raw text usually means deceased. If AI extracted death_date, also true.
  1614. if (isDeceased || person.death_date) {
  1615. passAwaySelect.value = '1'; // 已故
  1616. } else if (isDeceasedUnknown) {
  1617. passAwaySelect.value = '2'; // 未知
  1618. } else {
  1619. passAwaySelect.value = '0'; // 默认健在,除非有证据
  1620. }
  1621. }
  1622. // 4. 婚姻状况
  1623. const maritalSelect = form.querySelector('[name="marital_status"]');
  1624. if (maritalSelect) {
  1625. if (person.spouse_name) {
  1626. maritalSelect.value = '2'; // 已婚
  1627. } else {
  1628. maritalSelect.value = '0'; // 未知
  1629. }
  1630. }
  1631. // 4. 代数 -> 堂内排行
  1632. if (person.generation) {
  1633. const genMatch = person.generation.match(/\d+/);
  1634. // 这里将 AI 解析的 'generation' 填入 'family_rank' (堂内排行)
  1635. // 'name_word_generation' (世系世代) 保持为空
  1636. form.querySelector('[name="family_rank"]').value = person.generation;
  1637. }
  1638. // 4.5 字辈 (name_word)
  1639. let zibei = person.name_word;
  1640. if (!zibei && person.name) {
  1641. // Heuristic: If name starts with "留" and is 3 chars long (e.g. 留学勤), Zibei is index 1.
  1642. // If name starts with "留" and is > 3 chars, we can't be sure, but index 1 is a good guess for generation char.
  1643. // "留学公" -> "留" + "学" + "公". Zibei "学".
  1644. // "留学勤" -> "留" + "学" + "勤". Zibei "学".
  1645. // Let's use a safe heuristic: if name starts with '留' and length >= 3
  1646. if (person.name.startsWith('留') && person.name.length >= 3) {
  1647. zibei = person.name.charAt(1);
  1648. }
  1649. }
  1650. if (zibei) {
  1651. form.querySelector('[name="name_word"]').value = zibei;
  1652. person.name_word = zibei; // Update data object for display
  1653. }
  1654. // 5. 其他信息
  1655. if (person.education) form.querySelector('[name="educational"]').value = person.education;
  1656. if (person.title) form.querySelector('[name="occupation"]').value = person.title;
  1657. // 个人成就/备注字段追加信息
  1658. let extraInfo = [];
  1659. if (person.father_name) extraInfo.push(`父亲: ${person.father_name}`);
  1660. if (person.spouse_name) extraInfo.push(`配偶: ${person.spouse_name}`);
  1661. // 将亲属关系存入 'notes' (人员备注) 字段
  1662. const notesField = form.querySelector('[name="notes"]');
  1663. const currentNotes = notesField.value;
  1664. const newInfo = extraInfo.join('; ');
  1665. if (newInfo && !currentNotes.includes(newInfo)) {
  1666. notesField.value = currentNotes ? (currentNotes + '\n' + newInfo) : newInfo;
  1667. }
  1668. // 确保无论如何都触发一遍自动匹配事件
  1669. notesField.dispatchEvent(new Event('input', {bubbles: true}));
  1670. if (window.checkSpouseInNotes) {
  1671. window.checkSpouseInNotes();
  1672. }
  1673. // --- Auto-Linking Logic ---
  1674. if (person.matches) {
  1675. // Priority: Father > Spouse (Configurable?)
  1676. // For now, if father matches, select father.
  1677. if (person.matches.father && person.matches.father.length > 0) {
  1678. // Pick the first one for now (could show UI to choose if multiple)
  1679. const father = person.matches.father[0];
  1680. const relTypeSelect = form.querySelector('[name="relation_type"]');
  1681. if (relTypeSelect) {
  1682. // Set the related member
  1683. document.getElementById('related-member-display').value = father.name;
  1684. document.getElementById('related_mid').value = father.id;
  1685. // Set relation type to father
  1686. relTypeSelect.value = '1'; // 父子
  1687. // Trigger the lineage generation reference display
  1688. const relatedMid = document.getElementById('related_mid').value;
  1689. if (relTypeSelect.value == 1 && relatedMid) {
  1690. fetch(`/manager/api/member/${relatedMid}`, {
  1691. credentials: 'include'
  1692. })
  1693. .then(response => response.json())
  1694. .then(data => {
  1695. if (data.member && data.member.name_word_generation) {
  1696. // 显示父亲的世系世代
  1697. const lineageContainer = document.getElementById('lineage-generations-container');
  1698. const fatherLineageDiv = document.getElementById('father-lineage');
  1699. if (!fatherLineageDiv) {
  1700. const newDiv = document.createElement('div');
  1701. newDiv.id = 'father-lineage';
  1702. newDiv.className = 'father-lineage-hint';
  1703. newDiv.innerHTML = `
  1704. <div class="d-flex align-items-center">
  1705. <i class="bi bi-info-circle me-2 text-info"></i>
  1706. <div>
  1707. <strong class="text-info">父亲世系世代参考:</strong>
  1708. <span>${data.member.name_word_generation}</span>
  1709. </div>
  1710. </div>
  1711. `;
  1712. // Find the add button container
  1713. const addButtonContainer = document.querySelector('#lineage-generations-container .d-flex.justify-content-between.align-items-center');
  1714. if (addButtonContainer) {
  1715. // Insert after the add button container
  1716. addButtonContainer.parentNode.insertBefore(newDiv, addButtonContainer.nextSibling);
  1717. } else if (lineageContainer.firstChild) {
  1718. // Insert after the first child
  1719. lineageContainer.insertBefore(newDiv, lineageContainer.firstChild.nextSibling);
  1720. } else {
  1721. // Append to container
  1722. lineageContainer.appendChild(newDiv);
  1723. }
  1724. } else {
  1725. fatherLineageDiv.innerHTML = `
  1726. <div class="d-flex align-items-center">
  1727. <i class="bi bi-info-circle me-2 text-info"></i>
  1728. <div>
  1729. <strong class="text-info">父亲世系世代参考:</strong>
  1730. <span>${data.member.name_word_generation}</span>
  1731. </div>
  1732. </div>
  1733. `;
  1734. }
  1735. }
  1736. });
  1737. }
  1738. }
  1739. } else if (person.matches.spouse && person.matches.spouse.length > 0) {
  1740. const spouse = person.matches.spouse[0];
  1741. const relTypeSelect = form.querySelector('[name="relation_type"]');
  1742. if (relTypeSelect) {
  1743. // Set the related member
  1744. document.getElementById('related-member-display').value = spouse.name;
  1745. document.getElementById('related_mid').value = spouse.id;
  1746. // Set relation type to spouse
  1747. relTypeSelect.value = '10'; // 夫妻
  1748. }
  1749. }
  1750. }
  1751. // --- Show Details Panel ---
  1752. const detailContainer = document.getElementById('aiCurrentDetail');
  1753. const detailContent = document.getElementById('aiDetailContent');
  1754. let html = '<ul class="list-unstyled mb-0 font-monospace" style="font-size: 0.85rem;">';
  1755. const getLabel = (k) => fieldMapping[k] || (k === 'children' ? '子女' : k);
  1756. // 遍历属性显示
  1757. for (const key in person) {
  1758. // 隐藏内部属性
  1759. if (key.startsWith('_')) continue;
  1760. let val = person[key];
  1761. const label = getLabel(key);
  1762. // 特殊处理 children
  1763. if (key === 'children') {
  1764. if (Array.isArray(val) && val.length > 0) {
  1765. let childrenHtml = '<div class="d-flex flex-wrap gap-1 mt-1">';
  1766. val.forEach(child => {
  1767. // 使用 child._originalIndex 进行跳转填充
  1768. childrenHtml += `<button class="btn btn-sm btn-outline-info py-0 px-2" style="font-size: 0.75rem;" onclick="fillForm(${child._originalIndex})">${child.name || '未知'}</button>`;
  1769. });
  1770. childrenHtml += '</div>';
  1771. html += `<li class="mb-1"><span class="text-info opacity-75">${label}:</span> ${childrenHtml}</li>`;
  1772. }
  1773. continue;
  1774. }
  1775. // 默认显示
  1776. if (!val || val === '') val = '-';
  1777. html += `<li class="mb-1"><span class="text-info opacity-75">${label}:</span> <span class="text-white ms-1">${val}</span></li>`;
  1778. }
  1779. html += '</ul>';
  1780. detailContent.innerHTML = html;
  1781. detailContainer.style.display = 'block';
  1782. // Visual feedback
  1783. const btn = document.getElementById(`btn-fill-${index}`);
  1784. if(btn) {
  1785. const originalHtml = btn.innerHTML;
  1786. btn.innerHTML = '<i class="bi bi-check"></i> 已填';
  1787. btn.classList.remove('btn-outline-info');
  1788. btn.classList.add('btn-info', 'text-white');
  1789. setTimeout(() => {
  1790. btn.innerHTML = originalHtml;
  1791. btn.classList.add('btn-outline-info');
  1792. btn.classList.remove('btn-info', 'text-white');
  1793. }, 1000);
  1794. }
  1795. }
  1796. // --- Pre-fill Logic from Backend (Async AI Result) ---
  1797. const prefilledContent = {{ prefilled_content | tojson | safe if prefilled_content else 'null' }};
  1798. const sourceOssUrl = "{{ source_oss_url if source_oss_url else '' }}";
  1799. const sourceRecordId = "{{ source_record_id if source_record_id else '' }}";
  1800. if (prefilledContent && (sourceOssUrl || sourceRecordId)) {
  1801. // We have prefilled content from DB, simulate "Recognize Image" success
  1802. document.addEventListener('DOMContentLoaded', async () => {
  1803. // Wait a bit for UI to settle
  1804. setTimeout(async () => {
  1805. // Find image index
  1806. let imgIndex = -1;
  1807. if (sourceRecordId) {
  1808. //优先根据sourceRecordId查找
  1809. imgIndex = images.findIndex(img => img.id == sourceRecordId);
  1810. }
  1811. if (imgIndex === -1 && sourceOssUrl) {
  1812. //如果根据sourceRecordId没找到,再根据sourceOssUrl查找
  1813. imgIndex = images.findIndex(img => img.url === sourceOssUrl);
  1814. }
  1815. if (imgIndex !== -1) {
  1816. currentIndex = imgIndex;
  1817. updateDisplay();
  1818. }
  1819. // Parse and display results
  1820. try {
  1821. let data = prefilledContent;
  1822. if (typeof data === 'string') {
  1823. try {
  1824. data = JSON.parse(data);
  1825. } catch(e) {
  1826. console.error("Prefilled content parse error", e);
  1827. return;
  1828. }
  1829. }
  1830. if (!Array.isArray(data)) data = [data];
  1831. await processAiData(data);
  1832. // Open the log panel to show results
  1833. const aiPanel = document.getElementById('aiLogPanel');
  1834. if (aiPanel) aiPanel.style.display = 'block';
  1835. const status = document.getElementById('reasoningStatus');
  1836. if(status) {
  1837. status.textContent = '已加载历史解析';
  1838. status.className = 'badge bg-success ms-2';
  1839. }
  1840. const logContent = document.getElementById('aiLogContent');
  1841. if(logContent) logContent.textContent = "已加载历史 AI 解析记录。";
  1842. } catch (e) {
  1843. console.error("Error processing prefilled content", e);
  1844. }
  1845. }, 500);
  1846. });
  1847. } else {
  1848. // No prefilled content, initialize display for the first image
  1849. document.addEventListener('DOMContentLoaded', () => {
  1850. updateDisplay();
  1851. });
  1852. }
  1853. // --- Name Cleaning Logic (Matching Backend) ---
  1854. // 仅做繁 -> 简转换,不动姓氏/“公”处理,用于配偶等非留氏族人
  1855. function manualSimplify(text) {
  1856. if (!text) return text;
  1857. text = text.trim();
  1858. const mapping = {
  1859. '學': '学', '國': '国', '萬': '万', '寶': '宝', '興': '兴',
  1860. '華': '华', '會': '会', '葉': '叶', '藝': '艺', '號': '号',
  1861. '處': '处', '見': '见', '視': '视', '言': '言', '語': '语',
  1862. '貝': '贝', '車': '车', '長': '长', '門': '门', '韋': '韦',
  1863. '頁': '页', '風': '风', '飛': '飞', '食': '食', '馬': '马',
  1864. '魚': '鱼', '鳥': '鸟', '麥': '麦', '黃': '黄', '齊': '齐',
  1865. '齒': '齿', '龍': '龙', '龜': '龟', '壽': '寿', '榮': '荣',
  1866. '愛': '爱', '慶': '庆', '衛': '卫', '賢': '贤', '義': '义',
  1867. '禮': '礼', '樂': '乐', '靈': '灵', '滅': '灭', '氣': '气',
  1868. '智': '智', '信': '信', '仁': '仁', '勇': '勇', '嚴': '严',
  1869. '劉': '刘'
  1870. };
  1871. let result = '';
  1872. for (const ch of text) {
  1873. result += mapping[ch] || ch;
  1874. }
  1875. return result;
  1876. }
  1877. // 留氏本人姓名清洗:在 manualSimplify 基础上,处理“留”姓和“公”
  1878. function cleanName(name) {
  1879. if (!name) return name;
  1880. name = manualSimplify(name);
  1881. const exceptions = ['学公', '留学公'];
  1882. if (exceptions.includes(name)) {
  1883. if (!name.startsWith('留')) {
  1884. name = '留' + name;
  1885. }
  1886. return name;
  1887. }
  1888. // Remove '公' suffix
  1889. if (name.endsWith('公')) {
  1890. name = name.slice(0, -1);
  1891. }
  1892. // Ensure '留' prefix
  1893. if (!name.startsWith('留')) {
  1894. name = '留' + name;
  1895. }
  1896. return name;
  1897. }
  1898. function isFemaleSex(sexValue) {
  1899. if (sexValue === null || sexValue === undefined) return false;
  1900. const s = String(sexValue).trim().toLowerCase();
  1901. return s === '女' || s === '2' || s === 'female' || s === 'f';
  1902. }
  1903. function normalizeLookupName(name) {
  1904. if (!name) return '';
  1905. return manualSimplify(String(name)).trim();
  1906. }
  1907. // Extracted function to process AI data and render tree
  1908. async function processAiData(data) {
  1909. const spouseNameSet = new Set();
  1910. data.forEach(p => {
  1911. const n = normalizeLookupName(p.spouse_name);
  1912. if (n) spouseNameSet.add(n);
  1913. });
  1914. // Clean Names First
  1915. data.forEach(p => {
  1916. // Determine "Original" (Raw) and "Simplified" (Cleaned)
  1917. let rawName = p.original_name || p.name;
  1918. let simName = p.name || p.original_name; // Prefer AI simplified name; fallback to raw
  1919. const ownName1 = normalizeLookupName(p.name);
  1920. const ownName2 = normalizeLookupName(p.original_name);
  1921. const isFemaleSpouse = isFemaleSex(p.sex) && (
  1922. !!normalizeLookupName(p.spouse_name) ||
  1923. (ownName1 && spouseNameSet.has(ownName1)) ||
  1924. (ownName2 && spouseNameSet.has(ownName2))
  1925. );
  1926. // 女性配偶:只繁转简,不拼接“留”;其他人维持原规则
  1927. p.simplified_name = isFemaleSpouse ? manualSimplify(simName) : cleanName(simName);
  1928. // Set the name to be the Raw Name for storage in 'name' column
  1929. p.name = rawName;
  1930. // 父亲:同族,用 cleanName(加“留”、去“公”)
  1931. if (p.father_name) p.father_name = cleanName(p.father_name);
  1932. // 配偶:只做繁体 -> 简体,不拼接“留”姓
  1933. if (p.spouse_name) p.spouse_name = manualSimplify(p.spouse_name);
  1934. });
  1935. // Call Relation Check API
  1936. try {
  1937. // Send simplified_name for checking relations if available, or name?
  1938. // The API checks against DB 'name' column.
  1939. // Wait, DB 'name' column is now Traditional Raw.
  1940. // But existing data in DB is Simplified Cleaned.
  1941. // New data will be Traditional Raw in 'name', Simplified Cleaned in 'simplified_name'.
  1942. // The check_relations API uses `WHERE name IN (...)`.
  1943. // The AI returns `father_name` as Simplified (usually).
  1944. // So we are checking Simplified Father Name against...
  1945. // If DB 'name' is mixed (Old Simplified, New Traditional), this is messy.
  1946. // But `check_relations` logic:
  1947. // `names_to_check.add(p['father_name'])` -> Simplified.
  1948. // `SELECT ... WHERE name IN ...`
  1949. // If DB 'name' contains Traditional, we won't find match if we search Simplified.
  1950. // Unless we search `simplified_name` column too?
  1951. // I should update `check_relations` in app.py to search both `name` and `simplified_name`.
  1952. const checkRes = await fetch('/manager/api/check_relations', {
  1953. method: 'POST',
  1954. headers: { 'Content-Type': 'application/json' },
  1955. body: JSON.stringify({ people: data })
  1956. });
  1957. const checkResult = await checkRes.json();
  1958. if (checkResult.success && checkResult.matches) {
  1959. // Merge matches into data
  1960. for (const idx in checkResult.matches) {
  1961. const match = checkResult.matches[idx];
  1962. if (data[idx]) {
  1963. data[idx].matches = match;
  1964. }
  1965. }
  1966. }
  1967. } catch (e) {
  1968. console.warn("Auto-linking failed:", e);
  1969. }
  1970. currentParsedPeople = data;
  1971. document.getElementById('resultCount').innerText = data.length;
  1972. // Update Button State to "View Results"
  1973. updateAiButtonState(true);
  1974. // Build Relationship Tree
  1975. const personMap = {};
  1976. const roots = [];
  1977. // 1. Initialize map
  1978. data.forEach((p, index) => {
  1979. p._originalIndex = index; // Store original index for fillForm
  1980. p.children = [];
  1981. // Use simplified_name as key if available, otherwise name (for consistent lookup)
  1982. const lookupKey = p.simplified_name || p.name;
  1983. personMap[lookupKey] = p;
  1984. });
  1985. // 2. Build Hierarchy
  1986. data.forEach(p => {
  1987. let parentFound = false;
  1988. if (p.father_name) {
  1989. // Try exact match using simplified name (since father_name is usually simplified)
  1990. let father = personMap[p.father_name];
  1991. // Try loose match
  1992. if (!father) {
  1993. for (const name in personMap) {
  1994. if (name.includes(p.father_name) || p.father_name.includes(name)) {
  1995. father = personMap[name];
  1996. break;
  1997. }
  1998. }
  1999. }
  2000. if (father && father !== p) {
  2001. father.children.push(p);
  2002. parentFound = true;
  2003. }
  2004. }
  2005. if (!parentFound) {
  2006. roots.push(p);
  2007. }
  2008. });
  2009. // 3. Recursive Render Function
  2010. function renderNode(p, level = 0) {
  2011. const indent = level * 20;
  2012. let html = `
  2013. <div class="card bg-dark border-secondary mb-1" style="margin-left: ${indent}px; background-color: #2c3034;">
  2014. <div class="card-body p-2 d-flex justify-content-between align-items-center">
  2015. <div class="text-white">
  2016. <div class="fw-bold">
  2017. ${level > 0 ? '<i class="bi bi-arrow-return-right text-secondary me-1"></i>' : ''}
  2018. ${p.name || '未知姓名'}
  2019. <span class="badge bg-secondary text-light ms-1" style="font-size: 0.7rem">${p.sex || '-'}</span>
  2020. </div>
  2021. <div class="small text-white-50" style="font-size: 0.75rem; padding-left: ${level > 0 ? 18 : 0}px;">
  2022. ${p.generation ? '第'+p.generation+'世 ' : ''}
  2023. ${p.father_name ? '父:'+p.father_name : ''}
  2024. </div>
  2025. </div>
  2026. <button id="btn-fill-${p._originalIndex}"
  2027. class="btn btn-sm ${p.is_imported ? 'btn-success disabled' : 'btn-outline-info'} text-nowrap ms-2"
  2028. onclick="${p.is_imported ? '' : `fillForm(${p._originalIndex})`}">
  2029. ${p.is_imported ? '<i class="bi bi-check-lg"></i> 已录入' : '<i class="bi bi-pencil-square"></i> 填充'}
  2030. </button>
  2031. </div>
  2032. </div>
  2033. `;
  2034. if (p.children && p.children.length > 0) {
  2035. p.children.forEach(child => {
  2036. html += renderNode(child, level + 1);
  2037. });
  2038. }
  2039. return html;
  2040. }
  2041. // Render List
  2042. const resultList = document.getElementById('aiResultList');
  2043. const resultSection = document.getElementById('aiResultSection');
  2044. resultList.innerHTML = '';
  2045. // Fix: Use data directly if root finding logic fails or returns empty but data exists
  2046. if (roots.length === 0 && data.length > 0) {
  2047. // Just dump everything flat if tree building fails
  2048. data.forEach(p => resultList.innerHTML += renderNode(p, 0));
  2049. } else {
  2050. roots.forEach(p => {
  2051. resultList.innerHTML += renderNode(p, 0);
  2052. });
  2053. }
  2054. resultSection.style.display = 'block';
  2055. }
  2056. async function recognizeImage() {
  2057. if (images.length === 0) {
  2058. alert('没有可用的图片');
  2059. return;
  2060. }
  2061. const currentImg = images[currentIndex];
  2062. const btn = document.getElementById('aiBtn');
  2063. const originalContent = btn.innerHTML;
  2064. const logPanel = document.getElementById('aiLogPanel');
  2065. const logContent = document.getElementById('aiLogContent');
  2066. const resultSection = document.getElementById('aiResultSection');
  2067. const resultList = document.getElementById('aiResultList');
  2068. const reasoningStatus = document.getElementById('reasoningStatus');
  2069. btn.disabled = true;
  2070. btn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> 识别中...';
  2071. // Reset UI
  2072. logContent.textContent = '';
  2073. resultList.innerHTML = '';
  2074. resultSection.style.display = 'none';
  2075. logPanel.style.display = 'block';
  2076. reasoningStatus.textContent = '连接中...';
  2077. reasoningStatus.className = 'badge bg-secondary ms-2';
  2078. // Ensure reasoning panel is open
  2079. const collapseReasoning = document.getElementById('collapseReasoning');
  2080. if (collapseReasoning && !collapseReasoning.classList.contains('show')) {
  2081. new bootstrap.Collapse(collapseReasoning, { show: true });
  2082. }
  2083. // Retry logic function
  2084. async function fetchAndParse(url, retryCount = 0) {
  2085. const MAX_RETRIES = 2;
  2086. let fullText = '';
  2087. let jsonPart = '';
  2088. let hasJsonStarted = false;
  2089. try {
  2090. if (retryCount > 0) {
  2091. logContent.textContent = `\n[System] 解析失败,正在进行第 ${retryCount} 次重试...\n` + logContent.textContent;
  2092. reasoningStatus.textContent = `重试 ${retryCount}...`;
  2093. }
  2094. const response = await fetch('/manager/api/recognize_image', {
  2095. method: 'POST',
  2096. headers: { 'Content-Type': 'application/json' },
  2097. body: JSON.stringify({ image_url: url })
  2098. });
  2099. if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
  2100. const reader = response.body.getReader();
  2101. const decoder = new TextDecoder();
  2102. const separator = "|||JSON_START|||";
  2103. while (true) {
  2104. const { value, done } = await reader.read();
  2105. if (done) break;
  2106. const chunk = decoder.decode(value, { stream: true });
  2107. fullText += chunk;
  2108. // Only update display if not parsing JSON part yet or just started
  2109. if (!hasJsonStarted) {
  2110. const sepIndex = fullText.indexOf(separator);
  2111. if (sepIndex !== -1) {
  2112. hasJsonStarted = true;
  2113. reasoningStatus.textContent = '解析中...';
  2114. reasoningStatus.className = 'badge bg-info ms-2';
  2115. // Split content for display - only once
  2116. const reasoningPart = fullText.substring(0, sepIndex);
  2117. logContent.textContent = reasoningPart;
  2118. if (collapseReasoning) {
  2119. new bootstrap.Collapse(collapseReasoning, { hide: true });
  2120. }
  2121. } else {
  2122. // Update reasoning text
  2123. logContent.textContent = fullText;
  2124. logContent.scrollTop = logContent.scrollHeight;
  2125. }
  2126. }
  2127. }
  2128. // Parsing Logic
  2129. if (hasJsonStarted) {
  2130. const sepIndex = fullText.indexOf(separator);
  2131. jsonPart = fullText.substring(sepIndex + separator.length);
  2132. reasoningStatus.textContent = '完成';
  2133. reasoningStatus.className = 'badge bg-success ms-2';
  2134. } else {
  2135. // Fallback
  2136. jsonPart = fullText;
  2137. }
  2138. // Clean JSON
  2139. // 1. Try finding [...] array
  2140. let start = jsonPart.indexOf('[');
  2141. let end = jsonPart.lastIndexOf(']');
  2142. // 2. If not found, try finding {...} object and wrap it
  2143. let isSingleObject = false;
  2144. if (start === -1 || end === -1 || end <= start) {
  2145. start = jsonPart.indexOf('{');
  2146. end = jsonPart.lastIndexOf('}');
  2147. isSingleObject = true;
  2148. }
  2149. if (start !== -1 && end !== -1 && end > start) {
  2150. jsonPart = jsonPart.substring(start, end + 1);
  2151. } else {
  2152. // Try to extract any JSON-like array/object structure using regex as fallback
  2153. const jsonMatch = jsonPart.match(/(\[.*\]|\{.*\})/s);
  2154. if (jsonMatch) {
  2155. jsonPart = jsonMatch[0];
  2156. if (jsonPart.trim().startsWith('{')) isSingleObject = true;
  2157. } else {
  2158. // No valid JSON structure found
  2159. console.warn("No JSON brackets found in:", jsonPart);
  2160. throw new Error("未找到有效的 JSON 数据结构");
  2161. }
  2162. }
  2163. let data;
  2164. try {
  2165. // Pre-clean: Remove common markdown code block markers if stuck inside
  2166. jsonPart = jsonPart.replace(/^```json\s*/, '').replace(/```$/, '');
  2167. data = JSON.parse(jsonPart);
  2168. } catch (e) {
  2169. // Attempt to fix common JSON errors (e.g. trailing commas, unclosed strings) - simplified
  2170. console.error("JSON parse error. Content:", jsonPart);
  2171. // Force retry on parse error
  2172. throw new Error("JSON 格式解析错误");
  2173. }
  2174. if (isSingleObject && !Array.isArray(data)) {
  2175. data = [data]; // Normalize to array
  2176. } else if (!Array.isArray(data)) {
  2177. data = [data];
  2178. }
  2179. return data;
  2180. } catch (error) {
  2181. if (retryCount < MAX_RETRIES) {
  2182. // Wait 1s and retry
  2183. await new Promise(r => setTimeout(r, 1000));
  2184. return fetchAndParse(url, retryCount + 1);
  2185. }
  2186. throw error;
  2187. }
  2188. }
  2189. try {
  2190. const data = await fetchAndParse(currentImg.url);
  2191. // Use shared processing function
  2192. await processAiData(data);
  2193. // Update local state for persistence during session
  2194. if (images[currentIndex]) {
  2195. images[currentIndex].ai_status = 2;
  2196. images[currentIndex].ai_content = data;
  2197. }
  2198. } catch (error) {
  2199. console.error(error);
  2200. // Append error to log instead of overwriting valid reasoning
  2201. logContent.textContent += `\n\n[Error] ${error.message}`;
  2202. alert('AI 识别过程失败,请重试。\n错误详情: ' + error.message);
  2203. } finally {
  2204. btn.innerHTML = originalContent;
  2205. btn.disabled = false;
  2206. }
  2207. }
  2208. // Check for duplicate name
  2209. let nameCheckTimeout = null;
  2210. const nameInput = document.getElementById('nameInput');
  2211. const nameCheckResult = document.getElementById('nameCheckResult');
  2212. if (nameInput) {
  2213. nameInput.addEventListener('input', function() {
  2214. clearTimeout(nameCheckTimeout);
  2215. const nameVal = this.value.trim();
  2216. if (!nameVal) {
  2217. nameCheckResult.innerHTML = '';
  2218. return;
  2219. }
  2220. nameCheckTimeout = setTimeout(() => {
  2221. fetch(`/manager/api/check_name?name=${encodeURIComponent(nameVal)}`)
  2222. .then(r => r.json())
  2223. .then(data => {
  2224. if (data.success && data.exists) {
  2225. let html = `<div class="alert alert-warning py-2 mb-0 mt-2 small">
  2226. <i class="bi bi-exclamation-triangle-fill"></i> 发现 <strong>${data.matches.length}</strong> 个同名记录,请确认是否为同一人:
  2227. <ul class="mb-0 mt-1 ps-3">`;
  2228. data.matches.forEach(m => {
  2229. let sex = m.sex === 1 ? '男' : (m.sex === 2 ? '女' : '未知');
  2230. let deadStr = m.is_pass_away == 1 ? ' (已故)' : (m.is_pass_away == 2 ? ' (未知)' : '');
  2231. html += `<li><a href="/manager/member_detail/${m.id}" target="_blank" class="alert-link">${m.name}</a> - ${sex} - 出生: ${m.birthday_str}${deadStr}</li>`;
  2232. });
  2233. html += `</ul></div>`;
  2234. nameCheckResult.innerHTML = html;
  2235. } else {
  2236. nameCheckResult.innerHTML = '';
  2237. }
  2238. })
  2239. .catch(err => console.error('Error checking name:', err));
  2240. }, 600);
  2241. });
  2242. }
  2243. // Auto-link spouse from notes if female
  2244. const notesInput = document.querySelector('textarea[name="notes"]');
  2245. const sexSelect = document.querySelector('select[name="sex"]');
  2246. // Attach to window so fillForm can explicitly call it
  2247. window.checkSpouseInNotes = function() {
  2248. if (!notesInput || !sexSelect) return;
  2249. const val = notesInput.value;
  2250. // Check for father information
  2251. const fatherMatch = val.match(/父亲[::\s]*([^\s;;,,。]+)/);
  2252. if (fatherMatch && fatherMatch[1]) {
  2253. const fatherName = fatherMatch[1].trim();
  2254. const relationTypeSelect = document.querySelector('select[name="relation_type"]');
  2255. if (relationTypeSelect) {
  2256. // Find the father in the members list
  2257. fetch(`/manager/api/members?search=${encodeURIComponent(fatherName)}`, {
  2258. credentials: 'include'
  2259. })
  2260. .then(response => response.json())
  2261. .then(data => {
  2262. if (data.members && data.members.length > 0) {
  2263. // Find the best match
  2264. let bestMatch = null;
  2265. for (const member of data.members) {
  2266. const optName = member.name;
  2267. const normalizedOpt = optName.replace(/公$/, '').replace(/^留/, '');
  2268. const normalizedFather = fatherName.replace(/公$/, '').replace(/^留/, '');
  2269. if (optName === fatherName || normalizedOpt === normalizedFather ||
  2270. (normalizedOpt && normalizedFather && (normalizedOpt.includes(normalizedFather) || normalizedFather.includes(normalizedOpt)))) {
  2271. bestMatch = member;
  2272. break;
  2273. }
  2274. }
  2275. if (bestMatch) {
  2276. // Set the related member
  2277. document.getElementById('related-member-display').value = bestMatch.name;
  2278. document.getElementById('related_mid').value = bestMatch.id;
  2279. // Set relation type to father
  2280. relationTypeSelect.value = '1';
  2281. // Trigger the lineage generation reference display
  2282. const relatedMid = document.getElementById('related_mid').value;
  2283. if (relationTypeSelect.value == 1 && relatedMid) {
  2284. fetch(`/manager/api/member/${relatedMid}`, {
  2285. credentials: 'include'
  2286. })
  2287. .then(response => response.json())
  2288. .then(data => {
  2289. if (data.member && data.member.name_word_generation) {
  2290. // 显示父亲的世系世代
  2291. const lineageContainer = document.getElementById('lineage-generations-container');
  2292. const fatherLineageDiv = document.getElementById('father-lineage');
  2293. if (!fatherLineageDiv) {
  2294. const newDiv = document.createElement('div');
  2295. newDiv.id = 'father-lineage';
  2296. newDiv.className = 'father-lineage-hint';
  2297. newDiv.innerHTML = `
  2298. <div class="d-flex align-items-center">
  2299. <i class="bi bi-info-circle me-2 text-info"></i>
  2300. <div>
  2301. <strong class="text-info">父亲世系世代参考:</strong>
  2302. <span>${data.member.name_word_generation}</span>
  2303. </div>
  2304. </div>
  2305. `;
  2306. // Find the add button container
  2307. const addButtonContainer = document.querySelector('#lineage-generations-container .d-flex.justify-content-between.align-items-center');
  2308. if (addButtonContainer) {
  2309. // Insert after the add button container
  2310. addButtonContainer.parentNode.insertBefore(newDiv, addButtonContainer.nextSibling);
  2311. } else if (lineageContainer.firstChild) {
  2312. // Insert after the first child
  2313. lineageContainer.insertBefore(newDiv, lineageContainer.firstChild.nextSibling);
  2314. } else {
  2315. // Append to container
  2316. lineageContainer.appendChild(newDiv);
  2317. }
  2318. } else {
  2319. fatherLineageDiv.innerHTML = `
  2320. <div class="d-flex align-items-center">
  2321. <i class="bi bi-info-circle me-2 text-info"></i>
  2322. <div>
  2323. <strong class="text-info">父亲世系世代参考:</strong>
  2324. <span>${data.member.name_word_generation}</span>
  2325. </div>
  2326. </div>
  2327. `;
  2328. }
  2329. }
  2330. });
  2331. }
  2332. }
  2333. }
  2334. });
  2335. }
  2336. }
  2337. // Only check for spouse if female
  2338. if (sexSelect.value === '2') {
  2339. const spouseMatch = val.match(/配偶[::\s]*([^\s;;,,。]+)/);
  2340. if (spouseMatch && spouseMatch[1]) {
  2341. const spouseName = spouseMatch[1].trim();
  2342. const relationTypeSelect = document.querySelector('select[name="relation_type"]');
  2343. if (relationTypeSelect) {
  2344. // Find the spouse in the members list
  2345. fetch(`/manager/api/members?search=${encodeURIComponent(spouseName)}`, {
  2346. credentials: 'include'
  2347. })
  2348. .then(response => response.json())
  2349. .then(data => {
  2350. if (data.members && data.members.length > 0) {
  2351. // Find the best match
  2352. let bestMatch = null;
  2353. for (const member of data.members) {
  2354. const optName = member.name;
  2355. const normalizedOpt = optName.replace(/公$/, '').replace(/^留/, '');
  2356. const normalizedSpouse = spouseName.replace(/公$/, '').replace(/^留/, '');
  2357. if (optName === spouseName || normalizedOpt === normalizedSpouse ||
  2358. (normalizedOpt && normalizedSpouse && (normalizedOpt.includes(normalizedSpouse) || normalizedSpouse.includes(normalizedOpt)))) {
  2359. bestMatch = member;
  2360. break;
  2361. }
  2362. }
  2363. if (bestMatch) {
  2364. // Set the related member
  2365. document.getElementById('related-member-display').value = bestMatch.name;
  2366. document.getElementById('related_mid').value = bestMatch.id;
  2367. // Set relation type to spouse
  2368. relationTypeSelect.value = '10';
  2369. }
  2370. }
  2371. });
  2372. }
  2373. }
  2374. }
  2375. };
  2376. if (notesInput && sexSelect) {
  2377. notesInput.addEventListener('input', window.checkSpouseInNotes);
  2378. sexSelect.addEventListener('change', window.checkSpouseInNotes);
  2379. }
  2380. </script>
  2381. <script>
  2382. document.addEventListener('DOMContentLoaded', function() {
  2383. const container = document.getElementById('lineage-generations-container');
  2384. const addButton = document.getElementById('add-lineage');
  2385. const inputForm = document.getElementById('lineage-input-form');
  2386. const lineageInput = document.getElementById('lineage-input');
  2387. const confirmButton = document.getElementById('confirm-lineage');
  2388. const cancelButton = document.getElementById('cancel-lineage');
  2389. if (!container || !addButton || !inputForm || !lineageInput || !confirmButton || !cancelButton) return;
  2390. function addLineageTag(value) {
  2391. const tag = document.createElement('span');
  2392. tag.className = 'lineage-tag badge bg-primary bg-opacity-10 text-primary border border-primary border-opacity-25 px-3 py-2 rounded-pill d-inline-flex align-items-center';
  2393. tag.style.fontSize = '0.85rem';
  2394. tag.innerHTML = `${value}<button type="button" class="btn-close ms-2 remove-lineage" style="font-size:0.55rem;filter:none;opacity:0.6;" aria-label="删除"></button><input type="hidden" name="lineage_generations[]" value="${value}">`;
  2395. container.insertBefore(tag, inputForm);
  2396. }
  2397. container.addEventListener('click', function(e) {
  2398. const removeBtn = e.target.closest('.remove-lineage');
  2399. if (removeBtn) {
  2400. e.preventDefault();
  2401. e.stopPropagation();
  2402. const tag = removeBtn.closest('.lineage-tag');
  2403. if (tag) tag.remove();
  2404. }
  2405. });
  2406. addButton.addEventListener('click', function(e) {
  2407. e.preventDefault();
  2408. e.stopPropagation();
  2409. inputForm.classList.remove('d-none');
  2410. addButton.classList.add('d-none');
  2411. lineageInput.value = '';
  2412. lineageInput.focus();
  2413. });
  2414. function confirmInput() {
  2415. const value = lineageInput.value.trim();
  2416. if (value) {
  2417. addLineageTag(value);
  2418. }
  2419. lineageInput.value = '';
  2420. inputForm.classList.add('d-none');
  2421. addButton.classList.remove('d-none');
  2422. }
  2423. confirmButton.addEventListener('click', function(e) {
  2424. e.preventDefault();
  2425. e.stopPropagation();
  2426. confirmInput();
  2427. });
  2428. lineageInput.addEventListener('keydown', function(e) {
  2429. if (e.key === 'Enter') {
  2430. e.preventDefault();
  2431. e.stopPropagation();
  2432. confirmInput();
  2433. }
  2434. if (e.key === 'Escape') {
  2435. e.preventDefault();
  2436. lineageInput.value = '';
  2437. inputForm.classList.add('d-none');
  2438. addButton.classList.remove('d-none');
  2439. }
  2440. });
  2441. cancelButton.addEventListener('click', function(e) {
  2442. e.preventDefault();
  2443. e.stopPropagation();
  2444. lineageInput.value = '';
  2445. inputForm.classList.add('d-none');
  2446. addButton.classList.remove('d-none');
  2447. });
  2448. });
  2449. // 加载成员数据
  2450. function loadMembers(page = 1, search = '') {
  2451. console.log('Loading members...', { page, search });
  2452. fetch(`/manager/api/members?page=${page}&search=${encodeURIComponent(search)}`, {
  2453. credentials: 'include'
  2454. })
  2455. .then(response => {
  2456. console.log('Response status:', response.status);
  2457. return response.json();
  2458. })
  2459. .then(data => {
  2460. console.log('Response data:', data);
  2461. // Check if it's an error response
  2462. if (data.success === false) {
  2463. console.error('API error:', data.message);
  2464. // If unauthorized, redirect to login
  2465. if (data.message === 'Unauthorized') {
  2466. window.location.href = '/manager/login';
  2467. }
  2468. return;
  2469. }
  2470. // Handle success response - check if members and total exist
  2471. if (data.members !== undefined && data.total !== undefined) {
  2472. membersData = data.members;
  2473. totalMembers = data.total;
  2474. totalPages = Math.ceil(totalMembers / 10);
  2475. currentPage = page;
  2476. // 更新成员列表
  2477. updateMemberList();
  2478. // 更新分页
  2479. updatePagination();
  2480. // 更新总数
  2481. document.getElementById('total-members').textContent = totalMembers;
  2482. } else {
  2483. console.error('Invalid response structure:', data);
  2484. // Set default values
  2485. membersData = [];
  2486. totalMembers = 0;
  2487. totalPages = 1;
  2488. currentPage = 1;
  2489. updateMemberList();
  2490. updatePagination();
  2491. document.getElementById('total-members').textContent = 0;
  2492. }
  2493. })
  2494. .catch(error => {
  2495. console.error('Error loading members:', error);
  2496. });
  2497. }
  2498. // 更新成员列表
  2499. function updateMemberList() {
  2500. const memberList = document.getElementById('member-list');
  2501. memberList.innerHTML = '';
  2502. if (membersData.length === 0) {
  2503. memberList.innerHTML = '<div class="list-group-item text-center text-muted">暂无成员数据</div>';
  2504. return;
  2505. }
  2506. membersData.forEach(function(member) {
  2507. const item = document.createElement('div');
  2508. item.className = 'list-group-item list-group-item-action shadow-sm hover:bg-light';
  2509. item.style.transition = 'all 0.2s ease';
  2510. item.onclick = function() {
  2511. selectMemberById(member.id);
  2512. };
  2513. item.innerHTML = `
  2514. <div class="d-flex justify-content-between align-items-center">
  2515. <div>
  2516. <h6 class="mb-0">
  2517. <a href="/manager/member_detail/${member.id}" target="_blank" class="text-primary text-decoration-none" onclick="event.stopPropagation();">
  2518. ${member.name} ${member.simplified_name && member.simplified_name !== member.name ? `(${member.simplified_name})` : ''}
  2519. </a>
  2520. </h6>
  2521. <small class="text-muted">ID: ${member.id} | ${member.sex === 1 ? '男' : '女'}</small>
  2522. ${member.name_word_generation ? `<small class="d-block text-muted">世系世代: ${member.name_word_generation}</small>` : ''}
  2523. ${member.father_name ? `
  2524. <small class="d-block text-muted">
  2525. 父亲: ${member.father_name} ${member.father_simplified_name && member.father_simplified_name !== member.father_name ? `(${member.father_simplified_name})` : ''}
  2526. ${member.father_generation ? ` | 世系世代: ${member.father_generation}` : ''}
  2527. </small>
  2528. ` : ''}
  2529. </div>
  2530. <button type="button" class="btn btn-sm btn-primary" onclick="event.stopPropagation(); selectMemberById(${member.id});">
  2531. 选择
  2532. </button>
  2533. </div>
  2534. `;
  2535. memberList.appendChild(item);
  2536. });
  2537. }
  2538. // 更新分页
  2539. function updatePagination() {
  2540. const pagination = document.querySelector('.pagination');
  2541. pagination.innerHTML = '';
  2542. // 上一页
  2543. const prevLi = document.createElement('li');
  2544. prevLi.className = `page-item ${currentPage === 1 ? 'disabled' : ''}`;
  2545. prevLi.innerHTML = `<a class="page-link" href="#" onclick="changePage(${currentPage - 1})">&laquo;</a>`;
  2546. pagination.appendChild(prevLi);
  2547. // 页码
  2548. for (let i = 1; i <= totalPages; i++) {
  2549. const li = document.createElement('li');
  2550. li.className = `page-item ${i === currentPage ? 'active' : ''}`;
  2551. li.innerHTML = `<a class="page-link" href="#" onclick="changePage(${i})">${i}</a>`;
  2552. pagination.appendChild(li);
  2553. }
  2554. // 下一页
  2555. const nextLi = document.createElement('li');
  2556. nextLi.className = `page-item ${currentPage === totalPages ? 'disabled' : ''}`;
  2557. nextLi.innerHTML = `<a class="page-link" href="#" onclick="changePage(${currentPage + 1})">&raquo;</a>`;
  2558. pagination.appendChild(nextLi);
  2559. }
  2560. // 改变页码
  2561. function changePage(page) {
  2562. if (page < 1 || page > totalPages) return;
  2563. const search = document.getElementById('member-search').value;
  2564. loadMembers(page, search);
  2565. }
  2566. // 搜索成员
  2567. function searchMembers() {
  2568. const search = document.getElementById('member-search').value;
  2569. loadMembers(1, search);
  2570. }
  2571. // 选择成员(支持多条关系)
  2572. function selectMember(member) {
  2573. const index = window.currentRelationIndex || 0;
  2574. const rows = document.querySelectorAll('.relation-row');
  2575. if (rows[index]) {
  2576. const displayInput = rows[index].querySelector('.related-member-display');
  2577. const hiddenInput = rows[index].querySelector('.related_mid');
  2578. if (displayInput) {
  2579. let displayText = member.name;
  2580. if (member.simplified_name && member.simplified_name !== member.name) {
  2581. displayText += ` (${member.simplified_name})`;
  2582. }
  2583. if (member.name_word) {
  2584. displayText += ` · ${member.name_word}`;
  2585. }
  2586. displayInput.value = displayText;
  2587. }
  2588. if (hiddenInput) {
  2589. hiddenInput.value = member.id;
  2590. }
  2591. } else {
  2592. // Fallback for old single relation mode
  2593. const displayInput = document.getElementById('related-member-display');
  2594. const hiddenInput = document.getElementById('related_mid');
  2595. if (displayInput) {
  2596. displayInput.value = member.name;
  2597. }
  2598. if (hiddenInput) {
  2599. hiddenInput.value = member.id;
  2600. }
  2601. }
  2602. // 检查是否是父子关系,如果是,显示父亲的世系世代
  2603. const relationTypeSelect = rows[index] ? rows[index].querySelector('select.relation-type') : document.querySelector('select[name="relation_type"]');
  2604. const relationType = relationTypeSelect ? relationTypeSelect.value : '';
  2605. if (relationType == 1) { // 父子关系
  2606. fetch(`/manager/api/member/${member.id}`)
  2607. .then(response => response.json())
  2608. .then(data => {
  2609. if (data.member && data.member.name_word_generation) {
  2610. // 显示父亲的世系世代
  2611. const lineageContainer = document.getElementById('lineage-generations-container');
  2612. const fatherLineageDiv = document.getElementById('father-lineage');
  2613. if (!fatherLineageDiv) {
  2614. const newDiv = document.createElement('div');
  2615. newDiv.id = 'father-lineage';
  2616. newDiv.className = 'father-lineage-hint';
  2617. newDiv.innerHTML = `
  2618. <div class="d-flex align-items-center">
  2619. <i class="bi bi-info-circle me-2 text-info"></i>
  2620. <div>
  2621. <strong class="text-info">父亲世系世代参考:</strong>
  2622. <span>${data.member.name_word_generation}</span>
  2623. </div>
  2624. </div>
  2625. `;
  2626. // Find the add button container
  2627. const addButtonContainer = document.querySelector('#lineage-generations-container .d-flex.justify-content-between.align-items-center');
  2628. if (addButtonContainer) {
  2629. // Insert after the add button container
  2630. addButtonContainer.parentNode.insertBefore(newDiv, addButtonContainer.nextSibling);
  2631. } else if (lineageContainer.firstChild) {
  2632. // Insert after the first child
  2633. lineageContainer.insertBefore(newDiv, lineageContainer.firstChild.nextSibling);
  2634. } else {
  2635. // Append to container
  2636. lineageContainer.appendChild(newDiv);
  2637. }
  2638. } else {
  2639. fatherLineageDiv.innerHTML = `
  2640. <div class="d-flex align-items-center">
  2641. <i class="bi bi-info-circle me-2 text-info"></i>
  2642. <div>
  2643. <strong class="text-info">父亲世系世代参考:</strong>
  2644. <span>${data.member.name_word_generation}</span>
  2645. </div>
  2646. </div>
  2647. `;
  2648. }
  2649. }
  2650. });
  2651. }
  2652. // 关闭模态框
  2653. const modal = bootstrap.Modal.getInstance(document.getElementById('memberSelectModal'));
  2654. modal.hide();
  2655. }
  2656. // 通过ID选择成员
  2657. function selectMemberById(memberId) {
  2658. // 先在当前加载的成员中查找
  2659. const member = membersData.find(m => m.id === memberId);
  2660. if (member) {
  2661. selectMember(member);
  2662. } else {
  2663. // 如果没找到,从API获取
  2664. fetch(`/manager/api/member/${memberId}`)
  2665. .then(response => response.json())
  2666. .then(data => {
  2667. if (data.member) {
  2668. selectMember(data.member);
  2669. }
  2670. });
  2671. }
  2672. }
  2673. // 监听关系类型变化
  2674. document.addEventListener('DOMContentLoaded', function() {
  2675. // 监听关系类型变化
  2676. const relationTypeSelect = document.querySelector('select[name="relation_type"]');
  2677. if (relationTypeSelect) {
  2678. relationTypeSelect.addEventListener('change', function() {
  2679. const relatedMid = document.getElementById('related_mid').value;
  2680. if (this.value == 1 && relatedMid) {
  2681. // 如果选择了父子关系且已选择关联成员,显示父亲的世系世代
  2682. fetch(`/manager/api/member/${relatedMid}`, {
  2683. credentials: 'include'
  2684. })
  2685. .then(response => response.json())
  2686. .then(data => {
  2687. if (data.member && data.member.name_word_generation) {
  2688. const lineageContainer = document.getElementById('lineage-generations-container');
  2689. const fatherLineageDiv = document.getElementById('father-lineage');
  2690. if (!fatherLineageDiv) {
  2691. const newDiv = document.createElement('div');
  2692. newDiv.id = 'father-lineage';
  2693. newDiv.className = 'father-lineage-hint';
  2694. newDiv.innerHTML = `
  2695. <div class="d-flex align-items-center">
  2696. <i class="bi bi-info-circle me-2 text-info"></i>
  2697. <div>
  2698. <strong class="text-info">父亲世系世代参考:</strong>
  2699. <span>${data.member.name_word_generation}</span>
  2700. </div>
  2701. </div>
  2702. `;
  2703. // Find the add button container
  2704. const addButtonContainer = document.querySelector('#lineage-generations-container .d-flex.justify-content-between.align-items-center');
  2705. if (addButtonContainer) {
  2706. // Insert after the add button container
  2707. addButtonContainer.parentNode.insertBefore(newDiv, addButtonContainer.nextSibling);
  2708. } else if (lineageContainer.firstChild) {
  2709. // Insert after the first child
  2710. lineageContainer.insertBefore(newDiv, lineageContainer.firstChild.nextSibling);
  2711. } else {
  2712. // Append to container
  2713. lineageContainer.appendChild(newDiv);
  2714. }
  2715. } else {
  2716. fatherLineageDiv.innerHTML = `
  2717. <div class="d-flex align-items-center">
  2718. <i class="bi bi-info-circle me-2 text-info"></i>
  2719. <div>
  2720. <strong class="text-info">父亲世系世代参考:</strong>
  2721. <span>${data.member.name_word_generation}</span>
  2722. </div>
  2723. </div>
  2724. `;
  2725. }
  2726. }
  2727. });
  2728. }
  2729. });
  2730. }
  2731. // 监听成员选择模态框显示
  2732. const memberSelectModal = document.getElementById('memberSelectModal');
  2733. memberSelectModal.addEventListener('shown.bs.modal', function() {
  2734. loadMembers();
  2735. });
  2736. });
  2737. </script>
  2738. {% endblock %}