relationshipAdd.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. </style>
  104. <script>
  105. $(document).ready(function() {
  106. // 处理表单提交前的验证
  107. $('#save').click(function() {
  108. if (validateForm()) {
  109. $('#form1').submit();
  110. }
  111. });
  112. // 客户搜索功能
  113. var customerSearchTimeouts = {
  114. source: null,
  115. target: null
  116. };
  117. var customerIsComposing = false;
  118. // 监听输入法组合事件
  119. $(document).on('compositionstart', '.customer-search', function() {
  120. customerIsComposing = true;
  121. });
  122. $(document).on('compositionend', '.customer-search', function() {
  123. customerIsComposing = false;
  124. $(this).trigger('input'); // 手动触发一次input事件
  125. });
  126. // 客户搜索输入
  127. $(document).on('input', '.customer-search', function() {
  128. // 如果是输入法正在组合中文,不处理
  129. if (customerIsComposing) return;
  130. var $this = $(this);
  131. var searchType = $this.data('type'); // source 或 target
  132. var searchTerm = $this.val().trim();
  133. var customerDropdown = $('#' + searchType + '_customer_dropdown');
  134. // 清除之前的超时
  135. clearTimeout(customerSearchTimeouts[searchType]);
  136. // 隐藏之前的结果
  137. customerDropdown.hide();
  138. // 清除之前的选择
  139. $('#' + searchType + '_customer_id').val('');
  140. $('#' + searchType + '_customer_selected').hide();
  141. // 如果搜索词少于1个字符,不执行搜索
  142. if (searchTerm.length < 1) {
  143. return;
  144. }
  145. // 设置一个300毫秒的超时,以减少请求数量
  146. customerSearchTimeouts[searchType] = setTimeout(function() {
  147. $.ajax({
  148. url: 'get_customer_search.php',
  149. type: 'GET',
  150. data: {search: searchTerm},
  151. dataType: 'json',
  152. success: function(data) {
  153. customerDropdown.empty();
  154. if (data && data.customers && data.customers.length > 0) {
  155. $.each(data.customers, function(i, customer) {
  156. var displayText = customer.cs_company;
  157. if (customer.cs_code) {
  158. displayText = customer.cs_code + ' - ' + displayText;
  159. }
  160. var item = $('<div class="customer-item"></div>')
  161. .attr('data-id', customer.id)
  162. .attr('data-company', customer.cs_company)
  163. .attr('data-display', displayText)
  164. .text(displayText);
  165. customerDropdown.append(item);
  166. });
  167. customerDropdown.show();
  168. }
  169. },
  170. error: function() {
  171. console.log('搜索客户失败,请重试');
  172. }
  173. });
  174. }, 300);
  175. });
  176. // 点击选择客户
  177. $(document).on('click', '.customer-item', function() {
  178. var $this = $(this);
  179. var customerId = $this.data('id');
  180. var customerName = $this.data('company');
  181. var displayText = $this.data('display');
  182. var parentContainer = $this.parent();
  183. var searchType = parentContainer.attr('id').replace('_customer_dropdown', '');
  184. // 设置选中的客户ID和名称
  185. $('#' + searchType + '_customer_id').val(customerId);
  186. $('#' + searchType + '_customer_search').hide();
  187. $('#' + searchType + '_customer_selected').text(displayText).show();
  188. $('#' + searchType + '_customer_company').val(customerName);
  189. // 添加标题属性,便于查看完整信息
  190. $('#' + searchType + '_customer_selected').attr('title', displayText);
  191. // 隐藏下拉菜单
  192. parentContainer.hide();
  193. });
  194. // 点击已选客户可以重新选择
  195. $(document).on('click', '.selected-customer-info', function() {
  196. var $this = $(this);
  197. var searchType = $this.attr('id').replace('_customer_selected', '');
  198. // 显示搜索框,隐藏已选信息
  199. $('#' + searchType + '_customer_search').val('').show();
  200. $this.hide();
  201. // 清空客户ID
  202. $('#' + searchType + '_customer_id').val('');
  203. });
  204. // 点击其他地方隐藏下拉列表
  205. $(document).on('click', function(e) {
  206. if (!$(e.target).closest('.customer-search-container').length) {
  207. $('.customer-dropdown').hide();
  208. }
  209. });
  210. // 如果编辑模式下已有选中客户,显示客户信息
  211. <?php if ($isEdit): ?>
  212. $('#source_customer_selected').show();
  213. $('#target_customer_selected').show();
  214. $('#source_customer_search').hide();
  215. $('#target_customer_search').hide();
  216. <?php endif; ?>
  217. });
  218. // 表单验证函数
  219. function validateForm() {
  220. // 验证源客户和目标客户
  221. var sourceCustomerId = $('#source_customer_id').val();
  222. var targetCustomerId = $('#target_customer_id').val();
  223. var relationshipType = $('#relationship_type').val();
  224. if (!sourceCustomerId) {
  225. alert('请选择源客户');
  226. $('#source_customer_search').show().focus();
  227. $('#source_customer_selected').hide();
  228. return false;
  229. }
  230. if (!targetCustomerId) {
  231. alert('请选择目标客户');
  232. $('#target_customer_search').show().focus();
  233. $('#target_customer_selected').hide();
  234. return false;
  235. }
  236. if (sourceCustomerId === targetCustomerId) {
  237. alert('源客户和目标客户不能是同一个客户');
  238. return false;
  239. }
  240. if (!relationshipType) {
  241. alert('请选择关系类型');
  242. $('#relationship_type').focus();
  243. return false;
  244. }
  245. return true;
  246. }
  247. </script>
  248. </head>
  249. <body class="clear">
  250. <?php // require_once 'panel.php'; ?>
  251. <div id="man_zone">
  252. <form name="form1" id="form1" method="post" action="relationshipSave.php">
  253. <?php if ($isEdit): ?>
  254. <input type="hidden" name="id" value="<?= $relationshipData['id'] ?>">
  255. <?php endif; ?>
  256. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  257. <tbody>
  258. <tr>
  259. <th width="15%">源客户</th>
  260. <td>
  261. <div class="customer-search-container">
  262. <input type="text" id="source_customer_search" class="customer-search txt1" data-type="source" placeholder="输入客户名称搜索..." value="" />
  263. <div id="source_customer_selected" class="selected-customer-info" title="<?php
  264. if ($isEdit) {
  265. $sourceCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  266. $stmt = $conn->prepare($sourceCustQuery);
  267. $stmt->bind_param('i', $relationshipData['source_customer_id']);
  268. $stmt->execute();
  269. $result = $stmt->get_result();
  270. if ($row = $result->fetch_assoc()) {
  271. $displayText = $row['cs_company'];
  272. if ($row['cs_code']) {
  273. $displayText = $row['cs_code'] . ' - ' . $displayText;
  274. }
  275. echo htmlspecialchars(textDecode($displayText));
  276. }
  277. }
  278. ?>"><?php
  279. if ($isEdit) {
  280. $sourceCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  281. $stmt = $conn->prepare($sourceCustQuery);
  282. $stmt->bind_param('i', $relationshipData['source_customer_id']);
  283. $stmt->execute();
  284. $result = $stmt->get_result();
  285. if ($row = $result->fetch_assoc()) {
  286. $displayText = $row['cs_company'];
  287. if ($row['cs_code']) {
  288. $displayText = $row['cs_code'] . ' - ' . $displayText;
  289. }
  290. echo htmlspecialchars(textDecode($displayText));
  291. }
  292. }
  293. ?></div>
  294. <div id="source_customer_dropdown" class="customer-dropdown"></div>
  295. </div>
  296. <input type="hidden" id="source_customer_id" name="source_customer_id" value="<?= $isEdit ? $relationshipData['source_customer_id'] : '' ?>" />
  297. <input type="hidden" id="source_customer_company" name="source_customer_company" value="" />
  298. </td>
  299. </tr>
  300. <tr>
  301. <th>目标客户</th>
  302. <td>
  303. <div class="customer-search-container">
  304. <input type="text" id="target_customer_search" class="customer-search txt1" data-type="target" placeholder="输入客户名称搜索..." value="" />
  305. <div id="target_customer_selected" class="selected-customer-info" title="<?php
  306. if ($isEdit) {
  307. $targetCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  308. $stmt = $conn->prepare($targetCustQuery);
  309. $stmt->bind_param('i', $relationshipData['target_customer_id']);
  310. $stmt->execute();
  311. $result = $stmt->get_result();
  312. if ($row = $result->fetch_assoc()) {
  313. $displayText = $row['cs_company'];
  314. if ($row['cs_code']) {
  315. $displayText = $row['cs_code'] . ' - ' . $displayText;
  316. }
  317. echo htmlspecialchars(textDecode($displayText));
  318. }
  319. }
  320. ?>"><?php
  321. if ($isEdit) {
  322. $targetCustQuery = "SELECT cs_company, cs_code FROM customer WHERE id = ?";
  323. $stmt = $conn->prepare($targetCustQuery);
  324. $stmt->bind_param('i', $relationshipData['target_customer_id']);
  325. $stmt->execute();
  326. $result = $stmt->get_result();
  327. if ($row = $result->fetch_assoc()) {
  328. $displayText = $row['cs_company'];
  329. if ($row['cs_code']) {
  330. $displayText = $row['cs_code'] . ' - ' . $displayText;
  331. }
  332. echo htmlspecialchars(textDecode($displayText));
  333. }
  334. }
  335. ?></div>
  336. <div id="target_customer_dropdown" class="customer-dropdown"></div>
  337. </div>
  338. <input type="hidden" id="target_customer_id" name="target_customer_id" value="<?= $isEdit ? $relationshipData['target_customer_id'] : '' ?>" />
  339. <input type="hidden" id="target_customer_company" name="target_customer_company" value="" />
  340. </td>
  341. </tr>
  342. <tr>
  343. <th>关系类型</th>
  344. <td>
  345. <select id="relationship_type" name="relationship_type" class="txt1">
  346. <option value="">请选择关系类型</option>
  347. <option value="1" <?= $isEdit && $relationshipData['relationship_type'] == 1 ? 'selected' : '' ?>>母公司-子公司</option>
  348. <option value="2" <?= $isEdit && $relationshipData['relationship_type'] == 2 ? 'selected' : '' ?>>供应商-客户</option>
  349. <option value="3" <?= $isEdit && $relationshipData['relationship_type'] == 3 ? 'selected' : '' ?>>合作伙伴</option>
  350. <option value="4" <?= $isEdit && $relationshipData['relationship_type'] == 4 ? 'selected' : '' ?>>竞争对手</option>
  351. <option value="5" <?= $isEdit && $relationshipData['relationship_type'] == 5 ? 'selected' : '' ?>>推荐人</option>
  352. <option value="6" <?= $isEdit && $relationshipData['relationship_type'] == 6 ? 'selected' : '' ?>>其他</option>
  353. </select>
  354. </td>
  355. </tr>
  356. <tr>
  357. <th>关系状态</th>
  358. <td>
  359. <label>
  360. <input type="radio" name="relationship_status" value="1" <?= (!$isEdit || ($isEdit && $relationshipData['relationship_status'] == 1)) ? 'checked' : '' ?>>
  361. 启用
  362. </label>
  363. <label>
  364. <input type="radio" name="relationship_status" value="0" <?= ($isEdit && $relationshipData['relationship_status'] == 0) ? 'checked' : '' ?>>
  365. 停用
  366. </label>
  367. </td>
  368. </tr>
  369. <tr>
  370. <th>关系描述</th>
  371. <td>
  372. <textarea name="description" class="txt1" style="width:80%; height:100px;"><?= $isEdit ? htmlspecialchars(textDecode($relationshipData['description'])) : '' ?></textarea>
  373. </td>
  374. </tr>
  375. <tr>
  376. <th></th>
  377. <td>
  378. <input type="button" name="save" id="save" value="保存" class="btn1" />
  379. <input type="button" value="返回" class="btn1" onClick="location.href='relationships.php'" />
  380. </td>
  381. </tr>
  382. </tbody>
  383. </table>
  384. </form>
  385. </div>
  386. </body>
  387. </html>