

<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="#">نظام العقارات والوحدات </a></li>
            <li><a href="realestateunitsrenters.php?do=show">  تأجير الوحدات للمستأجرين   </a></li>
            {if $editdata->del lt 2}
            <li class="active">تعديل  </li> 
            {else}
            <li class="active">تفاصيل  </li> 
            {/if}
        </ol>
        <div class="panel panel-default panel-m">
            <div class="panel-heading">
                {if $editdata->del lt 2}
                <h3 class="panel-title">تعديل  </h3>
                {else}
                <h3 class="panel-title">تفاصيل  </h3> 
                {/if}
            </div>
            <div class="panel-body">


                {literal}
                 <form class="form-horizontal"  action="realestateunitsrenters.php"  id="submitformajax"  method="post"  enctype="multipart/form-data" data-dosave="?do=savedata" data-direction="?do=show" data-print="">

                    {/literal}


           
                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">الماك</label>
                        <div class="col-lg-4">
                            <input type="text" name="clientid" class="wdt100 select2client" id="select2client" data-placeholder="اختر " />
                            <input type="hidden" data-table="select2client" data-id="{$client.clientid}" data-text="{$client.clientname} / {$client.clientphone}" 
                            class="selectedit" />
                         </div>
                    </div>

                    
                     <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">العقار</label>
                        <div class="col-lg-4">
                            <input type="text" name="realestateid"  class="wdt100 select2realestates" id="select2realestates" data-placeholder="اختر " />
                            <input type="hidden" data-table="select2realestates" data-id="{$realestate->id}" data-text="{$realestate->realestatename}" 
                            class="selectedit" />
                         </div>
                    </div>


                      <div class="form-group">
                         <label for="txtName" class="col-lg-2 control-label">الوحدات</label>
                        <div class="col-lg-4">
                            <input type="text" name="realestateunitid" class="wdt100" id="select2realestatesunits" data-placeholder="اختر " />
                            <input type="hidden" data-table="select2realestatesunits" data-id="{$realestateunit->id}" data-text="{$realestateunit->unitname} / {$realestateunit->unitarea}" 
                            class="selectedit" />
                         </div>
                    </div>


                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">قيمه الايجار </label>
                        <div class="col-lg-4">
                            <input    required="required" id="valuerents"   type="text" name="valuerents" 
                            class="form-control installmentsorvaluerents nummbers"  placeholder="قيمه الايجار" value="{$editdata->valuerents}"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">المدفوع مقدمه </label>
                        <div class="col-lg-4">
                            <input    required="required" id="advancepayment"   type="text" name="advancepayment" class="form-control installmentsorvaluerents nummbers"  placeholder="المدفوع مقدمه" value="{$editdata->advancepayment}"/>
                        </div>
                    </div>

                     <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">المدفوع  </label>
                        <div class="col-lg-4">
                             <input  readonly  id="totalpaid"   type="text" name="totalpaid" 
                            class="form-control installmentsorvaluerents nummbers" value="{$editdata->totalpaid}" placeholder="المدفوع " />
                        </div>
                    </div>

                      <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">المتبقى  </label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" 
                            class="form-control nummbers" id="residual"  placeholder="المتبقى" value="{$editdata->valuerents - $editdata->totalpaid}" readonly />
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">عدد الاقساط </label>
                        <div class="col-lg-4">
                            <input    required="required" id="numberinstallments"   type="text" name="numberinstallments" class="form-control editnumberinstallments installmentsorvaluerents floats "  data-old="{$editdata->numberinstallments}" placeholder="عدد الاقساط "  value="{$editdata->numberinstallments}"/>
                            <input   id="oldnumberinstallments"   type="hidden" data-cold="{$realestateunitpaidinstallments|@count}"/>
                        </div>
                    </div>

                     <div class="form-group">
                        <div class="col-md-12">
                             <ul >
                                 <li  style="list-style-type: none;">
                                   <div class="col-lg-4">
                                       <label for="txtName" class="control-label">الاقسط</label>
                                   </div>
                                    <div class="col-lg-4">
                                     <label for="txtName" class="control-label">المدفوع</label>
                                   </div>
                                    <div class="col-lg-4">
                                     <label for="txtName" class="control-label"> التاريخ</label>
                                   </div>
                                </li>
                            </ul>                             
                            <ul>
                                 {assign var=itr value=1}
                                 {foreach $realestateunitpaidinstallments as $paidinstallment}
                                  <input type="hidden" name="realestateunitpaidinstallmentid_{$itr}" value="{$paidinstallment->id}">
                                  <li  style="list-style-type: none;">
                                    <div class="col-lg-4">
                                         <input  required="required"    type="text" name="valuerent_{$itr}" class="form-control   checktotalpaid" placeholder="* اسم  الوحده " id="valuerent_{$itr}" value="{$paidinstallment->valuerent}" data-itr="{$itr}"/>
                                    </div>
                                        <div class="col-lg-4">
                                           <input      type="text" name="totalpaid_{$itr}" class="form-control checktotalpaid alltotalpaid" placeholder="المدفوع " data-valuerentold="{$paidinstallment->valuerent}" id="totalpaid_{$itr}" value="{$paidinstallment->totalpaid}" data-itr="{$itr}"/>
                                    </div>
                                    <div class="col-lg-3">
                                         <input      type="text" name="installmentdate_{$itr}" class="form-control  newdatepicker" placeholder="تاريخ الاقسط " autocomplete="off"   data-date-format="yyyy-mm-dd" value="{$paidinstallment->installmentdate}"  />
                                    </div>

                                    <div class="col-lg-1 pull-right">
                                        {if $paidinstallment->totalpaid gt 0}
                                        <a href="javascript:;"  data-id="{$paidinstallment->id}" data-edit="1" data-controller="realestateunitsrenters" data-container="realestateunitpaidinstallments" class="btn btn-xs red pull-right removeappendn2"> x
                                        </a>
                                        {/if}
                                    </div>
                                </li>
                                {capture assign=itr}{$itr+1}{/capture}
                                {/foreach}
                                
                            </ul>   
                             <ul id="installmentsorvaluerents">
                             </ul> 
                        </div>
                    </div>

                    <input type="hidden" name="id" id="editid" value="{$editdata->id}" />
                    <input type="hidden" class="deldata" value="{$editdata->del}" />
                    {if $editdata->del lt 2}
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-4">
                             <button type="button" class="btn btn-primary pull-left submitformajax" >تعديل الامر</button>
                        </div>
                    </div>
                    {/if}




                </form>
            </div>
        </div>




    </div>
