<style>

    .clsSearch{

        z-index: 999999999999 !important;

    }

</style><?php



$cs = Yii::app()->getClientScript();

$cs->registerScript('multiSelectSerch', '

  $(document).ready(function() {

    $(".clsSearch").multiselect({

      enableFiltering: true,

      enableCaseInsensitiveFiltering: true,

    });

  });', CClientScript::POS_HEAD);

$cs->registerScript("addTablelist", " function addTablelist(jQtable) {

        formId              =   jQtable.attr('id');

        formIdStarter       =   jQtable.attr('id').split(/_(.+)?/)[0];

        lastId = jQtable.find('tr:last input:first').attr('id');

        thenum = lastId.match(/\d+/g);

        newId = Number(thenum) + 1;

        newrow= jQtable.find('tr:last').clone();

        newrow.find('label:first').html(newId+1);

        //newrow.find('.multiselect').removeClass('multiselect');

        newrow.find('.multiselect').siblings().remove();

        //newrow.find('.btn-select-group').siblings().remove();

        newrow.attr('class',newId);

        newrow.find('div,input,textarea,checkbox,td,select,.errorMessage,a,span').each(function() {

            this.id= this.id.replace(/\d+/,newId);

           if(!$(this).is(':checkbox'))

                this.value  = '';

            else

                $(this).prop('checked', false);

            (this.name!==undefined) ? this.name= this.name.replace(/\d+/,newId) : this.style  =   '';});

        jQtable.append(newrow);


  

        var multiselectId = '#KitItems_' + newId + '_product_id';
    if (!$(multiselectId).data('multiselect')) {
        $(multiselectId).multiselect({
            enableFiltering: true,
            enableCaseInsensitiveFiltering: true
        });
    }

            

}", CClientScript::POS_HEAD);

$cs->registerScript("removeTableitem", " function removeTableitem(jQtable,evt) {

        formIdStarter   =   jQtable.attr('id').split(/_(.+)?/)[0];

        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,select,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('Atleast One item needed here'); 

        

    }", CClientScript::POS_HEAD);

?>

<div class="popup_inner_element">

    <?php

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

        'id' => 'kit-form',

        'enableAjaxValidation' => false,

        'enableClientValidation' => false,

    ));

    ?>





    <div id="div_project_type_detl">

        <?php echo $form->hiddenField($model, 'id'); ?>



        <div class="table-responsive" style="min-height:400px;">

            <table class="table table-hover table-bordered personal-task" id="tbl_module_item">

                <thead>

                    <tr>

                        <th>No</th>

                        <th width="40%">GMC</th>

                        <th width="30%">Model</th>

                        <th>Quantity</th>

                        <!--<th>PO Status</th>-->

<?php   





$count = (empty($modelKitItems[0]['product_id']))? '0' : count($modelKitItems);



  if((int)$count <=0 ){ ?>

                        <th width="3%" class="text-center">

                            <a class="btn btn-sm btn-theme " href="javascript:void(0);" onclick="addTablelist($('#tbl_module_item'));"><i class="fa fa-plus"></i></a>

                        </th>

                        <?php }?>

                    </tr>

                </thead>

                <tbody>



                    <?php 

                    

                   

                 

                    foreach ($modelKitItems as $i => $Details) { ?>

                        <tr>

                            <td> <?php echo $form->hiddenField($Details,"[" . $i . "]id");?><label><?php echo ($i + 1); ?> </label></td>

                            

                            <td>

                                <?php

                                $modelInventory = new Inventory();

                                ?>

                                <?php echo $form->dropdownlist($Details, "[" . $i . "]product_id", $modelInventory->getGMCWithoutKit(), array('class' => 'form-control go clsSearch','prompt' => '-select-', 'data-search' => true,));

                                ?>

                            </td>

                            <td> <?php echo $form->textField($Details, "[" . $i . "]category_id", array('class' => 'form-control go ','readonly'=>'true'));

                                ?></td>





                            <td>

                                <?php echo $form->textField($Details, "[" . $i . "]quantity", array('class' => 'tooltipster go'));

                                ?>

                            </td>





                        <?php   if((int)$count <=0 ){ ?>

                            <td>

                                <?php

                              

                                    echo CHtml::link('<i class="fa fa-times"></i>', 'javascript:void(0);', array('class' => 'btn btn-sm btn-danger', 'id' => $i, 'onclick' => 'removeTableitem($(\'#tbl_module_item\'),this,this.id)', 'title' => 'Remove this item'));

                               

                                ?>

                            </td>



                         <?php } ?>

                        </tr>

                    <?php } ?>

                    </tr>



                </tbody>

            </table>

        </div>





    </div>



    <div class="modal-footer">

        <button type="button" class="btn btn-default" id="inventory_close" data-dismiss="modal">Close</button>

        <?php

        $url = Yii::app()->createUrl('inventory/SaveInventoryKits');

        if ($type != 'view') {





            echo CHtml::ajaxSubmitButton(

                'Save',

                $url,

                array(

                    'dataType' => 'json',

                    'type' => 'post',

                    'success' => 'function(data) {

            $("#loader").hide();

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

                    t8.success("Kit Items Saved Successfully!");

                    $("#inventory_close").click();

                    reloadTable("#tbl_inventory_details");

                } else { 

                    $("#kit-form .kit-save-btn").prop("disabled", false);                      

                    $("#kit-form .kit-save-btn").addClass("btn-theme").removeClass("btn_loading"); 

                    $.each(data, function(key, val) {

                        t8.error(""+val+""); 

                    }); 

                } 

        }',

                    'beforeSend' => 'function(){ 

            $("#loader").show();

            $(".toast8-error").hide();

            $("#kit-form .kit-save-btn").prop("disabled", true);                      

            $("#kit-form .kit-save-btn").addClass("btn_loading").removeClass("btn-theme"); 



        }'

                ),

                array('id' => 'mybtn' . rand(), 'class' => 'kit-save-btn btn btn-theme')

            );

        }

        ?>

    </div>

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