<?php
$FILE = '/var/www/portal/coupons.json';
$UPLOAD_DIR = '/var/www/portal/uploads/';

function load_c($f){ if(!file_exists($f)) return []; $d=json_decode(file_get_contents($f),true); return is_array($d)?$d:[]; }
function save_c($f,$d){ file_put_contents($f, json_encode(array_values($d), JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT)); }
function h($s){ return htmlspecialchars($s ?? '', ENT_QUOTES); }

$done = false; $err = '';

if ($_SERVER['REQUEST_METHOD']==='POST') {
    $shop = trim($_POST['shop']??'');
    $title = trim($_POST['title']??'');
    $contact = trim($_POST['contact']??'');
    if (!$shop || !$title || !$contact) {
        $err = '商家名称、优惠标题、联系方式为必填';
    } else {
        // 处理图片（最多3张，防滥用：限格式+大小）
        $images = [];
        if (!empty($_FILES['images']['name'][0])) {
            foreach ($_FILES['images']['name'] as $i=>$name) {
                if ($i >= 3) break; // 最多3张
                if ($_FILES['images']['error'][$i]!==0) continue;
                if ($_FILES['images']['size'][$i] > 5*1024*1024) continue; // 单张<5MB
                $ext=strtolower(pathinfo($name,PATHINFO_EXTENSION));
                if (in_array($ext,['png','jpg','jpeg','gif','webp'])) {
                    $fn='coupon-'.time().'-'.$i.'.'.$ext;
                    move_uploaded_file($_FILES['images']['tmp_name'][$i],$UPLOAD_DIR.$fn);
                    $images[]=$fn;
                }
            }
        }
        $list = load_c($FILE);
        $list[] = [
            'id'=>(string)round(microtime(true)*1000),
            'shop'=>mb_substr($shop,0,60),
            'area'=>mb_substr(trim($_POST['area']??''),0,40),
            'category'=>mb_substr(trim($_POST['category']??''),0,20),
            'title'=>mb_substr($title,0,60),
            'desc'=>mb_substr(trim($_POST['desc']??''),0,300),
            'valid'=>mb_substr(trim($_POST['valid']??''),0,40),
            'contact'=>mb_substr($contact,0,60),
            'images'=>$images,
            'status'=>'pending',
            'submit_time'=>date('Y-m-d H:i')
        ];
        save_c($FILE,$list);
        $done = true;
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>商家提交优惠</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:"PingFang SC","Microsoft YaHei",sans-serif;background:#f5f5f5;color:#333;line-height:1.6}
.header{background:#c0392b;color:#fff;padding:18px 24px;text-align:center}
.header h1{font-size:20px}
.back-link{display:inline-block;margin:16px 24px 0;color:#c0392b;text-decoration:none;font-size:14px}
.container{max-width:560px;margin:0 auto;padding:16px 24px 40px}
.card{background:#fff;border-radius:10px;padding:24px;box-shadow:0 1px 3px rgba(0,0,0,.06)}
label{display:block;font-size:13px;color:#666;margin:12px 0 4px}
.req{color:#c0392b}
input[type=text]{width:100%;padding:10px;border:1px solid #ddd;border-radius:6px;font-size:14px}
button{background:#c0392b;color:#fff;border:none;padding:11px 20px;border-radius:6px;font-size:15px;cursor:pointer;margin-top:18px;width:100%}
.hint{font-size:12px;color:#999;margin-top:4px}
.tip{background:#fff8e1;border-left:3px solid #f0ad4e;padding:10px 14px;border-radius:5px;font-size:13px;color:#8a6d3b;margin-bottom:16px}
.ok{background:#fff;border-radius:10px;padding:40px 24px;text-align:center}
.ok h2{color:#27ae60;font-size:20px;margin-bottom:10px}
.ok p{color:#666;font-size:14px}
.err{background:#f2dede;color:#a94442;padding:10px 14px;border-radius:6px;font-size:14px;margin-bottom:14px}
</style></head><body>
<div class="header"><h1>🏪 商家提交优惠</h1></div>
<a class="back-link" href="/coupons/">← 返回优惠列表</a>
<div class="container">
<?php if($done): ?>
  <div class="ok">
    <h2>✅ 提交成功！</h2>
    <p>您的优惠信息已提交，我们审核后会尽快上架到本地优惠页。<br>感谢您的参与！</p>
    <a class="back-link" href="/coupons/" style="margin-top:20px">返回优惠列表</a>
  </div>
<?php else: ?>
  <div class="card">
    <div class="tip">提交后需经审核才会显示，请如实填写。我们只展示真实、对本地华人有价值的优惠。</div>
    <?php if($err): ?><div class="err"><?=h($err)?></div><?php endif; ?>
    <form method="post" enctype="multipart/form-data">
      <label>商家名称 <span class="req">*</span></label>
      <input type="text" name="shop" required value="<?=h($_POST['shop']??'')?>">
      <label>地址</label>
      <input type="text" name="area" placeholder="如：Shop 5, 17 Main St, Box Hill" value="<?=h($_POST['area']??'')?>">
      <label>分类</label>
      <input type="text" name="category" placeholder="如：餐饮 / 美发 / 超市 / 教育" value="<?=h($_POST['category']??'')?>">
      <label>优惠标题 <span class="req">*</span></label>
      <input type="text" name="title" placeholder="如：全单 8 折" required value="<?=h($_POST['title']??'')?>">
      <label>优惠详情</label>
      <input type="text" name="desc" placeholder="如：周一至周五午市，凭此优惠享全单 8 折" value="<?=h($_POST['desc']??'')?>">
      <label>有效期至</label>
      <input type="text" name="valid" placeholder="如：2026-12-31" value="<?=h($_POST['valid']??'')?>">
      <label>您的联系方式 <span class="req">*</span></label>
      <input type="text" name="contact" placeholder="微信/电话，仅供我们审核联系，不公开" required value="<?=h($_POST['contact']??'')?>">
      <div class="hint">此联系方式不会公开显示，仅用于我们审核时与您联系</div>
      <label>商家/优惠图片（可选，最多 3 张）</label>
      <input type="file" name="images[]" accept="image/*" multiple>
      <button type="submit">提交优惠</button>
    </form>
  </div>
<?php endif; ?>
</div>
</body></html>
