<script type="text/javascript">
    $(document).ready(function(){
        $("#admFrmCategory").validate({
            rules: {
                txtCategory: {required: true}
            },
            messages: {
                txtCategory: {required:"Enter Category Name"}
            }
        });
    })
</script>
<div class="comm_div">
    <div class="comm_content_block">
        <h3><?php echo $this->parentId == 0 ? "Manage Category" : "Manage Sub Categories"?></h3>


        <div  style="color:maroon;font-size: 15px " ><?php echo  $this->message; ?></div>
        <form name="admFrmCategory" id="admFrmCategory" action="<?php echo ConfigUrl::base();?>index/category" method="post">
            <table class="form_tbls" width="100%" border="0" cellspacing="0" cellpadding="0">
                <input type="hidden" name="parentId" value="<?php echo $this->parentId;?>"
                <?php if($this->parentId > 0) { ?>
                <tr>
                    <td colspan="3">
						<div class="bc">
							<ul>
								<li><a href="<?php echo ConfigUrl::base('admin')?>index/category">Categories</a> >></li>
								<li><a href="<?php echo ConfigUrl::base('admin')?>index/category/<?php echo $this->parentId;?>"><?php echo $this->parentCategoryTitle?></a></li>
							</ul>
							<!--<a href="<?php echo ConfigUrl::base('admin')?>index/category">Categories</a>
							>>
							<a href="<?php echo ConfigUrl::base('admin')?>index/category/<?php echo $this->parentId;?>"><?php echo $this->parentCategoryTitle?></a>-->
						</div>
                    </td>
                </tr>
                
                <?php } ?>
                <tr>

                    <td align="right" width="18%" style="padding-bottom:24px;"><span class="label-stl"><?php echo $this->parentId == 0 ? "Add New Category" : "Add New Sub Category";?></span></td>
                    <td width="14%">
                        <input type="text" class="txt_area_2" name="txtCategory" id="txtCategory" style="display:block;">
                        <br/>
                    </td>
                    <td style="padding-bottom:24px;">

                        <input type="submit" class="btn_styles" name="Submit" value="Add">
                    </td>
                </tr>
                 <?php if($this->parentId== 0) { ?>
                    <tr>
                        <td colspan="3">Click on a category name to add/ view sub-categories</td>
                    </tr>
                  <?php
                 }?>

            </table>
        </form>
        <!-- Category Listing -->
        <div class="comm_content_block_items">
            <table class="list_table_new" width="100%" border="0"  cellspacing="0" cellpadding="0"s>
                <tr class="catman_stls">
                    <td width="10%" class="cat-hd">&nbsp;&nbsp;&nbsp;&nbsp;#</td>
                    <td width="79%" class="cat-hd"><strong>Category Name</strong></td>
                    <td width="13%" class="cat-hd"><strong style="padding-left:10px ">Options</strong></td>
                </tr>
                <tr>
                    <td colspan="3">
                        <div id="example-2-1">
                            <ul class="sortable-list">
                                <?php
                                if(sizeof($this->resCategories)>0) {
                                    $i=0;
                                    foreach($this->resCategories as $row) {
                                        $i++;
                                        ?>
                                <div id="catMsg<?php echo $row->nCategoryId;?>" style="color: green"> </div>
                                <li class="sortable-item" id="<?php echo $row->nCategoryId; ?>">
                                    <table width="100%">
                                        <tr>
                                            <td width="9%" style="text-align: left"><?php echo $i?></td>
                                            <td width="82%" style="text-align: left"><div id="catVal<?php echo $row->nCategoryId;?>">
                                                            <?php if($this->parentId == 0) {?>
                                                    <a href="<?php echo ConfigUrl::base('admin')?>index/category/<?php echo  $row->nCategoryId;?>"><?php echo stripslashes($row->vCategoryDesc)?></a>
                                                                <?php } else { ?>
                                                                <?php echo stripslashes($row->vCategoryDesc); ?>
                                                                <?php } ?>
                                                </div>
                                                <div id="cat<?php echo $row->nCategoryId;?>" style="display: none">
                                                    <input type="text" class="txt_area" id="txtCat<?php echo $row->nCategoryId; ?>" name="txtCat" value="<?php echo stripslashes($row->vCategoryDesc)?>"/>&nbsp;&nbsp;
                                                    <input type="submit"  value="Save" name="btnProfile" class="btn_styles" onclick="saveCat('<?php echo $row->nCategoryId;?>')"/>&nbsp;&nbsp;
                                                    <input type="button"  value="Cancel" name="cancel" class="btn_styles"  onclick="cancelCat('<?php echo $row->nCategoryId;?>')">
                                                </div>

                                            </td>
                                            <td width="2%"  style="text-align: left"><a class="lnk_styles" href="javascript:void(0)" onclick=" editCat('<?php echo $row->nCategoryId;?>')">Edit</a></td>
                                            <td width="9%"  style="text-align: left"><a class="lnk_styles" href="javascript:void(0)" onclick=" deleteCat('<?php echo $row->nCategoryId;?>')">Delete</a></td>
                                        </tr>
                                    </table>
                                </li>

                                        <?php
                                    }
                                }else {
                                    ?> <table width="100%">
                                    <tr>
                                        <td width="9%" style="text-align: left"><?php echo "No Records Found";?></td>
                                    </tr>
                                </table>

                                    <?php } ?>
                            </ul>
                        </div>
                    </td>
                </tr>

            </table>
            <div class="clear"></div>
        </div></div></div>
