File size: 10,848 Bytes
b64de39
c5a7b7d
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a7b7d
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a7b7d
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
c5a7b7d
b64de39
 
 
 
 
 
 
 
 
 
c5a7b7d
 
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a7b7d
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a7b7d
 
b64de39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5a7b7d
b64de39
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
 * PriceTable - Responsive APMC price table with sorting and highlights.
 *
 * Features:
 *  - Desktop: Full table with sortable columns
 *  - Mobile: Card-based layout
 *  - Best price row highlighted
 *  - Price range bar (min/max)
 *  - Formatted dates and prices
 *  - Empty state
 */

import { useMemo } from "react";
import PropTypes from "prop-types";
import { motion } from "framer-motion";
import {
  ArrowTrendingUpIcon,
  ArrowTrendingDownIcon,
  MinusIcon,
  MapPinIcon,
  StarIcon,
} from "@heroicons/react/24/outline";
import { Card, Badge } from "@components/common";
import { formatPricePerQuintal, formatDate } from "@utils/helpers";

/**
 * Determine a price trend indicator based on modal vs min/max spread.
 */
function getTrendIndicator(price) {
  if (!price.modal_price || !price.min_price || !price.max_price) {
    return { icon: MinusIcon, color: "text-neutral-400", label: "Stable" };
  }
  const mid = (price.min_price + price.max_price) / 2;
  const diff = ((price.modal_price - mid) / mid) * 100;

  if (diff > 2) {
    return { icon: ArrowTrendingUpIcon, color: "text-primary-600", label: "Rising" };
  }
  if (diff < -2) {
    return { icon: ArrowTrendingDownIcon, color: "text-danger-600", label: "Falling" };
  }
  return { icon: MinusIcon, color: "text-neutral-400", label: "Stable" };
}