</div>



{literal}

<script type="text/javascript">

    $(document).ready(function () {

       
         var formatResult;
        formatResult = function (data) {
            return  data.text;
        }


         $("#select2client").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2client",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        searchTerm: term,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });

    
        $("#select2realestates").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2realestates",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        searchTerm: term,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });


        $("#select2realestatesunits").select2({
            placeholder: "الوحده",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2realestatesunits",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    var select2realestates = Number($('#select2realestates').val());
                     if (!select2realestates) {
                        alert('يجب اختيار العقار اولا');
                        return false;
                     }
                    return {
                        searchTerm: term,
                        realestateid: select2realestates,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });


         $('body').on('change', '.editnumberinstallments', function () {
            var editid =   Number($('#editid').val());
            $.post('realestateunitsrenters.php?do=editid', {id: editid});
             // window.location.reload();
        });


        $('body').on('change', '.installmentsorvaluerents', function () {
            var valuerents = Number($('#valuerents').val()) - Number($('#advancepayment').val());
            var advancepayment = Number($('#advancepayment').val());
            var numberinstallments =  Number($('#numberinstallments').val());
            // var numberinstallmentold =   Number($('#oldnumberinstallments').attr('data-cold'));
            if (numberinstallments < 1) {
                numberinstallments = 1;
            }

            var alltotalpaid = 0;
            var numberinstallmentold = 0; 
            var valuerentold = 0;
            $(".alltotalpaid").each(function( ) {
                var alltotalpaidonw = Number($(this).val());
                if (alltotalpaidonw == 0) {
                     $(this).parent().parent().remove();
                }else{
                 valuerentold += Number($(this).attr('data-valuerentold'));
                 numberinstallmentold += 1;
                 alltotalpaid += alltotalpaidonw;
                }
                
            });            
            

            $("#installmentsorvaluerents").html('');
            var valuerent = ((valuerents - valuerentold) / (numberinstallments - numberinstallmentold)).toFixed(2);   
            $('.allvaluerent').val(valuerent);    
            text ='';
            for (let i = numberinstallmentold + 1; i <= (numberinstallments  - (numberinstallmentold - 1)); i++) {
              $("#installmentsorvaluerents").append(
                '<li  style="list-style-type: none;">'
                   +'<div class="col-lg-4">'
                    +'   <input  required="required"    type="text" name="valuerent_'+i+'" class="form-control allvaluerent checktotalpaid' 
                    +'" placeholder="* اسم  الوحده " id="valuerent_'+i+'" value="'+valuerent+'" data-itr="'+i+'"/>'
                    +'</div>'
                    +'<div class="col-lg-4">'
                    +'   <input      type="text" name="totalpaid_'+i+'" data-valuerentold="" class="form-control checktotalpaid alltotalpaid'
                    +'" placeholder="المدفوع "  id="totalpaid_'+i+'" value="" data-itr="'+i+'"/>'
                    +'</div>'
                    +'<div class="col-lg-4">'
                    +' <input      type="text" name="installmentdate_'+i+'" class="form-control  newdatepicker" '
                    +'placeholder="تاريخ الاقسط " autocomplete="off"   data-date-format="yyyy-mm-dd" value="'+dateWithMonthsDelay((i+numberinstallmentold)-1)+'"  />'
                    +'</div>'
                    +'</li>');
            }
            var allvaluerent = 0;
            $(".allvaluerent").each(function() {
                allvaluerent += Number($(this).val());
                var itrs = $(this).attr('data-itr');
                var valuerents = Number($('#valuerent_'+itrs).val());
                var totalpaids = Number($('#totalpaid_'+itrs).val());
                if (totalpaids > valuerents) {
                    $('#totalpaid_'+itrs).val(valuerents).trigger('change');
                }
            });


           
           
            $('#totalpaid').val(alltotalpaid + advancepayment);
            if ((valuerents - valuerentold) != allvaluerent) {
                $('.submitformajax').prop("disabled", true);
                $('.submitformajax').css("background", "red");
            }else{
                $('.submitformajax').prop("disabled", false); 
                $('.submitformajax').css("background", "blue");
            }
            newdatepicker();


        });
        $('body').on('change', '.allvaluerent', function () {
            var valuerents = Number($('#valuerents').val()) - Number($('#advancepayment').val());
            var allvaluerent = 0;
            $(".allvaluerent").each(function( ) {
                allvaluerent += Number($(this).val());
            });
            if (valuerents != allvaluerent) {
                $('.submitformajax').prop("disabled", true);
                $('.submitformajax').css("background", "red");
            }else{
                $('.submitformajax').prop("disabled", false); 
                $('.submitformajax').css("background", "blue");
            }
        });

         $('body').on('change', '.checktotalpaid', function () {
            var itr = $(this).attr('data-itr');
            var valuerent = Number($('#valuerent_'+itr).val());
            var totalpaid = Number($('#totalpaid_'+itr).val());
            if (totalpaid > valuerent) {
                $(this).val(valuerent).trigger('change');
            }
        });

                  // $("#numberinstallments").trigger("change");





        $('body').on('change', '.alltotalpaid', function () {
            var alltotalpaid = 0;
            $(".alltotalpaid").each(function( ) {
                alltotalpaid += Number($(this).val());
            });
            $('#totalpaid').val(alltotalpaid + Number($('#advancepayment').val()));
        });

        function dateWithMonthsDelay (months) {  
          const date = new Date();
          return new Date(date.setMonth(date.getMonth() + months)).toISOString().slice(0, 10);
        }



        function newdatepicker() {
            $('.newdatepicker').datepicker({
                orientation: "left",
                format: "yyyy-mm-dd",
                autoclose: true
            });
        }

        $('body').on('click', '.removeappendn2', function () {
            var controller = $(this).attr('data-controller');
            var container = $(this).attr('data-container');
            var editcheck = $(this).attr('data-edit');
            var id = $(this).attr('data-id');
            var thisis = $(this);
            if (confirm('هل انت متاكد من الحذف ؟')) {
                if (editcheck == 1) {
                    $.post(controller+'.php?do=removeappend', {id: id, container: container, editcheck: editcheck}, function (data) {
                        if (data == 1) {
                            window.location.reload();
                        }else{
                             alert('حدث خطأ أثناء التنفيذ');
                        }
                    });
                } else {
                    $('#r'+container+editcheck+id).remove();
                    window.location.reload();
                }
            } else {
               alert('لماذا ضغطت إلغاء؟ يجب أن تكون قد أكدت');
            }
        });


        deldata();
        selectedit2();



});
    
</script>
{/literal}



<!-- 

<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="#">نظام العقارات والوحدات </a></li>
            <li><a href="realestateunitsrenters.php?do=show">  تأجير الوحدات للمستأجرين   </a></li>
            {if $editdata->del lt 2}
            <li class="active">تعديل  </li> 
            {else}
            <li class="active">تفاصيل  </li> 
            {/if}
        </ol>
        <div class="panel panel-default panel-m">
            <div class="panel-heading">
                {if $editdata->del lt 2}
                <h3 class="panel-title">تعديل  </h3>
                {else}
                <h3 class="panel-title">تفاصيل  </h3> 
                {/if}
            </div>
            <div class="panel-body">


                {literal}
                 <form class="form-horizontal"  action="realestateunitsrenters.php"  id="submitformajax"  method="post"  enctype="multipart/form-data" data-dosave="?do=savedata" data-direction="?do=show" data-print="">

                    {/literal}

                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">الماك</label>
                        <div class="col-lg-4">
                            <input type="text" name="clientid" class="wdt100 select2client" id="select2client" data-placeholder="اختر " />
                            <input type="hidden" data-table="select2client" data-id="{$client.clientid}" data-text="{$client.clientname} / {$client.clientphone}" 
                            class="selectedit" />
                         </div>
                    </div>

                     <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">العقار</label>
                        <div class="col-lg-4">
                            <input type="text" name="realestateid"  class="wdt100 select2realestates" id="select2realestates" data-placeholder="اختر " />
                            <input type="hidden" data-table="select2realestates" data-id="{$realestate->id}" data-text="{$realestate->realestatename}" 
                            class="selectedit" />
                         </div>
                    </div>


                      <div class="form-group">
                         <label for="txtName" class="col-lg-2 control-label">الوحدات</label>
                        <div class="col-lg-4">
                            <input type="text" name="realestateunitid" class="wdt100" id="select2realestatesunits" data-placeholder="اختر " />
                            <input type="hidden" data-table="select2realestatesunits" data-id="{$realestateunit->id}" data-text="{$realestateunit->unitname} / {$realestateunit->unitarea}" 
                            class="selectedit" />
                         </div>
                    </div>




                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">قيمه الايجار </label>
                        <div class="col-lg-4">
                            <input    required="required" id="valuerents"   type="text" name="valuerents" 
                            class="form-control installmentsorvaluerents nummbers"  placeholder="قيمه الايجار" value="{$editdata->valuerents}"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">المدفوع مقدمه </label>
                        <div class="col-lg-4">
                            <input    required="required" id="advancepayment"   type="text" name="advancepayment" class="form-control installmentsorvaluerents nummbers"  placeholder="المدفوع مقدمه" value="{$editdata->advancepayment}"/>
                        </div>
                    </div>

                     <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">المدفوع  </label>
                        <div class="col-lg-4">
                             <input  readonly  id="totalpaid"   type="text" name="totalpaid" 
                            class="form-control installmentsorvaluerents nummbers" value="{$editdata->totalpaid}" placeholder="المدفوع " />
                        </div>
                    </div>

                      <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">المتبقى  </label>
                        <div class="col-lg-4">
                            <input    required="required"    type="text" 
                            class="form-control nummbers" id="residual"  placeholder="المتبقى" value="{$editdata->valuerents - $editdata->totalpaid}" readonly />
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="txtName" class="col-lg-2 control-label">عدد الاقساط </label>
                        <div class="col-lg-4">
                            <input    required="required" id="numberinstallments"   type="text" name="numberinstallments" class="form-control installmentsorvaluerents floats"   placeholder="عدد الاقساط "  value="{$editdata->numberinstallments}"/>
                            <input   id="oldnumberinstallments"   type="hidden" data-cold="{$realestateunitpaidinstallments|@count}"/>
                        </div>
                    </div>

                     <div class="form-group">
                        <div class="col-md-12">
                             <ul >
                                 <li  style="list-style-type: none;">
                                   <div class="col-lg-4">
                                       <label for="txtName" class="control-label">الاقسط</label>
                                   </div>
                                    <div class="col-lg-4">
                                     <label for="txtName" class="control-label">المدفوع</label>
                                   </div>
                                    <div class="col-lg-4">
                                     <label for="txtName" class="control-label"> التاريخ</label>
                                   </div>
                                </li>
                            </ul>                             
                            <ul>
                                 {assign var=itr value=1}
                                 {foreach $realestateunitpaidinstallments as $paidinstallment}
                                  <input type="hidden" name="realestateunitpaidinstallmentid_{$itr}" value="{$paidinstallment->id}">
                                  <li  style="list-style-type: none;">
                                    <div class="col-lg-4">
                                         <input  required="required"    type="text" name="valuerent_{$itr}" class="form-control allvaluerent checktotalpaid" placeholder="* اسم  الوحده " id="valuerent_{$itr}" value="{$paidinstallment->valuerent}" data-itr="{$itr}"/>
                                    </div>
                                        <div class="col-lg-4">
                                           <input      type="text" name="totalpaid_{$itr}" class="form-control checktotalpaid alltotalpaid" placeholder="المدفوع "  id="totalpaid_{$itr}" value="{$paidinstallment->totalpaid}" data-itr="{$itr}"/>
                                    </div>
                                    <div class="col-lg-3">
                                         <input      type="text" name="installmentdate_{$itr}" class="form-control  newdatepicker" placeholder="تاريخ الاقسط " autocomplete="off"   data-date-format="yyyy-mm-dd" value="{$paidinstallment->installmentdate}"  />
                                    </div>

                                    <div class="col-lg-1 pull-right">
                                        {if $paidinstallment->totalpaid gt 0}
                                        <a href="javascript:;"  data-id="{$paidinstallment->id}" data-edit="1" data-controller="realestateunitsrenters" data-container="realestateunitpaidinstallments" class="btn btn-xs red pull-right removeappendn2"> x
                                        </a>
                                        {/if}
                                    </div>
                                </li>
                                {capture assign=itr}{$itr+1}{/capture}
                                {/foreach}
                                
                            </ul>   
                             <ul id="installmentsorvaluerents">
                             </ul> 
                        </div>
                    </div>

                    <input type="hidden" name="id" id="editid" value="{$editdata->id}" />
                    <input type="hidden" class="deldata" value="{$editdata->del}" />
                    {if $editdata->del lt 2}
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-4">
                             <button type="button" class="btn btn-primary pull-left submitformajax" >تعديل الامر</button>
                        </div>
                    </div>
                    {/if}




                </form>
            </div>
        </div>




    </div>
</div>



{literal}

<script type="text/javascript">

    $(document).ready(function () {


         var formatResult;
        formatResult = function (data) {
            return  data.text;
        }


         $("#select2client").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2client",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        searchTerm: term,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });

        $("#select2realestates").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2realestates",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    return {
                        searchTerm: term,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });


        $("#select2realestatesunits").select2({
            placeholder: "الوحده",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "realestateunits.php?do=select2realestatesunits",
                type: "post",
                dataType: 'json',
                data: function (term, page) {
                    var select2realestates = Number($('#select2realestates').val());
                     if (!select2realestates) {
                        alert('يجب اختيار العقار اولا');
                        return false;
                     }
                    return {
                        searchTerm: term,
                        realestateid: select2realestates,
                    };
                },
                results: function (data) {
                    return {results: data};
                }
            },
            formatResult: formatResult,
            initSelection: function (element, callback) {
                var item = element.val().split(':');
                var data = {id: item[0], text: item[1]};
                callback(data);
            }
        });


        $('body').on('change', '.installmentsorvaluerents', function () {
            var valuerents = Number($('#valuerents').val()) - Number($('#advancepayment').val());
            var advancepayment = Number($('#advancepayment').val());
            var numberinstallments =  Number($('#numberinstallments').val());
            var numberinstallmentold =   Number($('#oldnumberinstallments').attr('data-cold'));
            if (numberinstallments < 1) {
                numberinstallments = 1;
            }


            

            $("#installmentsorvaluerents").html('');
            var valuerent = (valuerents / numberinstallments).toFixed(2);   
            $('.allvaluerent').val(valuerent);    
            text ='';
            for (let i = 1; i <= (numberinstallments - numberinstallmentold); i++) {
              $("#installmentsorvaluerents").append(
                '<li  style="list-style-type: none;">'
                   +'<div class="col-lg-4">'
                    +'   <input  required="required"    type="text" name="valuerent_'+i+'" class="form-control allvaluerent checktotalpaid' 
                    +'" placeholder="* اسم  الوحده " id="valuerent_'+i+'" value="'+valuerent+'" data-itr="'+i+'"/>'
                    +'</div>'
                    +'<div class="col-lg-4">'
                    +'   <input      type="text" name="totalpaid_'+i+'" class="form-control checktotalpaid alltotalpaid'
                    +'" placeholder="المدفوع "  id="totalpaid_'+i+'" value="" data-itr="'+i+'"/>'
                    +'</div>'
                    +'<div class="col-lg-4">'
                    +' <input      type="text" name="installmentdate_'+i+'" class="form-control  newdatepicker" '
                    +'placeholder="تاريخ الاقسط " autocomplete="off"   data-date-format="yyyy-mm-dd" value="'+dateWithMonthsDelay((i+numberinstallmentold)-1)+'"  />'
                    +'</div>'
                    +'</li>');
            }
            var allvaluerent = 0;
            $(".allvaluerent").each(function( ) {
                allvaluerent += Number($(this).val());
                var itrs = $(this).attr('data-itr');
                var valuerents = Number($('#valuerent_'+itrs).val());
                var totalpaids = Number($('#totalpaid_'+itrs).val());
                if (totalpaids > valuerents) {
                    $('#totalpaid_'+itrs).val(valuerents).trigger('change');
                }
            });


            var alltotalpaid = 0;
            $(".alltotalpaid").each(function( ) {
                alltotalpaid += Number($(this).val());
            });

           
            $('#totalpaid').val(alltotalpaid + advancepayment).toFixed(2);
            if (valuerents != allvaluerent) {
                $('.submitformajax').prop("disabled", true);
                $('.submitformajax').css("background", "red");
            }else{
                $('.submitformajax').prop("disabled", false); 
                $('.submitformajax').css("background", "blue");
            }
            newdatepicker();


        });
        $('body').on('change', '.allvaluerent', function () {
            var valuerents = Number($('#valuerents').val()) - Number($('#advancepayment').val());
            var allvaluerent = 0;
            $(".allvaluerent").each(function( ) {
                allvaluerent += Number($(this).val());
            });
            if (valuerents != allvaluerent) {
                $('.submitformajax').prop("disabled", true);
                $('.submitformajax').css("background", "red");
            }else{
                $('.submitformajax').prop("disabled", false); 
                $('.submitformajax').css("background", "blue");
            }
        });

         $('body').on('change', '.checktotalpaid', function () {
            var itr = $(this).attr('data-itr');
            var valuerent = Number($('#valuerent_'+itr).val());
            var totalpaid = Number($('#totalpaid_'+itr).val());
            if (totalpaid > valuerent) {
                $(this).val(valuerent).trigger('change');
            }
        });




        $('body').on('change', '.alltotalpaid', function () {
            var alltotalpaid = 0;
            $(".alltotalpaid").each(function( ) {
                alltotalpaid += Number($(this).val());
            });
            $('#totalpaid').val(alltotalpaid + Number($('#advancepayment').val()));
        });

        function dateWithMonthsDelay (months) {  
          const date = new Date();
          return new Date(date.setMonth(date.getMonth() + months)).toISOString().slice(0, 10);
        }



        function newdatepicker() {
            $('.newdatepicker').datepicker({
                orientation: "left",
                format: "yyyy-mm-dd",
                autoclose: true
            });
        }

        $('body').on('click', '.removeappendn2', function () {
            var controller = $(this).attr('data-controller');
            var container = $(this).attr('data-container');
            var editcheck = $(this).attr('data-edit');
            var id = $(this).attr('data-id');
            var thisis = $(this);
            if (confirm('هل انت متاكد من الحذف ؟')) {
                if (editcheck == 1) {
                    $.post(controller+'.php?do=removeappend', {id: id, container: container, editcheck: editcheck}, function (data) {
                        if (data == 1) {
                            window.location.reload();
                        }else{
                             alert('حدث خطأ أثناء التنفيذ');
                        }
                    });
                } else {
                    $('#r'+container+editcheck+id).remove();
                    window.location.reload();
                }
            } else {
               alert('لماذا ضغطت إلغاء؟ يجب أن تكون قد أكدت');
            }
        });


        deldata();
        selectedit2();



});
    
</script>
{/literal}



 -->