<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'goodsin_upload-form',
    'enableAjaxValidation' => false,
    'enableClientValidation' => false,
));
?>
<div class="tab-content">
    <div class="tab-pane active">
        <ul class="clearfix form-ul material">
            <?php echo $form->hiddenField($model, 'id'); ?>
            <?php 
            $model->type ='GoodsIn';
            echo $form->hiddenField($model, 'type'); ?>
            <li class="col-sm-4">
                <?php
                if ($model->year == '') {
                    $year = date('Y');
                } else {
                    $year = $model->year;
                }
                $option = array($year => array('selected' => 'selected'));
                ?>  
                <?php echo $form->dropDownList($model, 'year',Order::getyear(), array('data-label' => 'Year <span style="color:red">*</span>','empty'=>'-Select-','onchange'=>'checkHistory();','options' => $option)); ?>
            </li>
            <li class="col-sm-4">
                <?php echo $form->dropDownList($model, 'month', Order::getmonths(), array('data-label' => 'Month <span style="color:red">*</span>','empty'=>'-Select-','onchange'=>'checkHistory();')); ?>
            </li>
            
            <li  class="col-sm-4">
                <?php
                echo CHtml::label('Document', 'Document');
                $allowedExtensions = array("csv");
                $this->widget('ext.EAjaxUpload.EAjaxUpload', array('id' => rand(),
                    'config' => array('action' => Yii::app()->createUrl(Yii::app()->controller->id . '/UploadDocuments', array('folder' => 'upload/goodsin')),
                        'multiple' => false, 
                        'allowedExtensions' => $allowedExtensions,
                        'sizeLimit' => 1 * 1024 * 1024, 
                        'onStart' => "js:function(){}", 
                        'class' => 'pull-right', 
                        'onComplete' => "js:function(id, fileName, responseJSON){ 
                           // alert(fileName);
                        $('.icon_images').html('<input type=\"hidden\" value=\"'+responseJSON.filename+'\" name=\"InventoryUploads[file_name]\">');                                                                    "
                        . "}",)));
                ?>        
                <div class="icon_images">
                
                </div>
            </li>
            <?php
    if($flag==1){
        echo '<span style="color:red;align:center">GMC have empty GoodsIn Month & Year. Please Fill it.</span>';
    }
    ?>
        </ul>
    </div>
</div>
<div class="modal-footer">
    
    <button type="button" class="btn btn-default" id="goodsin_upload_close" data-dismiss="modal">Close</button>
    <?php if($flag!=1){
    $url = Yii::app()->createUrl('inventory/SaveUploadGoodsIn');
    echo CHtml::ajaxSubmitButton('Save', $url, array(
        'dataType' => 'json',
        'type' => 'post',
        'success' => 'function(data) {
            $("#loader").hide();
                if(data.status=="success"){  
                    t8.success("Goods In Uploaded Successfully!");
                    $("#goodsin_upload_close").click();
                    reloadTable("#tbl_uploadgoodsin_details");
                } else { 
                    $("#goodsin_upload-form .goodsin_upload-save-btn").prop("disabled", false);                      
                    $("#goodsin_upload-form .goodsin_upload-save-btn").addClass("btn-theme").removeClass("btn_loading"); 
                    $.each(data, function(key, val) {
                        t8.error(""+val+""); 
                    }); 
                } 
        }',
        'beforeSend' => 'function(){ 
            $("#loader").show();
            $(".toast8-error").hide();
            $("#goodsin_upload-form .goodsin_upload-save-btn").prop("disabled", true);                      
            $("#goodsin_upload-form .goodsin_upload-save-btn").addClass("btn_loading").removeClass("btn-theme"); 

        }'
    ), array('id' => 'mybtn' . rand(), 'class' => 'goodsin_upload-save-btn btn btn-theme'));
    }
    ?>
</div>
<?php $this->endWidget(); ?>
<?php
$cs = Yii::app()->getClientScript();

$cs->registerScript("checkHistory", "function checkHistory(){
    var year = $('#InventoryUploads_year').val();
    var month = $('#InventoryUploads_month').val();
    if(year!='' && month!=''){
        jQuery.ajax({
          'dataType'  : 'json',
          'type'  : 'post',
          'data'  : 'month=' +month+'&year=' + year+'&YII_CSRF_TOKEN=".Yii::app()->request->csrfToken."',
          'url'  : $('#liveSite').val()+'index.php?r=inventoryupload/checkHistory',
          'cache': false,
          'success':function(data){
            if(data.msg!=''){
                t8.info(data.msg);
            }
           }
        });
    }
}", CClientScript::POS_HEAD);?>