1import{d as T,f as E,c as D,a as M,r as c,j as e,L as d,b as x}from"./index-W04rzwDS.js";const C={ruleKey:"",ruleType:"coupon",label:"",customerMessage:"",adminNotes:"",couponCode:"",customerClass:"",customerId:"",isActive:!0,isPublic:!1,priority:100,stackable:!1,minSubtotal:"",minQuantity:"",discountTarget:"subtotal",discountType:"percent",discountValue:0,startsAt:"",expiresAt:"",legacyDiscountKey:"",legacyExpression:""};function P(){const{ruleKey:i}=T(),j=E(),l=i!==void 0,{isAuthenticated:N}=D(),{isAdmin:h,loading:k,scope:a}=M(),[u,w]=c.useState([]),[n,t]=c.useState(C),[g,b]=c.useState(null),[y,o]=c.useState(null),[v,r]=c.useState(!1);async function p(){if(!(!a||!h)){r(!0),o(null);try{const s=await x("GET","/admin-discounts.php",{query:{merchantId:a.merchantId,configId:a.configId}});w(s.data.rules)}catch(s){o(s instanceof Error?s.message:"Failed to load discount rules")}finally{r(!1)}}}c.useEffect(()=>{p()},[a==null?void 0:a.merchantId,a==null?void 0:a.configId,h]);function A(s){return{ruleKey:s.ruleKey,ruleType:s.ruleType,label:s.label,customerMessage:s.customerMessage??"",adminNotes:s.adminNotes??"",couponCode:s.couponCode??"",customerClass:s.customerClass??"",customerId:s.customerId===null?"":String(s.customerId),isActive:s.isActive,isPublic:s.isPublic,priority:s.priority,stackable:s.stackable,minSubtotal:s.minSubtotal===null?"":String(s.minSubtotal),minQuantity:s.minQuantity===null?"":String(s.minQuantity),discountTarget:s.discountTarget,discountType:s.discountType,discountValue:s.discountValue,startsAt:s.startsAt??"",expiresAt:s.expiresAt??"",legacyDiscountKey:s.legacyDiscountKey??"",legacyExpression:s.legacyExpression??""}}c.useEffect(()=>{if(!l)return;if(i==="new"){t(C);return}const s=u.find(m=>m.ruleKey===i);s&&t(A(s))},[l,i,u]);async function I(){if(a){r(!0),o(null),b(null);try{await x("POST","/admin-discounts.php",{body:{merchantId:a.merchantId,configId:a.configId,...n,customerId:n.customerId===""?null:Number(n.customerId),minSubtotal:n.minSubtotal===""?null:Number(n.minSubtotal),minQuantity:n.minQuantity===""?null:Number(n.minQuantity),discountValue:Number(n.discountValue)}}),b("Discount rule saved."),await p(),j(`/admin/discounts/${encodeURIComponent(n.ruleKey)}`,{replace:i==="new"})}catch(s){o(s instanceof Error?s.message:"Failed to save discount rule")}finally{r(!1)}}}async function S(s,m){if(!(!a||s.id===null)){r(!0),o(null);try{await x("POST","/admin-discounts.php",{body:{action:"set-active",merchantId:a.merchantId,configId:a.configId,id:s.id,isActive:m}}),await p()}catch(f){o(f instanceof Error?f.message:"Failed to update rule")}finally{r(!1)}}}return N?k?e.jsxs("section",{className:"panel stack",children:[e.jsx("h2",{children:"Discounts & coupons"}),e.jsx("p",{className:"muted",children:"Checking admin accessâ¦"})]}):!h||!a?e.jsxs("section",{className:"panel stack",children:[e.jsx("h2",{children:"Discounts & coupons"}),e.jsx("p",{className:"muted",children:"No admin scope for this user."})]}):e.jsxs("section",{className:"stack admin-page merchant-admin-page",children:[e.jsxs("div",{className:"page-header",children:[e.jsxs("div",{children:[e.jsx("h2",{children:l?i==="new"?"New discount or coupon":"Edit discount or coupon":"Discounts & coupons"}),e.jsxs("p",{className:"muted",children:["Rewrite-owned rules for ",a.merchantId,"/",a.configId,". ",l?"The complete rule form is shown on this dedicated page.":"Coupon codes are admin-only and are not displayed on the storefront."]})]}),e.jsxs("div",{className:"row wrap-row",children:[e.jsx(d,{className:"button-link secondary-link",to:"/admin",children:"Merchant admin"}),e.jsx(d,{className:"button-link secondary-link",to:"/admin/configsets",children:"Config sets"}),l?e.jsx(d,{className:"button-link secondary-link",to:"/admin/discounts",children:"Back to rules"}):e.jsx(d,{className:"button-link",to:"/admin/discounts/new",children:"New rule"})]})]}),g&&e.jsx("p",{className:"success",children:g}),y&&e.jsx("p",{className:"error-banner",children:y}),v?e.jsx("p",{className:"muted",children:"Workingâ¦"}):null,l?null:e.jsxs("div",{className:"panel stack",children:[e.jsx("h3",{children:"Existing rules"}),e.jsx("div",{className:"table-scroll",children:e.jsxs("table",{className:"data-table",children:[e.jsx("thead",{children:e.jsxs("tr",{children:[e.jsx("th",{children:"Rule"}),e.jsx("th",{children:"Type"}),e.jsx("th",{children:"Condition"}),e.jsx("th",{children:"Discount"}),e.jsx("th",{children:"Public"}),e.jsx("th",{children:"Active"}),e.jsx("th",{})]})}),e.jsxs("tbody",{children:[u.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:7,className:"muted",children:"No rewrite discount rules for this config."})}):null,u.map(s=>e.jsxs("tr",{children:[e.jsxs("td",{children:[e.jsx("strong",{children:s.label}),e.jsx("div",{className:"muted small-text",children:e.jsx("code",{children:s.ruleKey})}),s.ruleType==="coupon"&&s.couponCode?e.jsxs("div",{className:"muted small-text",children:["Coupon: ",e.jsx("code",{children:s.couponCode})]}):null]}),e.jsx("td",{children:s.ruleType}),e.jsxs("td",{children:[e.jsxs("div",{children:["Class: ",s.customerClass??"any"]}),e.jsxs("div",{children:["Min subtotal: ",s.minSubtotal??"â"]}),e.jsxs("div",{children:["Min qty: ",s.minQuantity??"â"]}
1)]}),e.jsxs("td",{children:[s.discountValue,s.discountType==="percent"?"%":""," on ",s.discountTarget]}),e.jsx("td",{children:s.isPublic?"Shown":"Hidden"}),e.jsx("td",{children:s.isActive?"Active":"Inactive"}),e.jsx("td",{children:e.jsxs("div",{className:"row wrap-row",children:[e.jsx(d,{className:"button-link button-secondary",to:`/admin/discounts/${encodeURIComponent(s.ruleKey)}`,children:"Edit"}),e.jsx("button",{type:"button",className:"button-secondary",onClick:()=>void S(s,!s.isActive),children:s.isActive?"Disable":"Enable"})]})})]},s.ruleKey))]})]})})]}),l?e.jsxs("div",{className:"panel stack",children:[e.jsx("h3",{children:i==="new"?"Create rule":`Edit ${i}`}),e.jsxs("div",{className:"form-grid two-col",children:[e.jsxs("label",{children:[e.jsx("span",{children:"Rule key"}),e.jsx("input",{value:n.ruleKey,onChange:s=>t({...n,ruleKey:s.target.value}),placeholder:"retailer_volume_50_over_500"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Rule type"}),e.jsxs("select",{value:n.ruleType,onChange:s=>t({...n,ruleType:s.target.value}),children:[e.jsx("option",{value:"automatic",children:"Automatic discount"}),e.jsx("option",{value:"coupon",children:"Coupon"})]})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Label"}),e.jsx("input",{value:n.label,onChange:s=>t({...n,label:s.target.value})})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Coupon code"}),e.jsx("input",{value:n.couponCode,onChange:s=>t({...n,couponCode:s.target.value}),disabled:n.ruleType!=="coupon"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Customer class"}),e.jsx("input",{value:n.customerClass,onChange:s=>t({...n,customerClass:s.target.value}),placeholder:"retailer"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Customer ID"}),e.jsx("input",{value:n.customerId,onChange:s=>t({...n,customerId:s.target.value}),inputMode:"numeric"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Minimum subtotal"}),e.jsx("input",{value:n.minSubtotal,onChange:s=>t({...n,minSubtotal:s.target.value}),inputMode:"decimal"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Minimum quantity"}),e.jsx("input",{value:n.minQuantity,onChange:s=>t({...n,minQuantity:s.target.value}),inputMode:"numeric"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Discount target"}),e.jsxs("select",{value:n.discountTarget,onChange:s=>t({...n,discountTarget:s.target.value}),children:[e.jsx("option",{value:"subtotal",children:"Subtotal"}),e.jsx("option",{value:"shipping",children:"Shipping"}),e.jsx("option",{value:"pdf_total",children:"PDF total"}),e.jsx("option",{value:"line_total",children:"Line total"})]})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Discount type"}),e.jsxs("select",{value:n.discountType,onChange:s=>t({...n,discountType:s.target.value}),children:[e.jsx("option",{value:"percent",children:"Percent"}),e.jsx("option",{value:"fixed_amount",children:"Fixed amount"}),e.jsx("option",{value:"free_shipping",children:"Free shipping"})]})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Discount value"}),e.jsx("input",{value:n.discountValue,onChange:s=>t({...n,discountValue:Number(s.target.value)}),inputMode:"decimal"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Priority"}),e.jsx("input",{value:n.priority,onChange:s=>t({...n,priority:Number(s.target.value)}),inputMode:"numeric"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Starts at"}),e.jsx("input",{value:n.startsAt,onChange:s=>t({...n,startsAt:s.target.value}),placeholder:"2026-05-01T00:00:00Z"})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Expires at"}),e.jsx("input",{value:n.expiresAt,onChange:s=>t({...n,expiresAt:s.target.value}),placeholder:"2026-12-31T23:59:59Z"})]}),e.jsxs("label",{className:"checkbox-label",children:[e.jsx("input",{type:"checkbox",checked:n.isActive,onChange:s=>t({...n,isActive:s.target.checked})})," Active"]}),e.jsxs("label",{className:"checkbox-label",children:[e.jsx("input",{type:"checkbox",checked:n.isPublic,onChange:s=>t({...n,isPublic:s.target.checked})})," Show condition/message on storefront"]}),e.jsxs("label",{className:"checkbox-label",children:[e.jsx("input",{type:"checkbox",checked:n.stackable,onChange:s=>t({...n,stackable:s.target.checked})})," Stackable"]}),e.jsxs("label",{className:"full-span",children:[e.jsx("span",{children:"Customer message"}),e.jsx("textarea",{value:n.customerMessage,onChange:s=>t({...n,customerMessage:s.target.value}),rows:3})]}),e.jsxs("label",{className:"full-span",children:[e.jsx("span",{children:"Admin notes"}),e.jsx("textarea",{value:n.adminNotes,onChange:s=>t({...n,adminNotes:s.target.value}),rows:3})]}),e.jsxs("label",{children:[e.jsx("span",{children:"Legacy discount key"}),e.jsx("input",{value:n.legacyDiscountKey,onChange:s=>t({...n,legacyDiscountKey:s.target.value})})]}),e.jsxs("label",{className:"full-span",children:[e.jsx("span",{children:"Legacy expression reference"}),e.jsx("textarea",{value:n.legacyExpression,onChange:s=>t({...n,legacyExpression:s.target.value}),rows:3})]})]}),e.jsxs("div",{className:"row wrap-row",children:[e.jsx("button",{type:"button",onClick:()=>void I(),disabled:v,children:"Save rule"}),e.jsx("button",{type:"button",className:"button-secondary",onClick:()=>j("/admin/discounts"),children:"Cancel"})]})]}):null]}):e.jsxs("section",{className:"panel stack",children:[e.jsx("h2",{children:"Discounts & coupons"}),e.jsx("p",{className:"muted",children:"Log in first."})]})}
1export{P as AdminDiscountRulesPage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.