<div id ="div_tracking_doc_details">
<div class="table-responsive" style="max-height:500px">
    <table class="table table-hover table-bordered personal-task" id="purchase_order_doc_table">        
        <tr>            
                <th style="width:1%">No</th>  
                <th style="width:16%">Document Name</th>
                <th class="text-center" style="width:2%"><i class="fa fa-upload"></i></th>
                <th class="text-center" style="width:2%"><i class="fa fa-download"></i></th>
                <th class="text-center">
                    <a class="btn btn-xs btn-theme" href="javascript:void(0);" onclick="adddocTableRow($('#purchase_order_doc_table'));"><i class="fa fa-plus"></i></a>
                </th>
        </tr>
       <?php    
            foreach($modelDocument as $i => $each){
        ?>
          
            
        <tr>                
            <td><label><?php echo ($i+1); ?></label></td>
            <td>
                <?php echo $form->hiddenField($each, "[" . $i . "]id"); ?> 
                <?php echo $form->textField($each, "[" . $i . "]document_name", array('class' => 'form-control')); ?>                                        
            </td> 
            <td class="text-center">
            <?php
                    $allowedExtensions = array( "csv","png", "txt", "pdf","doc", "docx", "jpg", "jpeg", "xls");
                    $this->widget('ext.EAjaxUpload.EAjaxUpload', array(
                        'id' => 'ShipmentDocs_'.$i.'_filename',
                        'config' => array(
                            'action' => Yii::app()->createUrl(Yii::app()->controller->id . '/UploadDocuments1', array('folder' => 'uploads/documents/shipment')),
                            'allowedExtensions' => $allowedExtensions,
                            'sizeLimit' => 2 * 1024 * 1024,
                            'multiple' => false,
                            'onStart' => "js:function(){}",
                            'onComplete' => "js:function(id, fileName, responseJSON){
                                $('#PurchasingOrderShipmentDocument_".$i."').val(responseJSON.filename);
                                $('.qq-upload-failed-text').hide();
                             }",
                           )
                        ));
                        ?>                   
                  <input type="hidden" name="PurchasingOrderShipmentDocument[<?php echo $i ?>][document_actual_file_name]" value="<?php echo $each->document_actual_file_name;  ?>" id="PurchasingOrderShipmentDocument_<?php echo $i ?>" />
            </td> 
            <td class="text-center nw">
                <?php if( $each->document_actual_file_name != "") : ?>
                    <a href="javascript:void(0);" class="tooltipster text-center btn-xs btn btn-theme" title="Download" onclick="window.open($('#liveSite').val()+'index.php?r=purchasingorder/download&filename=<?php echo $each->document_actual_file_name; ?>', '_blank');" title="Download Document"><i class="fa fa-download"></i></a>
                    <?php endif; ?>
            </td>
            <td class="text-center nw" width="1%">  
                    <?php echo CHtml::link('<i class="fa fa-times"></i>', 'javascript:void(0);', array('id'=>"tddoc_delete_".$i,'class'=>'btn btn-xs btn-danger','onclick' => 'removelineitem($(\'#purchase_order_doc_table\'),this,this.id)', 'title' => 'Remove this item')); ?>                  
            </td>
                        
        </tr>        
            <?php //$j++; 
            } ?>    



    </table>

</div>
</div>

<?php
    $cs = Yii::app()->getClientScript();
    $cs->registerScript("adddocTableRow", "
        function adddocTableRow(jQtable) {
            var newrow= jQtable.find('tr:last').clone();
            var lastId = jQtable.find('tr:last td:gt(0) input').attr('id');
            var thenum = lastId.match(/\d+/g);
            var newId = Number(thenum) + 1;
            var newrow= jQtable.find('tr:last').clone();
            newrow.find('label').html(newId+1);
            newrow.find('.hasDatepicker').removeClass('hasDatepicker');
            newrow.find('.datetimepicker').siblings().remove();
            newrow.find('a,select,label,input,textarea,hidden,div,.errorMessage').each(function() {
                this.id= this.id.replace(/\d+/,newId);
                this.value  = '';
                (this.name!==undefined) ? this.name= this.name.replace(/\d+/,newId) : this.style  =   'display:block;';
            });
            jQtable.append(newrow);
            var uploader = new qq.FileUploader({
                element: document.getElementById('ShipmentDocs_'+newId+'_filename'),
                params: { field_id: 'ShipmentDocs_'+newId+'_filename' },
                action : '" . Yii::app()->createUrl(Yii::app()->controller->id . '/UploadDocuments1', array('folder' => 'uploads/documents/shipment')) . "',
                allowedExtensions: ['csv','png', 'txt', 'pdf','doc', 'docx', 'jpg', 'jpeg', 'xls'],
                sizeLimit : 2 * 1024 * 1024,
                onComplete: function(id, fileName, responseJSON){
                    var field_name = responseJSON.field_id;
                    var file_name=responseJSON.filename;
                    $('.qq-upload-failed-text').css('display', 'none');
                    $('#PurchasingOrderShipmentDocument_'+newId).val(responseJSON.filename);
   
                },
            });
        }", CClientScript::POS_HEAD);
 $cs->registerScript("removelineitem", " function removelineitem(jQtable,evt,field_id) {
            var arr_fields= field_id.split('_');
            var row_field=arr_fields[2];
            var id=$('#PurchasingOrderShipmentDocument_'+row_field+'_id').val();
            formIdStarter   =   jQtable.attr('id').split(/_(.+)?/)[0];
            
            var num_rows    =   jQtable.find('tr:gt(0)').length;
            if(num_rows>1){

            var tr  =   $(evt).parent().parent();

            if(isRowEmpty(tr)) {
                if(id!=''){
                    jQuery.ajax({
                        'type': 'post',
                        'dataType': 'json',
                        'url': '" . Yii::app()->createUrl('purchasingorder/removelineitem') . "',
                        'data': {'id':id} ,
                        'cache': false,
                        'success': function(data) {
                            if(data.status=='success'){
                                $(evt).parents('tr').remove();
                                var i=0;
                                jQtable.find('tr:gt(0)').each(function() {
                                    $(this).find('input,textarea,.errorMessage,div,select,a').each(function()
                                    {
                                        old_id = $(this).attr('id');
                                        if(old_id!==undefined) {
                                            new_id = old_id.replace(/\d+/,i);
                                            $(this).attr('id',new_id);
                                        }
                                        old_name = $(this).attr('name');
                                        if(old_name!==undefined) {
                                            new_name = old_name.replace(/\d+/,i);
                                            $(this).attr('name',new_name);
                                        }
                                    });
                                    $(this).find('td:first label').html(++i);
                                });
                                t8.success('Deleted');
                            }else{
                                t8.error('Deletion Not Possible, History Exist');
                            }
                        }
                    });
                }else{
                    $(evt).parents('tr').remove();
                    var i=0;
                    jQtable.find('tr:gt(0)').each(function() {
                        $(this).find('input,textarea,.errorMessage,div,select,a').each(function()
                        {
                            old_id = $(this).attr('id');
                            if(old_id!==undefined) {
                                new_id = old_id.replace(/\d+/,i);
                                $(this).attr('id',new_id);
                            }
                            old_name = $(this).attr('name');
                            if(old_name!==undefined) {
                                new_name = old_name.replace(/\d+/,i);
                                $(this).attr('name',new_name);
                            }
                        });
                        $(this).find('td:first label').html(++i);
                    });
                }
            }
        }else {
            alert('Atleast one item needed here');
        }
    }", CClientScript::POS_HEAD);