

<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="#">المخازن</a></li>
            <li><a href="mechandiseReturnRequest.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="mechandiseReturnRequest.php?do=savedata"  id="submitformajax"  method="post"  enctype="multipart/form-data" data-dosave="?do=savedata" data-direction="?do=show" data-print="">

                    {/literal}

                    <div class="form-group">
                        <label class="col-lg-2 control-label">تالف او سليم    </label>
                        <div class="col-lg-4">
                            <div class="radio-inline">
                                <label>
                                    <input value="1" class="noradioR" name="damagedintact" type="radio" 
                                    {if $editdata->damagedintact eq 1} checked="checked" {/if}>
                                    تالف </label>
                            </div>
                            <div class="radio-inline">
                                <label>
                                    <input value="2" class="radioR" name="damagedintact" type="radio"
                                    {if $editdata->damagedintact eq 2} checked="checked" {/if}>
                                    سليم  </label>
                            </div>

                        </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="clientid" 
                            data-text="{$editdata->clientname}" data-id="{$editdata->clientid}"
                            class=" wdt100" id="select2client" data-placeholder="اختر " />
                            
                        </div>
                    </div>


                     
                     <div class="form-group">
                        <div class="col-md-12">
                              <div>
                                 <button type="button" class="btn green-meadow btn-sm pull-left addappend"  data-itr=''  style="font-size: 10px;background: #ccc">اضافه  </button>
                                  <input type="hidden" id="productsitr" name="productsitr"  value="{1 + $mechandisereturnproducts|@count}" />
                              </div>
                              <div class="clearfix"></div>
                              <!-- <ul >
                                <li style="list-style-type: none;">
                                   <div class="col-lg-4">
                                       <label for="txtName" class="control-label">المتجات  </label>
                                   </div>
                                   <div class="col-lg-7">
                                     <label for="txtName" class="control-label">مساحة الوحده</label>
                                   </div>
                                    <div class="col-lg-1 pull-right">
                                        <label for="txtName" class="control-label">حذف</label>
                                    </div>
                                </li>
                              </ul>   -->
                              <ul class="products" id="products">
                                {assign var=itr value=1}
                                {foreach $mechandisereturnproducts as $mechandisereturnproduct}
                                 <input type="hidden"  name="mechandiseReturnProductid_{$itr}" value="{$mechandisereturnproduct->id}" />
                                 <li  id="products1{$mechandisereturnproduct->id}" style="list-style-type: none;">
                                   <div class="col-lg-4">
                                        <input type="text" name="productid_{$itr}" data-itr="{$itr}"
                                        data-id="{$mechandisereturnproduct->productid}" data-text="{$mechandisereturnproduct->productname}"
                                         class=" wdt100 select2product" id="select2product{$itr}" data-placeholder="اختر " />

                                   </div>
                                   <div class="col-lg-7">
                                    <input      type="text" name="comment_{$itr}" class="form-control  "
                                     placeholder="التعليق  " value="{$mechandisereturnproduct->comment}" />
                                   </div>
                                    <div class="col-lg-1 pull-right">
                                        <a href="javascript:;"  data-id="{$mechandisereturnproduct->id}" data-edit="1"  class="btn btn-xs red pull-right removeappend"> x
                                        </a>
                                    </div>
                                </li>
                                {capture assign=itr}{$itr+1}{/capture}
                                 {/foreach}
                            </ul>  
                        </div>
                    </div>

                    
                    <input type="hidden" name="id" value="{$editdata->id}" />


                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-4">
                             <button type="submit" class="btn btn-primary pull-left submitformajax" >تعديل الامر</button>
                        </div>
                    </div>




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




    </div>
</div>





{literal}
<script type="text/javascript">
$(document).ready(function () {
        var formatResult;
        formatResult = function (data) {return  data.text;}
     
            $('body').on('click', '.addappend', function () {
                var itr = Number($("#productsitr").val()) + 1;
                $("#productsitr").val(itr);
              $.post('mechandiseReturnRequest.php?do=appendproduct', {itr: itr}, function (result) {
                $("#products").append(result);
                products(itr);
                

              });
            });

            $('body').on('click', '.removeappend', function () {
                var id = Number($(this).attr('data-id'));
                var edit = Number($(this).attr('data-edit'));
                if(edit == 1){
                    $.post('mechandiseReturnRequest.php?do=removeappend', {id: id}, function (result) {
                        $("#products1"+id).remove();
                    });
                }else{
                    $("#products"+edit+id).remove();

                }
                
            });

       
          
          function products($i){
            $("#select2product" + $i).select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "mechandiseReturnRequest.php?do=select2product",
                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);
            }
        });
          }
          $(".select2product").each(function( ) {
            var itr = Number($(this).attr('data-itr'));
            products(itr);
                var ids = Number($(this).attr('data-id'));
                var texts = $(this).attr('data-text');
                $(this).select2("data", {id: ids, text: texts});
            });



         $("#select2client").select2({
            placeholder: "إسم  المشروع",
            multiple: false,
            quietMillis: 100,
            ajax: {
                url: "mechandiseReturnRequest.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);
            }
        });




        $("#select2client").select2("data", {id: $('#select2client').attr('data-id'), text: $('#select2client').attr('data-text')});

});  
</script>
{/literal}
