<?php
if($model->id>0){
    $items =VendorShippingDetails::model()->findAll(array('condition'=>'vendor_id = "'.$model->id.'"'));
    if(empty($items))
        $items = array(new VendorShippingDetails());
}else{
    $items = array(new VendorShippingDetails());
}
?>
<div class="table-responsive">
    <input type="hidden" id="toggler" value="0">
        <table class="table table-hover table-bordered personal-task" id="shipping_address_table">
        <tr>
            <th>Address <span style="color:red;">*</span>   <a href="#" onclick="copyAdds()"><i id="copy_image" class="fa fa-files-o"></i></a></th>
            <th>PO Box</th>
            <th>City</th>
            <th>State/Province</th>
            <th>Post Code</th>
            <th>Country <span style="color:red;">*</span></th>
            <th>
            <a class="btn btn-sm btn-theme " href="javascript:void(0);" onclick="addTableRow($('#shipping_address_table'));"><i class="fa fa-plus"></i></a></th>
            </th>        
        </tr>        <?php foreach ($items as $i => $model): ?><tr>
            
            <td><?php echo $form->textArea($model, "[" . $i . "]street_address", array('class' => 'form-control go en-text-area','style' => 'height:50px;',)); ?>
                <?php echo $form->error($model, "[" . $i . "]street_address"); ?>
            </td>
            <td>
                <?php echo $form->textField($model, "[" . $i . "]po_box", array('class' => 'form-control')); ?>
                    <?php echo $form->error($model, "[" . $i . "]po_box"); ?>
            </td>
            <td><?php echo $form->textField($model, "[" . $i . "]city", array('class' => 'form-control go cell-center')); ?>
                <?php echo $form->error($model, "[" . $i . "]city"); ?>  
            </td>
            <td>
                <?php echo $form->textField($model, "[" . $i . "]state", array( 'class' => 'form-control go',));  ?>
                    <?php echo $form->error($model, "[" . $i . "]state");
                    echo $form->hiddenField($model, "[" . $i . "]id"); ?>
            </td>
            <td><?php echo $form->textField($model, "[" . $i . "]postal_code", array('class' => 'form-control go')); ?>
                    <?php echo $form->error($model, "[" . $i . "]postal_code"); ?>
            </td> 
            <td> <?php echo $form->dropDownList($model, "[" . $i . "]country_id",MasCountry::getCountry(), array('class' =>'form-control go', 'prompt' => 'Select','data-search' =>'true')); ?>
                    <?php echo $form->error($model, "[" . $i . "]country_id"); ?>
            </td>
            <td>
            <?php echo CHtml::link('<i class="fa fa-times"></i>', 'javascript:void(0);', array('class'=>'btn btn-sm btn-danger','onclick' => 'removeTableRow($(\'#shipping_address_table\'),this)', 'title' => 'Remove this item')); ?>
            </td>
        </tr>
            <?php endforeach; ?>
    </table>
</div>
<?php 
$cs = Yii::app()->getClientScript();
$cs->registerScript("copyAdds", "function copyAdds()
        {
        
       var t =  $('#toggler').val();
       var adds,pin,city,state,country;
       if(t == 0){
              adds = $('#Vendor_vendor_address').val();
              pin = $('#Vendor_vendor_postal_code').val();
              city = $('#Vendor_vendor_city').val();
              state = $('#Vendor_vendor_state').val();
              country = $('#Vendor_vendor_country').val();
              $('#toggler').val(1);
              $('#copy_image').removeClass('fa-files-o').addClass('fa-times-circle');
        }else{
            $('#toggler').val(0);
            $('#copy_image').removeClass('fa-times-circle').addClass('fa-files-o');
             adds,pin,city,state,country='';
        }
       
       
        var i = 0;
        for(i =0; i< 20 ; i++){
            $('#VendorShippingDetails_'+i+'_street_address').val(adds);
            $('#VendorShippingDetails_'+i+'_po_box').val(pin);
            $('#VendorShippingDetails_'+i+'_city').val(city);
            $('#VendorShippingDetails_'+i+'_state').val(state);
            try{
                document.getElementById('VendorShippingDetails_'+i+'_country_id').value = country;
            }catch(e){}
        }
        
    }", CClientScript::POS_HEAD);