relationshipAdd.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. require_once 'conn.php';
  3. checkLogin();
  4. // 检查是否为编辑模式
  5. $isEdit = false;
  6. $relationshipData = null;
  7. if (isset($_GET['id']) && is_numeric($_GET['id'])) {
  8. $id = intval($_GET['id']);
  9. $query = "SELECT * FROM customer_relationship WHERE id = ?";
  10. $stmt = $conn->prepare($query);
  11. $stmt->bind_param('i', $id);
  12. $stmt->execute();
  13. $result = $stmt->get_result();
  14. if ($result->num_rows > 0) {
  15. $relationshipData = $result->fetch_assoc();
  16. $isEdit = true;
  17. } else {
  18. echo "<script>alert('未找到指定的客户关系记录!'); window.location.href='relationships.php';</script>";
  19. exit;
  20. }
  21. }
  22. ?>
  23. <!DOCTYPE html>
  24. <html xmlns="http://www.w3.org/1999/xhtml">
  25. <head>
  26. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  27. <title><?= $isEdit ? '编辑' : '新增' ?>客户关系</title>
  28. <link rel="stylesheet" href="css/common.css" type="text/css" />
  29. <script src="js/jquery-1.7.2.min.js"></script>
  30. <script src="js/js.js"></script>
  31. <script src="js/ySearchSelect.js"></script>
  32. <style>
  33. body {
  34. margin: a;
  35. padding: 20px;
  36. background: #fff;
  37. }
  38. #man_zone {
  39. margin-left: 0;
  40. }
  41. .relationship-form-container {
  42. margin-bottom: 20px;
  43. }
  44. .customer-search-container {
  45. display: flex;
  46. align-items: center;
  47. margin-bottom: 10px;
  48. position: relative;
  49. width: 60%;
  50. }
  51. .customer-info {
  52. margin-top: 5px;
  53. padding: 5px;
  54. background-color: #f5f5f5;
  55. border: 1px solid #ddd;
  56. border-radius: 3px;
  57. display: none;
  58. }
  59. .customer-dropdown {
  60. display: none;
  61. position: absolute;
  62. background: white;
  63. border: 1px solid #ccc;
  64. max-height: 200px;
  65. overflow-y: auto;
  66. width: 100%;
  67. z-index: 1000;
  68. box-shadow: 0 3px 8px rgba(0,0,0,0.25);
  69. border-radius: 0 0 4px 4px;
  70. top: 100%;
  71. left: 0;
  72. }
  73. .customer-item {
  74. padding: 10px 12px;
  75. cursor: pointer;
  76. border-bottom: 1px solid #eee;
  77. transition: background-color 0.2s;
  78. }
  79. .customer-item:hover {
  80. background-color: #f0f0f0;
  81. }
  82. .customer-item:last-child {
  83. border-bottom: none;
  84. }
  85. .selected-customer-info {
  86. font-weight: bold;
  87. padding: 8px 10px;
  88. border: 1px solid #ddd;
  89. background-color: #f9f9f9;
  90. display: none;
  91. width: 100%;
  92. box-sizing: border-box;
  93. word-break: break-all;
  94. overflow: hidden;
  95. text-overflow: ellipsis;
  96. white-space: normal;
  97. min-height: 38px;
  98. cursor: pointer;
  99. }
  100. .selected-customer-info:hover {
  101. background-color: #f0f0f0;
  102. }
  103. .customer-clear-btn {
  104. margin-left: 5px;
  105. color: #e74c3c;
  106. font-weight: bold;
  107. cursor: pointer;
  108. float: right;
  109. }
  110. .customer-clear-btn:hover {
  111. color: #c0392b;
  112. }
  113. </style>
  114. <script>
  115. $(document).ready(function() {
  116. // 处理表单提交前的验证
  117. $('#save').click(function() {
  118. if (validateForm()) {
  119. $('#form1').submit();
  120. }
  121. });
  122. // 客户搜索功能
  123. var customerSearchTimeouts = {
  124. source: null,
  125. target: null
  126. };
  127. var customerIsComposing = false;
  128. // 监听输入法组合事件
  129. $(document).on('compositionstart', '.customer-search', function() {
  130. customerIsComposing = true;
  131. });
  132. $(document).on('compositionend', '.customer-search', function() {
  133. customerIsComposing = false;
  134. $(this).trigger('input'); // 手动触发一次input事件
  135. });
  136. // 客户搜索输入
  137. $(document).on('input', '.customer-search', function() {
  138. // 如果是输入法正在组合中文,不处理
  139. if (customerIsComposing) return;
  140. var $this = $(this);
  141. var searchType = $this.data('type'); // source 或 target
  142. var searchTerm = $this.val().trim();
  143. var customerDropdown = $('#' + searchType + '_customer_dropdown');
  144. // 清除之前的超时
  145. clearTimeout(customerSearchTimeouts[searchType]);
  146. // 隐藏之前的结果
  147. customerDropdown.hide();
  148. // 清除之前的选择
  149. $('#' + searchType + '_customer_id').val('');
  150. $('#' + searchType + '_customer_selected').hide();
  151. // 如果搜索词少于1个字符,不执行搜索
  152. if (searchTerm.length < 1) {
  153. return;
  154. }
  155. // 设置一个300毫秒的超时,以减少请求数量
  156. customerSearchTimeouts[searchType] = setTimeout(function() {
  157. $.ajax({
  158. url: 'get_customer_search.php',
  159. type: 'GET',
  160. data: {search: searchTerm},
  161. dataType: 'json',
  162. success: function(data) {
  163. customerDropdown.empty();
  164. if (data && data.customers && data.customers.length > 0) {
  165. $.each(data.customers, function(i, customer) {
  166. var displayText = customer.cs_company;
  167. if (customer.cs_code) {
  168. displayText = customer.cs_code + ' - ' + displayText;
  169. }
  170. var item = $('<div class="customer-item"></div>')
  171. .attr('data-id', customer.id)
  172. .attr('data-company', customer.cs_company)
  173. .attr('data-display', displayText)
  174. .text(displayText);
  175. customerDropdown.append(item);
  176. });
  177. customerDropdown.show();
  178. }
  179. },
  180. error: function() {
  181. console.log('搜索客户失败,请重试');
  182. }
  183. });
  184. }, 300);
  185. });
  186. // 点击选择客户
  187. $(document).on('click', '.customer-item', function() {
  188. var $this = $(this);
  189. var customerId = $this.data('id');
  190. var customerName = $this.data('company');
  191. var displayText = $this.data('display');
  192. var parentContainer = $this.parent();
  193. var searchType = parentContainer.attr('id').replace('_customer_dropdown', '');
  194. // 设置选中的客户ID和名称
  195. $('#' + searchType + '_customer_id').val(customerId);
  196. $('#' + searchType + '_customer_search').hide();
  197. $('#' + searchType + '_customer_selected').text(displayText).append('<span class="customer-clear-btn" data-type="' + searchType + '">X</span>').show();
  198. $('#' + searchType + '_customer_company').val(customerName);
  199. // 添加标题属性,便于查看完整信息
  200. $('#' + searchType + '_customer_selected').attr('title', displayText);
  201. // 隐藏下拉菜单
  202. parentContainer.hide();
  203. });
  204. // 点击已选客户可以重新选择
  205. $(document).on('click', '.selected-customer-info', function() {
  206. // 点击整个已选区域不再执行任何操作
  207. });
  208. // 点击X按钮清除选择的客户
  209. $(document).on('click', '.customer-clear-btn', function(e) {
  210. e.stopPropagation();
  211. var searchType = $(this).data('type');
  212. // 显示搜索框,隐藏已选信息
  213. $('#' + searchType + '_customer_search').val('').show();
  214. $('#' + searchType + '_customer_selected').hide();
  215. // 清空客户ID
  216. $('#' + searchType + '_customer_id').val('');
  217. });
  218. // 点击其他地方隐藏下拉列表
  219. $(document).on('click', function(e) {
  220. if (!$(e.target).closest('.customer-search-container').length) {
  221. $('.customer-dropdown').hide();
  222. }
  223. });
  224. // 如果编辑模式下已有选中客户,显示客户信息
  225. <?php if ($isEdit): ?>
  226. $('#source_customer_selected').show();
  227. $('#target_customer_selected').show();
  228. $('#source_customer_search').hide();
  229. $('#target_customer_search').hide();
  230. <?php endif; ?>
  231. });
  232. // 表单验证函数
  233. function validateForm() {
  234. // 验证源客户和目标客户
  235. var sourceCustomerId = $('#source_customer_id').val();
  236. var targetCustomerId = $('#target_customer_id').val();
  237. var relationshipType = $('#relationship_type').val();
  238. if (!sourceCustomerId) {
  239. alert('请选择源客户');
  240. $('#source_customer_search').show().focus();
  241. $('#source_customer_selected').hide();
  242. return false;
  243. }
  244. if (!targetCustomerId) {
  245. alert('请选择目标客户');
  246. $('#target_customer_search').show().focus();
  247. $('#target_customer_selected').hide();
  248. return false;
  249. }
  250. if (sourceCustomerId === targetCustomerId) {
  251. alert('源客户和目标客户不能是同一个客户');
  252. return false;
  253. }
  254. if (!relationshipType) {
  255. alert('请选择关系类型');
  256. $('#relationship_type').focus();
  257. return false;
  258. }
  259. return true;
  260. }
  261. </script>
  262. </head>
  263. <body class="clear">
  264. <?php // require_once 'panel.php'; ?>
  265. <div id="man_zone">
  266. <form name="form1" id="form1" method="post" action="relationshipSave.php">
  267. <?php if ($isEdit): ?>
  268. <input type="hidden" name="id" value="<?= $relationshipData['id'] ?>">
  269. <?php endif; ?>
  270. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  271. <tbody>
  272. <tr>
  273. <th width="15%">源客户</th>
  274. <td>
  275. <div class="customer-search-container">
  276. <input type="text" id="source_customer_search" class="customer-search txt1" data-type="source" placeholder="输入客户名称搜索..." value="" />
  277. <div id="source_customer_selected" class="selected-customer-info" title="<?php
  278. if ($isEdit) {
  279. $sourceCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  280. $stmt = $conn->prepare($sourceCustQuery);
  281. $stmt->bind_param('i', $relationshipData['source_customer_id']);
  282. $stmt->execute();
  283. $result = $stmt->get_result();
  284. if ($row = $result->fetch_assoc()) {
  285. $displayText = $row['cs_company'];
  286. if ($row['cs_code']) {
  287. $displayText = $row['cs_code'] . ' - ' . $displayText;
  288. }
  289. echo textDecode($displayText);
  290. }
  291. }
  292. ?>"><?php
  293. if ($isEdit) {
  294. $sourceCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  295. $stmt = $conn->prepare($sourceCustQuery);
  296. $stmt->bind_param('i', $relationshipData['source_customer_id']);
  297. $stmt->execute();
  298. $result = $stmt->get_result();
  299. if ($row = $result->fetch_assoc()) {
  300. $displayText = $row['cs_company'];
  301. if ($row['cs_code']) {
  302. $displayText = $row['cs_code'] . ' - ' . $displayText;
  303. }
  304. echo textDecode($displayText);
  305. }
  306. }
  307. ?><span class="customer-clear-btn" data-type="source">X</span></div>
  308. <div id="source_customer_dropdown" class="customer-dropdown"></div>
  309. </div>
  310. <input type="hidden" id="source_customer_id" name="source_customer_id" value="<?= $isEdit ? $relationshipData['source_customer_id'] : '' ?>" />
  311. <input type="hidden" id="source_customer_company" name="source_customer_company" value="" />
  312. </td>
  313. </tr>
  314. <tr>
  315. <th>目标客户</th>
  316. <td>
  317. <div class="customer-search-container">
  318. <input type="text" id="target_customer_search" class="customer-search txt1" data-type="target" placeholder="输入客户名称搜索..." value="" />
  319. <div id="target_customer_selected" class="selected-customer-info" title="<?php
  320. if ($isEdit) {
  321. $targetCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  322. $stmt = $conn->prepare($targetCustQuery);
  323. $stmt->bind_param('i', $relationshipData['target_customer_id']);
  324. $stmt->execute();
  325. $result = $stmt->get_result();
  326. if ($row = $result->fetch_assoc()) {
  327. $displayText = $row['cs_company'];
  328. if ($row['cs_code']) {
  329. $displayText = $row['cs_code'] . ' - ' . $displayText;
  330. }
  331. echo textDecode($displayText);
  332. }
  333. }
  334. ?>"><?php
  335. if ($isEdit) {
  336. $targetCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  337. $stmt = $conn->prepare($targetCustQuery);
  338. $stmt->bind_param('i', $relationshipData['target_customer_id']);
  339. $stmt->execute();
  340. $result = $stmt->get_result();
  341. if ($row = $result->fetch_assoc()) {
  342. $displayText = $row['cs_company'];
  343. if ($row['cs_code']) {
  344. $displayText = $row['cs_code'] . ' - ' . $displayText;
  345. }
  346. echo textDecode($displayText);
  347. }
  348. }
  349. ?><span class="customer-clear-btn" data-type="target">X</span></div>
  350. <div id="target_customer_dropdown" class="customer-dropdown"></div>
  351. </div>
  352. <input type="hidden" id="target_customer_id" name="target_customer_id" value="<?= $isEdit ? $relationshipData['target_customer_id'] : '' ?>" />
  353. <input type="hidden" id="target_customer_company" name="target_customer_company" value="" />
  354. </td>
  355. </tr>
  356. <tr>
  357. <th>关系类型</th>
  358. <td>
  359. <select id="relationship_type" name="relationship_type" class="txt1">
  360. <option value="">请选择关系类型</option>
  361. <option value="1" <?= $isEdit && $relationshipData['relationship_type'] == 1 ? 'selected' : '' ?>>母公司-子公司</option>
  362. <option value="2" <?= $isEdit && $relationshipData['relationship_type'] == 2 ? 'selected' : '' ?>>供应商-客户</option>
  363. <option value="3" <?= $isEdit && $relationshipData['relationship_type'] == 3 ? 'selected' : '' ?>>合作伙伴</option>
  364. <option value="4" <?= $isEdit && $relationshipData['relationship_type'] == 4 ? 'selected' : '' ?>>竞争对手</option>
  365. <option value="5" <?= $isEdit && $relationshipData['relationship_type'] == 5 ? 'selected' : '' ?>>推荐人</option>
  366. <option value="6" <?= $isEdit && $relationshipData['relationship_type'] == 6 ? 'selected' : '' ?>>其他</option>
  367. </select>
  368. </td>
  369. </tr>
  370. <tr>
  371. <th>关系状态</th>
  372. <td>
  373. <label>
  374. <input type="radio" name="relationship_status" value="1" <?= (!$isEdit || ($isEdit && $relationshipData['relationship_status'] == 1)) ? 'checked' : '' ?>>
  375. 启用
  376. </label>
  377. <label>
  378. <input type="radio" name="relationship_status" value="0" <?= ($isEdit && $relationshipData['relationship_status'] == 0) ? 'checked' : '' ?>>
  379. 停用
  380. </label>
  381. </td>
  382. </tr>
  383. <tr>
  384. <th>关系描述</th>
  385. <td>
  386. <textarea name="description" class="txt1" style="width:80%; height:100px;"><?= $isEdit ? htmlspecialchars(textDecode($relationshipData['description'])) : '' ?></textarea>
  387. </td>
  388. </tr>
  389. <tr>
  390. <th></th>
  391. <td>
  392. <input type="button" name="save" id="save" value="保存" class="btn1" />
  393. <input type="button" value="返回" class="btn1" onClick="location.href='relationships.php'" />
  394. </td>
  395. </tr>
  396. </tbody>
  397. </table>
  398. </form>
  399. </div>
  400. </body>
  401. </html>