<style>
.deal-form-page{padding-top:20px}.deal-form-card{overflow:hidden;border:1px solid #e5e9f0!important;border-radius:15px!important;box-shadow:0 9px 30px rgba(15,23,42,.055)!important}.deal-form-intro{display:flex;align-items:center;gap:14px;padding:20px 22px;border-bottom:1px solid #e8ebf0;background:linear-gradient(135deg,#fff,#f7f9ff)}.deal-form-intro>span{display:flex;width:46px;height:46px;align-items:center;justify-content:center;border-radius:13px;color:#fff;font-size:18px;background:#3454d1;box-shadow:0 7px 16px rgba(52,84,209,.2)}.deal-form-intro h5{margin:0 0 3px;color:#253047;font-size:15px}.deal-form-intro p{margin:0;color:#8792a2;font-size:11px}.deal-form-body{padding:24px!important}.deal-form-section{margin:8px 0 18px;padding-top:19px;border-top:1px solid #edf0f4}.deal-form-section:first-child{margin-top:0;padding-top:0;border:0}.deal-form-section>div{display:flex;align-items:center;gap:9px;color:#253047;font-size:13px}.deal-form-section i{display:flex;width:28px;height:28px;align-items:center;justify-content:center;border-radius:8px;color:#3454d1;background:#eef2ff}.deal-form-section small{display:block;margin:3px 0 0 37px;color:#8792a2}.deal-form-grid{margin:0 -9px}.deal-form-grid>[class*=col-]{margin-bottom:18px;padding:0 9px}.deal-form-grid label{display:block;margin-bottom:7px;color:#4c596c;font-size:10px;font-weight:750}.deal-form-grid label span{color:#dc3545}.deal-form-grid .form-control,.deal-form-grid .form-select{min-height:43px;border-color:#dce2eb;border-radius:9px;font-size:11px;box-shadow:none}.deal-form-grid textarea.form-control{min-height:90px}.deal-form-grid .input-group-text{width:43px;justify-content:center;border-color:#dce2eb;color:#718096;background:#f8f9fc}.deal-form-grid .input-group>:last-child{border-radius:0 9px 9px 0}.deal-form-grid .form-control:focus,.deal-form-grid .form-select:focus{border-color:#3454d1;box-shadow:0 0 0 3px rgba(52,84,209,.1)}.deal-inventory-shell{padding:18px;border:1px solid #e1e6ee;border-radius:13px;background:#fbfcfe}.deal-inventory-table{min-width:1160px;margin:0}.deal-inventory-table th{padding:11px 8px;color:#68768a;font-size:9px;font-weight:800;text-transform:uppercase;background:#f3f5f9}.deal-inventory-table td{padding:8px;border-color:#e8ecf1;background:#fff}.deal-inventory-table th:first-child,.deal-inventory-table td:first-child{width:48px;text-align:center}.deal-inventory-table th:nth-child(2){width:250px}.deal-inventory-table th:last-child,.deal-inventory-table td:last-child{width:82px;text-align:center}.deal-inventory-table .form-control,.deal-inventory-table .form-select{min-height:38px;border-color:#dde3eb;border-radius:8px;font-size:10px}.row-number{display:inline-flex;width:27px;height:27px;align-items:center;justify-content:center;border-radius:8px;font-size:10px;font-weight:800;background:#eef1f6}.row-actions{display:flex;gap:5px}.row-actions button{display:flex;width:31px;height:31px;align-items:center;justify-content:center;border:0;border-radius:8px}.row-add{color:#3454d1;background:#edf1ff}.row-remove{color:#d64b4b;background:#fff0f0}.row-actions button:disabled{opacity:.35}.inventory-help{margin:12px 0;color:#8491a3;font-size:9px}.inventory-summary{width:100%;max-width:470px;margin:14px 0 0 auto;padding:15px;border:1px solid #e0e5ed;border-radius:11px;background:#fff}.inventory-summary>div{display:flex;align-items:center;justify-content:space-between;padding:8px 0;border-bottom:1px solid #edf0f4;color:#677589;font-size:10px;font-weight:700}.inventory-summary input{width:150px;border:0;text-align:right;outline:0;background:transparent}.inventory-summary>div:nth-child(2) input{color:#d44a4a}.inventory-summary>div:nth-child(3) input{color:#bd7512}.inventory-summary .grand{margin:5px -15px -15px;padding:14px 15px;border:0;border-radius:0 0 10px 10px;color:#fff;background:linear-gradient(135deg,#263d9f,#4e6ee1)}.inventory-summary .grand input{color:#fff;font-size:16px;font-weight:800}.deal-form-actions{position:sticky;bottom:0;z-index:9;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:14px 20px;border-top:1px solid #e5e9f0;background:rgba(255,255,255,.97);backdrop-filter:blur(8px)}.deal-form-actions>div{display:flex;gap:7px;flex-wrap:wrap}.deal-form-actions .btn{display:inline-flex;min-height:39px;align-items:center;gap:6px;border-radius:9px;font-size:10px;font-weight:700}.deal-form-page .page-header-title h5{margin-bottom:2px}.deal-form-page .page-header .btn{border-radius:9px}
.deal-form-grid .input-group .select2-container{flex:1 1 auto;width:1%!important}.deal-form-grid .select2-container .select2-selection--multiple{min-height:43px;padding:4px 8px;border-color:#dce2eb;border-radius:0 9px 9px 0}.deal-form-grid .select2-selection__choice{border:0!important;border-radius:6px!important;color:#3454d1;background:#eef2ff!important;font-size:10px}
@media(max-width:767.98px){.deal-form-page{padding-top:12px}.deal-form-card{border-radius:11px!important}.deal-form-body{padding:17px!important}.deal-form-actions{align-items:stretch;flex-direction:column}.deal-form-actions>div{display:grid;grid-template-columns:repeat(2,1fr)}.deal-form-actions .btn{justify-content:center}.save-actions{order:-1}}
</style>
<script>
document.addEventListener('DOMContentLoaded',()=>{const body=document.querySelector('#dealInventory tbody');if(!body)return;const discount=document.querySelector('#total_discount'),tax=document.querySelector('#total_tax'),probability=document.querySelector('#closeProbability'),tradeType=document.querySelector('#tradeType'),template=document.querySelector('#documentTemplate');let manualDiscount={{ old('total_discount')!==null?'true':'false' }},manualTax={{ old('total_tax')!==null?'true':'false' }};const renumber=()=>{const rows=[...body.querySelectorAll('.inventory-line')];rows.forEach((r,i)=>{r.querySelector('.row-number').textContent=i+1;r.querySelector('.row-remove').disabled=rows.length===1});};const calculate=()=>{let sub=0,rowDiscount=0,rowTax=0;body.querySelectorAll('.inventory-line').forEach(r=>{const q=Math.max(0,+r.querySelector('.qty').value||0),p=Math.max(0,+r.querySelector('.price').value||0),d=Math.min(100,Math.max(0,+r.querySelector('.discount').value||0)),t=Math.min(100,Math.max(0,+r.querySelector('.tax').value||0)),base=q*p,disc=base*d/100,taxAmount=(base-disc)*t/100,total=base-disc+taxAmount;sub+=base;rowDiscount+=disc;rowTax+=taxAmount;r.querySelector('.line-total').value=total.toFixed(2)});if(!manualDiscount)discount.value=rowDiscount.toFixed(2);if(!manualTax)tax.value=rowTax.toFixed(2);const grand=Math.max(0,sub-(+discount.value||0)+(+tax.value||0));document.querySelector('#sub_total').value=sub.toFixed(2);document.querySelector('#grand_total').value=grand.toFixed(2);document.querySelector('#total_amount').value=grand.toFixed(2);document.querySelector('#dealValue').value=grand.toFixed(2);document.querySelector('#forecastValue').value=(grand*(+probability.value||0)/100).toFixed(2)};const clone=r=>{const n=r.cloneNode(true);n.querySelectorAll('select').forEach(x=>x.selectedIndex=0);n.querySelectorAll('input').forEach(x=>x.value=x.classList.contains('qty')?'1':x.classList.contains('discount')||x.classList.contains('tax')||x.classList.contains('line-total')?'0':'');return n};body.addEventListener('click',e=>{const add=e.target.closest('.row-add'),remove=e.target.closest('.row-remove');if(add){const source=add.closest('tr'),row=clone(source);source.after(row);renumber();calculate();row.querySelector('.product-select').focus()}if(remove&&body.children.length>1){remove.closest('tr').remove();renumber();calculate()}});body.addEventListener('input',calculate);body.addEventListener('change',calculate);discount.addEventListener('input',()=>{manualDiscount=true;calculate()});tax.addEventListener('input',()=>{manualTax=true;calculate()});probability.addEventListener('input',calculate);const filterTemplates=()=>{[...template.options].forEach(o=>{if(!o.value)return;o.hidden=Boolean(tradeType.value)&&!['all',tradeType.value].includes(o.dataset.type)});if(template.selectedOptions[0]?.hidden)template.value=''};tradeType.addEventListener('change',filterTemplates);renumber();filterTemplates();calculate()});
document.addEventListener('DOMContentLoaded',()=>{const contacts=document.querySelector('#dealContacts'),owner=document.querySelector('#dealOwner'),primary=document.querySelector('#primaryContact');if(!contacts||!owner||!primary)return;const syncPrimary=()=>{const selected=[...contacts.selectedOptions].map(option=>option.value);if(!selected.includes(primary.value))primary.value=selected[0]||''};contacts.addEventListener('change',syncPrimary);if(window.jQuery&&jQuery.fn.select2){const field=jQuery(contacts);field.select2({width:'100%',closeOnSelect:false,placeholder:'Search Contacts by name',minimumInputLength:0,ajax:{url:@json(route('workflow.related-records')),dataType:'json',delay:250,data:params=>({type:'contact',q:params.term||'',owner_id:owner.value}),processResults:items=>({results:items.map(item=>({...item,text:item.text+' · Owner: '+item.owner_name}))}),cache:false}});jQuery(owner).on('change.dealContacts',()=>{field.val(null).trigger('change');primary.value=''});}syncPrimary()});
</script>
