<?php
/* @var $this AssetController */
/* @var $model Asset */
/* @var $form CActiveForm */
?>
<style>
    .accordion-group {
	width: 1181px !important;
    }

    #Asset_tid_id
    {
        width: 176px !important;
        border-bottom-right-radius: 0px !important;
        border-top-right-radius: 0px !important;
    }
</style>
<script language="javascript" type="text/javascript">
    var view_url = "<?php echo Yii::app()->createAbsoluteUrl("/invoice/view"); ?>";
    var create_url = "<?php echo Yii::app()->createAbsoluteUrl("/asset/create"); ?>";

</script>
<div id='dialog'></div>
<div id="loading-div" >



    <?php
    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
		'id' => 'asset_form',
		'enableAjaxValidation' => false,
	    ));

    $this->widget('bootstrap.widgets.TbAlert');
    ?>
    <div id="flash-msg">
	<br>
	<?php $this->widget('bootstrap.widgets.TbAlert'); ?>
    </div>
    <?php if ($invoice_id != '') {
    ?>

	    <div class="accordion-group">
		<div class="accordion-heading">
		    <a class="accordion-toggle" data-toggle="collapse"
		       data-parent="#accordion2" href="#collapse0">
		   <?php echo Yii::t('default', 'Saved Assets'); ?>
	    </a>
	</div>
	<div id="collapse0" class="accordion-body collapse in">
	    <div class="accordion-inner">
		<?php
		   Yii::app()->controller->renderPartial('/asset/viewAssetInInvoice', array('model' => new VAsset('search'), 'invoice_id' => $invoice_id, 'page_name' => 'saveAndAdd'));
		?>
   	    </div>
   	</div>
       </div>
       <br/>
    <?php } ?>
    <?php
	       echo $form->labelEx($model, 'tid_id');
	       $form->widget('zii.widgets.jui.CJuiAutoComplete', array(
		   'model' => $model,
		   'attribute' => 'tid_id',
		   'source' => array_values(tid::getNumbersForDropDown()),
		   'options' => array(
		       'minLength' => '0',
		       'select' => 'js:function(event, ui){ viewInvoicesInTid(ui.item.value);}',
		   ),
		   'htmlOptions' => array(
		       'disabled' => (($invoice_id != null && $invoice_id != '') || ($tid_id != null && $tid_id != '')),
		       'placeholder' => 'Search By TID No'
		   )
	       ));
	       echo TbHtml::image(Yii::app()->request->baseUrl . '/css/flags/search.png', '', array("style" => ""
		   . "display: inline-block;
                                        vertical-align: top;
                                        margin-top: 0px;
                                        margin-left: 0px;
                                        border: 1px solid #CCC;
                                        padding: 2px;                                    
                                        -webkit-border-bottom-right-radius: 6px;
                                        -webkit-border-top-right-radius: 6px;                                        
                                        -moz-border-bottom-right-radius: 6px;
                                        -moz-border-top-right-radius: 6px;
                                        border-bottom-right-radius: 6px;
                                        border-top-right-radius: 6px;",
		       )
	       );

	       $invoices = ($model->isNewRecord && !isset($model->tid_id)) ? array() : Invoice::getNumbersForDropDown($model->tid_id, $invoice_id);
	       echo $form->dropDownListControlGroup($model, 'invoice_id', $invoices, array(
		   'empty' => ($invoice_id != null && $invoice_id != '') ? null : Yii::t('default', 'Select Invoice'),
		   'disabled' => ($invoice_id != null),
	       ));

	       echo $form->textFieldControlGroup($model, 'po_number', array(
		   'size' => 15, 'maxlength' => 15,
		   'ajax' => array(
		       'type' => 'POST',
		       'data' => 'js:{po_number:$(this).val(),ajax:true}',
		       'url' => CController::createUrl('asset/processPONumber'),
		       'beforeSend' => 'js:function(){
			     $("#loading-div").addClass("loading");
			}',
		       'success' => 'js:function(data) {
		    $("#loading-div").removeClass("loading");
		    if(data != "")
			$("#Asset_segment_id").val(data);
		    }
		',
		       'error' => 'js:function(data) {
		$("#loading-div").removeClass("loading");
		}'
		   )
		       )
	       );
	       echo $form->dropDownListControlGroup($model, 'segment_id', Segment::getNamesForDropDown(), array('empty' => Yii::t('default', 'Select Segment')));

	       echo $form->textFieldControlGroup($model, 'serial_number', array('size' => 40, 'maxlength' => 40));

	       echo $form->textFieldControlGroup($model, 'asset_number', array('size' => 40, 'maxlength' => 40));

	       echo $form->textFieldControlGroup($model, 'part_number', array('size' => 40, 'maxlength' => 40));

	       echo $form->textAreaControlGroup($model, 'description', array('size' => 255, 'maxlength' => 255,));


	       echo $form->dropDownListControlGroup($model, 'asset_status', Asset::getAssetStatusOptions(), array('empty' => Yii::t('default', 'Select Asset Status')));


	       echo $form->dropDownListControlGroup($model, 'type_of_asset_id', TypeOfAsset::getNamesForDropDown($model->type_of_asset_id),
		       array(
			   'empty' => ($model->isNewRecord) ? Yii::t('default', 'Select Type of Asset') : null,
			   'readonly' => ((!$model->isNewRecord) ? true : false),
			   'ajax' => array(
			       'type' => 'POST',
			       'data' => 'js:{type_of_asset:$(this).val(),ajax:true}',
			       'url' => CController::createUrl('asset/processTypeOfAsset'),
			       'success' => 'js:function(data) {
			checkTypeOfAsset(data,"ajax");
		    }',
			   )
	       ));
    ?>
	       <div id="vehicle" style="display: none">
	<?php
	       echo $form->textFieldControlGroup($model, 'chassis_number', array('size' => 40, 'maxlength' => 40));
	       echo $form->textFieldControlGroup($model, 'plate_number', array('size' => 10, 'maxlength' => 10));


	       echo $form->labelEx($model, 'vehicle_book_expiry_date');

	       $this->widget('yiiwheels.widgets.datepicker.WhDatePicker', array(
		   'model' => $model,
		   'id' => 'vehicle_book_expiry_date',
		   'attribute' => 'vehicle_book_expiry_date',
		   'pluginOptions' => array(
		       'format' => 'yyyy-mm-dd'
		   )
	       ));
	?>

           </div>

           <div id="radioactive"  style="display: none">
	<?php
	       echo $form->textFieldControlGroup($model, 'radioactive_license_number', array('size' => 10, 'maxlength' => 10));

	       echo $form->labelEx($model, 'radioactive_license_expiry_date');

	       $this->widget('yiiwheels.widgets.datepicker.WhDatePicker', array(
		   'model' => $model,
		   'id' => 'radioactive_license_expiry_date',
		   'attribute' => 'radioactive_license_expiry_date',
		   'pluginOptions' => array(
		       'format' => 'yyyy-mm-dd'
		   )
	       ));
	?>

           </div>

           <div class="actions">
	<?php
	       echo CHtml::ajaxSubmitButton(
		       Yii::t('default', 'Save'), ($model->isNewRecord) ? array('create', 'invoice_id' => $invoice_id, 'tid_id' => $tid_id) : array('update', 'id' => $model->id), array(
		   'type' => "post",
		   'success' => 'js:function(data) {
			   if(data.indexOf("</div>") != -1){
                                $("#flash-msg").html(data);
				$("html, body").animate({ scrollTop: 0 }, "slow");
				}
                           else
                           if(data.indexOf("dialog") != -1){
                                var result = data.split("#");                                
                                $("#dialog").html(result[1]);
                                $("#dialog").dialog({
                                        width: "490px",  
                                        title: "Confirmation Message",
                                        modal: true,
                                        buttons: {
                                        Yes: function() {
                                        $("#asset_form").append("<input type=\'hidden\' name=\'dialogResult\' id=\'pass\' value=\'true\' />");
                                                $("#submit_button").click();
                                               $(this).dialog("close");
                                        },
                                        No: function() {
                                                $(this).dialog("close");
                                        }				
                                    },
                                });
                           }
                           else
                            {
                                var result = data.split("#"); 
                                window.location.href=addParam(view_url,"id",result[0]);
                             }


		    }',
		       ), array('type' => 'submit', 'class' => 'btn primary', 'name' => 'submit_button', 'id' => 'submit_button')
	       );
	?>
              	&ensp;
	<?php
	       echo CHtml::ajaxSubmitButton(
		       Yii::t('default', 'Save and Add Asset'), ($model->isNewRecord) ? array('create', 'from' => 'New', 'invoice_id' => $invoice_id, 'tid_id' => $tid_id) : array('update', 'id' => $model->id, 'from' => 'New'), array(
		   'type' => "post",
		   'success' => 'js:function(data) {
			   if(data.indexOf("</div>") != -1){
                                $("#flash-msg").html(data);
				$("html, body").animate({ scrollTop: 0 }, "slow");
				}
                           else
                           if(data.indexOf("dialog") != -1){
                                var result = data.split("#");
                                $("#dialog").html(result[1]);
                                $("#dialog").dialog({
                                width: "490px", 
                                        title: "Confirmation Message",
                                        modal: true,
                                        buttons: {
                                        Yes: function() {
                                        $("#asset_form").append("<input type=\'hidden\' name=\'dialogResult\' id=\'pass\' value=\'true\' />");
                                                $("#submit_button_new").click();
                                               $(this).dialog("close");
                                        },
                                        No: function() {
                                                $(this).dialog("close");
                                        }				
                                    },
                                });
                           }
                           else{
                           var result = data.split("#"); 
                               var url =addParam(create_url,"invoice_id",result[0]);
                                window.location.href=addParam(url,"tid_id",result[2]);
                                }
                                


		    }',
		       ), array('type' => 'submit', 'class' => 'btn primary', 'name' => 'submit_button_new', 'id' => 'submit_button_new')
	       );
	?>

              	&ensp;
	<?php echo CHtml::link(Yii::t('default', 'Cancel'), array('index'), array('class' => 'btn primary')); ?>
           </div>

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


	   </div>



	   <script type="text/javascript">



	       function viewInvoicesInTid(value) {
	   	$.ajax({
	   	    type: 'POST',
	   	    data: {
	   		'tid_id': value
	   	    },
	   	    url: '<?php echo Yii::app()->createAbsoluteUrl("asset/viewInvoiceInTid"); ?>',
	   	    beforeSend: function() {
	   		$("#loading-div").addClass("loading");
	   	    },
	   	    success: function(data) {
	   		$("#Asset_invoice_id").find("option").remove().end().append(data);
	   		$("#loading-div").removeClass("loading");
	   	    },
	   	    error: function(data) { // if error occured
	   		$("#loading-div").removeClass("loading");
	   	    }
	   	});
	       }

	       var vehicle_id = '<?php echo Setting::getVehicleTypeOfAsset(); ?>';
	       var radio_active_id = '<?php echo Setting::getRadioActiveTypeOfAsset(); ?>';

	       $(document).ready(function() {



	   	var hasError = '<?php echo $model->hasErrors() ?>';
	   	if (hasError != '1') {
	   	    $("label[for=Asset_radioactive_license_number]").addClass("required");
	   	    $("label[for=Asset_radioactive_license_number]").append('<span class="required"> *</span>');
	   	    $("label[for=Asset_radioactive_license_expiry_date]").addClass("required");
	   	    $("label[for=Asset_radioactive_license_expiry_date]").append('<span class="required"> *</span>');
	   	    //Vehicle
	   	    $("label[for=Asset_chassis_number]").addClass("required");
	   	    $("label[for=Asset_chassis_number]").append('<span class="required"> *</span>');
	   	    $("label[for=Asset_plate_number]").addClass("required");
	   	    $("label[for=Asset_plate_number]").append('<span class="required"> *</span>');
	   	    $("label[for=Asset_vehicle_book_expiry_date]").addClass("required");
	   	    $("label[for=Asset_vehicle_book_expiry_date]").append('<span class="required"> *</span>');
	   	}

	   	var type_of_asset = '<?php echo $model->type_of_asset_id; ?>';
	checkTypeOfAsset(type_of_asset, 'page');
    });

    function checkTypeOfAsset(data, from) {
	if (from != 'page') {
	    if (data == 'Vehicle') {
		$('#vehicle').attr('style', 'display:block');
		$('#radioactive').attr('style', 'display:none');
	    }
	    else
		if (data == 'RadioActive') {
		    $('#vehicle').attr('style', 'display:none');
		    $('#radioactive').attr('style', 'display:block');
		}
	    else
	    {
		$('#vehicle').attr('style', 'display:none');
		$('#radioactive').attr('style', 'display:none');
	    }
	}
	else {
	    if (data == vehicle_id) {
		$('#vehicle').attr('style', 'display:block');
		$('#radioactive').attr('style', 'display:none');
	    }
	    else
		if (data == radio_active_id) {
		    $('#vehicle').attr('style', 'display:none');
		    $('#radioactive').attr('style', 'display:block');
		}
	    else
	    {
		$('#vehicle').attr('style', 'display:none');
		$('#radioactive').attr('style', 'display:none');
	    }
	}

	if (vehicle_id == '')
	    $('#vehicle').attr('style', 'display:none');
	if (radio_active_id == '')
	    $('#radioactive').attr('style', 'display:none');
    }


</script>


