<script>
    $(document).ready(function() {
      //    $("select").select2()
        $.each($(".select2-container"), function(i, n) {
            $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
            $(n).prepend($(n).next());
            $(n).delay(500).queue(function() {
                $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                $(this).dequeue();
            });
        });
       var oTable = $("#myTab").dataTable( {
        "iDisplayLength": 10,
		 "aLengthMenu": [5,10,25,50,100],
		 "asSorting": [ "asc"], "aTargets": [ 1 ],
		 "sPaginationType": "full_numbers", "aoColumns": [ { 
		 	"bSortable": false }, 
			null,
			null,
			null, { 
			"bSortable": false } ]});
     oTable.fnSort( [ [1,'asc'] ] );
     
    });
  
    function saveNewDepartment() {
            if ($("#paymentDetail").validationEngine('validate')) {
                if ($("#myForm").attr("data-action") == "new") {
                    $.ajax({
                        type: "POST",
                        data: {
                            code : $("#code").val(),
                            desc : $("#description").val(),
                            hod : $("#headOfDep").val()
                        },
                        url: "<?php echo base_url() ?>admin/saveNewDepartment",
                        success: function(data) {
                             if(data > 0){
                                 alert ("Departmemt insert succesffly !");
                                 location.reload();
                                // history.go(0);
                             }else 
                             {
                                 alert ("Fail to insert a new department !");
                             }
                        }
                    });
                }else {
                    $.ajax({
                        type: "POST",
                        data: {
                            code : $("#code").val(),
                            desc : $("#description").val(),
                            hod : $("#headOfDep").val(),
                            myid : $("#myForm").attr("data-deptID")
                        },
                        url: "<?php echo base_url() ?>admin/updNewDepartment",
                        success: function(data) {
                             if(data > 0){
                                 alert ("Departmemt updated succesffly !");
                                 location.reload();
                                //history.go(0);
                             }else 
                             {
                                 alert ("Fail to update a department !");
                             }
                        }
                    });
                }
            }
        }
    
    function addnewdep(){
     $("#myForm").attr("style","display:block");
     $("#myForm").attr("data-action", "new");
    
    }
    
    function cancelAction(){
         $("#myForm").attr("style","display:none");
    }
    
    function editRow(me){
        $("#myForm").attr("style","display:block");
         $("#myForm").attr("data-action", "edit");
         $("#myForm").attr("data-deptID", $(me).attr("data-myid"));
        tr = $(me).parent().parent();
         $("#headOfDep").val($(me).attr("data-hod"));
         $("#description").val($(tr).find("td").eq("2").text());
        $("#code").val($(tr).find("td").eq("1").text());
    }
    
    function deleteDep(me) {
        if (confirm("Confirm delete !?")) {
            $.ajax({
                type: "POST",
                data: {
                    rowID: $(me).attr("data-myid")
                },
                url: "<?php echo base_url() ?>admin/deleteDepartment",
                success: function(data) {
                    if (data > 0) {
                        alert("Departement deleted succesffly !");
                        var td = me.parentNode;
                        var tr = td.parentNode;
                        var tBody = tr.parentNode;
                        tBody.removeChild(tr);
                    } else {
                        alert("Fail to delete a Departement !");
                    }
                }
            });
        }
    }
    
