Browse Source

fleat: contact 3

igb 4 weeks ago
parent
commit
77407edeb2
3 changed files with 1183 additions and 320 deletions
  1. 482 47
      customerEdit.php
  2. 228 131
      customerSave.php
  3. 473 142
      customers.php

+ 482 - 47
customerEdit.php

@@ -10,12 +10,8 @@ $hrefstr = "?keys=$keys&Page=$page";
 
 // Validate and fetch customer data
 if (!empty($id) && is_numeric($id)) {
-    $sql = "SELECT c.*, cc.contact_name as cs_name, cc.tel as cs_tel, cc.email as cs_email, 
-            cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, cc.linkedin as cs_linkedin, 
-            cc.facebook as cs_facebook, cc.alibaba as cs_alibaba 
-            FROM customer c 
-            LEFT JOIN customer_contact cc ON c.id = cc.customer_id
-            WHERE c.cs_belong = ? AND c.id = ?";
+    // Fetch customer basic information
+    $sql = "SELECT c.* FROM customer c WHERE c.cs_belong = ? AND c.id = ?";
     
     $stmt = $conn->prepare($sql);
     $stmt->bind_param("ii", $_SESSION['employee_id'], $id);
@@ -25,27 +21,51 @@ if (!empty($id) && is_numeric($id)) {
     if ($row = $result->fetch_assoc()) {
         $customer = [
             'cs_company' => textUncode($row['cs_company']),
-            'cs_name' => textUncode($row['cs_name']),
-            'cs_country' => $row['cs_country'],
-            'cs_tel' => textUncode($row['cs_tel']),
-            'cs_email' => textUncode($row['cs_email']),
-            'cs_whatsapp' => textUncode($row['cs_whatsapp']),
-            'cs_wechat' => textUncode($row['cs_wechat']),
-            'cs_linkedin' => textUncode($row['cs_linkedin']),
-            'cs_facebook' => textUncode($row['cs_facebook']),
             'cs_address' => textUncode($row['cs_address']),
-            'cs_alibaba' => textUncode($row['cs_alibaba']),
             'cs_code' => textUncode($row['cs_code']),
             'cs_deal' => textUncode($row['cs_deal']),
             'cs_addtime' => $row['cs_addtime'],
             'cs_belongclient' => $row['cs_belongclient'],
             'cs_updatetime' => $row['cs_updatetime'],
             'cs_from' => $row['cs_from'],
+            'cs_country' => $row['cs_country'],
             'cs_type' => $row['cs_type'],
             'cs_note' => htmlUnCode($row['cs_note']),
             'cs_claimFrom' => $row['cs_claimFrom'],
             'allowedit' => $row['allowedit']
         ];
+        
+        // Fetch all contact records for this customer
+        $contactSql = "SELECT cc.* FROM customer_contact cc WHERE cc.customer_id = ?";
+        $contactStmt = $conn->prepare($contactSql);
+        $contactStmt->bind_param("i", $id);
+        $contactStmt->execute();
+        $contactResult = $contactStmt->get_result();
+        
+        $contacts = [];
+        while ($contactRow = $contactResult->fetch_assoc()) {
+            $contact = [
+                'id' => $contactRow['id'],
+                'contact_name' => textUncode($contactRow['contact_name']),
+                'created_at' => $contactRow['created_at'],
+                'updated_at' => $contactRow['updated_at']
+            ];
+            
+            // Process each contact method type (up to 3 entries each)
+            $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
+            foreach ($methodTypes as $type) {
+                for ($i = 1; $i <= 3; $i++) {
+                    $fieldBase = $type . '_' . $i;
+                    $contact[$fieldBase] = textUncode($contactRow[$fieldBase]);
+                    if ($type == 'tel' || $type == 'whatsapp') {
+                        $contact[$fieldBase . '_format'] = textUncode($contactRow[$fieldBase . '_format']);
+                    }
+                    $contact[$fieldBase . '_bu'] = textUncode($contactRow[$fieldBase . '_bu']);
+                }
+            }
+            
+            $contacts[] = $contact;
+        }
     } else {
         echo "<script>alert('客户不存在或你没权限查看!');history.back();</script>";
         exit;
@@ -82,7 +102,295 @@ if (!empty($id) && is_numeric($id)) {
             upMediaUrl:"upload.php",
             upMediaExt:"wmv,avi,wma,mp3,mid"
         });
+        
+        // Add contact form
+        $('.add-contact-btn').click(function() {
+            var contactsContainer = $('#contacts-container');
+            var contactIndex = contactsContainer.children('.contact-form').length;
+            var contactForm = `
+                <div class="contact-form" id="contact-form-${contactIndex}">
+                    <div class="contact-header">
+                        <button type="button" class="remove-contact-btn" data-index="${contactIndex}">删除</button>
+                        <h3>联系人 #${contactIndex + 1}</h3>
+                    </div>
+                    <input type="hidden" name="contact[${contactIndex}][id]" value="">
+                    <table width="100%" border="0" cellpadding="3" cellspacing="1" class="contact-table">
+                        <tr>
+                            <th width="8%">联系人</th>
+                            <td><input type="text" name="contact[${contactIndex}][contact_name]" class="txt1" placeholder="联系人姓名"/></td>
+                        </tr>
+                    </table>
+                    <div class="contact-methods-container" id="contact-methods-${contactIndex}">
+                        <!-- Contact methods will be added here -->
+                    </div>
+                    <button type="button" class="add-method-btn" data-contact-index="${contactIndex}">添加联系方式</button>
+                </div>
+            `;
+            contactsContainer.append(contactForm);
+        });
+        
+        // Add contact method
+        $(document).on('click', '.add-method-btn', function() {
+            var contactIndex = $(this).data('contact-index');
+            var methodsContainer = $('#contact-methods-' + contactIndex);
+            
+            // Count existing methods by type
+            var methodCounts = {};
+            methodsContainer.find('select.method-select').each(function() {
+                var type = $(this).val();
+                if (type) {
+                    methodCounts[type] = (methodCounts[type] || 0) + 1;
+                }
+            });
+            
+            var methodRow = `
+                <div class="contact-method-row">
+                    <select class="method-select" onchange="updateMethodSelectAndPlaceholder(this)">
+                        <option value="">请选择联系方式</option>
+                        <option value="tel" ${(methodCounts.tel || 0) >= 3 ? 'disabled' : ''}>电话</option>
+                        <option value="wechat" ${(methodCounts.wechat || 0) >= 3 ? 'disabled' : ''}>微信</option>
+                        <option value="whatsapp" ${(methodCounts.whatsapp || 0) >= 3 ? 'disabled' : ''}>WhatsApp</option>
+                        <option value="email" ${(methodCounts.email || 0) >= 3 ? 'disabled' : ''}>邮箱</option>
+                        <option value="linkedin" ${(methodCounts.linkedin || 0) >= 3 ? 'disabled' : ''}>领英</option>
+                        <option value="facebook" ${(methodCounts.facebook || 0) >= 3 ? 'disabled' : ''}>Facebook</option>
+                        <option value="alibaba" ${(methodCounts.alibaba || 0) >= 3 ? 'disabled' : ''}>阿里巴巴</option>
+                    </select>
+                    <input type="text" class="txt1 method-input" style="width:60%;" placeholder="请选择联系方式类型">
+                    <button type="button" class="remove-method-btn">删除</button>
+                </div>
+            `;
+            
+            methodsContainer.append(methodRow);
+            updateMethodFields(methodsContainer.find('.contact-method-row:last-child'));
+        });
+        
+        // Remove contact method
+        $(document).on('click', '.remove-method-btn', function() {
+            var methodRow = $(this).closest('.contact-method-row');
+            var contactIndex = methodRow.closest('.contact-form').attr('id').split('-')[2];
+            var type = methodRow.find('select.method-select').val();
+            methodRow.remove();
+            
+            // Update available options in other selects
+            updateAvailableMethodTypes(contactIndex);
+        });
     });
