<?php
/**
 * 销售工作台 · 登录入口
 * 用内销 CRM（crm.ferrobotics.cn）账号密码校验，成功后写 session。
 */
session_start();
date_default_timezone_set('Asia/Shanghai');

$CRM = 'https://crm.ferrobotics.cn';
$OWNERS = [
    'wj' => '王建', 'yzn' => '叶忠南', 'zht' => '张宏涛', 'cxl' => '程训令',
    'xiangmu' => '项目', 'lgj' => '刘国军', 'cxf' => 'cxf',
    'lijijiu' => '李继久', 'tianying' => '田英',
];
$SUPERVISORS = ['cxf', 'cxl', 'lijijiu'];

if (!empty($_SESSION['viewer'])) {
    header('Location: /dashboard.php');
    exit;
}

$err = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $user = trim($_POST['user'] ?? '');
    $pass = $_POST['pass'] ?? '';

    // 先按账号表校验是否是允许的业务员账号
    if (!isset($OWNERS[$user])) {
        $err = '账号不存在，请联系管理员';
    } else {
        // 调内销 CRM API 校验密码（Basic Auth）
        $ch = curl_init($CRM . '/api/v1/Account?maxSize=1');
        curl_setopt_array($ch, [
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
            CURLOPT_USERPWD => $user . ':' . $pass,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_TIMEOUT => 20,
        ]);
        $resp = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($code === 200) {
            session_regenerate_id(true);
            $_SESSION['viewer'] = $user;
            $_SESSION['viewerName'] = $OWNERS[$user];
            $_SESSION['isSupervisor'] = in_array($user, $SUPERVISORS, true);
            header('Location: /dashboard.php');
            exit;
        } elseif ($code === 401) {
            $err = '账号或密码错误';
        } else {
            $err = 'CRM 连接失败（HTTP ' . $code . '），请稍后重试';
        }
    }
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>登录 · 防护服销售工作台</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{background:#f5f6f8;color:#1f2430;font-family:-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh}
.card{background:#fff;border:1px solid #e6e8ec;border-radius:14px;box-shadow:0 4px 20px rgba(16,24,40,.08);padding:36px 40px;width:360px}
h1{font-size:18px;font-weight:700;margin-bottom:6px}
.sub{font-size:12px;color:#6b7280;margin-bottom:24px}
label{display:block;font-size:12px;color:#6b7280;margin:14px 0 6px}
input{width:100%;padding:10px 12px;border:1px solid #e6e8ec;border-radius:8px;font-size:14px;font-family:inherit}
input:focus{outline:none;border-color:#2563eb}
button{width:100%;margin-top:24px;padding:11px;border:0;border-radius:8px;background:#2563eb;color:#fff;font-size:14px;font-weight:600;cursor:pointer}
button:hover{background:#1d4ed8}
.err{margin-top:14px;padding:9px 12px;background:#fef2f2;color:#dc2626;border-radius:7px;font-size:12px}
.foot{margin-top:20px;text-align:center;font-size:11px;color:#9ca3af}
</style>
</head>
<body>
<div class="card">
  <h1>🛡️ 防护服销售工作台</h1>
  <div class="sub">请使用 CRM 账号登录</div>
  <form method="post" autocomplete="on">
    <label>账号</label>
    <input type="text" name="user" placeholder="如 wj / yzn / tianying" value="<?=htmlspecialchars($_POST['user'] ?? '')?>" required autofocus>
    <label>密码</label>
    <input type="password" name="pass" placeholder="CRM 登录密码" required>
    <button type="submit">登 录</button>
  </form>
  <?php if ($err): ?><div class="err"><?=htmlspecialchars($err)?></div><?php endif; ?>
  <div class="foot">账号由管理员统一开通 · 权限按 CRM 角色分配</div>
</div>
</body>
</html>
