File size: 4,319 Bytes
862613a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import React from 'react';
import { Minus, Plus, ShoppingBag } from 'lucide-react';

const QuickCart = ({ cart, onRemoveItem, onUpdateQuantity, onOpenCart }) => {
  const formatPrice = (price) => {
    return new Intl.NumberFormat('th-TH', {
      style: 'currency',
      currency: 'THB'
    }).format(price);
  };

  const cartTotal = cart.reduce((sum, item) => {
    const price = item.discount > 0 
      ? item.price * (1 - item.discount/100) 
      : item.price;
    return sum + (price * item.quantity);
  }, 0);

  const cartCount = cart.reduce((sum, item) => sum + item.quantity, 0);

  return (
    <div className="bg-white rounded-2xl border border-pos-border p-4">
      <div className="flex items-center justify-between mb-4">
        <div className="flex items-center gap-2">
          <div className="bg-primary-600 p-2 rounded-lg">
            <ShoppingBag className="w-5 h-5 text-white" />
          </div>
          <div>
            <h3 className="font-bold text-pos-text">ตะกร้า</h3>
            <p className="text-xs text-pos-text-secondary">{cartCount} รายการ</p>
          </div>
        </div>
        <button 
          onClick={onOpenCart}
          className="text-sm font-medium text-primary-600 hover:bg-primary-50 px-3 py-1 rounded-lg transition-colors"
        >
          ดูทั้งหมด
        </button>
      </div>
      
      {cart.length === 0 ? (
        <div className="py-8 text-center text-pos-text-secondary">
          <ShoppingBag className="w-8 h-8 mx-auto mb-2 opacity-50" />
          <p className="text-sm">ไม่มีสินค้าในตะกร้า</p>
        </div>
      ) : (
        <div className="space-y-3">
          {cart.slice(0, 3).map((item) => {
            const price = item.discount > 0 
              ? item.price * (1 - item.discount/100) 
              : item.price;
            const itemTotal = price * item.quantity;
            
            return (
              <div key={item.id} className="flex items-center gap-3">
                <div className="w-10 h-10 flex-shrink-0 rounded-lg overflow-hidden">
                  <img 
                    src={item.image} 
                    alt={item.name}
                    className="w-full h-full object-cover"
                  />
                </div>
                <div className="flex-1 min-w-0">
                  <p className="font-medium text-sm text-pos-text truncate">{item.name}</p>
                  <p className="text-xs text-pos-text-secondary">
                    {formatPrice(price)} x {item.quantity}
                  </p>
                </div>
                <div className="flex flex-col items-end gap-1">
                  <p className="font-bold text-sm text-primary-600">{formatPrice(itemTotal)}</p>
                  <div className="flex items-center gap-1">
                    <button 
                      onClick={() => onUpdateQuantity(item.id, Math.max(1, item.quantity - 1))}
                      className="p-0.5 rounded hover:bg-pos-bg"
                    >
                      <Minus className="w-3 h-3 text-pos-text-secondary" />
                    </button>
                    <span className="text-xs w-4 text-center">{item.quantity}</span>
                    <button 
                      onClick={() => onUpdateQuantity(item.id, item.quantity + 1)}
                      className="p-0.5 rounded hover:bg-pos-bg"
                    >
                      <Plus className="w-3 h-3 text-pos-text-secondary" />
                    </button>
                  </div>
                </div>
              </div>
            );
          })}
          
          {cart.length > 3 && (
            <div className="text-center">
              <p className="text-xs text-pos-text-secondary">
                และอีก {cart.length - 3} รายการ
              </p>
            </div>
          )}
          
          <div className="border-t border-pos-border pt-3 mt-3 flex justify-between items-center">
            <p className="text-sm font-medium text-pos-text-secondary">ยอดรวม</p>
            <p className="text-lg font-bold text-primary-600">{formatPrice(cartTotal)}</p>
          </div>
        </div>
      )}
    </div>
  );
};

export default QuickCart;