<?php
/** @var array $orders 每项: ['order' => array, 'payments' => array] */
/** @var string $no */
/** @var string $name */
/** @var string $phone */
/** @var string $err */
?>
<section class="page-hero"><div class="container"><h1>订单查询</h1><p>凭 客户名 + 手机号 核验身份，即可查询您的订单与付款信息（也可加订单号精确查单笔）</p></div></section>
<div class="container">
 <div class="form-card">
   <form method="post" action="<?php echo site_url('order/query'); ?>">
     <?php echo csrf_field(); ?>
     <div class="field"><label>客户名（姓名/单位）*</label><input name="name" value="<?php echo e($name); ?>" placeholder="下单时填写的姓名或单位" required></div>
     <div class="field"><label>手机号 *</label><input name="phone" value="<?php echo e($phone); ?>" placeholder="下单时填写的手机号" required></div>
     <div class="field"><label>订单号（选填）</label><input name="order_no" value="<?php echo e($no); ?>" placeholder="如留空则列出该客户名下全部订单"></div>
     <button class="btn btn-primary" type="submit">查询</button>
   </form>
   <?php if ($err): ?><div class="alert alert-err" style="margin-top:18px"><?php echo e($err); ?></div><?php endif; ?>
   <?php if ($orders): ?>
     <div class="alert alert-ok" style="margin-top:18px">✓ 共找到 <?php echo count($orders); ?> 笔订单</div>
     <?php foreach ($orders as $item): ?>
       <?php $o = $item['order']; $ps = $item['payments']; ?>
       <div class="order-card">
         <table class="spec-table">
           <tr><td>订单号</td><td><?php echo e($o['order_no']); ?></td></tr>
           <tr><td>商品</td><td><?php echo e($o['product_name']); ?></td></tr>
           <tr><td>数量</td><td><?php echo e($o['qty']); ?> 套</td></tr>
           <tr><td>金额</td><td>¥<?php echo e($o['amount']); ?></td></tr>
           <tr><td>状态</td><td><?php echo $o['status'] === 'paid' ? '<span class="ok">已支付</span>' : '<span class="pend">待支付</span>'; ?></td></tr>
         </table>
         <?php if ($o['status'] !== 'paid'): ?>
           <a class="btn btn-primary" style="margin-top:14px" href="<?php echo site_url('order/pay/' . $o['order_no']); ?>">去支付</a>
         <?php endif; ?>
         <?php if ($ps): ?>
           <h4 style="margin:18px 0 8px">付款记录</h4>
           <table class="admin-table">
             <thead><tr><th>通道</th><th>交易流水号</th><th>金额</th><th>付款时间</th></tr></thead>
             <tbody>
             <?php foreach ($ps as $p): ?>
               <tr><td><?php echo $p['channel'] === 'wechat' ? '微信支付' : '支付宝'; ?></td><td><?php echo e($p['trade_no']); ?></td><td>¥<?php echo e($p['amount']); ?></td><td><?php echo e($p['paid_at']); ?></td></tr>
             <?php endforeach; ?>
             </tbody>
           </table>
         <?php endif; ?>
       </div>
     <?php endforeach; ?>
   <?php endif; ?>
 </div>
</div>