+
+    // Update method fields based on selection
+    function updateMethodFields(methodRow) {
+        var select = methodRow.find('select.method-select');
+        var input = methodRow.find('input.method-input');
+        var contactForm = methodRow.closest('.contact-form');
+        var contactIndex = contactForm.attr('id').split('-')[2];
+        var type = select.val();
+        
+        if (!type) return;
+        
+        // Count existing methods of this type
+        var count = 1;
+        contactForm.find('select.method-select').each(function() {
+            if ($(this).val() === type && $(this)[0] !== select[0]) {
+                count++;
+            }
+        });
+        
+        // Update field names
+        select.attr('name', `contact[${contactIndex}][${type}_${count}]`);
+        input.attr('name', `contact[${contactIndex}][${type}_${count}]`);
+        
+        // Add format field for tel and whatsapp
+        if (type === 'tel' || type === 'whatsapp') {
+            if (!methodRow.find('.format-input').length) {
+                input.after(`<input type="hidden" class="format-input" name="contact[${contactIndex}][${type}_${count}_format]">`);
+            }
+        }
+        
+        // Add backup field
+        if (!methodRow.find('.backup-input').length) {
+            methodRow.append(`<input type="hidden" class="backup-input" name="contact[${contactIndex}][${type}_${count}_bu]">`);
+        }
+    }
+
+    // Update available method types for a contact
+    function updateAvailableMethodTypes(contactIndex) {
+        var methodsContainer = $('#contact-methods-' + contactIndex);
+        
+        // Count methods by type
+        var methodCounts = {};
+        methodsContainer.find('select.method-select').each(function() {
+            var type = $(this).val();
+            if (type) {
+                methodCounts[type] = (methodCounts[type] || 0) + 1;
+            }
+        });
+        
+        // Update all selects in this contact
+        methodsContainer.find('select.method-select').each(function() {
+            var currentValue = $(this).val();
+            
+            $(this).find('option').each(function() {
+                var optionValue = $(this).val();
+                if (optionValue && optionValue !== currentValue) {
+                    $(this).prop('disabled', (methodCounts[optionValue] || 0) >= 3);
+                }
+            });
+        });
+    }
+
+    // Update placeholder and handle method fields
+    function updateMethodSelectAndPlaceholder(selectElement) {
+        var methodRow = $(selectElement).closest('.contact-method-row');
+        updateMethodPlaceholder(selectElement);
+        updateMethodFields(methodRow);
+        
+        var contactIndex = methodRow.closest('.contact-form').attr('id').split('-')[2];
+        updateAvailableMethodTypes(contactIndex);
+    }
+
+    // Look for the updateMethodPlaceholder function and replace it with this modified version
+    function updateMethodPlaceholder(selectElement) {
+        var placeholder = "";
+        var value = $(selectElement).val();
+        
+        switch(value) {
+            case "tel":
+                placeholder = "电话格式:区号+号码 如:+86 15012345678";
+                break;
+            case "wechat":
+                placeholder = "微信";
+                break;
+            case "whatsapp":
+                placeholder = "Whatsapp 格式:区号+号码 如:+86 15012345678";
+                break;
+            case "email":
+                placeholder = "邮件";
+                break;
+            case "linkedin":
+                placeholder = "领英链接";
+                break;
+            case "facebook":
+                placeholder = "Facebook";
+                break;
+            case "alibaba":
+                placeholder = "阿里巴巴";
+                break;
+            default:
+                placeholder = "请选择联系方式类型";
+        }
+        
+        $(selectElement).next('.method-input').attr('placeholder', placeholder);
+    }
+
+    // Custom validation function for multiple contacts form with contact methods
+    function validateMultipleContactsForm() {
+        var clientCode = $("#cs_code").val();
+        var clientCompany = $("#cs_company").val();
+        var clientFrom = $("#cs_from").val();
+        var clientCountry = $("#cs_country").val();
+        
+        // Validate basic customer info
+        if (clientCode == "" || clientCode == null) {
+            alert("客户代码不能为空!");
+            $("#cs_code").focus();
+            return false;
+        }
+        
+        if (clientCountry == 0) {
+            alert("这是哪个国家的客户?");
+            return false;
+        }
+        
+        if (clientFrom == "0") {
+            alert("请填写客户来源!");
+            return false;
+        }
+        
+        // Validate that at least one contact has at least one contact method
+        var hasContactMethod = false;
+        var allContactsValid = true;
+        
+        $('.contact-form').each(function(contactIndex) {
+            var $form = $(this);
+            var contactName = $form.find('input[name*="[contact_name]"]').val();
+            var hasMethodInThisContact = false;
+            
+            // Check if this contact has methods
+            $form.find('.contact-method-row').each(function() {
+                var methodType = $(this).find('select.method-select').val();
+                var methodValue = $(this).find('input.method-input').val();
+                
+                if (methodValue) {
+                    hasMethodInThisContact = true;
+                    hasContactMethod = true;
+                }
+                
+                // Check if method type is selected but value is empty
+                if (methodType && !methodValue) {
+                    alert("联系方式类型已选择但值为空");
+                    allContactsValid = false;
+                    return false;
+                }
+            });
+            
+            // If contact has a name but no methods, or has methods but no name
+            if ((contactName && !hasMethodInThisContact) || (!contactName && hasMethodInThisContact)) {
+                alert("联系人 #" + (contactIndex + 1) + " 缺少联系人姓名或联系方式");
+                allContactsValid = false;
+                return false;
+            }
+            
+            // If contact has neither name nor methods, it's an empty contact
+            if (!contactName && !hasMethodInThisContact) {
+                alert("联系人 #" + (contactIndex + 1) + " 是空的,请填写信息或删除此联系人");
+                allContactsValid = false;
+                return false;
+            }
+        });
+        
+        if (!allContactsValid) {
+            return false;
+        }
+        
+        if (!hasContactMethod) {
+            alert("至少需要添加一个联系人,且联系人至少需要一种联系方式!");
+            return false;
+        }
+        
+        // Set tag values
+        $("input#mytag").val($(".taglist").html());
+        
+        // Convert the dynamic contact methods to the standard format expected by the server
+        $('.contact-form').each(function(contactIndex) {
+            var methodsData = {};
+            
+            $(this).find('.contact-method-row').each(function() {
+                var type = $(this).find('select.method-select').val();
+                var value = $(this).find('input.method-input').val();
+                
+                if (type && value) {
+                    methodsData[type] = value;
+                }
+            });
+            
+            // Create hidden inputs for each method
+            for (var type in methodsData) {
+                $('<input>').attr({
+                    type: 'hidden',
+                    name: 'contact[' + contactIndex + '][' + type + ']',
+                    value: methodsData[type]
+                }).appendTo(this);
+            }
+        });
+        
+        return true;
+    }
+
+    // Modified submission function
+    function submitCustomerForm() {
+        if (validateMultipleContactsForm()) {
+            $("#form1").submit();
+        }
+    }
     </script>
     <style>
         body {
@@ -93,12 +401,82 @@ if (!empty($id) && is_numeric($id)) {
         #man_zone {
             margin-left: 0;
         }
+        .contact-form {
+            margin-bottom: 10px;
+            /*border: 1px solid #ddd;*/
+            padding: 8px;
+            background-color: #FFFFFF;
+        }
+        .contact-header {
+            display: flex;
+            align-items: center;
+            margin-bottom: 8px;
+            gap: 10px;
+        }
+        .contact-header h3 {
+            margin: 0;
+            order: 2;
+            flex-grow: 1;
+        }
+        .remove-contact-btn {
+            background-color: #f44336;
+            color: white;
+            border: none;
+            padding: 4px 8px;
+            cursor: pointer;
+            order: 1;
+        }
+        .add-contact-btn {
+            background-color: #4CAF50;
+            color: white;
+            border: none;
+            padding: 6px 12px;
+            margin-bottom: 10px;
+            cursor: pointer;
+        }
+        .contact-methods-container {
+            margin-top: 8px;
+        }
+        .contact-method-row {
+            margin-bottom: 6px;
+            padding: 6px;
+            border: 1px solid #eee;
+           /*background-color: #f5f5f5;*/
+            display: flex;
+            align-items: center;
+            gap: 8px;
+        }
+        .add-method-btn {
+            background-color: #2196F3;
+            color: white;
+            border: none;
+            padding: 4px 8px;
+            margin-top: 4px;
+            cursor: pointer;
+        }
+        .remove-method-btn {
+            background-color: #f44336;
+            color: white;
+            border: none;
+            padding: 2px 4px;
+            cursor: pointer;
+        }
+        .method-select {
+            margin-right: 8px;
+            padding: 3px;
+        }
+        .contact-table {
+            margin-bottom: 6px;
+        }
+        .contact-table td, .contact-table th {
+            padding: 4px 6px;
+        }
     </style>
 </head>
 <body class="clear">
 <?php // require_once 'panel.php'; ?>
 <div id="man_zone">
-    <form name="form1" id="form1" method="post" action="customerSave.php<?= $hrefstr ?>" onSubmit="return checkInput();">
+    <form name="form1" id="form1" method="post" action="customerSave.php<?= $hrefstr ?>">
         <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
             <tbody>
                 <tr>
@@ -115,10 +493,6 @@ if (!empty($id) && is_numeric($id)) {
                     <th width="8%">公司名称</th>
                     <td><input type="text" id="cs_company" name="cs_company" value="<?= htmlspecialcharsFix($customer['cs_company']) ?>" class="txt1" /></td>
                 </tr>
-                <tr>
-                    <th width="8%">联系人</th>
-                    <td><input type="text" id="cs_name" name="cs_name" value="<?= htmlspecialcharsFix($customer['cs_name']) ?>" class="txt1" /></td>
-                </tr>
                 <tr>
                     <th width="8%">地区</th>
                     <td>
@@ -173,26 +547,89 @@ if (!empty($id) && is_numeric($id)) {
                     </td>
                 </tr>
                 <tr>
-                    <th rowspan="7">联系方式</th>
-                    <td><input type="text" id="cs_tel" name="cs_tel" value="<?= htmlspecialcharsFix($customer['cs_tel']) ?>" class="txt1 tel" placeholder="电话格式:区号+号码 如:+86 15012345678" /></td>
-                </tr>
-                <tr>
-                    <td><input type="text" id="cs_wechat" name="cs_wechat" value="<?= htmlspecialcharsFix($customer['cs_wechat']) ?>" class="txt1 wechat" placeholder="微信"/></td>
-                </tr>
-                <tr>
-                    <td><input type="text" id="cs_whatsapp" name="cs_whatsapp" value="<?= htmlspecialcharsFix($customer['cs_whatsapp']) ?>" class="txt1 whatsapp" placeholder="Whatsapp 格式:区号+号码 如:+86 15012345678"/></td>
-                </tr>
-                <tr>
-                    <td><input type="text" id="cs_email" name="cs_email" value="<?= htmlspecialcharsFix($customer['cs_email']) ?>" class="txt1 mail" placeholder="邮件" /></td>
-                </tr>
-                <tr>
-                    <td><input type="text" id="cs_linkedin" name="cs_linkedin" value="<?= htmlspecialcharsFix($customer['cs_linkedin']) ?>" class="txt1 linkedin" placeholder="领英链接"/></td>
-                </tr>
-                <tr>
-                    <td><input type="text" id="cs_facebook" name="cs_facebook" value="<?= htmlspecialcharsFix($customer['cs_facebook']) ?>" class="txt1 facebook" placeholder="Facebook" /></td>
-                </tr>
-                <tr>
-                    <td><input type="text" id="cs_alibaba" name="cs_alibaba" value="<?= htmlspecialcharsFix($customer['cs_alibaba']) ?>" class="txt1 alibaba" placeholder="alibaba" /></td>
+                    <th width="8%" valign="top">联系人信息</th>
+                    <td>
+                        <button type="button" class="add-contact-btn">添加联系人</button>
+                        
+                        <div id="contacts-container">
+                            <?php if (!empty($contacts)): ?>
+                                <?php foreach ($contacts as $index => $contact): ?>
+                                <div class="contact-form" id="contact-form-<?= $index ?>">
+                                    <div class="contact-header">
+                                        <button type="button" class="remove-contact-btn" data-index="<?= $index ?>">删除</button>
+                                        <h3>联系人 #<?= $index + 1 ?></h3>
+                                    </div>
+                                    <input type="hidden" name="contact[<?= $index ?>][id]" value="<?= $contact['id'] ?>">
+                                    <table width="100%" border="0" cellpadding="3" cellspacing="1" class="contact-table">
+                                        <tr>
+                                            <th width="8%">联系人</th>
+                                            <td><input type="text" name="contact[<?= $index ?>][contact_name]" value="<?= htmlspecialcharsFix($contact['contact_name']) ?>" class="txt1" placeholder="联系人姓名"/></td>
+                                        </tr>
+                                    </table>
+                                    <div class="contact-methods-container" id="contact-methods-<?= $index ?>">
+                                        <?php
+                                        $methodTypes = [
+                                            'tel' => '电话',
+                                            'wechat' => '微信',
+                                            'whatsapp' => 'WhatsApp',
+                                            'email' => '邮箱',
+                                            'linkedin' => '领英',
+                                            'facebook' => 'Facebook',
+                                            'alibaba' => '阿里巴巴'
+                                        ];
+                                        
+                                        foreach ($methodTypes as $type => $label) {
+                                            for ($i = 1; $i <= 3; $i++) {
+                                                $fieldName = $type . '_' . $i;
+                                                if (!empty($contact[$fieldName])) {
+                                                    echo '<div class="contact-method-row">';
+                                                    echo '<select class="method-select" name="contact[' . $index . '][' . $fieldName . ']" onchange="updateMethodSelectAndPlaceholder(this)">';
+                                                    echo '<option value="">请选择联系方式</option>';
+                                                    
+                                                    foreach ($methodTypes as $optionType => $optionLabel) {
+                                                        $selected = ($optionType === $type) ? 'selected' : '';
+                                                        echo '<option value="' . $optionType . '" ' . $selected . '>' . $optionLabel . '</option>';
+                                                    }
+                                                    
+                                                    echo '</select>';
+                                                    echo '<input type="text" class="txt1 method-input" style="width:60%;" name="contact[' . $index . '][' . $fieldName . ']" value="' . htmlspecialcharsFix($contact[$fieldName]) . '">';
+                                                    
+                                                    if ($type === 'tel' || $type === 'whatsapp') {
+                                                        echo '<input type="hidden" class="format-input" name="contact[' . $index . '][' . $fieldName . '_format]" value="' . htmlspecialcharsFix($contact[$fieldName . '_format']) . '">';
+                                                    }
+                                                    
+                                                    echo '<input type="hidden" class="backup-input" name="contact[' . $index . '][' . $fieldName . '_bu]" value="' . htmlspecialcharsFix($contact[$fieldName . '_bu']) . '">';
+                                                    echo '<button type="button" class="remove-method-btn">删除</button>';
+                                                    echo '</div>';
+                                                }
+                                            }
+                                        }
+                                        ?>
+                                    </div>
+                                    <button type="button" class="add-method-btn" data-contact-index="<?= $index ?>">添加联系方式</button>
+                                </div>
+                                <?php endforeach; ?>
+                            <?php else: ?>
+                                <div class="contact-form" id="contact-form-0">
+                                    <div class="contact-header">
+                                        <button type="button" class="remove-contact-btn" data-index="0">删除</button>
+                                        <h3>联系人 #1</h3>
+                                    </div>
+                                    <input type="hidden" name="contact[0][id]" value="">
+                                    <table width="100%" border="0" cellpadding="3" cellspacing="1" class="contact-table">
+                                        <tr>
+                                            <th width="8%">联系人</th>
+                                            <td><input type="text" name="contact[0][contact_name]" class="txt1" placeholder="联系人姓名"/></td>
+                                        </tr>
+                                    </table>
+                                    <div class="contact-methods-container" id="contact-methods-0">
+                                        <!-- Contact methods will be added here -->
+                                    </div>
+                                    <button type="button" class="add-method-btn" data-contact-index="0">添加联系方式</button>
+                                </div>
+                            <?php endif; ?>
+                        </div>
+                    </td>
                 </tr>
                 <tr>
                     <th width="8%">地址</th>
@@ -278,15 +715,13 @@ if (!empty($id) && is_numeric($id)) {
                     <th width="8%">备注</th>
                     <td><textarea name="cs_note" class="txt2"><?= htmlspecialcharsFix($customer['cs_note']) ?></textarea></td>
                 </tr>
-                <tr>
-                    <th></th>
-                    <td>
-                        <input type="button" name="save" id="save" value="确定" class="btn1" onclick="subform();">
-                        <input type="button" value="返回" class="btn1" onClick="location.href='customers.php<?= $hrefstr ?>'" />
-                    </td>
-                </tr>
             </tbody>
         </table>
+        
+        <div class="form-actions">
+            <input type="button" name="save" id="save" value="确定" class="btn1" onclick="submitCustomerForm();">
+            <input type="button" value="返回" class="btn1" onClick="location.href='customers.php<?= $hrefstr ?>'" />
+        </div>
     </form>
 </div>
 </body>

+ 228 - 131
customerSave.php

@@ -23,18 +23,8 @@ $fliterDeal = $_GET['Deal'] ?? '';
 $id = $_POST['id'] ?? '';
 $cs_code = textEncode($_POST['cs_code'] ?? '');
 $cs_company = textEncode($_POST['cs_company'] ?? '');
-$cs_name = textEncode($_POST['cs_name'] ?? '');
 $cs_country = $_POST['cs_country'] ?? '';
 $cs_from = $_POST['cs_from'] ?? '';
-$cs_tel = textEncode($_POST['cs_tel'] ?? '');
-$cs_wechat = textEncode($_POST['cs_wechat'] ?? '');
-$cs_whatsapp = textEncode($_POST['cs_whatsapp'] ?? '');
-$cs_email = textEncode($_POST['cs_email'] ?? '');
-$cs_linkedin = textEncode($_POST['cs_linkedin'] ?? '');
-$cs_facebook = textEncode($_POST['cs_facebook'] ?? '');
-$cs_alibaba = textEncode($_POST['cs_alibaba'] ?? '');
-$cs_alibaba = str_replace(':', ':', $cs_alibaba);
-$cs_alibaba = str_replace('ID:', '', $cs_alibaba);
 $cs_address = textEncode($_POST['cs_address'] ?? '');
 $cs_type = textEncode($_POST['cs_type'] ?? '');
 $cs_belongclient = $_POST['cs_belongclient'] ?? '';
@@ -43,10 +33,11 @@ $cs_updatetime = date('Y-m-d H:i:s');
 $cs_belong = $_SESSION['employee_id'];
 $cs_state = 1;
 $cs_deal = $_POST['cs_deal'] ?? '';
-$cs_telformat = numFormat($_POST['cs_tel'] ?? '');
-$cs_whatsappformat = numFormat($_POST['cs_whatsapp'] ?? '');
 $allowedit = $_POST['Permissions'] ?? '0';
 
+// Get contact information from the form
+$contacts = $_POST['contact'] ?? [];
+
 // Validate numeric values
 $allowedit = is_numeric($allowedit) ? $allowedit : 0;
 $cs_country = (is_numeric($cs_country) && $cs_country !== '') ? $cs_country : 0;
@@ -68,6 +59,19 @@ if (strpos($cs_code, ';阿里') !== false) {
     $cs_from = 2; // International station
 }
 
+// For validation, we'll check the first contact (primary contact)
+$primary_contact = !empty($contacts) ? current($contacts) : [];
+$cs_name = textEncode($primary_contact['contact_name'] ?? '');
+$cs_tel = textEncode($primary_contact['tel'] ?? '');
+$cs_wechat = textEncode($primary_contact['wechat'] ?? '');
+$cs_whatsapp = textEncode($primary_contact['whatsapp'] ?? '');
+$cs_email = textEncode($primary_contact['email'] ?? '');
+$cs_linkedin = textEncode($primary_contact['linkedin'] ?? '');
+$cs_facebook = textEncode($primary_contact['facebook'] ?? '');
+$cs_alibaba = textEncode($primary_contact['alibaba'] ?? '');
+$cs_telformat = numFormat($cs_tel);
+$cs_whatsappformat = numFormat($cs_whatsapp);
+
 // Validation checks
 if ($allowedit != 1) {
     // Alibaba validation
@@ -127,59 +131,119 @@ if (empty($cs_code)) {
 }
 
 // Check for duplicate customer information
-$checkStr = "SELECT c.*, cc.tel, cc.tel_format, cc.email, cc.whatsapp, cc.whatsapp_format, cc.wechat, cc.linkedin, cc.facebook, cc.alibaba 
+$checkStr = "SELECT c.*, cc.* 
              FROM customer c 
              LEFT JOIN customer_contact cc ON c.id = cc.customer_id
              WHERE c.cs_belong != " . $_SESSION['employee_id'] . " AND (c.id = 0 ";
 
 $Dupli = "";
 
-if (!empty($cs_tel)) {
-    $checkStr .= " OR cc.tel_format LIKE '%" . substr($cs_telformat, 3, 9) . "%'" . 
-                 " OR cc.wechat LIKE '%" . substr($cs_telformat, 3, 9) . "%'" . 
-                 " OR cc.whatsapp_format LIKE '%" . $cs_telformat . "%'";
-    $Dupli .= "电话:" . $cs_tel;
+// Get the first contact's information for validation
+$primary_contact = !empty($contacts) ? current($contacts) : [];
+
+// Check all phone numbers
+for ($i = 1; $i <= 3; $i++) {
+    $tel_field = 'tel_' . $i;
+    $tel_format_field = 'tel_' . $i . '_format';
+    if (!empty($primary_contact[$tel_field])) {
+        $tel_format = numFormat($primary_contact[$tel_field]);
+        $checkStr .= " OR cc.tel_1_format LIKE '%" . substr($tel_format, 3, 9) . "%'" .
+                    " OR cc.tel_2_format LIKE '%" . substr($tel_format, 3, 9) . "%'" .
+                    " OR cc.tel_3_format LIKE '%" . substr($tel_format, 3, 9) . "%'" .
+                    " OR cc.wechat_1 LIKE '%" . substr($tel_format, 3, 9) . "%'" .
+                    " OR cc.wechat_2 LIKE '%" . substr($tel_format, 3, 9) . "%'" .
+                    " OR cc.wechat_3 LIKE '%" . substr($tel_format, 3, 9) . "%'";
+        $Dupli .= "电话" . $i . ":" . $primary_contact[$tel_field] . " ";
+    }
 }
 
-if (!empty($cs_email)) {
-    $checkStr .= " OR cc.email = '" . $conn->real_escape_string($cs_email) . "'";
-    $Dupli .= "邮箱:" . $cs_email;
+// Check all email addresses
+for ($i = 1; $i <= 3; $i++) {
+    $email_field = 'email_' . $i;
+    if (!empty($primary_contact[$email_field])) {
+        $checkStr .= " OR cc.email_1 = '" . $conn->real_escape_string($primary_contact[$email_field]) . "'" .
+                    " OR cc.email_2 = '" . $conn->real_escape_string($primary_contact[$email_field]) . "'" .
+                    " OR cc.email_3 = '" . $conn->real_escape_string($primary_contact[$email_field]) . "'";
+        $Dupli .= "邮箱" . $i . ":" . $primary_contact[$email_field] . " ";
+    }
 }
 
-if (!empty($cs_whatsapp)) {
-    $checkStr .= " OR cc.whatsapp_format LIKE '%" . substr($cs_whatsappformat, 3, 9) . "%'" . 
-                 " OR cc.tel_format LIKE '%" . substr($cs_whatsappformat, 3, 9) . "%'";
-    $Dupli .= "WhatsApp:" . $cs_whatsapp;
+// Check all WhatsApp numbers
+for ($i = 1; $i <= 3; $i++) {
+    $whatsapp_field = 'whatsapp_' . $i;
+    $whatsapp_format_field = 'whatsapp_' . $i . '_format';
+    if (!empty($primary_contact[$whatsapp_field])) {
+        $whatsapp_format = numFormat($primary_contact[$whatsapp_field]);
+        $checkStr .= " OR cc.whatsapp_1_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
+                    " OR cc.whatsapp_2_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
+                    " OR cc.whatsapp_3_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
+                    " OR cc.tel_1_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
+                    " OR cc.tel_2_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'" .
+                    " OR cc.tel_3_format LIKE '%" . substr($whatsapp_format, 3, 9) . "%'";
+        $Dupli .= "WhatsApp" . $i . ":" . $primary_contact[$whatsapp_field] . " ";
+    }
 }
 
-if (!empty($cs_wechat)) {
-    if (strlen($cs_wechat) < 10) {
-        $checkStr .= " OR cc.wechat LIKE '%" . $conn->real_escape_string($cs_wechat) . "%'" . 
-                    " OR cc.tel_format LIKE '%" . $conn->real_escape_string($cs_wechat) . "%'";
-    } else {
-        $checkStr .= " OR cc.wechat LIKE '%" . substr($cs_wechat, 2, 12) . "%'" . 
-                    " OR cc.tel_format LIKE '%" . substr($cs_wechat, 2, 12) . "%'";
+// Check all WeChat accounts
+for ($i = 1; $i <= 3; $i++) {
+    $wechat_field = 'wechat_' . $i;
+    if (!empty($primary_contact[$wechat_field])) {
+        if (strlen($primary_contact[$wechat_field]) < 10) {
+            $checkStr .= " OR cc.wechat_1 LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
+                        " OR cc.wechat_2 LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
+                        " OR cc.wechat_3 LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
+                        " OR cc.tel_1_format LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
+                        " OR cc.tel_2_format LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'" .
+                        " OR cc.tel_3_format LIKE '%" . $conn->real_escape_string($primary_contact[$wechat_field]) . "%'";
+        } else {
+            $checkStr .= " OR cc.wechat_1 LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
+                        " OR cc.wechat_2 LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
+                        " OR cc.wechat_3 LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
+                        " OR cc.tel_1_format LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
+                        " OR cc.tel_2_format LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'" .
+                        " OR cc.tel_3_format LIKE '%" . substr($primary_contact[$wechat_field], 2, 12) . "%'";
+        }
+        $Dupli .= "微信" . $i . ":" . $primary_contact[$wechat_field] . " ";
     }
-    $Dupli .= "微信:" . $cs_wechat;
 }
 
-if (!empty($cs_linkedin)) {
-    $checkStr .= " OR cc.linkedin LIKE '%" . $conn->real_escape_string($cs_linkedin) . "%'";
-    $Dupli .= "Linked:" . $cs_linkedin;
+// Check all LinkedIn accounts
+for ($i = 1; $i <= 3; $i++) {
+    $linkedin_field = 'linkedin_' . $i;
+    if (!empty($primary_contact[$linkedin_field])) {
+        $checkStr .= " OR cc.linkedin_1 LIKE '%" . $conn->real_escape_string($primary_contact[$linkedin_field]) . "%'" .
+                    " OR cc.linkedin_2 LIKE '%" . $conn->real_escape_string($primary_contact[$linkedin_field]) . "%'" .
+                    " OR cc.linkedin_3 LIKE '%" . $conn->real_escape_string($primary_contact[$linkedin_field]) . "%'";
+        $Dupli .= "LinkedIn" . $i . ":" . $primary_contact[$linkedin_field] . " ";
+    }
 }
 
-if (!empty($cs_facebook)) {
-    $checkStr .= " OR cc.facebook LIKE '%" . $conn->real_escape_string($cs_facebook) . "%'";
-    $Dupli .= "Facebook:" . $cs_facebook;
+// Check all Facebook accounts
+for ($i = 1; $i <= 3; $i++) {
+    $facebook_field = 'facebook_' . $i;
+    if (!empty($primary_contact[$facebook_field])) {
+        $checkStr .= " OR cc.facebook_1 LIKE '%" . $conn->real_escape_string($primary_contact[$facebook_field]) . "%'" .
+                    " OR cc.facebook_2 LIKE '%" . $conn->real_escape_string($primary_contact[$facebook_field]) . "%'" .
+                    " OR cc.facebook_3 LIKE '%" . $conn->real_escape_string($primary_contact[$facebook_field]) . "%'";
+        $Dupli .= "Facebook" . $i . ":" . $primary_contact[$facebook_field] . " ";
+    }
 }
 
-if (!empty($cs_alibaba)) {
-    if (strlen($cs_alibaba) < 10) {
-        $checkStr .= " OR cc.alibaba LIKE '" . $conn->real_escape_string($cs_alibaba) . "'";
-    } else {
-        $checkStr .= " OR cc.alibaba LIKE '%" . substr($cs_alibaba, 3, 12) . "%'";
+// Check all Alibaba accounts
+for ($i = 1; $i <= 3; $i++) {
+    $alibaba_field = 'alibaba_' . $i;
+    if (!empty($primary_contact[$alibaba_field])) {
+        if (strlen($primary_contact[$alibaba_field]) < 10) {
+            $checkStr .= " OR cc.alibaba_1 LIKE '" . $conn->real_escape_string($primary_contact[$alibaba_field]) . "'" .
+                        " OR cc.alibaba_2 LIKE '" . $conn->real_escape_string($primary_contact[$alibaba_field]) . "'" .
+                        " OR cc.alibaba_3 LIKE '" . $conn->real_escape_string($primary_contact[$alibaba_field]) . "'";
+        } else {
+            $checkStr .= " OR cc.alibaba_1 LIKE '%" . substr($primary_contact[$alibaba_field], 3, 12) . "%'" .
+                        " OR cc.alibaba_2 LIKE '%" . substr($primary_contact[$alibaba_field], 3, 12) . "%'" .
+                        " OR cc.alibaba_3 LIKE '%" . substr($primary_contact[$alibaba_field], 3, 12) . "%'";
+        }
+        $Dupli .= "阿里旺旺" . $i . ":" . $primary_contact[$alibaba_field] . " ";
     }
-    $Dupli .= "阿里旺旺:" . $cs_alibaba;
 }
 
 $checkStr .= " ) ORDER BY c.id ASC";
@@ -244,64 +308,84 @@ if ($act == "editSave" || $allowedit == 1) {
 
     $conn->query($updateSql);
     
-    // 检查是否已有联系人记录
-    $contact_sql = "SELECT id FROM customer_contact WHERE customer_id = " . intval($id);
-    $contact_result = mysqli_query($conn, $contact_sql);
+    // 处理联系人信息 - 首先删除已有的不在提交列表中的联系人
+    $existingContactIds = [];
+    foreach ($contacts as $contact) {
+        if (!empty($contact['id'])) {
+            $existingContactIds[] = (int)$contact['id'];
+        }
+    }
     
-    if ($contact_row = mysqli_fetch_assoc($contact_result)) {
-        // 更新联系人信息
-        $contact_id = $contact_row['id'];
-        $contact_sql = "UPDATE customer_contact SET 
-            contact_name='" . $conn->real_escape_string($cs_name) . "',
-            tel='" . $conn->real_escape_string($cs_tel) . "',
-            tel_format='" . $conn->real_escape_string($cs_telformat) . "',
-            tel_bu='" . $conn->real_escape_string($cs_tel) . "',
-            email='" . $conn->real_escape_string($cs_email) . "',
-            email_bu='" . $conn->real_escape_string($cs_email) . "',
-            whatsapp='" . $conn->real_escape_string($cs_whatsapp) . "',
-            whatsapp_format='" . $conn->real_escape_string($cs_whatsappformat) . "',
-            whatsapp_bu='" . $conn->real_escape_string($cs_whatsapp) . "',
-            wechat='" . $conn->real_escape_string($cs_wechat) . "',
-            wechat_bu='" . $conn->real_escape_string($cs_wechat) . "',
-            linkedin='" . $conn->real_escape_string($cs_linkedin) . "',
-            linkedin_bu='" . $conn->real_escape_string($cs_linkedin) . "',
-            facebook='" . $conn->real_escape_string($cs_facebook) . "',
-            facebook_bu='" . $conn->real_escape_string($cs_facebook) . "',
-            alibaba='" . $conn->real_escape_string($cs_alibaba) . "',
-            alibaba_bu='" . $conn->real_escape_string($cs_alibaba) . "',
-            updated_at='" . $cs_updatetime . "'
-            WHERE id=" . $contact_id;
-        $conn->query($contact_sql);
+    if (!empty($existingContactIds)) {
+        $idsToKeep = implode(',', $existingContactIds);
+        $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id) . 
+                             " AND id NOT IN (" . $idsToKeep . ")";
     } else {
-        // 插入新的联系人记录
-        $contact_sql = "INSERT INTO customer_contact (
-            customer_id, contact_name, tel, tel_format, tel_bu, 
-            email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
-            wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
-            facebook_bu, alibaba, alibaba_bu, created_at, updated_at
-        ) VALUES (
-            " . intval($id) . ",
-            '" . $conn->real_escape_string($cs_name) . "',
-            '" . $conn->real_escape_string($cs_tel) . "',
-            '" . $conn->real_escape_string($cs_telformat) . "',
-            '" . $conn->real_escape_string($cs_tel) . "',
-            '" . $conn->real_escape_string($cs_email) . "',
-            '" . $conn->real_escape_string($cs_email) . "',
-            '" . $conn->real_escape_string($cs_whatsapp) . "',
-            '" . $conn->real_escape_string($cs_whatsappformat) . "',
-            '" . $conn->real_escape_string($cs_whatsapp) . "',
-            '" . $conn->real_escape_string($cs_wechat) . "',
-            '" . $conn->real_escape_string($cs_wechat) . "',
-            '" . $conn->real_escape_string($cs_linkedin) . "',
-            '" . $conn->real_escape_string($cs_linkedin) . "',
-            '" . $conn->real_escape_string($cs_facebook) . "',
-            '" . $conn->real_escape_string($cs_facebook) . "',
-            '" . $conn->real_escape_string($cs_alibaba) . "',
-            '" . $conn->real_escape_string($cs_alibaba) . "',
-            NOW(), 
-            NOW()
-        )";
-        $conn->query($contact_sql);
+        $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = " . intval($id);
+    }
+    
+    $conn->query($deleteContactsSql);
+    
+    // 处理联系人信息 - 更新或添加联系人
+    foreach ($contacts as $contact) {
+        $contact_id = !empty($contact['id']) ? (int)$contact['id'] : 0;
+        $contact_name = textEncode($contact['contact_name'] ?? '');
+        
+        // 准备SQL字段和值
+        $fields = ['contact_name'];
+        $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
+        $updates = ["contact_name = '" . $conn->real_escape_string($contact_name) . "'"];
+        
+        // 处理所有联系方式类型
+        $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
+        foreach ($methodTypes as $type) {
+            for ($i = 1; $i <= 3; $i++) {
+                $field = $type . '_' . $i;
+                $format_field = $field . '_format';
+                $bu_field = $field . '_bu';
+                
+                $value = textEncode($contact[$field] ?? '');
+                $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
+                $bu_value = textEncode($contact[$bu_field] ?? $value);
+                
+                // 添加字段名
+                $fields[] = $field;
+                $fields[] = $bu_field;
+                if ($type == 'tel' || $type == 'whatsapp') {
+                    $fields[] = $format_field;
+                }
+                
+                // 添加值
+                $values[] = "'" . $conn->real_escape_string($value) . "'";
+                $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
+                if ($type == 'tel' || $type == 'whatsapp') {
+                    $values[] = "'" . $conn->real_escape_string($format_value) . "'";
+                }
+                
+                // 添加更新语句
+                $updates[] = $field . " = '" . $conn->real_escape_string($value) . "'";
+                $updates[] = $bu_field . " = '" . $conn->real_escape_string($bu_value) . "'";
+                if ($type == 'tel' || $type == 'whatsapp') {
+                    $updates[] = $format_field . " = '" . $conn->real_escape_string($format_value) . "'";
+                }
+            }
+        }
+        
+        if ($contact_id > 0) {
+            // 更新已有联系人
+            $updateContactSql = "UPDATE customer_contact SET " .
+                implode(", ", $updates) . ", updated_at = NOW() " .
+                "WHERE id = " . $contact_id . " AND customer_id = " . intval($id);
+            
+            $conn->query($updateContactSql);
+        } else {
+            // 添加新联系人
+            $insertContactSql = "INSERT INTO customer_contact (" .
+                implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
+                implode(", ", $values) . ", " . intval($id) . ", NOW(), NOW())";
+            
+            $conn->query($insertContactSql);
+        }
     }
 
     // Update tags
@@ -345,37 +429,50 @@ if ($act == "editSave" || $allowedit == 1) {
     $conn->query($insertSql);
     $new_customer_id = $conn->insert_id;
     
-    // Insert contact information
+    // Insert contact information for all contacts
     if ($new_customer_id > 0) {
-        $contactSql = "INSERT INTO customer_contact (
-            customer_id, contact_name, tel, tel_format, tel_bu, 
-            email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
-            wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
-            facebook_bu, alibaba, alibaba_bu, created_at, updated_at
-        ) VALUES (
-            " . $new_customer_id . ",
-            '" . $conn->real_escape_string($cs_name) . "',
-            '" . $conn->real_escape_string($cs_tel) . "',
-            '" . $conn->real_escape_string($cs_telformat) . "',
-            '" . $conn->real_escape_string($cs_tel) . "',
-            '" . $conn->real_escape_string($cs_email) . "',
-            '" . $conn->real_escape_string($cs_email) . "',
-            '" . $conn->real_escape_string($cs_whatsapp) . "',
-            '" . $conn->real_escape_string($cs_whatsappformat) . "',
-            '" . $conn->real_escape_string($cs_whatsapp) . "',
-            '" . $conn->real_escape_string($cs_wechat) . "',
-            '" . $conn->real_escape_string($cs_wechat) . "',
-            '" . $conn->real_escape_string($cs_linkedin) . "',
-            '" . $conn->real_escape_string($cs_linkedin) . "',
-            '" . $conn->real_escape_string($cs_facebook) . "',
-            '" . $conn->real_escape_string($cs_facebook) . "',
-            '" . $conn->real_escape_string($cs_alibaba) . "',
-            '" . $conn->real_escape_string($cs_alibaba) . "',
-            NOW(), 
-            NOW()
-        )";
-        
-        $conn->query($contactSql);
+        foreach ($contacts as $contact) {
+            $contact_name = textEncode($contact['contact_name'] ?? '');
+            
+            // 准备SQL字段和值
+            $fields = ['contact_name'];
+            $values = ["'" . $conn->real_escape_string($contact_name) . "'"];
+            
+            // 处理所有联系方式类型
+            $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
+            foreach ($methodTypes as $type) {
+                for ($i = 1; $i <= 3; $i++) {
+                    $field = $type . '_' . $i;
+                    $format_field = $field . '_format';
+                    $bu_field = $field . '_bu';
+                    
+                    $value = textEncode($contact[$field] ?? '');
+                    $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
+                    $bu_value = textEncode($contact[$bu_field] ?? $value);
+                    
+                    // 添加字段名
+                    $fields[] = $field;
+                    $fields[] = $bu_field;
+                    if ($type == 'tel' || $type == 'whatsapp') {
+                        $fields[] = $format_field;
+                    }
+                    
+                    // 添加值
+                    $values[] = "'" . $conn->real_escape_string($value) . "'";
+                    $values[] = "'" . $conn->real_escape_string($bu_value) . "'";
+                    if ($type == 'tel' || $type == 'whatsapp') {
+                        $values[] = "'" . $conn->real_escape_string($format_value) . "'";
+                    }
+                }
+            }
+            
+            // 添加新联系人
+            $insertContactSql = "INSERT INTO customer_contact (" .
+                implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
+                implode(", ", $values) . ", " . $new_customer_id . ", NOW(), NOW())";
+            
+            $conn->query($insertContactSql);
+        }
         
         // Save tags for new customer
         foreach ($mytag as $tag) {

+ 473 - 142
customers.php

@@ -34,31 +34,105 @@ if ($act == "save") {
     $cs_from = $_POST['cs_from'];
     $cs_state = $_POST['cs_state'];
     $cs_deal = $_POST['cs_deal'];
+    $cs_type = $_POST['cs_type'] ?? 5;
+    $cs_belongclient = $_POST['cs_belongclient'] ?? 0;
     $no_content = htmlEncode($_POST['no_content']);
     $allowedit = isset($_POST['allowedit']) ? 1 : 0;
     $cs_address = textEncode($_POST['cs_address'] ?? '');
+    $mytag = textEncode($_POST['mytag'] ?? '');
+    $mytag = str_replace(['&#60;&#47;span&#62;&#60;span&#62;', '&#60;&#47;span&#62;', '&#60;span&#62;'], [',', '', ''], $mytag);
+    $mytag = explode(',', $mytag);
+
+    // 获取联系人信息
+    $contacts = $_POST['contact'] ?? [];
+
+    // 验证必填字段
+    if (empty($cs_code)) {
+        echo "<script>alert('客户编码不能为空');history.back();</script>";
+        exit;
+    }
+
+    if ($cs_country == 0) {
+        echo "<script>alert('这是哪个国家的客户?');history.back();</script>";
+        exit;
+    }
+
+    if ($cs_from == "0") {
+        echo "<script>alert('请填写客户来源!');history.back();</script>";
+        exit;
+    }
+
+    // 自动检测来源
+    if (strpos($cs_code, ';1688') !== false) {
+        $cs_from = 1; // 1688
+    }
+    if (strpos($cs_code, ';阿里') !== false) {
+        $cs_from = 2; // International station
+    }
+    if (strpos($cs_code, '官网') !== false) {
+        $cs_from = 3; // Website
+    }
+
+    // 验证联系方式
+    $primary_contact = !empty($contacts) ? current($contacts) : [];
     
-    // 获取表单数据 - 联系人信息
-    $contact_name = textEncode($_POST['cs_name']);
-    $tel = textEncode($_POST['cs_tel']);
-    $tel_format = textEncode($_POST['cs_telformat'] ?? '');
-    $tel_bu = textEncode($_POST['cs_telBu'] ?? '');
-    $email = textEncode($_POST['cs_email']);
-    $email_bu = textEncode($_POST['cs_emailBu'] ?? '');
-    $whatsapp = textEncode($_POST['cs_whatsapp']);
-    $whatsapp_format = textEncode($_POST['cs_whatsappformat'] ?? '');
-    $whatsapp_bu = textEncode($_POST['cs_whatsappBu'] ?? '');
-    $wechat = textEncode($_POST['cs_wechat']);
-    $wechat_bu = textEncode($_POST['cs_wechatBu'] ?? '');
-    $linkedin = textEncode($_POST['cs_linkedin']);
-    $linkedin_bu = textEncode($_POST['cs_linkedinBu'] ?? '');
-    $facebook = textEncode($_POST['cs_facebook']);
-    $facebook_bu = textEncode($_POST['cs_facebookBu'] ?? '');
-    $alibaba = textEncode($_POST['cs_alibaba']);
-    $alibaba_bu = textEncode($_POST['cs_alibabaBu'] ?? '');
+    if ($allowedit != 1) {
+        // 阿里巴巴验证
+        if (($cs_from == 1 || $cs_from == 2) && empty($primary_contact['alibaba_1'])) {
+            echo "<script>alert('阿里旺旺为必填项');history.back();</script>";
+            exit;
+        }
+
+        // 官网来源验证
+        if ($cs_from == 3) {
+            $has_required = false;
+            for ($i = 1; $i <= 3; $i++) {
+                if (!empty($primary_contact['tel_' . $i]) || 
+                    !empty($primary_contact['whatsapp_' . $i]) || 
+                    !empty($primary_contact['wechat_' . $i])) {
+                    $has_required = true;
+                    break;
+                }
+            }
+            if (!$has_required) {
+                echo "<script>alert('电话和WhatsApp为必填项');history.back();</script>";
+                exit;
+            }
+        }
+
+        // 市场客户验证
+        if ($cs_from == 8) {
+            $has_wechat = false;
+            for ($i = 1; $i <= 3; $i++) {
+                if (!empty($primary_contact['wechat_' . $i])) {
+                    $has_wechat = true;
+                    break;
+                }
+            }
+            if (!$has_wechat) {
+                echo "<script>alert('微信为必填项');history.back();</script>";
+                exit;
+            }
+        }
+
+        // Facebook验证
+        if ($cs_from == 12) {
+            $has_facebook = false;
+            for ($i = 1; $i <= 3; $i++) {
+                if (!empty($primary_contact['facebook_' . $i])) {
+                    $has_facebook = true;
+                    break;
+                }
+            }
+            if (!$has_facebook) {
+                echo "<script>alert('Facebook为必填项');history.back();</script>";
+                exit;
+            }
+        }
+    }
 
     if ($isedit) {
-        // 更新现有记录
+        // 验证客户所有权
         $sql = "SELECT cs_chain FROM customer WHERE id = $id";
         $result = mysqli_query($conn, $sql);
         if ($row = mysqli_fetch_assoc($result)) {
@@ -80,56 +154,107 @@ if ($act == "save") {
                     cs_from = '$cs_from',
                     cs_state = '$cs_state',
                     cs_deal = '$cs_deal',
+                    cs_type = '$cs_type',
+                    cs_belongclient = '$cs_belongclient',
                     cs_note = '$no_content',
                     allowedit = $allowedit,
                     cs_chain = '$cs_chain',
-                    cs_updatetime = NOW()
-                    WHERE id = $id";
-                    
+                    cs_updatetime = NOW()";
+
+            // 处理cs_dealdate
+            if ($cs_deal == 3) {
+                $sql .= ", cs_dealdate = CASE WHEN cs_dealdate IS NULL THEN NOW() ELSE cs_dealdate END";
+            }
+            
+            $sql .= " WHERE id = $id";
             mysqli_query($conn, $sql);
             
-            // 检查是否已有联系人记录
-            $contact_sql = "SELECT id FROM customer_contact WHERE customer_id = $id";
-            $contact_result = mysqli_query($conn, $contact_sql);
+            // 处理联系人信息
+            $existingContactIds = [];
+            foreach ($contacts as $contact) {
+                if (!empty($contact['id'])) {
+                    $existingContactIds[] = (int)$contact['id'];
+                }
+            }
             
-            if ($contact_row = mysqli_fetch_assoc($contact_result)) {
-                // 更新联系人信息
-                $contact_id = $contact_row['id'];
-                $sql = "UPDATE customer_contact SET 
-                        contact_name = '$contact_name',
-                        tel = '$tel',
-                        tel_format = '$tel_format',
-                        tel_bu = '$tel_bu',
-                        email = '$email',
-                        email_bu = '$email_bu',
-                        whatsapp = '$whatsapp',
-                        whatsapp_format = '$whatsapp_format',
-                        whatsapp_bu = '$whatsapp_bu',
-                        wechat = '$wechat',
-                        wechat_bu = '$wechat_bu',
-                        linkedin = '$linkedin',
-                        linkedin_bu = '$linkedin_bu',
-                        facebook = '$facebook',
-                        facebook_bu = '$facebook_bu',
-                        alibaba = '$alibaba',
-                        alibaba_bu = '$alibaba_bu',
-                        updated_at = NOW()
-                        WHERE id = $contact_id";
-                mysqli_query($conn, $sql);
+            // 删除不再使用的联系人记录
+            if (!empty($existingContactIds)) {
+                $idsToKeep = implode(',', $existingContactIds);
+                $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = $id AND id NOT IN ($idsToKeep)";
             } else {
-                // 插入新的联系人记录
-                $sql = "INSERT INTO customer_contact (
-                        customer_id, contact_name, tel, tel_format, tel_bu, 
-                        email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
-                        wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
-                        facebook_bu, alibaba, alibaba_bu, created_at, updated_at
-                    ) VALUES (
-                        $id, '$contact_name', '$tel', '$tel_format', '$tel_bu',
-                        '$email', '$email_bu', '$whatsapp', '$whatsapp_format', '$whatsapp_bu',
-                        '$wechat', '$wechat_bu', '$linkedin', '$linkedin_bu', '$facebook',
-                        '$facebook_bu', '$alibaba', '$alibaba_bu', NOW(), NOW()
-                    )";
-                mysqli_query($conn, $sql);
+                $deleteContactsSql = "DELETE FROM customer_contact WHERE customer_id = $id";
+            }
+            mysqli_query($conn, $deleteContactsSql);
+            
+            // 更新或添加联系人信息
+            foreach ($contacts as $contact) {
+                $contact_id = !empty($contact['id']) ? (int)$contact['id'] : 0;
+                $contact_name = textEncode($contact['contact_name'] ?? '');
+                
+                // 准备SQL字段和值
+                $fields = ['contact_name'];
+                $values = ["'" . mysqli_real_escape_string($conn, $contact_name) . "'"];
+                $updates = ["contact_name = '" . mysqli_real_escape_string($conn, $contact_name) . "'"];
+                
+                // 处理所有联系方式类型
+                $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
+                foreach ($methodTypes as $type) {
+                    for ($i = 1; $i <= 3; $i++) {
+                        $field = $type . '_' . $i;
+                        $format_field = $field . '_format';
+                        $bu_field = $field . '_bu';
+                        
+                        $value = textEncode($contact[$field] ?? '');
+                        $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
+                        $bu_value = textEncode($contact[$bu_field] ?? $value);
+                        
+                        // 添加字段名
+                        $fields[] = $field;
+                        $fields[] = $bu_field;
+                        if ($type == 'tel' || $type == 'whatsapp') {
+                            $fields[] = $format_field;
+                        }
+                        
+                        // 添加值
+                        $values[] = "'" . mysqli_real_escape_string($conn, $value) . "'";
+                        $values[] = "'" . mysqli_real_escape_string($conn, $bu_value) . "'";
+                        if ($type == 'tel' || $type == 'whatsapp') {
+                            $values[] = "'" . mysqli_real_escape_string($conn, $format_value) . "'";
+                        }
+                        
+                        // 添加更新语句
+                        $updates[] = $field . " = '" . mysqli_real_escape_string($conn, $value) . "'";
+                        $updates[] = $bu_field . " = '" . mysqli_real_escape_string($conn, $bu_value) . "'";
+                        if ($type == 'tel' || $type == 'whatsapp') {
+                            $updates[] = $format_field . " = '" . mysqli_real_escape_string($conn, $format_value) . "'";
+                        }
+                    }
+                }
+                
+                if ($contact_id > 0) {
+                    // 更新已有联系人
+                    $updateContactSql = "UPDATE customer_contact SET " .
+                        implode(", ", $updates) . ", updated_at = NOW() " .
+                        "WHERE id = $contact_id AND customer_id = $id";
+                    mysqli_query($conn, $updateContactSql);
+                } else {
+                    // 添加新联系人
+                    $insertContactSql = "INSERT INTO customer_contact (" .
+                        implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
+                        implode(", ", $values) . ", $id, NOW(), NOW())";
+                    mysqli_query($conn, $insertContactSql);
+                }
+            }
+
+            // 更新标签
+            mysqli_query($conn, "DELETE FROM tagtable WHERE customerId = $id");
+            foreach ($mytag as $tag) {
+                if (!empty(trim($tag))) {
+                    $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" . 
+                             mysqli_real_escape_string($conn, $tag) . "', " . 
+                             $_SESSION['employee_id'] . ", $id)";
+                    mysqli_query($conn, $tagSql);
+                }
             }
             
             $page = $_GET['Page'] ?? '';
@@ -142,30 +267,71 @@ if ($act == "save") {
         // 插入客户基本信息
         $sql = "INSERT INTO customer (
                 cs_code, cs_company, cs_country, cs_address, cs_from, 
-                cs_belong, cs_state, cs_deal, cs_note, allowedit, 
-                cs_chain, cs_addtime, cs_updatetime
+                cs_belong, cs_state, cs_deal, cs_type, cs_belongclient,
+                cs_note, allowedit, cs_chain, cs_addtime, cs_updatetime,
+                is_silent, cs_dealdate
             ) VALUES (
                 '$cs_code', '$cs_company', '$cs_country', '$cs_address', '$cs_from',
-                '$cs_belong', '$cs_state', '$cs_deal', '$no_content', $allowedit,
-                '$cs_belong', NOW(), NOW()
+                '$cs_belong', '$cs_state', '$cs_deal', '$cs_type', '$cs_belongclient',
+                '$no_content', $allowedit, '$cs_belong', NOW(), NOW(),
+                0, " . ($cs_deal == 3 ? "NOW()" : "NULL") . "
             )";
         mysqli_query($conn, $sql);
         $new_customer_id = mysqli_insert_id($conn);
         
         // 插入联系人信息
         if ($new_customer_id > 0) {
-            $sql = "INSERT INTO customer_contact (
-                    customer_id, contact_name, tel, tel_format, tel_bu, 
-                    email, email_bu, whatsapp, whatsapp_format, whatsapp_bu, 
-                    wechat, wechat_bu, linkedin, linkedin_bu, facebook, 
-                    facebook_bu, alibaba, alibaba_bu, created_at, updated_at
-                ) VALUES (
-                    $new_customer_id, '$contact_name', '$tel', '$tel_format', '$tel_bu',
-                    '$email', '$email_bu', '$whatsapp', '$whatsapp_format', '$whatsapp_bu',
-                    '$wechat', '$wechat_bu', '$linkedin', '$linkedin_bu', '$facebook',
-                    '$facebook_bu', '$alibaba', '$alibaba_bu', NOW(), NOW()
-                )";
-            mysqli_query($conn, $sql);
+            foreach ($contacts as $contact) {
+                $contact_name = textEncode($contact['contact_name'] ?? '');
+                
+                // 准备SQL字段和值
+                $fields = ['contact_name'];
+                $values = ["'" . mysqli_real_escape_string($conn, $contact_name) . "'"];
+                
+                // 处理所有联系方式类型
+                $methodTypes = ['tel', 'email', 'whatsapp', 'wechat', 'linkedin', 'facebook', 'alibaba'];
+                foreach ($methodTypes as $type) {
+                    for ($i = 1; $i <= 3; $i++) {
+                        $field = $type . '_' . $i;
+                        $format_field = $field . '_format';
+                        $bu_field = $field . '_bu';
+                        
+                        $value = textEncode($contact[$field] ?? '');
+                        $format_value = ($type == 'tel' || $type == 'whatsapp') ? numFormat($value) : '';
+                        $bu_value = textEncode($contact[$bu_field] ?? $value);
+                        
+                        // 添加字段名
+                        $fields[] = $field;
+                        $fields[] = $bu_field;
+                        if ($type == 'tel' || $type == 'whatsapp') {
+                            $fields[] = $format_field;
+                        }
+                        
+                        // 添加值
+                        $values[] = "'" . mysqli_real_escape_string($conn, $value) . "'";
+                        $values[] = "'" . mysqli_real_escape_string($conn, $bu_value) . "'";
+                        if ($type == 'tel' || $type == 'whatsapp') {
+                            $values[] = "'" . mysqli_real_escape_string($conn, $format_value) . "'";
+                        }
+                    }
+                }
+                
+                // 添加新联系人
+                $insertContactSql = "INSERT INTO customer_contact (" .
+                    implode(", ", $fields) . ", customer_id, created_at, updated_at) VALUES (" .
+                    implode(", ", $values) . ", $new_customer_id, NOW(), NOW())";
+                mysqli_query($conn, $insertContactSql);
+            }
+
+            // 保存标签
+            foreach ($mytag as $tag) {
+                if (!empty(trim($tag))) {
+                    $tagSql = "INSERT INTO tagtable (tagName, employeeId, customerId) VALUES ('" . 
+                             mysqli_real_escape_string($conn, $tag) . "', " . 
+                             $_SESSION['employee_id'] . ", $new_customer_id)";
+                    mysqli_query($conn, $tagSql);
+                }
+            }
             
             $page = $_GET['Page'] ?? '';
             $keys = urlencode($_GET['Keys'] ?? '');
@@ -185,10 +351,29 @@ if ($act == "edit") {
     
     if ($isedit) {
         // 联合查询客户基本信息和联系人信息
-        $sql = "SELECT c.*, cc.contact_name, cc.tel, cc.tel_format, cc.tel_bu, 
-                cc.email, cc.email_bu, cc.whatsapp, cc.whatsapp_format, cc.whatsapp_bu, 
-                cc.wechat, cc.wechat_bu, cc.linkedin, cc.linkedin_bu, cc.facebook, 
-                cc.facebook_bu, cc.alibaba, cc.alibaba_bu, n.c_code 
+        $sql = "SELECT c.*, 
+                cc.id as contact_id, cc.contact_name,
+                cc.tel_1, cc.tel_1_format, cc.tel_1_bu,
+                cc.tel_2, cc.tel_2_format, cc.tel_2_bu,
+                cc.tel_3, cc.tel_3_format, cc.tel_3_bu,
+                cc.email_1, cc.email_1_bu,
+                cc.email_2, cc.email_2_bu,
+                cc.email_3, cc.email_3_bu,
+                cc.whatsapp_1, cc.whatsapp_1_format, cc.whatsapp_1_bu,
+                cc.whatsapp_2, cc.whatsapp_2_format, cc.whatsapp_2_bu,
+                cc.whatsapp_3, cc.whatsapp_3_format, cc.whatsapp_3_bu,
+                cc.wechat_1, cc.wechat_1_bu,
+                cc.wechat_2, cc.wechat_2_bu,
+                cc.wechat_3, cc.wechat_3_bu,
+                cc.linkedin_1, cc.linkedin_1_bu,
+                cc.linkedin_2, cc.linkedin_2_bu,
+                cc.linkedin_3, cc.linkedin_3_bu,
+                cc.facebook_1, cc.facebook_1_bu,
+                cc.facebook_2, cc.facebook_2_bu,
+                cc.facebook_3, cc.facebook_3_bu,
+                cc.alibaba_1, cc.alibaba_1_bu,
+                cc.alibaba_2, cc.alibaba_2_bu,
+                cc.alibaba_3, cc.alibaba_3_bu
                 FROM customer c 
                 LEFT JOIN customer_contact cc ON c.id = cc.customer_id
                 WHERE c.id = $id";
@@ -199,20 +384,20 @@ if ($act == "edit") {
             $cs_name = textDecode($row['contact_name']);
             $cs_country = $row['cs_country'];
             $cs_from = $row['cs_from'];
-            $cs_tel = textDecode($row['tel']);
-            $cs_telBu = textDecode($row['tel_bu']);
-            $cs_email = textDecode($row['email']);
-            $cs_emailBu = textDecode($row['email_bu']);
-            $cs_whatsapp = textDecode($row['whatsapp']);
-            $cs_whatsappBu = textDecode($row['whatsapp_bu']);
-            $cs_wechat = textDecode($row['wechat']);
-            $cs_wechatBu = textDecode($row['wechat_bu']);
-            $cs_linkedin = textDecode($row['linkedin']);
-            $cs_linkedinBu = textDecode($row['linkedin_bu']);
-            $cs_facebook = textDecode($row['facebook']);
-            $cs_facebookBu = textDecode($row['facebook_bu']);
-            $cs_alibaba = textDecode($row['alibaba']);
-            $cs_alibabaBu = textDecode($row['alibaba_bu']);
+            $cs_tel = textDecode($row['tel_1']);
+            $cs_telBu = textDecode($row['tel_1_bu']);
+            $cs_email = textDecode($row['email_1']);
+            $cs_emailBu = textDecode($row['email_1_bu']);
+            $cs_whatsapp = textDecode($row['whatsapp_1']);
+            $cs_whatsappBu = textDecode($row['whatsapp_1_bu']);
+            $cs_wechat = textDecode($row['wechat_1']);
+            $cs_wechatBu = textDecode($row['wechat_1_bu']);
+            $cs_linkedin = textDecode($row['linkedin_1']);
+            $cs_linkedinBu = textDecode($row['linkedin_1_bu']);
+            $cs_facebook = textDecode($row['facebook_1']);
+            $cs_facebookBu = textDecode($row['facebook_1_bu']);
+            $cs_alibaba = textDecode($row['alibaba_1']);
+            $cs_alibabaBu = textDecode($row['alibaba_1_bu']);
             $cs_address = textDecode($row['cs_address']);
             $cs_addtime = $row['cs_addtime'];
             $cs_updatetime = $row['cs_updatetime'];
@@ -284,14 +469,14 @@ if (!empty($fliterEmployee)) {
 }
 
 if (!empty($fliterContact)) {
-    switch($fliterContact) {
-        case "1": $fliterStr .= " AND cs_tel != ''"; break;
-        case "2": $fliterStr .= " AND cs_wechat != ''"; break;
-        case "3": $fliterStr .= " AND cs_whatsapp != ''"; break;
-        case "4": $fliterStr .= " AND cs_email != ''"; break;
-        case "5": $fliterStr .= " AND cs_linkedin != ''"; break;
-        case "6": $fliterStr .= " AND cs_facebook != ''"; break;
-        case "7": $fliterStr .= " AND cs_alibaba != ''"; break;
+    switch ($fliterContact) {
+        case "1": $fliterStr .= " AND (cc.tel_1 != '' OR cc.tel_2 != '' OR cc.tel_3 != '')"; break;
+        case "2": $fliterStr .= " AND (cc.wechat_1 != '' OR cc.wechat_2 != '' OR cc.wechat_3 != '')"; break;
+        case "3": $fliterStr .= " AND (cc.whatsapp_1 != '' OR cc.whatsapp_2 != '' OR cc.whatsapp_3 != '')"; break;
+        case "4": $fliterStr .= " AND (cc.email_1 != '' OR cc.email_2 != '' OR cc.email_3 != '')"; break;
+        case "5": $fliterStr .= " AND (cc.linkedin_1 != '' OR cc.linkedin_2 != '' OR cc.linkedin_3 != '')"; break;
+        case "6": $fliterStr .= " AND (cc.facebook_1 != '' OR cc.facebook_2 != '' OR cc.facebook_3 != '')"; break;
+        case "7": $fliterStr .= " AND (cc.alibaba_1 != '' OR cc.alibaba_2 != '' OR cc.alibaba_3 != '')"; break;
     }
     $urlStr .= "&fliterContact=" . $fliterContact;
 }
@@ -307,21 +492,60 @@ $ordStr = !empty($ord) ? "$ord," : "";
 // 构建查询SQL - 修改为联合查询
 $sqlStr = "SELECT c.id, c.cs_code, c.cs_company, c.cs_country, c.cs_address, c.cs_from, 
            c.cs_deal, c.cs_addtime, c.cs_updatetime, c.cs_belong, c.cs_note, c.cs_claimFrom, 
-           c.cs_chain, c.cs_dealdate, cc.contact_name as cs_name, cc.tel as cs_tel, 
-           cc.email as cs_email, cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, 
-           cc.linkedin as cs_linkedin, cc.facebook as cs_facebook, cc.alibaba as cs_alibaba,
-           cc.tel_format as cs_telformat, cc.whatsapp_format as cs_whatsappformat
+           c.cs_chain, c.cs_dealdate, c.cs_type, c.cs_belongclient, c.allowedit,
+           cc.id as contact_id, cc.contact_name,
+           cc.tel_1, cc.tel_1_format, cc.tel_1_bu,
+           cc.tel_2, cc.tel_2_format, cc.tel_2_bu,
+           cc.tel_3, cc.tel_3_format, cc.tel_3_bu,
+           cc.email_1, cc.email_1_bu,
+           cc.email_2, cc.email_2_bu,
+           cc.email_3, cc.email_3_bu,
+           cc.whatsapp_1, cc.whatsapp_1_format, cc.whatsapp_1_bu,
+           cc.whatsapp_2, cc.whatsapp_2_format, cc.whatsapp_2_bu,
+           cc.whatsapp_3, cc.whatsapp_3_format, cc.whatsapp_3_bu,
+           cc.wechat_1, cc.wechat_1_bu,
+           cc.wechat_2, cc.wechat_2_bu,
+           cc.wechat_3, cc.wechat_3_bu,
+           cc.linkedin_1, cc.linkedin_1_bu,
+           cc.linkedin_2, cc.linkedin_2_bu,
+           cc.linkedin_3, cc.linkedin_3_bu,
+           cc.facebook_1, cc.facebook_1_bu,
+           cc.facebook_2, cc.facebook_2_bu,
+           cc.facebook_3, cc.facebook_3_bu,
+           cc.alibaba_1, cc.alibaba_1_bu,
+           cc.alibaba_2, cc.alibaba_2_bu,
+           cc.alibaba_3, cc.alibaba_3_bu
            FROM customer c 
            LEFT JOIN customer_contact cc ON c.id = cc.customer_id
-           WHERE (c.cs_code LIKE '%$keyscode%' 
+           WHERE 1=1";
+
+if (!empty($keyscode)) {
+    $sqlStr .= " AND (c.cs_code LIKE '%$keyscode%' 
            OR cc.contact_name LIKE '%$keyscode%' 
-           OR cc.wechat LIKE '%$keyscode%' 
-           OR cc.alibaba LIKE '%$keyscode%' 
-           OR cc.tel_format LIKE '%$keyscode%' 
-           OR cc.whatsapp_format LIKE '%$keyscode%' 
-           OR cc.email LIKE '%$keyscode%') 
-           $fliterStr 
-           ORDER BY {$ordStr}c.cs_updatetime DESC";
+           OR cc.tel_1 LIKE '%$keyscode%'
+           OR cc.tel_2 LIKE '%$keyscode%'
+           OR cc.tel_3 LIKE '%$keyscode%'
+           OR cc.email_1 LIKE '%$keyscode%'
+           OR cc.email_2 LIKE '%$keyscode%'
+           OR cc.email_3 LIKE '%$keyscode%'
+           OR cc.wechat_1 LIKE '%$keyscode%'
+           OR cc.wechat_2 LIKE '%$keyscode%'
+           OR cc.wechat_3 LIKE '%$keyscode%'
+           OR cc.whatsapp_1_format LIKE '%$keyscode%'
+           OR cc.whatsapp_2_format LIKE '%$keyscode%'
+           OR cc.whatsapp_3_format LIKE '%$keyscode%'
+           OR cc.linkedin_1 LIKE '%$keyscode%'
+           OR cc.linkedin_2 LIKE '%$keyscode%'
+           OR cc.linkedin_3 LIKE '%$keyscode%'
+           OR cc.facebook_1 LIKE '%$keyscode%'
+           OR cc.facebook_2 LIKE '%$keyscode%'
+           OR cc.facebook_3 LIKE '%$keyscode%'
+           OR cc.alibaba_1 LIKE '%$keyscode%'
+           OR cc.alibaba_2 LIKE '%$keyscode%'
+           OR cc.alibaba_3 LIKE '%$keyscode%')";
+}
+
+$sqlStr .= " $fliterStr ORDER BY {$ordStr}c.cs_updatetime DESC";
 
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@@ -389,13 +613,13 @@ if (!empty($filters['Business'])) {
 
 if (!empty($filters['Contact'])) {
     switch ($filters['Contact']) {
-        case "1": $filterStr .= " AND cs_tel<>''"; break;
-        case "2": $filterStr .= " AND cs_wechat<>''"; break;
-        case "3": $filterStr .= " AND cs_whatsapp<>''"; break;
-        case "4": $filterStr .= " AND cs_email<>''"; break;
-        case "5": $filterStr .= " AND cs_linkedin<>''"; break;
-        case "6": $filterStr .= " AND cs_facebook<>''"; break;
-        default: $filterStr .= " AND cs_alibaba<>''";
+        case "1": $filterStr .= " AND (cc.tel_1 != '' OR cc.tel_2 != '' OR cc.tel_3 != '')"; break;
+        case "2": $filterStr .= " AND (cc.wechat_1 != '' OR cc.wechat_2 != '' OR cc.wechat_3 != '')"; break;
+        case "3": $filterStr .= " AND (cc.whatsapp_1 != '' OR cc.whatsapp_2 != '' OR cc.whatsapp_3 != '')"; break;
+        case "4": $filterStr .= " AND (cc.email_1 != '' OR cc.email_2 != '' OR cc.email_3 != '')"; break;
+        case "5": $filterStr .= " AND (cc.linkedin_1 != '' OR cc.linkedin_2 != '' OR cc.linkedin_3 != '')"; break;
+        case "6": $filterStr .= " AND (cc.facebook_1 != '' OR cc.facebook_2 != '' OR cc.facebook_3 != '')"; break;
+        case "7": $filterStr .= " AND (cc.alibaba_1 != '' OR cc.alibaba_2 != '' OR cc.alibaba_3 != '')"; break;
     }
     $urlStr .= "&fliterContact=" . $filters['Contact'];
 }
@@ -494,28 +718,65 @@ $hrefstr = "?keys=" . $keys;
         </div>
 
         <?php
-        $sql = "SELECT c.id, c.cs_code, c.cs_from, c.cs_country, c.cs_type, c.cs_deal, c.cs_addtime, 
-                c.colortag, c.cs_note, c.cs_claimFrom, cc.tel as cs_tel, cc.email as cs_email, 
-                cc.whatsapp as cs_whatsapp, cc.wechat as cs_wechat, cc.linkedin as cs_linkedin, 
-                cc.facebook as cs_facebook, cc.alibaba as cs_alibaba, cc.tel_format as cs_telformat,
-                cc.whatsapp_format as cs_whatsappformat
+        $sql = "SELECT c.id, c.cs_code, c.cs_company, c.cs_country, c.cs_address, c.cs_from, 
+                c.cs_deal, c.cs_addtime, c.cs_updatetime, c.cs_belong, c.cs_note, c.cs_claimFrom, 
+                c.cs_chain, c.cs_dealdate, c.cs_type, c.cs_belongclient, c.allowedit,c.colortag,
+                cc.id as contact_id, cc.contact_name,
+                cc.tel_1, cc.tel_1_format, cc.tel_1_bu,
+                cc.tel_2, cc.tel_2_format, cc.tel_2_bu,
+                cc.tel_3, cc.tel_3_format, cc.tel_3_bu,
+                cc.email_1, cc.email_1_bu,
+                cc.email_2, cc.email_2_bu,
+                cc.email_3, cc.email_3_bu,
+                cc.whatsapp_1, cc.whatsapp_1_format, cc.whatsapp_1_bu,
+                cc.whatsapp_2, cc.whatsapp_2_format, cc.whatsapp_2_bu,
+                cc.whatsapp_3, cc.whatsapp_3_format, cc.whatsapp_3_bu,
+                cc.wechat_1, cc.wechat_1_bu,
+                cc.wechat_2, cc.wechat_2_bu,
+                cc.wechat_3, cc.wechat_3_bu,
+                cc.linkedin_1, cc.linkedin_1_bu,
+                cc.linkedin_2, cc.linkedin_2_bu,
+                cc.linkedin_3, cc.linkedin_3_bu,
+                cc.facebook_1, cc.facebook_1_bu,
+                cc.facebook_2, cc.facebook_2_bu,
+                cc.facebook_3, cc.facebook_3_bu,
+                cc.alibaba_1, cc.alibaba_1_bu,
+                cc.alibaba_2, cc.alibaba_2_bu,
+                cc.alibaba_3, cc.alibaba_3_bu
                 FROM customer c 
-                LEFT JOIN customer_contact cc ON c.id = cc.customer_id 
+                LEFT JOIN customer_contact cc ON c.id = cc.customer_id
                 WHERE c.is_silent=0 AND c.cs_deal>0 AND c.cs_belong=" . (int)$_SESSION['employee_id'];
         
         $searchPattern = mysqli_real_escape_string($conn, $keyscode);
         if(!empty($searchPattern)) {
             $sql .= " AND (c.cs_code LIKE '%$searchPattern%' 
                     OR cc.contact_name LIKE '%$searchPattern%' 
-                    OR cc.email LIKE '%$searchPattern%'
-                    OR cc.wechat LIKE '%$searchPattern%' 
-                    OR cc.tel_format LIKE '%$searchPattern%'
-                    OR cc.whatsapp_format LIKE '%$searchPattern%' 
-                    OR c.cs_code LIKE '%$searchPattern%')";
+                    OR cc.tel_1 LIKE '%$searchPattern%'
+                    OR cc.tel_2 LIKE '%$searchPattern%'
+                    OR cc.tel_3 LIKE '%$searchPattern%'
+                    OR cc.email_1 LIKE '%$searchPattern%'
+                    OR cc.email_2 LIKE '%$searchPattern%'
+                    OR cc.email_3 LIKE '%$searchPattern%'
+                    OR cc.wechat_1 LIKE '%$searchPattern%'
+                    OR cc.wechat_2 LIKE '%$searchPattern%'
+                    OR cc.wechat_3 LIKE '%$searchPattern%'
+                    OR cc.whatsapp_1_format LIKE '%$searchPattern%'
+                    OR cc.whatsapp_2_format LIKE '%$searchPattern%'
+                    OR cc.whatsapp_3_format LIKE '%$searchPattern%'
+                    OR cc.linkedin_1 LIKE '%$searchPattern%'
+                    OR cc.linkedin_2 LIKE '%$searchPattern%'
+                    OR cc.linkedin_3 LIKE '%$searchPattern%'
+                    OR cc.facebook_1 LIKE '%$searchPattern%'
+                    OR cc.facebook_2 LIKE '%$searchPattern%'
+                    OR cc.facebook_3 LIKE '%$searchPattern%'
+                    OR cc.alibaba_1 LIKE '%$searchPattern%'
+                    OR cc.alibaba_2 LIKE '%$searchPattern%'
+                    OR cc.alibaba_3 LIKE '%$searchPattern%')";
         }
         
         $sql .= " $filterStr ORDER BY c.colortag DESC, c.id DESC";
-        
+
+
         $result = mysqli_query($conn, $sql);
 
         // Pagination logic
@@ -587,13 +848,83 @@ $hrefstr = "?keys=" . $keys;
                 <div class="notepanel clear">
                     <div class="noteItem">联系方式</div>
                     <div class="lx">
-                        <div class="tel"><?= $row['cs_tel'] ?></div>
-                        <div class="mail"><a href="mailto:<?= $row['cs_email'] ?>"><?= $row['cs_email'] ?></a></div>
-                        <div class="whatsapp"><?= $row['cs_whatsapp'] ?></div>
-                        <div class="wechat"><?= $row['cs_wechat'] ?></div>
-                        <div class="linkedin"><?= $row['cs_linkedin'] ?></div>
-                        <div class="facebook"><?= $row['cs_facebook'] ?></div>
-                        <div class="alibaba"><?= $row['cs_alibaba'] ?></div>
+                        <div class="tel">
+                            <?php if(!empty($row['tel_1'])): ?>
+                                <div><?= $row['tel_1'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['tel_2'])): ?>
+                                <div><?= $row['tel_2'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['tel_3'])): ?>
+                                <div><?= $row['tel_3'] ?></div>
+                            <?php endif; ?>
+                        </div>
+                        <div class="mail">
+                            <?php if(!empty($row['email_1'])): ?>
+                                <div><a href="mailto:<?= $row['email_1'] ?>"><?= $row['email_1'] ?></a></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['email_2'])): ?>
+                                <div><a href="mailto:<?= $row['email_2'] ?>"><?= $row['email_2'] ?></a></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['email_3'])): ?>
+                                <div><a href="mailto:<?= $row['email_3'] ?>"><?= $row['email_3'] ?></a></div>
+                            <?php endif; ?>
+                        </div>
+                        <div class="whatsapp">
+                            <?php if(!empty($row['whatsapp_1'])): ?>
+                                <div><?= $row['whatsapp_1'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['whatsapp_2'])): ?>
+                                <div><?= $row['whatsapp_2'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['whatsapp_3'])): ?>
+                                <div><?= $row['whatsapp_3'] ?></div>
+                            <?php endif; ?>
+                        </div>
+                        <div class="wechat">
+                            <?php if(!empty($row['wechat_1'])): ?>
+                                <div><?= $row['wechat_1'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['wechat_2'])): ?>
+                                <div><?= $row['wechat_2'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['wechat_3'])): ?>
+                                <div><?= $row['wechat_3'] ?></div>
+                            <?php endif; ?>
+                        </div>
+                        <div class="linkedin">
+                            <?php if(!empty($row['linkedin_1'])): ?>
+                                <div><?= $row['linkedin_1'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['linkedin_2'])): ?>
+                                <div><?= $row['linkedin_2'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['linkedin_3'])): ?>
+                                <div><?= $row['linkedin_3'] ?></div>
+                            <?php endif; ?>
+                        </div>
+                        <div class="facebook">
+                            <?php if(!empty($row['facebook_1'])): ?>
+                                <div><?= $row['facebook_1'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['facebook_2'])): ?>
+                                <div><?= $row['facebook_2'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['facebook_3'])): ?>
+                                <div><?= $row['facebook_3'] ?></div>
+                            <?php endif; ?>
+                        </div>
+                        <div class="alibaba">
+                            <?php if(!empty($row['alibaba_1'])): ?>
+                                <div><?= $row['alibaba_1'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['alibaba_2'])): ?>
+                                <div><?= $row['alibaba_2'] ?></div>
+                            <?php endif; ?>
+                            <?php if(!empty($row['alibaba_3'])): ?>
+                                <div><?= $row['alibaba_3'] ?></div>
+                            <?php endif; ?>
+                        </div>
                     </div>
                     <div class="noteItem2">备注</div>
                     <div class="notecontent"><?= htmlUnCode($row['cs_note']) ?></div>