import{jsxs as o,jsx as e}from"react/jsx-runtime";import{B as s}from"./button-BvFBRYHq.js";import{D,a as C,b,c as x,d as N,e as v}from"./dialog-BfGkTFdF.js";import{A}from"./layout-CssQHtfO.js";import{Head as B,Link as U,router as f}from"@inertiajs/react";import{format as H}from"date-fns";import{Plus as P}from"lucide-react";import{useState as r,useEffect as q}from"react";import{VoucherTable as z}from"./VoucherTable-8H3nmUBY.js";import{VoucherFormFields as G}from"./VoucherFormFields-hsRlnR0-.js";import"clsx";import{u as J}from"./useItemName-E4cepDO_.js";import"@radix-ui/react-slot";import"class-variance-authority";import"./utils-CMbmcP35.js";import"tailwind-merge";import"@radix-ui/react-dialog";import"./app-sidebar-header-BX2s-HO_.js";import"@radix-ui/react-collapsible";import"./dropdown-menu-BP80qPHw.js";import"@radix-ui/react-dropdown-menu";import"./app-logo-icon-beRg1XGs.js";import"./fonts-CqawFh47.js";import"@radix-ui/react-tooltip";import"@radix-ui/react-avatar";import"../app.js";/* empty css     */import"axios";import"sonner";import"react-dom/client";import"./toaster-DuLpwsc2.js";import"./table-BrXe3s2z.js";import"./VoucherUsageBar-iE_Hzjkx.js";import"./input-qMvM9qch.js";import"./label-DMnntKPk.js";import"@radix-ui/react-label";import"./select-DfhewM4T.js";import"@radix-ui/react-select";import"./checkbox-DXBO59MA.js";import"@radix-ui/react-checkbox";import"./MaskSelect-db8-mWU9.js";import"./TemplateSelect-DfGd1_U3.js";const K=[{title:"Dashboard",href:"/admin"},{title:"Voucher Management",href:"/admin/vouchers"}];function Be({vouchers:m,masks:g}){const[S,V]=r(m),[_,c]=r(!1),[E,l]=r(!1),[i,d]=r(null),[a,n]=r(!1),[I,p]=r({}),{itemName:k,isLoading:w,fetchItemName:y,setItemName:F}=J(),[h,u]=r({item_id:"",count:"1",proctype:"0",octet:"0",expire_date:"",mask:"0"});q(()=>{V(m)},[m]);const L=()=>{u({item_id:"",count:"1",proctype:"0",octet:"0",expire_date:"",mask:"0"}),p({}),F("")},O=t=>{d(t),u({item_id:String(t.item_id),count:String(t.count),proctype:String(t.proctype),octet:String(t.octet),expire_date:t.expire_date?H(new Date(t.expire_date),"yyyy-MM-dd"):"",mask:String(t.mask)}),c(!0),y(String(t.item_id))},T=t=>{d(t),l(!0)},M=()=>{i&&(n(!0),p({}),f.put(route("admin.vouchers.update",i.id),h,{preserveScroll:!0,onSuccess:()=>{c(!1),d(null),L()},onError:t=>{p(t)},onFinish:()=>{n(!1)}}))},j=()=>{i&&(n(!0),f.delete(route("admin.vouchers.destroy",i.id),{preserveScroll:!0,onSuccess:()=>{l(!1),d(null)},onFinish:()=>{n(!1)}}))};return o(A,{breadcrumbs:K,children:[e(B,{title:"Voucher Management"}),o("div",{className:"space-y-6",children:[o("div",{className:"flex items-center justify-between",children:[e("h1",{className:"text-2xl font-bold",children:"Voucher Management"}),e(U,{href:"/admin/vouchers/create",children:o(s,{children:[e(P,{className:"h-4 w-4"}),e("span",{className:"ml-2 hidden md:inline",children:"Create Voucher"})]})})]}),e("div",{className:"rounded-md border",children:e(z,{vouchers:S,masks:g,onEdit:O,onDelete:T,onClone:t=>{n(!0),f.post(route("admin.vouchers.clone",t.id),{},{preserveScroll:!0,onFinish:()=>{n(!1)}})},onCopyCode:async t=>{try{await navigator.clipboard.writeText(t)}catch{}},isLoading:a})})]}),e(D,{open:_,onOpenChange:c,children:o(C,{className:"sm:max-w-[500px]",children:[o(b,{children:[e(x,{children:"Edit Voucher"}),o(N,{children:["Update voucher details. Code: ",e("span",{className:"font-mono",children:i?.code})]})]}),e("div",{className:"py-4",children:e(G,{formData:h,setFormData:u,errors:I,masks:g,itemName:k,isLoadingItemName:w,onItemIdBlur:()=>y(h.item_id),showExpireDate:!0})}),o(v,{children:[e(s,{variant:"outline",onClick:()=>c(!1),disabled:a,children:"Cancel"}),e(s,{onClick:M,disabled:a,children:"Update Voucher"})]})]})}),e(D,{open:E,onOpenChange:l,children:o(C,{children:[o(b,{children:[e(x,{children:"Delete Voucher"}),o(N,{children:["Are you sure you want to delete voucher ",e("span",{className:"font-mono",children:i?.code}),"? This action cannot be undone."]})]}),o(v,{children:[e(s,{variant:"outline",onClick:()=>l(!1),disabled:a,children:"Cancel"}),e(s,{variant:"destructive",onClick:j,disabled:a,children:"Delete"})]})]})})]})}export{Be as default};
