<div class="row">
    <div class="container">

        <ol class="breadcrumb">
            <li><a href="#">العقارات</a></li>
            <li><a href="transactionsCommercialController.php?do=show">معاملات تجاري</a></li>
            <li class="active">اضافة  </li>
        </ol>


        <div class="panel panel-default panel-m">
            <div class="panel-heading">
                <h3 class="panel-title">اضافة  </h3>
            </div>
            <div class="panel-body">


                {literal}
                <form class="form-horizontal"  action="transactionsCommercialController.php"  id="formidajax" method="post"  enctype="multipart/form-data" data-dosave="?do=savedata"
                data-doprint="">
                    {/literal}

                    <div class="form-group">
                        <label for="transactionestate" class="col-lg-2 control-label">المعاملة</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="transactionestate" id="transactionestate" 
                            class="form-control"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="transactionestate" data-tableid=""
                            data-parentid="" data-parentcol="" />
                            
                        </div>
                        <label for="transactionvalue" class="col-lg-2 control-label">قيمة المعاملة</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="transactionvalue" id="transactionvalue" 
                            class="form-control floats transactionvalue"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="transactionvalue" data-tableid=""
                            data-parentid="" data-parentcol=""  />
                        </div>
                    </div>


                     <div class="form-group"  >
                       <label for="delegateid" class="col-lg-2 control-label">المندوب</label>
                        <div class="col-lg-4">
                             <input id="delegateid" type="text" name="delegateid" class="getselectjoin"  data-placeholder="اختر  "   data-table="user"  data-tableid="user.userid"
                             data-where=" and user.userid > 0"   required="required"
                            data-join=""   data-filter="" data-parent="" data-itr=""  
                            data-search="employeename,'/',userid"/> 
                        </div>
                        <label for="fullcommission" class="col-lg-2 control-label">العموله كامله</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="fullcommission" id="fullcommission" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="fullcommission" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="services" class="col-lg-2 control-label"> الخدمات</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="services" id="services" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="services" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                        <label for="netservices" class="col-lg-2 control-label"> صافي الخدمات</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="netservices" id="netservices" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="netservices" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                    </div>
            
                    <div class="form-group">
                        <label for="delegatereliefid" class="col-lg-2 control-label"> مندوب الفرجه</label>
                        <div class="col-lg-4">
                             <input id="delegatereliefid" type="text" name="delegatereliefid" class="getselectjoin"  data-placeholder="اختر  "   data-table="user"  data-tableid="user.userid"
                             data-where=" and user.userid > 0"  required="required"
                            data-join=""   data-filter="" data-parent="" data-itr=""  
                            data-search="employeename,'/',userid"/> 
                        </div>
                        <label for="reliefvalue" class="col-lg-2 control-label"> الفرجه</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="reliefvalue" id="reliefvalue" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="reliefvalue" data-tableid=""
                            data-parentid="" data-parentcol="" value="300" />
                        </div>
                    </div>


                     <div class="form-group">
                        <label for="delegatedataid" class="col-lg-2 control-label"> مندوب الداتا   </label>
                        <div class="col-lg-4">
                             <input id="delegatedataid" type="text" name="delegatedataid" class="getselectjoin"  data-placeholder="اختر  "   data-table="user"  data-tableid="user.userid"
                             data-where=" and user.userid > 0"  required="required"
                            data-join=""   data-filter="" data-parent="" data-itr=""  
                            data-search="employeename,'/',userid"/> 
                        </div>
                        <label for="datavalue" class="col-lg-2 control-label"> الداتا 10%</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="datavalue" id="datavalue" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="datavalue" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                    </div>



                    <div class="form-group">
                        <label for="delegateclearanceid" class="col-lg-2 control-label">مندوب التخليص</label>
                        <div class="col-lg-4">
                             <input id="delegateclearanceid" type="text" name="delegateclearanceid" class="getselectjoin"  data-placeholder="اختر  "   data-table="user"  data-tableid="user.userid"
                             data-where=" and user.userid > 0"  required="required"
                            data-join=""   data-filter="" data-parent="" data-itr=""  
                            data-search="employeename,'/',userid"/> 
                        </div>
                        <label for="delegateclearanceidvalue" class="col-lg-2 control-label"> التخليص 10% مندوب</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="delegateclearanceidvalue" id="delegateclearanceidvalue" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="delegateclearanceidvalue" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                    </div>



                    <div class="form-group">
                        <label for="onlinevalue" class="col-lg-2 control-label"> اونلاين</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="onlinevalue" id="onlinevalue" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="onlinevalue" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                        <label for="externalcommission" class="col-lg-2 control-label"> عموله خارجه3%</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="externalcommission" id="externalcommission" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="externalcommission" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="representativecommission" class="col-lg-2 control-label"> عموله المندوب</label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="representativecommission" id="representativecommission" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="representativecommission" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                        <label for="officecommission" class="col-lg-2 control-label"> عموله المكتب </label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="officecommission" id="officecommission" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="officecommission" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="savevalue" class="col-lg-2 control-label">  الخزنه </label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" name="savevalue" id="savevalue" 
                            class="form-control floats"  placeholder=""  
                            data-table="transactionsestates" data-tablecol="savevalue" data-tableid=""
                            data-parentid="" data-parentcol="" />
                        </div>
                    </div>



                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-4">
                            <button type="button" class="btn btn-primary pull-left buttonformajax" >حفظ الامر</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        selectedit();
            // عند تغيير قيمة المعاملة الأساسية
        $('#transactionvalue').on('change', function () {
            calculateServices();
            calculateExternalCommission();
            calculateFullCommission();
        });

        // عند تغيير قيمة الخصم relief
        $('#reliefvalue').on('change', function () {
            calculateOfficeCommission();
        });

        // عند تغيير العمولة الكاملة
        $('#fullcommission').on('change', function () {
            calculateDataValue();
        });

        // عند تغيير قيمة data
        $('#datavalue').on('change', function () {
            calculateOfficeCommission();
        });

        // عند تغيير عمولة المكتب
        $('#officecommission').on('change', function () {
            calculateDelegateClearanceValue();
        });

        // عند تغيير قيمة التخليص
        $('#delegateclearanceidvalue').on('change', function () {
            calculateRepresentativeCommission();
        });

        // عند تغيير القيم المضافة إلى العمولة الكاملة يدويًا
        $('#onlinevalue, #netservices').on('change', function () {
            calculateFullCommissionFromInputs();
        });

        // ======================
        // دوال الحسابات
        // ======================

        function calculateServices() {
            const value = Number($('#transactionvalue').val());
            $('#services').val((value * 3) / 100);
        }

        function calculateExternalCommission() {
            const value = Number($('#transactionvalue').val());
            $('#externalcommission').val((value * 3) / 100);
            $('#netservices').val((value * 3) / 100);
        }

        function calculateFullCommission() {
            const value = Number($('#transactionvalue').val());
            const full = (value * 10) / 100;
            updateInputIfChanged('#fullcommission', full);
            calculateDataValue();
        }

        function calculateDataValue() {
            const full = Number($('#fullcommission').val());
            const data = (full * 10) / 100;
            updateInputIfChanged('#datavalue', data);
            calculateOfficeCommission();
        }

        function calculateOfficeCommission() {
            const full = Number($('#fullcommission').val());
            const relief = Number($('#reliefvalue').val());
            const data = Number($('#datavalue').val());
            const result = (full - relief - data) / 2;
            updateInputIfChanged('#officecommission', result);
            calculateDelegateClearanceValue();
            calculateFullCommissionFromInputs(); // لإعادة جمع المدخلات
        }

        function calculateDelegateClearanceValue() {
            const full = Number($('#fullcommission').val());
            const relief = Number($('#reliefvalue').val());
            const data = Number($('#datavalue').val());
            const office = Number($('#officecommission').val());

            // تأكد من الحساب الصحيح (هل الضرب ×10 مقصود؟)
            const result = ((full - relief - data - office) * 10) / 100;
            updateInputIfChanged('#delegateclearanceidvalue', result);
            calculateRepresentativeCommission();
        }

        function calculateRepresentativeCommission() {
            const full = Number($('#fullcommission').val());
            const delegate = Number($('#delegateclearanceidvalue').val());
            const relief = Number($('#reliefvalue').val());
            const data = Number($('#datavalue').val());
            const office = Number($('#officecommission').val());

            const result = full - delegate - relief - data - office;
            updateInputIfChanged('#representativecommission', result);
        }

        function calculateFullCommissionFromInputs() {
            const office = Number($('#officecommission').val());
            const online = Number($('#onlinevalue').val());
            const net = Number($('#netservices').val());

            const total = office + online + net;
            updateInputIfChanged('#savevalue', total);
        }

        // ======================
        // دالة لتجنب التكرار غير الضروري
        // ======================
        function updateInputIfChanged(selector, newValue) {
            const current = Number($(selector).val());
             console.log(selector, newValue);
            if (current !== newValue && !isNaN(newValue)) {
                $(selector).val(newValue).trigger('change');
            }
        }
     });
</script>