<script type="text/javascript" src="<?php echo ConfigUrl::root()?>public/js/library/jquery-ui-1.8.custom.min.js"></script>
<script type="text/javascript" src="<?php echo ConfigUrl::root()?>public/js/library/jquery.cookie.js"></script>
<!-- category listing Ends-->
<!-- Example jQuery code (JavaScript)  -->
<script type="text/javascript">

    $(document).ready(function(){



        // Get items
        function getItems(exampleNr)
        {
            var columns = [];

            $(exampleNr + ' ul.sortable-list').each(function(){
                columns.push($(this).sortable('toArray').join('~'));
            });

            return columns.join('|');
        }
        // Example 2.3: Save items automaticly
        $('#example-2-1 .sortable-list').sortable({
            connectWith: '#example-2-3 .sortable-list',
            update: function(){
                // alert(getItems('#example-2-1'));
                //$.cookie('cookie-b', getItems('#example-2-1'));
                var catorder	=	'catorder='+getItems('#example-2-1');
                $.post("<?php echo ConfigUrl::base()?>index/updatecategoryorder", catorder, function(response){
                    if(response=="success")
                        alert("Categeory order saved successfully")
                });
            }
        });

    });
    function editCat(id)
    {
        $("#cat"+id).show("slow");
        $("#catVal"+id).hide("slow");
    }
    function deleteCat(id)
    {

        if(confirm("Are you want to delete this category ?")) {
            var data 	= 'id='  + id;
            $.ajax({
                url: "<?php echo ConfigUrl::base(); ?>index/deleteCategory/",
                type: "POST",
                data: data,
                cache: false,
                success: function (html) {
                    if (html==1) {
                        alert('Category deleted Successfully');
                        $("#"+id).hide("slow");


                    }
                    else if(html==2)
                    {
                        alert('Please remove the products added under this category.');
                    }
                    else alert('Sorry, unexpected error. Please try again later.');
                }
            });

        }

    }

    function saveCat(id)
    {

        var cat = $("#txtCat"+id).val();
        if(cat==""){
            $("#catMsg"+id).html('Please enter category');
            $("#catMsg"+id).fadeIn("Slow");
            setTimeout(function() {$("#catMsg"+id).fadeOut("slow");$("#catMsg"+id).html('')},2000);
        }
        else
        {
            var data 	= 'id='  + id+'&cat='+cat;
            $.ajax({
                url: "<?php echo ConfigUrl::base(); ?>index/updateCategory/",
                type: "POST",
                data: data,
                cache: false,
                success: function (html) {
                    if (html==1) {
                        $("#cat"+id).hide("slow");
                        $("#catVal"+id).html(cat);
                        $("#catVal"+id).show("slow");
                        $("#catMsg"+id).html('<font color="green">Category updated Successfully</font>');
                        $("#catMsg"+id).fadeIn("Slow");
                        setTimeout(function() {$("#catMsg"+id).fadeOut("slow");$("#catMsg"+id).html('')},2000);
                        return false;
                    }
                    else if (html==2) {
                        $("#catMsg"+id).html('Already existing category');
                        $("#catMsg"+id).fadeIn("Slow");
                        setTimeout(function() {$("#catMsg"+id).fadeOut("slow");$("#catMsg"+id).html('')},2000);
                    }
                    else alert('Sorry, unexpected error. Please try again later.');
                }
            });
        }
    }
    function cancelCat(id)
    {
        $("#cat"+id).hide("slow");
        $("#catVal"+id).show("slow");
    }
</script>




