<?php $form = $this->beginWidget('CActiveForm', array(

    'id' => 'target-form',

    'enableAjaxValidation' => false,

    'enableClientValidation' => false,


    )); 
    $cs = Yii::app()->getClientScript();
    $cs->registerScript("calculatePropotion",  "function calculatePropotion(field_id, value, type){
        
        var daily = 0;
        var weekly = 0;
        var monthly = 0;
        var quaterly = 0;
        var yearly = 0;

        var arr_field = field_id.split('_');
        var i = arr_field[1];
        
        if(parseFloat(value) > 0 ){
            if( type == 'd' ){
                var daily = value;
                var weekly = value*7;
                var monthly = value*30;
                var quaterly = value*90;
                var yearly = value*365;
            }
            if( type == 'w' ){
                var daily = value/7;
                var weekly = value;
                var monthly = value*4;
                var quaterly = value*12;
                var yearly = value*48;
            }
            if( type == 'm' ){
                var daily = value/30;
                var weekly = value/4;
                var monthly = value;
                var quaterly = value*3;
                var yearly = value*12;
            }
            if( type == 'q' ){
                var daily = value/90;
                var weekly = value/12;
                var monthly = value/3;
                var quaterly = value;
                var yearly = value*4;
            }
            if( type == 'y' ){
                var daily = value/365;
                var weekly = value/48;
                var monthly = value/12;
                var quaterly = value/4;
                var yearly = value;
            }
        }

        $('#SalesManTarget_'+i+'_target_daily').val(daily);
        $('#SalesManTarget_'+i+'_target_weekly').val(weekly);
        $('#SalesManTarget_'+i+'_target_monthly').val(monthly);
        $('#SalesManTarget_'+i+'_target_quarterly').val(quaterly);
        $('#SalesManTarget_'+i+'_target_yearly').val(yearly);
    };", CClientScript::POS_HEAD);
    
    ?> 
<div class="table-responsive">
        <table class="table table-hover table-bordered personal-task" id="sales_target_tabel">
            <tr>
                <th>Particular</th>
                <!-- <th>Type</th> -->
                <th>Daily</th> 
                <th>Weekly</th>   
                <th>Monthly</th>  
                <th>Quarterly</th>  
                <th>Yearly</th>     
             </tr>
            
     
      <?php foreach ($targetContents as $i => $item){ ?>
             <?php $targetvalue =''; ?> 
             <tr>
                 <td><?php echo $item->activity; ?>
                      <?php echo CHtml::hiddenField('SalesManTarget['.$i.'][target_master_id]',$item->id); ?>
                      <?php echo CHtml::hiddenField('SalesManTarget['.$i.'][user_id]',$user_id); ?>
                      <?php echo CHtml::hiddenField('SalesManTarget['.$i.'][id]',$id); ?>
                 </td>
               
                     <?php if(!empty($model)){
                        
                        foreach($model as $j=>$eachsales){
                            if($eachsales->target_master_id == $item->id){
                                $targetDaily=$eachsales->target_daily;
                                $targetWeekly=$eachsales->target_weekly;
                                $targetMonthly=$eachsales->target_monthly;
                                $targetQuarterly=$eachsales->target_quarterly;
                                $targetYearly=$eachsales->target_yearly;
                                $targettype = $eachsales->target_type;
                                $id   = $eachsales->id;
                            }
                        }
                     }
                    
                     ?>
                 <!-- <td><?php // echo CHtml::dropDownList('SalesManTarget['.$i.'][target_type]',$targettype,array('C'=>'Count', 'V' => 'Value'),array('class' =>'go unit')); ?></td> -->
                 <td> <?php echo CHtml::textField('SalesManTarget['.$i.'][target_daily]',$targetDaily,array('class' =>'go unit', 'onkeyup' => 'calculatePropotion(this.id, this.value, "d");')); ?></td>
                 <td> <?php echo CHtml::textField('SalesManTarget['.$i.'][target_weekly]',$targetWeekly,array('class' =>'go unit', 'onkeyup' => 'calculatePropotion(this.id, this.value, "w");')); ?></td>
                 <td> <?php echo CHtml::textField('SalesManTarget['.$i.'][target_monthly]',$targetMonthly,array('class' =>'go unit', 'onkeyup' => 'calculatePropotion(this.id, this.value, "m");')); ?></td>
                 <td> <?php echo CHtml::textField('SalesManTarget['.$i.'][target_quarterly]',$targetQuarterly,array('class' =>'go unit', 'onkeyup' => 'calculatePropotion(this.id, this.value, "q");')); ?></td>
                 <td> <?php echo CHtml::textField('SalesManTarget['.$i.'][target_yearly]',$targetYearly,array('class' =>'go unit', 'onkeyup' => 'calculatePropotion(this.id, this.value, "y");')); ?></td>

             </tr>
      <?php } ?>
       </table>
  </div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
            <?php
                 $url = Yii::app()->createUrl('Users/SaveTarget');
                 $rand = rand();
                 echo CHtml::ajaxSubmitButton('Save', $url, array(

                            'dataType' => 'json',

                            'type' => 'post',

                            'success' => 'function(data) {

                                    if(data.status=="success"){  

                                         
                                         $(".x.close").click();
                                         $("#loader").hide();
                                         t8.success("Saved!");
                                         reloadTable("#tbl_user_details");

                                    } else { 
                                        $("#loader").hide();
                                         $("#target-form .target-save-btn").prop("disabled", false);                      
                                        $("#target-form .target-save-btn").addClass("btn-theme").removeClass("btn_loding"); 
                                         $.each(data, function(key, val) {
                                              t8.error(""+val+""); 
                                         }); 

                                    } 

                                    }',

                            'beforeSend' => 'function(){
                                $("#loader").show();
                                 $("#target-form .target-save-btn").prop("disabled", true);                      
                                $("#target-form .target-save-btn").addClass("btn_loding").removeClass("btn-theme"); 
                            }'

                                

                    ), array('id' => 'mybtn_'.$rand.'', 'class' => 'target-save-btn btn btn-theme'));

           

    ?>

    </div>
<?php $this->endWidget(); ?>