<link href="<?php echo Yii::app()->baseUrl; ?>/css/bootstrap.css" rel="stylesheet">
<div class="main_content">
<div class="innercontent_outerbox">
    <div class="title-btn-block">
      <h1>Earn Points - Product </h1>
      <div class="btn-block">
        <a class="btn btn-theme btn-ep" href="<?php echo Yii::app()->createUrl('site/wallet'); ?>">Back</a>
      </div>
    <h4> <?php echo CHtml::link("Earn Points by Product", Yii::app()->createUrl('site/earnpoints')); ?> </a> | 
            <?php echo CHtml::link("Earn Points by Certification", Yii::app()->createUrl('site/earncertificationpoints')); ?> </h4>
  <div class="row">
    <div class="span8">
          <?php $form=$this->beginWidget('CActiveForm', array(
			'id'=>'reward-form',
			'enableAjaxValidation'=>false,
			'htmlOptions' => array('enctype' => 'multipart/form-data'),
		)); 
          $modelproducts = array(new RewardProducts());?>
        <?php //echo $form->errorSummary($model); ?>
        <?php
    foreach(Yii::app()->user->getFlashes() as $key => $message) {
        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
    }
    ?>
            <table width="100%" border="1" cellspacing="0" cellpadding="10" id="tbl_product" class="table table-fixed table-primary">
                <tr>
                    <th class="text-center">Product </th>`
                    <th class="text-center">Product Serial No</th>
                    <th class="text-center"><button class="btn" type="button" onclick="addTableRow($('#tbl_product'));">+</button></th>
                </tr>
                <?php
            foreach ($modelproducts as $i=>$items) {  ?> 
              <tr>
                <td align="center"><?php echo $form->dropDownList($items,"[" . $i . "]product_id",Product::getPromotioanlProducts(),array('prompt'=>'select product')); ?><?php echo $form->error($model,'product_id'); ?></td>
                <td align="center"><?php echo $form->textField($items,"[" . $i . "]serial_no",array('size'=>20,'maxlength'=>255,'placeholder'=>'Product Serial No:','onchange'=>'addRow(this.id)')); ?><?php echo $form->error($model,'serial_number_of_product'); ?>
                <td align="center">
                  <?php echo CHtml::link('-', 'javascript:void(0);', array('id'=>'RewardProducts_0_remove','onclick' => 'removeRow($(\'#tbl_product\'),this,this.id)', 'title' => 'Remove this item','class'=>'btn btn-danger'));?>
                </td>               
              </tr>
              <?php  } ?>
            </table>
        <table width="100%" border="0" cellspacing="0" cellpadding="10">
              <tr>
                <td><?php echo $form->labelEx($model,'end_customer_name'); ?></td>
                <td><?php echo $form->textField($model,'end_customer_name',array('size'=>20,'maxlength'=>255,'placeholder'=>'Customer name:')); ?><?php echo $form->error($model,'end_customer_name'); ?></td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td><?php echo $form->labelEx($model,'date_of_purchase',array('label'=>'Date of purchase')); ?></td>
                <td><?php
				  $this->widget('zii.widgets.jui.CJuiDatePicker', array(
	 'model'=>$model,
     'attribute'=>"date_of_purchase", // the name of the field
     'options'=>array(
            'dateFormat'=>'yy-mm-dd',
            'showAnim'=>'fold',
            'showButtonPanel'=>false,
            'yearRange'=>'1900',
     'debug'=>true,

    ),
     'htmlOptions'=>array(
     'style'=>'height:20px;'
     ),
     ));
				  ?>
                  <?php echo $form->error($model,'date_of_purchase'); ?></td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td><?php echo $form->labelEx($model,'invoice_number'); ?></td>
                <td><?php echo $form->textField($model,'invoice_number',array('size'=>20,'maxlength'=>255,'placeholder'=>'Invoice number:')); ?>
                <?php echo $form->error($model,'invoice_number'); ?></td>
                <td></td>
              </tr>
              <tr>
                <td><?php echo $form->labelEx($model,'invoice'); ?></td>
                <td><?php echo $form->fileField($model,'invoice'); ?>
                <?php echo $form->error($model,'invoice'); ?>
                  <span style="color:#999;">Upload photo/Scanned image of invoice</span></td>
                <td></td>
              </tr>
              <tr>
                <td><?php echo $form->checkBox($model,'agreement',array('value'=>null, 'checkValue'=>'1')); ?>
                <?php echo $form->labelEx($model,'agreement'); ?>
                <?php echo $form->error($model,'agreement'); ?>
                </td>
              </tr>
              <tr>
                <td><?php echo CHtml::submitButton('Submit'); ?></td>
              </tr>
            </table>
          <?php $this->endWidget(); ?>
        </div>
  </div>
</div>
<?php 
$cs = Yii::app()->getClientScript();
$cs->registerScript('addRow','function addRow(id) {			
    var product_id = $("#Reward_product_id").val();
    var serial_no  = $("#"+id).val();
    if(serial_no!=""){
        jQuery.ajax({
            "dataType":"json",
            "type"  : "post",
            "data"  : "serial_no=" +serial_no,
            "url" : "'.Yii::app()->createUrl("site/checkSerialNo").'",
            "cache": false,
            "success":function(data){
            if(data.status=="success")
            {
                
            }else{
               $("#"+id).val("");
               alert("Serial no already used");
            }
        }
    });
    }else{
        alert("Please enter Serial No");
    }
}',CClientScript::POS_END);
$cs->registerScript("addTableRow", "
            function addTableRow(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('a,select,label,input,textarea,hidden,.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:none;';
                });
                jQtable.append(newrow);
                
        }", CClientScript::POS_HEAD);
        
            $cs->registerScript("isRowEmpty", " function isRowEmpty(tr) {
                    var rowfilled=false; tr.find('td:gt(0)').each(function()
                    {
                        inpt    =   $(this).find('input').val();
                        if(typeof inpt !== 'undefined' && inpt!=='' && inpt!=='0.00') {
                            rowfilled=true; return false;
                        }
                    });
                    if(rowfilled) {
                        return(confirm('Do you want to remove this item?'));
                    }
                    return true;
            } ", CClientScript::POS_HEAD);
            $cs->registerScript("removeRow", "function removeRow(jQtable,evt,field_id) {
                var arr_fields= field_id.split('_');
                var row_field=arr_fields[1];
                var num_rows    =   jQtable.find('tr:gt(0)').length;
                if(num_rows>1){
                    var tr  =   $(evt).parent().parent();
                    if(isRowEmpty(tr))
                    {
                        $(evt).parents('tr').remove();
                        var i=0;
                        jQtable.find('tr:gt(0)').each(function() {
                            $(this).find('input,textarea,.errorMessage').each(function()
                            {
                                old_id   =   $(this).attr('id');
                                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('Only one row remaining');
                }
            };", CClientScript::POS_HEAD);?>