</script>
<div id="content">                        
    <div class="wrap">

        <div class="head">
            <div class="info">
                <h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
                    <?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>
                <ul class="breadcrumb">
                    <li><a href="#"><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                    <li><a href="#"><?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                    <li class="active"><?php echo $this->lang->line('title') ?></li>
                </ul>
            </div>

            <div class="search">
                <form action="<?php echo base_url() ?>admin/search" method="post">
                    <input name="search_text" type="text" placeholder="search..."/>                                
                    <button type="submit"><span class="i-magnifier"></span></button>
                </form>
            </div>                        
        </div>  <!-- Head -->                                                                  

        <div class="content">

            <div class="row-fluid">
                <div class="span12">
                    <?php
                    $hidden = array('order_time' => date('Y-m-d H-i-s'));
                   
                    $attrib = array('id' => 'validate');
                    echo form_open('', $attrib, $hidden);
                    ?>
                    <div data-action="" data-deptID ="" class="block" id="myForm" style="display: none;">
                        <div class="head">
                            <h2><?php echo $this->lang->line('entryForm') ?></h2>
                            <ul class="buttons">                                            
                                <li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
                            </ul>                                         
                        </div>
                        <div class="content np"> <!-- Content 1 -->   
                            <div class="controls-row">
                                <div class="span2"><?php echo $this->lang->line('code') ?></div>
                                <div class="span3" id="co">
                                    <?php
                                    $data = array(
                                        'name' => 'code',
                                        'id' => 'code',
                                        'class' => 'span12 validate[required]',);
                                    
                                        echo form_input($data, set_value('code'));
                                    
                                    ?> 
                                </div>
                            </div>
                            <div class="controls-row">
                                <div class="span2"><?php echo $this->lang->line('description') ?></div>
                                <div class="span5">
                                    <textarea id="description" name="description" class="span12 validate[required] validate[maxSize[100]"></textarea>
                                </div>
                            </div>

                            <div class="controls-row">
                                <div class="span2"><?php echo $this->lang->line('headOfDep') ?></div>
                                <div class="span5">
                                    <select class="span12" id="headOfDep">
                                                <option></option>
<?php
if ($employee ) {
    foreach ($employee as $row) {
        ?>
                                                        <option value="<?php echo $row->ID; ?>"> <?php echo $row->employeeName; ?></option>
    <?php
    }
}
?>
                                            </select>
                                </div>
                            </div>
 <div class="footer">
                            <div class="side fr">
                                <div class="btn-group">
                                     <a onclick="cancelAction()" class="btn-form btn-danger-form"><?php echo $this->lang->line('cancel') ?></a>
                                     <a onclick="saveNewDepartment()" class="btn-form btn-success-form"><?php echo $this->lang->line('save') ?></a>
                               
                                </div>
                            </div>
                        </div>    
                        </div><!-- End Content-->
                       
                    </div> <!-- Block -->                                   
                                                        
                    <div class="block">
                        <div class="head">
                            <h2><?php echo $this->lang->line('ListOfDepartment') ?></h2>
                            <ul class="buttons fr">
                                <li><a href="#" onclick="addnewdep()" title="Add New"><span class="i-plus-2"></span></a></li>
                            </ul>                                      
                        </div>
                        <div class="content np table-sorting">    
                             <table id="myTab" cellpadding="0" cellspacing="0" width="100%">
                                    <thead>
                                        <tr> 
                                            <th width="5px">#</th>
                                            <th><?php echo $this->lang->line('code') ?></th>
                                            <th><?php echo $this->lang->line('description') ?></th>
                                            <th><?php echo $this->lang->line('headOfDep') ?></th>
                                            <th><?php echo $this->lang->line('action') ?></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                         $lineNo = 0;
                                        if($listDetail){
                                           
                                            foreach ($listDetail as $row){
                                        ?>
                                        <tr>
                                    <td><?php echo ++$lineNo; ?></td>
                                    <td><?php echo $row->code; ?></td>
                                    <td><?php echo $row->description; ?></td>
                                    <td><?php echo $row->hod; ?></td>
                                    <td class="tac">
                                        <a onClick="editRow(this)" data-hod ="<?php echo $row->dept_head; ?>" data-myid="<?php echo $row->ID; ?>" class="btn-icon btn-mini btn-warning-form"><i class="i-pen"></i></a>
                                        <a onClick="deleteDep(this)" data-myid="<?php echo $row->ID; ?>" class="btn-icon btn-mini btn-danger-form"><i class="i-trashcan"></i></a>
                                    </td>
                                        </tr>
                                        <?php
                                            } }
                                        ?>
                                        
                                    </tbody>
                                </table> 
                        </div><!-- End Content-->
                   
                    <div class="content np">    
                        <div class="footer">
                            <div class="side fr">
                                <div class="btn-group">
                                    
                                </div>
                            </div>
                        </div>                                    
                    </div>
                </div>
                </form>
            </div>
        </div>                        
    </div>
</div>
</div>
