<?php
require_once 'conn.php';
checkLogin();
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>管理区域</title>
    <link rel="stylesheet" href="css/common.css" type="text/css" />
    <script src="system/js/jquery-1.7.2.min.js"></script>
    <script src="js/js.js"></script>
    <script src="system/xheditor-1.1.9/xheditor-1.1.9-zh-cn.min.js"></script>
</head>
<style>
    body {
        margin: 0;
        padding: 20px;
        background: #fff;
    }
    #man_zone {
        margin-left: 0;
    }
</style>
<body class="clear">
<?php // require_once 'panel.php'; ?>
<div id="man_zone">
<?php
$id = $_GET['id'] ?? '';
$page = $_GET['Page'] ?? '';
$keys = urlencode($_GET['Keys'] ?? '');
$ord = urlencode($_GET['Ord'] ?? '');
$hrefstr = "?keys=" . $keys . "&Ord=" . $ord . "&Page=" . $page;

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 IN (SELECT id FROM employee WHERE em_role=" . $_SESSION['employee_id'] . ") 
            AND c.id=" . (int)$id;
            
    $result = $conn->query($sql);
    
    if ($result && $row = $result->fetch_assoc()) {
        $cs_company = textUncode($row['cs_company']);
        $cs_name = textUncode($row['cs_name']);
        $cs_country = textUncode($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_alibaba = textUncode($row['cs_alibaba']);
        $cs_address = textUncode($row['cs_address']);
        $cs_code = textUncode($row['cs_code']);
        $cs_deal = textUncode($row['cs_deal']);
        $cs_addtime = $row['cs_addtime'];
        $cs_updatetime = $row['cs_updatetime'];
        $cs_from = $row['cs_from'];
        $cs_note = $row['cs_note'];
?>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
        <tbody>
            <tr>
                <th width="8%">客户编号</th>
                <td><?= htmlspecialcharsFix($cs_code) ?></td>
            </tr>
            <tr>
                <th width="8%">公司名称</th>
                <td><?= htmlspecialcharsFix($cs_company) ?></td>
            </tr>
            <tr>
                <th width="8%">联系人</th>
                <td><?= htmlspecialcharsFix($cs_name) ?></td>
            </tr>
            <tr>
                <th width="8%">地区</th>
                <td>
                    <?php
                    $countryResult = $conn->query("SELECT countryCode, countryName FROM country WHERE countryCode='" . 
                                                $conn->real_escape_string($cs_country) . "'");
                    if ($countryRow = $countryResult->fetch_assoc()) {
                        echo "(+" . htmlspecialcharsFix($countryRow['countryCode']) . ")" . 
                             htmlspecialcharsFix($countryRow['countryName']);
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <th width="8%">客户来源</th>
                <td>
                    <?php
                    $qudaoResult = $conn->query("SELECT id, ch_name FROM qudao WHERE id=" . (int)$cs_from);
                    if ($qudaoRow = $qudaoResult->fetch_assoc()) {
                        echo htmlspecialcharsFix($qudaoRow['ch_name']);
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <th rowspan="7">联系方式</th>
                <td><?= htmlspecialcharsFix($cs_tel) ?></td>
            </tr>
            <tr>
                <td><span class="wechat"><?= htmlspecialcharsFix($cs_wechat) ?></span></td>
            </tr>
            <tr>
                <td><span class="whatsapp"><?= htmlspecialcharsFix($cs_whatsapp) ?></span></td>
            </tr>
            <tr>
                <td><span class="mail"><?= htmlspecialcharsFix($cs_email) ?></span></td>
            </tr>
            <tr>
                <td><span class="linkedin"><?= htmlspecialcharsFix($cs_linkedin) ?></span></td>
            </tr>
            <tr>
                <td><span class="facebook"><?= htmlspecialcharsFix($cs_facebook) ?></span></td>
            </tr>
            <tr>
                <td><span class="alibaba"><?= htmlspecialcharsFix($cs_alibaba) ?></span></td>
            </tr>
            <tr>
                <th width="8%">地址</th>
                <td><?= htmlspecialcharsFix($cs_address) ?></td>
            </tr>
            <tr>
                <th>是否成交</th>
                <td>
                    <?php
                    if ($cs_deal == 1) {
                        echo "<span style='color:red;'>已经成交</span>";
                    } elseif ($cs_deal == 2) {
                        echo "无响应";
                    } else {
                        echo "未成交";
                    }
                    ?>
                </td>
            </tr>
            <tr>
                <th width="8%">备注</th>
                <td><?= htmlUnCode($cs_note) ?></td>
            </tr>
            <tr>
                <th></th>
                <td>
                    <input type="button" value="返回" class="btn1" 
                           onClick="location.href='subcustomers.php<?= $hrefstr ?>'" />
                </td>
            </tr>
        </tbody>
    </table>
<?php
    } else {
        echo "<script>alert('客户不存在或你没权限查看!');history.back();</script>";
        exit;
    }
} else {
    echo "<script>alert('客户不存在!');history.back();</script>";
    header("Location: " . $hrefstr);
    exit;
}
?>
</div>
</body>
</html>