import{g as w,r as f}from"./api-DZ3cXm1m.js";$(document).ready(function(){const s=window.overviewConfig;$('meta[name="csrf-token"]').attr("content"),[].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]')).map(function(a){return new bootstrap.Tooltip(a)});async function l(a,n="GET",i=null){const o={method:n,headers:{"Content-Type":"application/json",Accept:"application/json",Authorization:`Bearer ${w()}`}};i&&(o.body=JSON.stringify(i));let e=await fetch(a,o);if(e.status===401){const t=await f();t&&(o.headers.Authorization=`Bearer ${t}`,e=await fetch(a,o))}return e}$(".btn-change-password").on("click",function(a){a.preventDefault(),$("#changePasswordForm")[0].reset(),new bootstrap.Modal("#changePasswordModal").show()}),$("#changePasswordForm").on("submit",async function(a){a.preventDefault();const n=$("#newPassword").val(),i=$("#confirmPassword").val();if(n!==i){window.showWarning(s.translations.passwordMismatch);return}const o=$(this).find('button[type="submit"]');o.prop("disabled",!0);try{const t=await(await l(s.routes.changePassword,"POST",{password:n,password_confirmation:i})).json();t.success?(window.showSuccess(s.translations.passwordChanged),bootstrap.Modal.getInstance(document.getElementById("changePasswordModal")).hide()):window.showWarning(t.message||s.translations.error)}catch{window.showWarning(s.translations.error)}finally{o.prop("disabled",!1)}}),$("#sendEmailForm").on("submit",async function(a){a.preventDefault();const n=$("#emailTemplate").val();if(!n){window.showWarning(s.translations.selectTemplate);return}const i=$(this).find('button[type="submit"]');i.prop("disabled",!0);try{const e=await(await l(s.routes.sendEmail,"POST",{template_id:n})).json();e.success?(window.showSuccess(s.translations.emailSent),$("#emailTemplate").val("")):window.showWarning(e.message||s.translations.error)}catch{window.showWarning(s.translations.error)}finally{i.prop("disabled",!1)}}),$(".btn-login-as-client").on("click",async function(a){if(a.preventDefault(),!!confirm(s.translations.confirmLoginAsClient))try{const i=await(await l(s.routes.impersonate,"POST")).json();i.success&&i.url?window.open(i.url,"_blank"):window.showWarning(i.message||s.translations.error)}catch{window.showWarning(s.translations.error)}}),$(".btn-generate-due-invoices").on("click",async function(a){if(a.preventDefault(),!confirm(s.translations.generateDueConfirm))return;const n=$(this),i=n.html();n.html('<i class="fas fa-spinner fa-spin me-2" style="width: 14px; font-size: 0.7rem;"></i>'+s.translations.generateDueProcessing);try{const e=await(await l(s.routes.generateDueInvoices,"POST",{client_id:s.clientId,send_email:!0})).json();e.success?e.invoices_created>0?(window.showSuccess(s.translations.generateDueSuccess.replace(":count",e.invoices_created)),setTimeout(()=>location.reload(),1500)):window.showWarning(s.translations.generateDueNoItems):window.showWarning(e.message||s.translations.generateDueFailed)}catch{window.showWarning(s.translations.generateDueFailed)}finally{n.html(i)}}),$(".btn-add-funds").on("click",function(a){a.preventDefault(),$("#addFundsForm")[0].reset(),$("#addFundsSendEmail").prop("checked",!0),new bootstrap.Modal("#addFundsModal").show()}),$("#addFundsForm").on("submit",async function(a){a.preventDefault();const n=parseFloat($("#addFundsAmount").val()),i=$("#addFundsCurrency").val(),o=$("#addFundsSendEmail").is(":checked");if(!n||n<=0)return;const e=$(this).find('button[type="submit"]');e.prop("disabled",!0);try{const t=await l(s.routes.addFunds,"POST",{client_id:s.clientId,amount:n,currency_id:i||null,send_email:o}),r=await t.json();if(r.success)window.showSuccess(s.translations.addFundsSuccess),bootstrap.Modal.getInstance(document.getElementById("addFundsModal")).hide(),setTimeout(()=>location.reload(),1500);else if(t.status===422&&r.errors){const d=Object.values(r.errors)[0][0];window.showWarning(d)}else window.showWarning(r.message||s.translations.addFundsFailed)}catch{window.showWarning(s.translations.addFundsFailed)}finally{e.prop("disabled",!1)}});let c=1;$(".btn-add-billable-item").on("click",function(a){a.preventDefault(),$("#billableItemForm")[0].reset(),$("#billable-items .billable-item-row:not(:first)").remove(),c=1,$("#billableSendEmail").prop("checked",!0),new bootstrap.Modal("#billableItemModal").show()}),$("#add-billable-row").on("click",function(){const a=`<div class="billable-item-row mb-3 p-2 border rounded">
            <div class="d-flex justify-content-end mb-1">
                <button type="button" class="btn btn-outline-danger btn-sm remove-billable-row"><i class="fas fa-times"></i></button>
            </div>
            <div class="mb-2">
                <label class="form-label">${s.translations.billableItemDescription||"Description"} *</label>
                <input type="text" class="form-control form-control-sm" name="bi_items[${c}][description]" required>
            </div>
            <div class="row g-2">
                <div class="col-6">
                    <label class="form-label">${s.translations.billableItemQuantity||"Quantity"}</label>
                    <input type="number" class="form-control form-control-sm" name="bi_items[${c}][quantity]" value="1" min="1" step="1">
                </div>
                <div class="col-6">
                    <label class="form-label">${s.translations.billableItemUnitPrice||"Unit Price"} *</label>
                    <input type="number" class="form-control form-control-sm" name="bi_items[${c}][unit_price]" step="0.01" min="0" required placeholder="0.00">
                </div>
            </div>
        </div>`;$("#billable-items").append(a),c++}),$(document).on("click",".remove-billable-row",function(){$(".billable-item-row").length>1&&$(this).closest(".billable-item-row").remove()}),$("#billableItemForm").on("submit",async function(a){a.preventDefault();const n=[];if($(".billable-item-row").each(function(){const e=$(this).find('input[name*="[description]"]').val(),t=$(this).find('input[name*="[quantity]"]').val(),r=$(this).find('input[name*="[unit_price]"]').val();e&&r&&n.push({description:e,quantity:parseInt(t)||1,unit_price:parseFloat(r)})}),n.length===0)return;const i=$("#billableSendEmail").is(":checked"),o=$(this).find('button[type="submit"]');o.prop("disabled",!0);try{const e=await l(s.routes.billableItem,"POST",{client_id:s.clientId,items:n,send_email:i}),t=await e.json();if(t.success)window.showSuccess(s.translations.billableItemSuccess),bootstrap.Modal.getInstance(document.getElementById("billableItemModal")).hide(),setTimeout(()=>location.reload(),1500);else if(e.status===422&&t.errors){const r=Object.values(t.errors)[0][0];window.showWarning(r)}else window.showWarning(t.message||s.translations.billableItemFailed)}catch{window.showWarning(s.translations.billableItemFailed)}finally{o.prop("disabled",!1)}});function u(){$("#billingAddressForm")[0].reset(),$("#billingAddressId").val(""),$("#billingAddressModalTitle").html('<i class="fas fa-map-marker-alt me-2"></i>'+s.translations.addBillingAddress)}$(".btn-add-billing-address").on("click",function(){u(),new bootstrap.Modal("#billingAddressModal").show()}),$(document).on("click",".btn-edit-billing-address",async function(){const a=$(this).data("address-id");u(),$("#billingAddressModalTitle").html('<i class="fas fa-map-marker-alt me-2"></i>'+s.translations.editBillingAddress);try{const n=s.routes.getBillingAddress.replace(":id",a),o=await(await l(n)).json();if(o.success){const e=o.address;$("#billingAddressId").val(e.id);const t=$("#billingAddressForm");t.find('[name="title"]').val(e.title),t.find('[name="first_name"]').val(e.first_name),t.find('[name="last_name"]').val(e.last_name),t.find('[name="company_name"]').val(e.company_name),t.find('[name="tax_id"]').val(e.tax_id),t.find('[name="email"]').val(e.email),t.find('[name="phone"]').val(e.phone),t.find('[name="country_id"]').val(e.country_id),t.find('[name="city"]').val(e.city),t.find('[name="address1"]').val(e.address1),t.find('[name="address2"]').val(e.address2),t.find('[name="state"]').val(e.state),t.find('[name="postcode"]').val(e.postcode),t.find('[name="is_default"]').prop("checked",e.is_default),new bootstrap.Modal("#billingAddressModal").show()}else window.showWarning(s.translations.error)}catch{window.showWarning(s.translations.error)}}),$("#billingAddressForm").on("submit",async function(a){a.preventDefault();const n=$("#billingAddressId").val(),i={};$(this).serializeArray().forEach(function(e){e.name!=="address_id"&&(i[e.name]=e.value)}),i.is_default=$("#addressIsDefault").is(":checked")?1:0;const o=$(this).find('button[type="submit"]');o.prop("disabled",!0);try{let e,t;n?(e=s.routes.updateBillingAddress.replace(":id",n),t="PUT"):(e=s.routes.storeBillingAddress,t="POST");const r=await l(e,t,i),d=await r.json();if(d.success)window.showSuccess(s.translations.addressSaved),bootstrap.Modal.getInstance(document.getElementById("billingAddressModal")).hide(),location.reload();else if(r.status===422&&d.errors){const m=Object.values(d.errors)[0][0];window.showWarning(m)}else window.showWarning(d.message||s.translations.error)}catch{window.showWarning(s.translations.error)}finally{o.prop("disabled",!1)}}),$(document).on("click",".btn-delete-billing-address",async function(){if(!confirm(s.translations.confirmDeleteAddress))return;const a=$(this).data("address-id"),n=$(this);n.prop("disabled",!0);try{const i=s.routes.deleteBillingAddress.replace(":id",a),e=await(await l(i,"DELETE")).json();e.success?(window.showSuccess(e.message),n.closest(".p-2.border-bottom").fadeOut(300,function(){$(this).remove()})):window.showWarning(e.message||s.translations.error)}catch{window.showWarning(s.translations.error)}finally{n.prop("disabled",!1)}}),$(document).on("click",".btn-delete-credit-card",async function(){if(!confirm(s.translations.confirmDeleteCard))return;const a=$(this).data("card-id"),n=$(this);n.prop("disabled",!0);try{const i=s.routes.deleteSavedCard.replace(":id",a),e=await(await l(i,"DELETE")).json();e.success?(window.showSuccess(s.translations.cardDeleted),n.closest(".p-2.border-bottom").fadeOut(300,function(){$(this).remove()})):window.showWarning(e.message||s.translations.error)}catch{window.showWarning(s.translations.error)}finally{n.prop("disabled",!1)}})});