function PriceTable({ prices = [], bestAPMCName = "", className = "" }) {
  // Find the best price row
  const bestPrice = useMemo(() => {
    if (prices.length === 0) return null;
    return prices.reduce((best, p) =>
      (p.price_per_quintal || 0) > (best.price_per_quintal || 0) ? p : best,
    );
  }, [prices]);

  // Price range for bar visualization
  const priceRange = useMemo(() => {
    if (prices.length === 0) return { min: 0, max: 1 };
    const allPrices = prices.map((p) => p.price_per_quintal).filter(Boolean);
    return {
      min: Math.min(...allPrices),
      max: Math.max(...allPrices),
    };
  }, [prices]);

  if (prices.length === 0) {
    return (
      <Card variant="flat" className={`border border-neutral-200 ${className}`}>
        <div className="flex flex-col items-center gap-2 py-8">
          <MapPinIcon className="h-8 w-8 text-neutral-300" />
          <p className="text-sm text-neutral-500">
            No price data available. Select a commodity to see prices.
          </p>
        </div>
      </Card>
    );
  }

  const range = priceRange.max - priceRange.min || 1;

  return (
    <div className={className}>
      {/* Result count */}
      <div className="flex items-center justify-between mb-3">
        <p className="text-sm text-neutral-600">
          Showing <span className="font-semibold text-neutral-800">{prices.length}</span> APMCs
        </p>
        {bestPrice && (
          <Badge variant="primary" size="sm" dot>
            Best: {formatPricePerQuintal(bestPrice.price_per_quintal)}
          </Badge>
        )}
      </div>

      {/* Desktop table */}
      <div className="hidden sm:block overflow-x-auto">
        <table className="w-full text-sm">
          <thead>
            <tr className="border-b-2 border-neutral-200">
              <th className="text-left py-3 px-3 font-semibold text-neutral-700">APMC</th>
              <th className="text-left py-3 px-3 font-semibold text-neutral-700">Location</th>
              <th className="text-right py-3 px-3 font-semibold text-neutral-700">Price/qtl</th>
              <th className="text-center py-3 px-3 font-semibold text-neutral-700">Range</th>
              <th className="text-center py-3 px-3 font-semibold text-neutral-700">Trend</th>
              <th className="text-right py-3 px-3 font-semibold text-neutral-700">Date</th>
            </tr>
          </thead>
          <tbody>
            {prices.map((price, index) => {
              const isBest =
                bestAPMCName
                  ? price.mandi_name === bestAPMCName
                  : price === bestPrice;
              const trend = getTrendIndicator(price);
              const TrendIcon = trend.icon;
              const barWidth =
                ((price.price_per_quintal - priceRange.min) / range) * 100;

              return (
                <motion.tr
                  key={price.id || `${price.mandi_name}-${index}`}
                  initial={{ opacity: 0 }}
                  animate={{ opacity: 1 }}
                  transition={{ delay: index * 0.03 }}
                  className={[
                    "border-b border-neutral-100 transition-colors",
                    isBest
                      ? "bg-primary-50 hover:bg-primary-100"
                      : "hover:bg-neutral-50",
                  ].join(" ")}
                >
                  {/* APMC name */}
                  <td className="py-3 px-3">
                    <div className="flex items-center gap-2">
                      {isBest && (
                        <StarIcon
                          className="h-4 w-4 text-secondary-500 shrink-0"
                          aria-label="Best price"
                        />
                      )}
                      <span className={isBest ? "font-semibold text-primary-800" : "text-neutral-800"}>
                        {price.mandi_name}
                      </span>
                    </div>
                  </td>

                  {/* Location */}
                  <td className="py-3 px-3 text-neutral-600">
                    {[price.district, price.state].filter(Boolean).join(", ")}
                  </td>

                  {/* Price */}
                  <td className="py-3 px-3 text-right">
                    <span className={`font-semibold tabular-nums ${isBest ? "text-primary-700" : "text-neutral-900"}`}>
                      {formatPricePerQuintal(price.price_per_quintal)}
                    </span>
                  </td>

                  {/* Range bar */}
                  <td className="py-3 px-3">
                    <div className="flex items-center gap-2">
                      <span className="text-xs text-neutral-400 tabular-nums w-12 text-right">
                        {price.min_price ? `${Math.round(price.min_price)}` : "--"}
                      </span>
                      <div className="flex-1 h-1.5 rounded-full bg-neutral-200 overflow-hidden min-w-[60px]">
                        <div
                          className={`h-full rounded-full ${isBest ? "bg-primary-500" : "bg-secondary-400"}`}
                          style={{ width: `${Math.max(barWidth, 5)}%` }}
                        />
                      </div>
                      <span className="text-xs text-neutral-400 tabular-nums w-12">
                        {price.max_price ? `${Math.round(price.max_price)}` : "--"}
                      </span>
                    </div>
                  </td>

                  {/* Trend */}
                  <td className="py-3 px-3 text-center">
                    <div className="flex items-center justify-center gap-1">
                      <TrendIcon className={`h-4 w-4 ${trend.color}`} aria-hidden="true" />
                      <span className={`text-xs ${trend.color}`}>{trend.label}</span>
                    </div>
                  </td>

                  {/* Date */}
                  <td className="py-3 px-3 text-right text-neutral-500 text-xs tabular-nums">
                    {formatDate(price.arrival_date)}
                  </td>
                </motion.tr>
              );
            })}
          </tbody>
        </table>
      </div>

      {/* Mobile card layout */}
      <div className="sm:hidden space-y-3">
        {prices.map((price, index) => {
          const isBest =
            bestAPMCName
              ? price.mandi_name === bestAPMCName
              : price === bestPrice;
          const trend = getTrendIndicator(price);
          const TrendIcon = trend.icon;

          return (
            <motion.div
              key={price.id || `${price.mandi_name}-${index}`}
              initial={{ opacity: 0, y: 8 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ delay: index * 0.05 }}
              className={[
                "rounded-lg border p-3.5",
                isBest
                  ? "border-primary-300 bg-primary-50"
                  : "border-neutral-200 bg-white",
              ].join(" ")}
            >
              {/* Header */}
              <div className="flex items-start justify-between mb-2">
                <div>
                  <div className="flex items-center gap-1.5">
                    {isBest && (
                      <StarIcon className="h-4 w-4 text-secondary-500" aria-label="Best price" />
                    )}
                    <h4 className={`text-sm font-semibold ${isBest ? "text-primary-800" : "text-neutral-800"}`}>
                      {price.mandi_name}
                    </h4>
                  </div>
                  <p className="text-xs text-neutral-500 mt-0.5">
                    {[price.district, price.state].filter(Boolean).join(", ")}
                  </p>
                </div>
                {isBest && (
                  <Badge variant="primary" size="sm">Best Price</Badge>
                )}
              </div>

              {/* Price row */}
              <div className="flex items-center justify-between">
                <span className={`text-lg font-bold tabular-nums ${isBest ? "text-primary-700" : "text-neutral-900"}`}>
                  {formatPricePerQuintal(price.price_per_quintal)}
                </span>
                <div className="flex items-center gap-1">
                  <TrendIcon className={`h-4 w-4 ${trend.color}`} aria-hidden="true" />
                  <span className={`text-xs ${trend.color}`}>{trend.label}</span>
                </div>
              </div>

              {/* Meta */}
              <div className="flex items-center justify-between mt-2 text-xs text-neutral-500">
                <span>
                  Range: {price.min_price ? `Rs ${Math.round(price.min_price)}` : "--"} - {price.max_price ? `Rs ${Math.round(price.max_price)}` : "--"}
                </span>
                <span>{formatDate(price.arrival_date)}</span>
              </div>
            </motion.div>
          );
        })}
      </div>
    </div>
  );
}

PriceTable.propTypes = {
  prices: PropTypes.arrayOf(
    PropTypes.shape({
      id: PropTypes.number,
      mandi_name: PropTypes.string,
      state: PropTypes.string,
      district: PropTypes.string,
      commodity: PropTypes.string,
      price_per_quintal: PropTypes.number,
      min_price: PropTypes.number,
      max_price: PropTypes.number,
      modal_price: PropTypes.number,
      arrival_date: PropTypes.string,
    }),
  ),
  bestAPMCName: PropTypes.string,
  className: PropTypes.string,
};

export default PriceTable;