$(document).ready(function(){const a=window.fraudReviewConfig;let c=null;$(".view-details").on("click",function(){const s=$(this).data("order-id");l(s)}),$(".approve-order").on("click",async function(){const s=$(this).data("order-id");await window.showConfirm(a.translations.approve_confirm,{confirmClass:"btn-success",icon:"question"})&&f(s,$(this))}),$(".reject-order").on("click",function(){c=$(this).data("order-id"),$("#rejectReason").val(""),window.bootstrap.Modal.getOrCreateInstance(document.getElementById("rejectReasonModal")).show()}),$("#confirmReject").on("click",function(){const s=$("#rejectReason").val().trim();if(s.length<10){window.showWarning(a.translations.rejection_reason_required);return}m(c,s)}),$(".recheck-order").on("click",function(){const s=$(this).data("order-id");p(s,$(this))});function l(s){window.bootstrap.Modal.getOrCreateInstance(document.getElementById("fraudDetailsModal")).show(),$("#fraudDetailsContent").html(`
            <div class="text-center py-5">
                <div class="spinner-border text-primary" role="status">
                    <span class="visually-hidden">${a.translations.loading}</span>
                </div>
            </div>
        `),$.ajax({url:a.routes.checkDetails.replace(":id",s),type:"GET",success:function(e){e.success&&u(e)},error:function(){$("#fraudDetailsContent").html(`
                    <div class="alert alert-danger">
                        ${a.translations.error_occurred}
                    </div>
                `)}})}function u(s){let e=`
            <div class="mb-3">
                <div class="d-flex justify-content-between align-items-center">
                    <h6>Fraud Status:</h6>
                    <span class="badge bg-${s.fraud_status==="approved"?"success":s.fraud_status==="rejected"?"danger":"warning"}">
                        ${s.fraud_status}
                    </span>
                </div>
                <div class="d-flex justify-content-between align-items-center mt-2">
                    <h6>Fraud Score:</h6>
                    <span class="badge bg-${s.fraud_score>=70?"danger":s.fraud_score>=30?"warning":"success"} fs-5">
                        ${s.fraud_score}
                    </span>
                </div>
            </div>
            <hr>
            <h6 class="mb-3">Fraud Checks:</h6>
            <div class="list-group list-group-flush">
        `;s.fraud_checks.forEach(t=>{const r={passed:"success",failed:"danger",warning:"warning",skipped:"secondary"}[t.status]||"secondary";e+=`
                <div class="list-group-item px-0">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <strong>${t.name}</strong>
                        <span class="badge bg-${r}">${t.status}</span>
                    </div>
                    <div class="d-flex justify-content-between align-items-center">
                        <small class="text-muted">${t.reason}</small>
                        <span class="badge bg-${t.risk_score>=70?"danger":t.risk_score>=40?"warning":"info"}">
                            Risk: ${t.risk_score}
                        </span>
                    </div>
                    <small class="text-muted">Checked: ${t.checked_at} (${t.execution_time_ms}ms)</small>
                </div>
            `}),e+="</div>",$("#fraudDetailsContent").html(e)}function f(s,e){const t=e.html();e.prop("disabled",!0).html('<i class="fas fa-spinner fa-spin"></i>'),$.ajax({url:a.routes.approve.replace(":id",s),type:"POST",headers:{},success:function(r){r.success&&(o("success",r.message||a.translations.approve_success),e.closest("tr").fadeOut(300,function(){$(this).remove()}))},error:function(r){var i;e.prop("disabled",!1).html(t);const n=((i=r.responseJSON)==null?void 0:i.message)||a.translations.error_occurred;o("error",n)}})}function m(s,e){const t=$(`.reject-order[data-order-id="${s}"]`),r=t.html();t.prop("disabled",!0).html('<i class="fas fa-spinner fa-spin"></i>'),$.ajax({url:a.routes.reject.replace(":id",s),type:"POST",headers:{},data:{note:e},success:function(n){var i;n.success&&((i=window.bootstrap.Modal.getInstance(document.getElementById("rejectReasonModal")))==null||i.hide(),o("success",n.message||a.translations.reject_success),t.closest("tr").fadeOut(300,function(){$(this).remove()}))},error:function(n){var d;t.prop("disabled",!1).html(r);const i=((d=n.responseJSON)==null?void 0:d.message)||a.translations.error_occurred;o("error",i)}})}function p(s,e){const t=e.html();e.prop("disabled",!0).html('<i class="fas fa-spinner fa-spin"></i>'),$.ajax({url:a.routes.recheck.replace(":id",s),type:"POST",headers:{},success:function(r){r.success&&(o("success",r.message||a.translations.recheck_success),setTimeout(()=>{window.location.reload()},1500))},error:function(r){var i;e.prop("disabled",!1).html(t);const n=((i=r.responseJSON)==null?void 0:i.message)||a.translations.error_occurred;o("error",n)}})}function o(s,e){const t=s==="success"?"alert-success":"alert-danger",r=s==="success"?"check-circle":"exclamation-circle",n=$(`
            <div class="alert ${t} alert-dismissible fade show position-fixed top-0 end-0 m-3" role="alert" style="z-index: 9999;">
                <i class="fas fa-${r} me-2"></i>${e}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        `);$("body").append(n),setTimeout(()=>{n.fadeOut(300,function(){$(this).remove()})},5e3)}});
