Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | "use client"; import { useState, useEffect, useCallback } from "react"; import { Button } from "@/components/ui"; import { Icon } from "@/components/ui/icons"; // Types interface PricingCondition { field: string; operator: string; value: string | number | string[] | number[]; } interface PricingRule { id: number; name: string; description: string | null; type: PricingRuleType; discountType: "PERCENTAGE" | "FIXED_AMOUNT"; discountValue: number; conditions: PricingCondition[]; priority: number; stackable: boolean; minimumQuantity: number | null; minimumPurchase: number | null; maximumDiscount: number | null; startDate: string | null; endDate: string | null; isActive: boolean; targetType: PricingTargetType; targetProductIds: number[]; targetCategoryIds: number[]; targetSegmentIds: number[]; abTestEnabled: boolean; abTestVariant: string | null; abTestPercent: number | null; // Read-only fields from API (not editable) applicationCount?: number; totalDiscounted?: number; createdAt?: string; updatedAt?: string; } type PricingRuleType = | "VOLUME_DISCOUNT" | "TIME_BASED" | "INVENTORY_BASED" | "SEGMENT_BASED" | "CLEARANCE" | "FIRST_TIME_BUYER" | "BUNDLE_DISCOUNT" | "COMPETITIVE" | "SEASONAL" | "DEMAND_BASED"; type PricingTargetType = | "ALL_PRODUCTS" | "SPECIFIC_PRODUCTS" | "SPECIFIC_CATEGORIES" | "CART_TOTAL"; // Type for save data - excludes read-only fields type PricingRuleSaveData = Omit< PricingRule, "id" | "applicationCount" | "totalDiscounted" | "createdAt" | "updatedAt" >; interface PricingRuleBuilderProps { rule: PricingRule | null; onSave: (data: PricingRuleSaveData) => Promise<void>; onCancel: () => void; } // Constants const RULE_TYPES: { value: PricingRuleType; label: string; description: string }[] = [ { value: "VOLUME_DISCOUNT", label: "Volume Discount", description: "Buy more, save more" }, { value: "TIME_BASED", label: "Time-Based", description: "Happy hour, flash sale" }, { value: "INVENTORY_BASED", label: "Inventory-Based", description: "Price based on stock levels" }, { value: "SEGMENT_BASED", label: "Segment-Based", description: "VIP customer pricing" }, { value: "CLEARANCE", label: "Clearance", description: "Clearance pricing" }, { value: "FIRST_TIME_BUYER", label: "First-Time Buyer", description: "New customer discount" }, { value: "BUNDLE_DISCOUNT", label: "Bundle Discount", description: "Bundle pricing" }, { value: "COMPETITIVE", label: "Competitive", description: "Match competitor pricing" }, { value: "SEASONAL", label: "Seasonal", description: "Seasonal adjustments" }, { value: "DEMAND_BASED", label: "Demand-Based", description: "Price based on demand" }, ]; const TARGET_TYPES: { value: PricingTargetType; label: string }[] = [ { value: "ALL_PRODUCTS", label: "All Products" }, { value: "SPECIFIC_PRODUCTS", label: "Specific Products" }, { value: "SPECIFIC_CATEGORIES", label: "Specific Categories" }, { value: "CART_TOTAL", label: "Cart Total" }, ]; const CONDITION_FIELDS = [ { value: "quantity", label: "Quantity", type: "number" }, { value: "cart_total", label: "Cart Total ($)", type: "number" }, { value: "stock_level", label: "Stock Level", type: "number" }, { value: "day_of_week", label: "Day of Week", type: "select" }, { value: "hour_of_day", label: "Hour of Day", type: "number" }, { value: "traffic_level", label: "Traffic Level", type: "select" }, ]; const OPERATOR_OPTIONS: Record<string, { value: string; label: string }[]> = { number: [ { value: "equals", label: "Equals" }, { value: "not_equals", label: "Does not equal" }, { value: "greater_than", label: "Greater than" }, { value: "less_than", label: "Less than" }, { value: "greater_than_or_equal", label: "Greater than or equal" }, { value: "less_than_or_equal", label: "Less than or equal" }, { value: "between", label: "Between" }, ], select: [ { value: "equals", label: "Is" }, { value: "not_equals", label: "Is not" }, { value: "in", label: "Is one of" }, { value: "not_in", label: "Is not one of" }, ]}; const DAY_OF_WEEK_OPTIONS = [ { value: "0", label: "Sunday" }, { value: "1", label: "Monday" }, { value: "2", label: "Tuesday" }, { value: "3", label: "Wednesday" }, { value: "4", label: "Thursday" }, { value: "5", label: "Friday" }, { value: "6", label: "Saturday" }, ]; const TRAFFIC_LEVEL_OPTIONS = [ { value: "low", label: "Low" }, { value: "normal", label: "Normal" }, { value: "high", label: "High" }, ]; export default function PricingRuleBuilder({ rule, onSave, onCancel}: PricingRuleBuilderProps) { // Form state const [name, setName] = useState(rule?.name || ""); const [description, setDescription] = useState(rule?.description || ""); const [type, setType] = useState<PricingRuleType>(rule?.type || "VOLUME_DISCOUNT"); const [discountType, setDiscountType] = useState<"PERCENTAGE" | "FIXED_AMOUNT">( rule?.discountType || "PERCENTAGE" ); const [discountValue, setDiscountValue] = useState(rule?.discountValue || 0); const [conditions, setConditions] = useState<PricingCondition[]>( rule?.conditions || [] ); const [priority, setPriority] = useState(rule?.priority || 0); const [stackable, setStackable] = useState(rule?.stackable || false); const [minimumQuantity, setMinimumQuantity] = useState<number | null>( rule?.minimumQuantity ?? null ); const [minimumPurchase, setMinimumPurchase] = useState<number | null>( rule?.minimumPurchase ?? null ); const [maximumDiscount, setMaximumDiscount] = useState<number | null>( rule?.maximumDiscount ?? null ); const [startDate, setStartDate] = useState( rule?.startDate ? rule.startDate.split("T")[0] : "" ); const [endDate, setEndDate] = useState( rule?.endDate ? rule.endDate.split("T")[0] : "" ); const [isActive, setIsActive] = useState(rule?.isActive ?? true); const [targetType, setTargetType] = useState<PricingTargetType>( rule?.targetType || "ALL_PRODUCTS" ); const [abTestEnabled, setAbTestEnabled] = useState(rule?.abTestEnabled || false); const [abTestPercent, setAbTestPercent] = useState<number | null>( rule?.abTestPercent ?? null ); const [saving, setSaving] = useState(false); const [previewResult, setPreviewResult] = useState<{ originalPrice: number; finalPrice: number; discountAmount: number; discountPercent: number; } | null>(null); const [previewLoading, setPreviewLoading] = useState(false); const getFieldType = (field: string): string => { const fieldOption = CONDITION_FIELDS.find((f) => f.value === field); return fieldOption?.type || "number"; }; // Condition management const addCondition = () => { setConditions([ ...conditions, { field: "quantity", operator: "greater_than", value: 1 }, ]); }; const removeCondition = (index: number) => { setConditions(conditions.filter((_, i) => i !== index)); }; const updateCondition = ( index: number, field: keyof PricingCondition, value: string | number | string[] | number[] ) => { const updated = [...conditions]; const condition = updated[index]; if (field === "field") { const fieldType = getFieldType(value as string); const operators = OPERATOR_OPTIONS[fieldType]; condition.field = value as string; condition.operator = operators[0]?.value || "equals"; condition.value = fieldType === "number" ? 0 : ""; } else if (field === "operator") { condition.operator = value as string; } else { condition.value = value; } setConditions(updated); }; // Preview pricing const previewPricing = useCallback(async () => { if (!discountValue) return; setPreviewLoading(true); try { const res = await fetch("/api/admin/pricing/preview", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ productId: 1, // Sample product quantity: minimumQuantity || 1})}); const result = await res.json(); if (result.success) { setPreviewResult(result.data.pricing); } } catch { // Silently fail preview } finally { setPreviewLoading(false); } }, [discountValue, minimumQuantity]); // Preview on value changes useEffect(() => { const timer = setTimeout(() => { if (discountValue > 0) { previewPricing(); } }, 500); return () => clearTimeout(timer); }, [discountValue, discountType, previewPricing]); const handleSubmit = async () => { if (!name.trim()) return; setSaving(true); try { await onSave({ name: name.trim(), description: description.trim() || null, type, discountType, discountValue, conditions, priority, stackable, minimumQuantity, minimumPurchase, maximumDiscount, startDate: startDate ? `${startDate}T00:00:00.000Z` : null, endDate: endDate ? `${endDate}T23:59:59.999Z` : null, isActive, targetType, targetProductIds: [], targetCategoryIds: [], targetSegmentIds: [], abTestEnabled, abTestVariant: null, abTestPercent}); } finally { setSaving(false); } }; const renderConditionValue = (condition: PricingCondition, index: number) => { const fieldType = getFieldType(condition.field); if (condition.field === "day_of_week") { if (condition.operator === "in" || condition.operator === "not_in") { return ( <select multiple value={Array.isArray(condition.value) ? condition.value.map(String) : []} onChange={(e) => { const selected = Array.from(e.target.selectedOptions).map( (opt) => opt.value ); updateCondition(index, "value", selected); }} className="flex-1 min-w-[150px] px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > {DAY_OF_WEEK_OPTIONS.map((day) => ( <option key={day.value} value={day.value}> {day.label} </option> ))} </select> ); } return ( <select value={String(condition.value)} onChange={(e) => updateCondition(index, "value", e.target.value)} className="flex-1 min-w-[150px] px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > {DAY_OF_WEEK_OPTIONS.map((day) => ( <option key={day.value} value={day.value}> {day.label} </option> ))} </select> ); } if (condition.field === "traffic_level") { return ( <select value={String(condition.value)} onChange={(e) => updateCondition(index, "value", e.target.value)} className="flex-1 min-w-[150px] px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > {TRAFFIC_LEVEL_OPTIONS.map((level) => ( <option key={level.value} value={level.value}> {level.label} </option> ))} </select> ); } if (condition.operator === "between" && fieldType === "number") { const values = Array.isArray(condition.value) ? condition.value : [0, 0]; return ( <div className="flex items-center gap-2"> <input type="number" value={Number(values[0]) || 0} onChange={(e) => updateCondition(index, "value", [ parseFloat(e.target.value) || 0, Number(values[1]) || 0, ]) } className="w-24 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> <span className="text-gray-500 dark:text-gray-400">and</span> <input type="number" value={Number(values[1]) || 0} onChange={(e) => updateCondition(index, "value", [ Number(values[0]) || 0, parseFloat(e.target.value) || 0, ]) } className="w-24 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> </div> ); } if (fieldType === "number") { return ( <input type="number" value={Number(condition.value) || 0} onChange={(e) => updateCondition(index, "value", parseFloat(e.target.value) || 0) } className="flex-1 min-w-[150px] px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> ); } return ( <input type="text" value={String(condition.value)} onChange={(e) => updateCondition(index, "value", e.target.value)} placeholder="Enter value..." className="flex-1 min-w-[150px] px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> ); }; return ( <div className="bg-white dark:bg-gray-800 rounded-lg shadow-lg"> {/* Header */} <div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700"> <h2 className="text-xl font-semibold text-gray-900 dark:text-white"> {rule ? "Edit Pricing Rule" : "Create Pricing Rule"} </h2> <p className="text-sm text-gray-600 dark:text-gray-400 mt-1"> Define dynamic pricing rules to automatically adjust prices </p> </div> <div className="p-6 space-y-6"> {/* Basic Info */} <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Rule Name * </label> <input type="text" value={name} onChange={(e) => setName(e.target.value)} placeholder="e.g., Buy 3 Get 10% Off" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-primary focus:border-transparent" /> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Rule Type </label> <select value={type} onChange={(e) => setType(e.target.value as PricingRuleType)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > {RULE_TYPES.map((rt) => ( <option key={rt.value} value={rt.value}> {rt.label} </option> ))} </select> </div> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Description </label> <textarea value={description} onChange={(e) => setDescription(e.target.value)} placeholder="Describe this pricing rule..." rows={2} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100 focus:ring-2 focus:ring-primary focus:border-transparent resize-none" /> </div> {/* Discount Settings */} <div className="border border-gray-200 dark:border-gray-700 rounded-lg p-4"> <h3 className="text-lg font-medium text-gray-900 dark:text-white mb-4"> Discount Settings </h3> <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Discount Type </label> <select value={discountType} onChange={(e) => setDiscountType(e.target.value as "PERCENTAGE" | "FIXED_AMOUNT") } className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > <option value="PERCENTAGE">Percentage (%)</option> <option value="FIXED_AMOUNT">Fixed Amount ($)</option> </select> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Discount Value </label> <div className="relative"> <input type="number" value={discountValue} onChange={(e) => setDiscountValue(parseFloat(e.target.value) || 0) } min={0} max={discountType === "PERCENTAGE" ? 100 : undefined} step={discountType === "PERCENTAGE" ? 1 : 0.01} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> <span className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 dark:text-gray-400"> {discountType === "PERCENTAGE" ? "%" : "$"} </span> </div> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Maximum Discount </label> <div className="relative"> <input type="number" value={maximumDiscount ?? ""} onChange={(e) => setMaximumDiscount( e.target.value ? parseFloat(e.target.value) : null ) } min={0} step={0.01} placeholder="No limit" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> <span className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-500 dark:text-gray-400"> $ </span> </div> </div> </div> {/* Preview */} {previewResult && ( <div className="mt-4 p-3 bg-blue-50 dark:bg-blue-900/30 rounded-lg"> <div className="flex items-center gap-2 text-sm text-blue-700 dark:text-blue-300"> <Icon name="info-circle" size={16} /> <span> Preview: ${previewResult.originalPrice.toFixed(2)} →{" "} <strong>${previewResult.finalPrice.toFixed(2)}</strong> {previewLoading && " (calculating...)"} </span> </div> </div> )} </div> {/* Requirements */} <div className="border border-gray-200 dark:border-gray-700 rounded-lg p-4"> <h3 className="text-lg font-medium text-gray-900 dark:text-white mb-4"> Requirements </h3> <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Minimum Quantity </label> <input type="number" value={minimumQuantity ?? ""} onChange={(e) => setMinimumQuantity( e.target.value ? parseInt(e.target.value) : null ) } min={1} placeholder="No minimum" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Minimum Purchase ($) </label> <input type="number" value={minimumPurchase ?? ""} onChange={(e) => setMinimumPurchase( e.target.value ? parseFloat(e.target.value) : null ) } min={0} step={0.01} placeholder="No minimum" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> </div> </div> </div> {/* Conditions */} <div className="border border-gray-200 dark:border-gray-700 rounded-lg p-4"> <div className="flex items-center justify-between mb-4"> <div> <h3 className="text-lg font-medium text-gray-900 dark:text-white"> Conditions </h3> <p className="text-sm text-gray-600 dark:text-gray-400"> Optional: Add conditions for when this rule applies </p> </div> <Button variant="ghost" size="sm" onClick={addCondition}> <Icon name="plus" size={14} className="mr-1" /> Add Condition </Button> </div> {conditions.length === 0 ? ( <p className="text-sm text-gray-500 dark:text-gray-400 italic text-center py-4"> No conditions. This rule will apply to all eligible products. </p> ) : ( <div className="space-y-3"> {conditions.map((condition, index) => ( <div key={index} className="flex items-center gap-3 flex-wrap"> <span className="text-xs font-medium text-gray-500 dark:text-gray-400 uppercase w-10"> {index === 0 ? "If" : "And"} </span> {/* Field Select */} <select value={condition.field} onChange={(e) => updateCondition(index, "field", e.target.value) } className="min-w-[140px] px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > {CONDITION_FIELDS.map((field) => ( <option key={field.value} value={field.value}> {field.label} </option> ))} </select> {/* Operator Select */} <select value={condition.operator} onChange={(e) => updateCondition(index, "operator", e.target.value) } className="min-w-[160px] px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > {OPERATOR_OPTIONS[getFieldType(condition.field)]?.map( (op) => ( <option key={op.value} value={op.value}> {op.label} </option> ) )} </select> {/* Value Input */} {renderConditionValue(condition, index)} {/* Remove */} <button onClick={() => removeCondition(index)} className="p-1.5 text-gray-400 hover:text-red-500 dark:text-gray-500 dark:hover:text-red-400" title="Remove condition" > <Icon name="x" size={16} /> </button> </div> ))} </div> )} </div> {/* Targeting */} <div className="border border-gray-200 dark:border-gray-700 rounded-lg p-4"> <h3 className="text-lg font-medium text-gray-900 dark:text-white mb-4"> Targeting </h3> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Apply To </label> <select value={targetType} onChange={(e) => setTargetType(e.target.value as PricingTargetType) } className="w-full md:w-auto px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" > {TARGET_TYPES.map((tt) => ( <option key={tt.value} value={tt.value}> {tt.label} </option> ))} </select> </div> </div> {/* Scheduling */} <div className="border border-gray-200 dark:border-gray-700 rounded-lg p-4"> <h3 className="text-lg font-medium text-gray-900 dark:text-white mb-4"> Scheduling </h3> <div className="grid grid-cols-1 md:grid-cols-2 gap-4"> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Start Date </label> <input type="date" value={startDate} onChange={(e) => setStartDate(e.target.value)} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> End Date </label> <input type="date" value={endDate} onChange={(e) => setEndDate(e.target.value)} min={startDate} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> </div> </div> </div> {/* Advanced Settings */} <div className="border border-gray-200 dark:border-gray-700 rounded-lg p-4"> <h3 className="text-lg font-medium text-gray-900 dark:text-white mb-4"> Advanced Settings </h3> <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Priority (0-100) </label> <input type="number" value={priority} onChange={(e) => setPriority(parseInt(e.target.value) || 0)} min={0} max={100} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> <p className="text-xs text-gray-500 dark:text-gray-400 mt-1"> Higher priority rules are applied first </p> </div> <div> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Status </label> <div className="flex items-center gap-4 h-[42px]"> <label className="flex items-center gap-2 cursor-pointer"> <input type="radio" checked={isActive} onChange={() => setIsActive(true)} className="text-primary focus:ring-primary" /> <span className="text-gray-700 dark:text-gray-300">Active</span> </label> <label className="flex items-center gap-2 cursor-pointer"> <input type="radio" checked={!isActive} onChange={() => setIsActive(false)} className="text-primary focus:ring-primary" /> <span className="text-gray-700 dark:text-gray-300">Inactive</span> </label> </div> </div> <div> <label className="flex items-center gap-2 cursor-pointer"> <input type="checkbox" checked={stackable} onChange={(e) => setStackable(e.target.checked)} className="rounded text-primary focus:ring-primary" /> <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> Stackable </span> </label> <p className="text-xs text-gray-500 dark:text-gray-400 mt-1 ml-6"> Can combine with other rules </p> </div> </div> {/* A/B Testing */} <div className="mt-4 pt-4 border-t border-gray-200 dark:border-gray-700"> <label className="flex items-center gap-2 cursor-pointer mb-3"> <input type="checkbox" checked={abTestEnabled} onChange={(e) => setAbTestEnabled(e.target.checked)} className="rounded text-primary focus:ring-primary" /> <span className="text-sm font-medium text-gray-700 dark:text-gray-300"> Enable A/B Testing </span> </label> {abTestEnabled && ( <div className="ml-6"> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> Traffic Percentage </label> <input type="number" value={abTestPercent ?? ""} onChange={(e) => setAbTestPercent( e.target.value ? parseInt(e.target.value) : null ) } min={0} max={100} placeholder="50" className="w-32 px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" /> <span className="ml-2 text-gray-500 dark:text-gray-400">%</span> </div> )} </div> </div> </div> {/* Footer */} <div className="px-6 py-4 border-t border-gray-200 dark:border-gray-700 flex items-center justify-end gap-3"> <Button variant="ghost" onClick={onCancel} disabled={saving}> Cancel </Button> <Button variant="primary" onClick={handleSubmit} disabled={saving || !name.trim() || discountValue <= 0} > {saving ? ( <> <Icon name="reload" size={16} className="mr-2 animate-spin" /> Saving... </> ) : ( <> <Icon name="check" size={16} className="mr-2" /> {rule ? "Update Rule" : "Create Rule"} </> )} </Button> </div> </div> ); } |