<?php
Yii::app()->clientScript->registerScript('addremoveF',"


$('.addOneMore_product_image a').live('click',function()
{
//$('#productImagesTable tr:last').after($('#productImagesTable_row').clone().find('input').val('').end());
var html	=	'<tr valign=\"middle\"><td>Product Image</td><td>:</td><td></td><td><input type=\"hidden\"name=\"ProductImagesId[]\"><input type=\"hidden\" name=\"ProductImagesFF[]\" value=\"\" ><input type=\"file\"  name=\"ProductImagesFF[]\"></td><td><div class=\"removeOne_product_image\"> <a id=\"0\" title=\"remove this image\" href=\"javascript:;\"><img height=\"15\" width=\"15\" src=\"/partner/images/admin/green--sign-md.png\" class=\"plus_position\"></a> </div></td></tr>';
$('#productImagesTable tr:last').after(html);

}
);

$('.removeOne_product_image a').live('click',function()
{
var id = $(this).attr('id')
var p	=	this;
$.ajax({ 
          url: '".Yii::app()->createUrl('product/deleteproductimage')."',
		  success: function(str){
		  $(p).closest('tr').remove();
		  },
			  type: 'POST',
			  cache: false,
			  data: {id : id}, 
			  dataType: 'html' 
        });


});

$('#addOneMoreFeature a').live('click',function()
{
$('#productFeaturesTable tr:last').after($('#tableRowRepeatableFeature').clone().find('input').val('').end());
}
);

$('.removeOneFeature a').live('click',function()
{
if($('#productFeaturesTable tr').length>2)
$(this).closest('tr').remove();
});

$('#addOneMoreDownloadable a').live('click',function()
{
$('#productDownloadable tr:last').after($('#tableRowRepeatableDownloadable').clone().find('textarea').val('').end());
//$('#productDownloadable tr:last').after($('.downloadableReapeat').clone().end());

}
);

$('.removeOneDownloadable a').live('click',function()
{
if($('#productDownloadable tr').length>2)
{
	var id = $(this).attr('id')
	var file1    = new Array()
	file1[0] = $(this).attr('file1')
	var model	=	'ProductDownloadableRelation'
	var p	=	this;
	$.ajax({ 
          url: '".Yii::app()->createUrl('product/deletefile')."',
		  success: function(str){
		  $(p).closest('tr').remove();
		  },
			  type: 'POST',
			  cache: false,
			  data: {id : id, file1 : file1, model:model}, 
			  dataType: 'html' 
        });
}

});


$('#add_to_another_group').live('click',function()
{
var p	=	'.mapping';
$.ajax({ 
          url: '".Yii::app()->createUrl('product/mapptoanother')."',
		  beforeSend:function()
		  {
			  $(p).append('<span>Please wait...fetching data .....</span>');
		  },
		  success: function(str){
		  $(p+' span').remove();
		  $(p).append(str);
		  },
			  type: 'POST',
			  cache: false,
			  //data: {id : id}, 
			  dataType: 'html' 
        });


});

"); ?>
<div id="communicationsbox" style="height:525px; background-color:#f7eee1;">
  <?php
    foreach(Yii::app()->user->getFlashes() as $key => $message) {
        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";
    }
?>
  <div id="form_wrapper">
    <?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'product-form',
	'enableAjaxValidation'=>false,
	'enableClientValidation'=>true,
	'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),

)); ?>
    <?php echo $form->errorSummary($model); ?>
    <div class="row">
      <div id="text"><?php echo $form->labelEx($model,'product_generic_name'); ?></div>
      <div id="box"> <?php echo $form->textField($model,'product_generic_name',array('size'=>20,'maxlength'=>255)); ?> <?php echo $form->error($model,'product_generic_name'); ?> </div>
    </div>
    <div class="row">
      <div id="text"><?php echo $form->labelEx($model,'product_name'); ?></div>
      <div id="box"> <?php echo $form->textField($model,'product_name',array('size'=>20,'maxlength'=>255)); ?> <?php echo $form->error($model,'product_name'); ?> </div>
    </div>
    <div class="row">
      <fieldset class="current">
        <legend><strong>Product images</strong></legend>
        <br />
        <table width="800" border="0" id="productImagesTable">
          <tr id="productImagesTable_row" valign="middle">
            <td><?php echo $form->labelEx($model,'product_image'); ?></td>
            <td>:</td>
            <td><?php if(!$model->isNewRecord && $model->product_image!=''): ?>
              <?php echo CHtml::image(Yii::app()->request->baseUrl.'/media/product_images/thumbs/'.$model->product_image,"image",array("width"=>200)); ?>
              <?php endif; ?></td>
            <td><?php echo $form->fileField($model,'product_image',array('size'=>20,'maxlength'=>255)); ?> <?php echo $form->error($model,'product_image'); ?></td>
            <td><div class="addOneMore_product_image"> <a href="javascript:;"><img title="Add one more image" width="15" height="15" src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green-plus-sign-md.png" alt="pluse" class="plus_position"></a> </div></td>
            <?php 
		  if(!$model->isNewRecord)
		  foreach($model4 as $i => $EachProductImage): ?>
            <?php if(!$model->isNewRecord && $EachProductImage->product_id!=''): ?>
          <tr id="productImagesTable_row" valign="middle">
            <td>Product Image</td>
            <td>:</td>
            <td><?php echo CHtml::image(Yii::app()->request->baseUrl.'/media/product_images/thumbs/'.$EachProductImage->product_image,"image",array("width"=>200)); ?></td>
            <td><?php echo $form->hiddenField($EachProductImage,'id',array('name'=>'ProductImagesId[]',)); ?> <?php echo $form->hiddenField($EachProductImage,'product_image',array('name'=>'ProductImagesFFh[]',)); ?> <?php echo $form->fileField($EachProductImage,'product_image',array('name'=>'ProductImagesFF[]',)); ?></td>
            <td><div class="removeOne_product_image"> <a id="<?php echo $EachProductImage->id;  ?>" title="remove this image" href="javascript:;"><img width="15" height="15" class="plus_position" src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green--sign-md.png"></a> </div></td>
          </tr>
          <?php endif; ?>
          <?php endforeach; ?>
        </table>
      </fieldset>
    </div>
    <div class="row">
      <fieldset class="current">
        <legend>Group</legend>
        <table width="800" border="0" cellspacing="0" cellpadding="0" >
          <tr>
            <td width="400"><table width="96%" border="0" align="center" cellpadding="2" cellspacing="2">
                <tr>
                  <td width="42%"><?php echo $form->labelEx($model,'user_group_id'); ?></td>
                  <td width="58%"><?php echo $form->dropDownList($model,'user_group_id',UserGroup::getGroups(),
											 array(
												'style'=>'width:150px',
												'prompt'=>'Select user group',
												'ajax' => array(
												'type'=>'POST',
												'url'=>CController::createUrl('Product/updateProductGroup'), 
												'data'=>array('user_group_id'=>'js:this.value'), 
												'success'=>'js:function(data){
													$("#Product_product_group_id").html(data)
													$("#Product_product_category_id").html("<option value=\"0\">No category</option>")
													$("#Product_type_id").html("<option value=\"0\">No Type</option>")
													}'
            ))); ?> <?php echo $form->error($model,'user_group_id'); ?></td>
                </tr>
                <tr>
                  <td><?php echo $form->labelEx($model,'product_group_id'); ?></td>
                  <td><?php echo $form->dropDownList($model,'product_group_id',ProductGroup::getProductGroup($model->user_group_id),array(
												'style'=>'width:150px',
												'prompt'=>'Select group',
												'ajax' => array(
												'type'=>'POST',
												'url'=>CController::createUrl('Product/updateProductCategory'), 
												'success'=>'js:function(data){
													$("#Product_product_category_id").html(data)
													$("#Product_type_id").html("<option value=\"0\">No Type</option>")
													}',
												'data'=>array(
															  'user_group_id'=>'js:$("#Product_user_group_id :selected").val()',  
															  'product_group_id'=>'js:this.value'
															  ),  
																		  ))); ?> <?php echo $form->error($model,'product_group_id'); ?></td>
                </tr>
                <tr>
                  <td><?php echo $form->labelEx($model,'product_category_id'); ?></td>
                  <td><?php  echo $form->dropDownList($model,'product_category_id',ProductCategory::getProductCategory($model->user_group_id,$model->product_group_id),array(
												'style'=>'width:150px',
												'empty'=>array(0=>'No category'),
												'style'=>'width:150px',
												'ajax' => array(
												'type'=>'POST',
												'url'=>CController::createUrl('Product/updateProductType'), 
												'data'=>array(
															  'user_group_id'=>'js:$("#Product_user_group_id :selected").val()',  
															  'product_group_id'=>'js:$("#Product_product_group_id :selected").val()',
															  'product_category_id'=>'js:$("#Product_product_category_id :selected").val()',
															  ),
												'success'=>'js:function(data){
													$("#Product_type_id").html(data)
													}'
            )
												
												)); ?>
                    <?php echo $form->error($model,'product_category_id'); ?></td>
                </tr>
                <tr>
                  <td><?php echo $form->labelEx($model,'type_id'); ?></td>
                  <td><?php  echo $form->dropDownList($model,'type_id',ProductType::getProductType($model->user_group_id,$model->product_group_id,$model->product_category_id),array(
												'style'=>'width:150px',
												'empty'=>array(0=>'No Type'),
												)); ?>
                    <?php echo $form->error($model,'type_id'); ?></td>
                </tr>
              </table></td>
            <td width="35%"><a href="javascript:;" id="add_to_another_group" title="Map to another group"><img src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green-plus-sign-md.png" alt="pluse" width="15" height="15" border="0" class="plus_position"></a></td>
          </tr><?php foreach($mappingModel as $i=>$eachModel) : if($eachModel->user_group_id!=0){?>
          <tr>
          	
          	<td width="400"><table id="mapping<?php echo $i; ?>" width="96%" border="0" align="center" cellpadding="2" cellspacing="2">
                <tr>
                  <td><strong>Product Mapping</strong></td>
                  <?php
					Yii::app()->clientScript->registerScript('dynamic'.$i,"
					jQuery('#remove_this".$i."').click(function()
					{
						$('#mapping".$i."').remove();
					});
					");
					?>
                  <td width="58%" align="right"><a href="javascript:;" id="remove_this<?php echo $i; ?>" title="Remove"><img class="plus_position" height="15" width="15" src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green--sign-md.png" /></a></td>
                </tr>
                <tr>
                  <td width="42%"><?php echo $form->labelEx($eachModel,'['.$i.']user_group_id'); ?></td>
                  <td width="58%"><?php echo $form->dropDownList($eachModel,'['.$i.']user_group_id',UserGroup::getGroups(),
											 array(
												'style'=>'width:150px',
												'prompt'=>'Select user group',
												'ajax' => array(
												'type'=>'POST',
												'url'=>CController::createUrl('Product/updateProductGroup'), 
												'data'=>array('user_group_id'=>'js:this.value'), 
												'success'=>'js:function(data){
													$("#ProductMapping_'.$i.'_product_group_id").html(data)
													$("#ProductMapping_'.$i.'_category_id").html("<option value=\"0\">No category</option>")
													$("#ProductMapping_'.$i.'_type_id").html("<option value=\"0\">No Type</option>")
													}'
            ))); ?> <?php echo $form->error($eachModel,'['.$i.']user_group_id'); ?></td>
                </tr>
                <tr>
                  <td><?php echo $form->labelEx($eachModel,'['.$i.']product_group_id'); ?></td>
                  <td><?php echo $form->dropDownList($eachModel,'['.$i.']product_group_id',ProductGroup::getProductGroup($eachModel->user_group_id),array(
												'style'=>'width:150px',
												'prompt'=>'Select group',
												'ajax' => array(
												'type'=>'POST',
												'url'=>CController::createUrl('Product/updateProductCategory'), 
												'success'=>'js:function(data){
													$("#ProductMapping_'.$i.'_category_id").html(data)
													$("#ProductMapping_'.$i.'_type_id").html("<option value=\"0\">No Type</option>")
													}',
												'data'=>array(
															  'user_group_id'=>'js:$("#ProductMapping_'.$i.'_user_group_id :selected").val()',  
															  'product_group_id'=>'js:this.value'
															  ),  
																		  ))); ?> <?php echo $form->error($eachModel,'product_group_id'); ?></td>
                </tr>
                <tr>
                  <td><?php echo $form->labelEx($eachModel,'['.$i.']category_id'); ?></td>
                  <td><?php  echo $form->dropDownList($eachModel,'['.$i.']category_id',ProductCategory::getProductCategory($eachModel->user_group_id,$eachModel->product_group_id),array(
												'style'=>'width:150px',
												'empty'=>array(0=>'No category'),
												'style'=>'width:150px',
												'ajax' => array(
												'type'=>'POST',
												'url'=>CController::createUrl('Product/updateProductType'), 
												'data'=>array(
															  'user_group_id'=>'js:$("#ProductMapping_'.$i.'_user_group_id :selected").val()',  
															  'product_group_id'=>'js:$("#ProductMapping_'.$i.'_product_group_id :selected").val()',
															  'product_category_id'=>'js:$("#ProductMapping_'.$i.'_category_id :selected").val()',
															  ),
												'success'=>'js:function(data){
													$("#ProductMapping_'.$i.'_type_id").html(data)
													}'
            )
												
												)); ?>
                    <?php echo $form->error($eachModel,'['.$i.']category_id'); ?></td>
                </tr>
                <tr>
                  <td><?php echo $form->labelEx($eachModel,'['.$i.']type_id'); ?></td>
                  <td><?php  echo $form->dropDownList($eachModel,'['.$i.']type_id',ProductType::getProductType($eachModel->user_group_id,$eachModel->product_group_id,$eachModel->category_id),array(
												'style'=>'width:150px',
												'empty'=>array(0=>'No Type'),
												)); ?>
                    <?php echo $form->error($eachModel,'['.$i.']type_id'); ?></td>
                </tr>
              </table></td>
             
            <td></td>
            <td>&nbsp;</td>
          </tr><?php } endforeach; ?>
          <tr>
            <td class="mapping"></td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </fieldset>
    </div>
    <br />
    <div class="row">
      <fieldset class="current">
        <legend><strong>Product Features</strong></legend>
        <br />
        <table width="800" border="0" cellspacing="2" cellpadding="3" id="productFeaturesTable">
          <tr>
            <td width="400">Features</td>
            <td width="400">Value
              <div class="addOneMore" id="addOneMoreFeature"> <a href="javascript:;"><img width="15" height="15" src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green-plus-sign-md.png" alt="pluse" class="plus_position"></a></div></td>
          </tr>
          <?php foreach($model2 as $i => $productFeature): ?>
          <tr id="tableRowRepeatableFeature">
            <td><?php echo $form->dropDownList($productFeature,'feature_id',ProductFeature::getProductFeature(),array('name'=>'ProductFeatureRelation[feature_id][]','empty'=>'Select')); ?></td>
            <td><?php echo $form->textField($productFeature,'feature_value',array('name'=>'ProductFeatureRelation[feature_value][]','size'=>20,'maxlength'=>20)); ?>
              <div class="removeOne removeOneFeature"> <a id="<?php echo $productFeature->id; ?>" href="javascript:;"> <img width="15" height="15" class="plus_position" src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green--sign-md.png"> </a> </div></td>
          </tr>
          <?php endforeach; ?>
        </table>
      </fieldset>
    </div>
    <br />
    <div class="row">
      <fieldset class="current">
        <legend><strong>Downloadables</strong></legend>
        <br />
        <br />
        <table width="800" border="0" cellspacing="2" cellpadding="3" id="productDownloadable">
          <tr>
            <td width="199">Item</td>
            <td width="331">Value</td>
            <td width="331">Files
              <div class="addOneMore" id="addOneMoreDownloadable"><a href="javascript:;"><img width="15" height="15" src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green-plus-sign-md.png" alt="pluse" class="plus_position"></a></div></td>
            <td width="331">&nbsp;</td>
          </tr>
          <?php foreach($model3 as $i => $productDownloadable): ?>
          <tr id="tableRowRepeatableDownloadable">
            <td><?php echo $form->dropDownList($productDownloadable,'item_id',ProductDownloadable::getDownloadableItems(),array('name'=>'ProductDownloadableRelation[item_id][]','empty'=>'Select')); ?></td>
            <td><?php echo $form->textArea($productDownloadable,'item_value',array('name'=>'ProductDownloadableRelation[item_value][]')); ?></td>
            <td><?php echo $form->hiddenField($productDownloadable,'files_available',array('name'=>'ProductDownloadableRelationFFh[]',)); ?> <?php echo $form->fileField($productDownloadable,'files_available',array('id'=>'a[]','name'=>'ProductDownloadableRelationFF[]',)); ?>
              <div class="removeOne removeOneDownloadable"> <a file1="media/product_item_downloadables/<?php echo $productDownloadable->files_available;?>" id="<?php echo $productDownloadable->id; ?>" href="javascript:;"> <img width="15" height="15" class="plus_position" src="<?php echo Yii::app()->baseUrl; ?>/images/admin/green--sign-md.png"> </a> </div></td>
            <td><?php if(!$model->isNewRecord && $productDownloadable->files_available!=''){ ?>
              <?php echo CHtml::link('File',
		  Yii::app()->createUrl(Yii::app()->controller->id.'/download',array('file'=>Yii::app()->request->getBaseUrl(true).'/media/product_item_downloadables/'.$productDownloadable->files_available))
		  ); ?>
              <?php } ?></td>
          </tr>
          <?php endforeach; ?>
        </table>
      </fieldset>
    </div>
    <div class="row">
      <fieldset class="current">
        <legend><strong>Promotion</strong></legend>
        <br />
        <br />
        <table width="800" border="0" cellspacing="2" cellpadding="3" >
          <tr>
            <td><?php echo $form->labelEx($model,'isIntoPromotion'); ?> <?php echo $form->checkBox($model,'isIntoPromotion',array('value'=>'Y', 'uncheckValue'=>'N')); ?> <?php echo $form->error($model,'isIntoPromotion'); ?></td>
          </tr>
          <tr>
            <td><table width="100%" border="0" cellspacing="2" cellpadding="2">
                <tr>
                  <td width="25%">Reward Points</td>
                  <td width="75%"><?php echo $form->textField($model,'rewardPoints',array('size'=>20,'maxlength'=>255)); ?> <?php echo $form->error($model,'rewardPoints'); ?></td>
                </tr>
                <tr>
                  <td>Promotion From</td>
                  <td><?php //echo $form->textField($model,'promotionFrom',array('size'=>20,'maxlength'=>255)); ?>
                    <?php
				  $this->widget('zii.widgets.jui.CJuiDatePicker', array(
    // 'name'=>'birthdate',
	//'id'=>'User_promotionFrom',
	 'model'=>$model,
     'attribute'=>"promotionFrom", // the name of the field
     //'value'=>$model->issued_date,  // pre-fill the value
     // additional javascript options for the date picker plugin
     'options'=>array(
            'dateFormat'=>'yy-mm-dd',
            //'defaultDate'=>date('Y-m-d'),
            //'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
            //'buttomImageOnly'=>true,
            'buttonText'=>'Select',
            'showAnim'=>'fold',
            'showOn'=>'button',
            'showButtonPanel'=>false,
            'yearRange'=>'1900',
     'debug'=>true,

    ),
     'htmlOptions'=>array(
     'style'=>'height:20px;'
     ),
     ));
				  ?>
                    <?php echo $form->error($model,'promotionFrom'); ?></td>
                </tr>
                <tr>
                  <td>Promotion To</td>
                  <td><?php //echo $form->textField($model,'promotionTo',array('size'=>20,'maxlength'=>255)); ?>
                    <?php echo $form->error($model,'promotionTo'); ?>
                    <?php
				  $this->widget('zii.widgets.jui.CJuiDatePicker', array(
    // 'name'=>'birthdate',
	 //'id'=>'User_promotionTo',
	 'model'=>$model,
     'attribute'=>"promotionTo", // the name of the field
     //'value'=>$model->issued_date,  // pre-fill the value
     // additional javascript options for the date picker plugin
     'options'=>array(
            'dateFormat'=>'yy-mm-dd',
            'defaultDate'=>date('Y-m-d'),
            //'buttonImage'=>Yii::app()->baseUrl.'/images/icons.date.png',
            //'buttomImageOnly'=>true,
            'buttonText'=>'Select',
            'showAnim'=>'fold',
            'showOn'=>'button',
            'showButtonPanel'=>false,
            'yearRange'=>'1900',
     'debug'=>true,

    ),
     'htmlOptions'=>array(
     'style'=>'height:20px;'
     ),
     ));
				  ?>
                    <?php echo $form->error($model,'promotionTo'); ?></td>
                </tr>
              </table></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <div class="row">
      <fieldset class="current">
        <legend><strong>Featured</strong></legend>
        <br />
        <br />
        <table width="800" border="0" cellspacing="2" cellpadding="3" >
          <tr>
            <td width="199"><?php echo $form->labelEx($model,'isFeaturedItem'); ?> <?php echo $form->checkBox($model,'isFeaturedItem',array('value'=>'Y', 'uncheckValue'=>'N')); ?> <?php echo $form->error($model,'isFeaturedItem'); ?></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <br />
    <div class="row">
      <fieldset class="current">
        <legend><strong>Specifications</strong></legend>
        <br />
        <br />
        <table width="800" border="0" cellspacing="2" cellpadding="3" >
          <tr>
            <td ><?php echo $form->textArea($model,'specifications',array('class'=>'ckeditor','cols'=>"80",'rows'=>"10")); ?><?php echo $form->error($model,'specifications'); ?></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <br />
    <div class="row">
      <fieldset class="current">
        <legend><strong>Overview</strong></legend>
        <br />
        <br />
        <table width="800" border="0" cellspacing="2" cellpadding="3" >
          <tr>
            <td><?php echo $form->textArea($model,'overview',array('class'=>'ckeditor','cols'=>"80",'rows'=>"10")); ?><?php echo $form->error($model,'overview'); ?></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <br />
    <div class="row">
      <fieldset class="current">
        <legend><strong>Description</strong></legend>
        <br />
        <br />
        <table width="800" border="0" cellspacing="2" cellpadding="3">
          <tr>
            <td><?php echo $form->textArea($model,'description',array('class'=>'ckeditor','cols'=>"80",'rows'=>"10")); ?><?php echo $form->error($model,'description'); ?></td>
          </tr>
        </table>
      </fieldset>
    </div>
    <br />
    <div class="row">
      <table width="800" border="0" cellspacing="2" cellpadding="3" >
        <tr>
          <td ><table width="100%" border="0" cellspacing="2" cellpadding="2">
              <tr>
                <td height="30" align="center"><?php if($model->isNewRecord): ?>
                  <?php echo CHtml::submitButton('Save & Enter Another'); ?> &nbsp;&nbsp;&nbsp;&nbsp; <?php echo CHtml::submitButton('Save & Duplicate'); ?>
                  <?php endif; ?>
                  &nbsp;&nbsp;&nbsp; <?php echo CHtml::submitButton($model->isNewRecord ? 'Submit & Close' : 'Save & Close'); ?></td>
              </tr>
            </table></td>
        </tr>
      </table>
    </div>
    <?php $this->endWidget(); ?>
  </div>
</div>
