import{r as _,b as T,j as e,c4 as V}from"./vendor-BKmmeuxg.js";import{I as d}from"./input-error-LaPr_4wd.js";import{L}from"./loading-button-BnTVAMJ7.js";import{B as f}from"./button-BpNwv2aE.js";import{D as w,a as U,b as k,c as A,d as B,e as M,f as q}from"./dialog-B2gN9MvT.js";import{I as m}from"./input-DM0goJrn.js";import{L as r}from"./label-HmIcv5Yx.js";import{S as u,a as p,b as C,c as S,d as x}from"./select-CD9o3HN1.js";import{S as H}from"./switch-DFFqeub-.js";import{o as h}from"./inertia-SZXqLvQm.js";import"./utils-DxSsnXxD.js";import"./index-WW_1X1qx.js";const Z=({title:y,handler:b,coupon:s,exams:D})=>{var g;const[n,o]=_.useState(!1),c=a=>a?a.replace(" ","T").substring(0,16):"",{data:t,setData:i,post:F,put:N,reset:j,processing:v,errors:l}=T({exam_id:(s==null?void 0:s.exam_id)||"",code:(s==null?void 0:s.code)||"",discount_type:(s==null?void 0:s.discount_type)||"percentage",discount:(s==null?void 0:s.discount)||0,valid_from:c(s==null?void 0:s.valid_from),valid_to:c(s==null?void 0:s.valid_to),is_active:(s==null?void 0:s.is_active)??!0}),E=a=>{a.preventDefault(),s?N(route("exam-coupons.update",s.id),{preserveScroll:!0,onSuccess:()=>{o(!1)}}):F(route("exam-coupons.store"),{preserveScroll:!0,onSuccess:()=>{j(),o(!1)}})},I=()=>{const a=Math.random().toString(36).substring(2,10).toUpperCase();i("code",a)};return _.useEffect(()=>{n&&s?i({exam_id:s.exam_id||"",code:s.code||"",discount_type:s.discount_type||"percentage",discount:s.discount||0,valid_from:c(s.valid_from),valid_to:c(s.valid_to),is_active:s.is_active??!0}):n||j()},[n,s]),e.jsxs(w,{open:n,onOpenChange:o,children:[e.jsx(U,{asChild:!0,children:b}),e.jsxs(k,{className:"max-w-2xl",children:[e.jsx(A,{children:e.jsx(B,{children:y})}),e.jsxs("form",{onSubmit:E,children:[e.jsx("div",{className:"space-y-4 py-4",children:e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"col-span-2",children:[e.jsx(r,{htmlFor:"code",children:"Coupon Code *"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(m,{id:"code",name:"code",value:t.code,onChange:a=>i("code",a.target.value.toUpperCase()),placeholder:"SUMMER2024",required:!0}),e.jsx(f,{type:"button",variant:"outline",onClick:I,children:e.jsx(V,{className:"h-4 w-4"})})]}),e.jsx(d,{message:l.code})]}),e.jsxs("div",{children:[e.jsx(r,{htmlFor:"discount_type",children:"Discount Type *"}),e.jsxs(u,{name:"discount_type",value:t.discount_type,onValueChange:a=>i("discount_type",a),children:[e.jsx(p,{children:e.jsx(C,{placeholder:"Select type"})}),e.jsxs(S,{children:[e.jsx(x,{value:"percentage",children:"Percentage (%)"}),e.jsx(x,{value:"fixed",children:"Fixed Amount ($)"})]})]}),e.jsx(d,{message:l.discount_type})]}),e.jsxs("div",{children:[e.jsx(r,{htmlFor:"discount",children:"Discount Value *"}),e.jsx(m,{id:"discount",name:"discount",type:"number",value:t.discount,onChange:a=>h(a,i),min:"0",step:"0.01",required:!0}),e.jsx(d,{message:l.discount})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx(r,{htmlFor:"exam_id",children:"Select Exam"}),e.jsxs(u,{name:"exam_id",value:(g=t.exam_id)==null?void 0:g.toString(),onValueChange:a=>i("exam_id",a?parseInt(a):""),children:[e.jsx(p,{children:e.jsx(C,{placeholder:"All exams (global coupon)"})}),e.jsx(S,{children:D.map(a=>e.jsx(x,{value:a.id.toString(),children:a.title},a.id))})]}),e.jsx(d,{message:l.exam_id})]}),e.jsxs("div",{children:[e.jsx(r,{htmlFor:"valid_from",children:"Valid From"}),e.jsx(m,{id:"valid_from",name:"valid_from",type:"datetime-local",value:t.valid_from,onChange:a=>h(a,i)}),e.jsx(d,{message:l.valid_from})]}),e.jsxs("div",{children:[e.jsx(r,{htmlFor:"valid_to",children:"Valid To"}),e.jsx(m,{id:"valid_to",name:"valid_to",type:"datetime-local",value:t.valid_to,onChange:a=>h(a,i)}),e.jsx(d,{message:l.valid_to})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(r,{htmlFor:"is_active",children:"Active"}),e.jsx(H,{id:"is_active",checked:t.is_active,onCheckedChange:a=>i("is_active",a)})]})]})}),e.jsxs(M,{className:"gap-2",children:[e.jsx(q,{asChild:!0,children:e.jsx(f,{type:"button",variant:"outline",children:"Cancel"})}),e.jsx(L,{loading:v,disabled:v,children:s?"Update":"Create"})]})]})]})]})};export{Z as default};
