<?php $this->load->view('common/head')?>
<link href="//cdnjs.cloudflare.com/ajax/libs/summernote/0.8.7/summernote.css" rel="stylesheet">
<body>
<div class="page-container list-menu-view">
<!--Leftbar Start Here -->
<?php $this->load->view('common/sidebar')?>

    <div class="page-content">
    <!--Topbar Start Here -->
    <?php $this->load->view('common/header') ?>
        <div class="main-container">
            <div class="container-fluid">
                <div class="page-breadcrumb">
                    <div class="row">
                        <?php if(validation_errors()!=''){?>
                    <div class="alert alert-danger alert-dismissible">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        <?php echo validation_errors(); ?>
                    </div>
                        <?php }?>
                        <div class="col-md-7">
                            <div class="page-breadcrumb-wrap">
                                <div class="page-breadcrumb-info">
                                    
                                    <ul class="list-page-breadcrumb">
                                        <li><a href="#">Home</a>
                                        </li>
                                        <li class="active-page"> Add Products</li>
                                    </ul>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-5">

                        </div>
                    </div>
                </div>
                 <?php 
                    if($this->session->flashdata('msg')!=''){
                        echo '<div class="alert alert-success alert-dismissible">
                              <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                              <strong>'.$this->session->flashdata('msg').'</strong> 
                            </div>';
                    }
                    if($this->session->flashdata('err')!=''){
                        echo '<div class="alert alert-danger alert-dismissible">
                              <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                              <strong>'.$this->session->flashdata('err').'</strong> 
                            </div>';
                    }
                ?>
                <div class="row">
                    <div class="col-md-12 ">
                    <div class="box-widget widget-module">
                            <div class="widget-head clearfix">
                                <span class="h-icon"><i class="fa fa-bars"></i></span>
                                <h4>Add Products</h4>
                            </div>
                            <div class="widget-container">
                                <div class=" widget-block">
                                    <form action="<?= base_url('admin/products/edit/'.$products['id'])?>" method="post" enctype="multipart/form-data">
                                         <legend>Add Products</legend>
                                   
                                    <div class="form-group">
                                        <label>Category</label>
                                         <select class="form-control" name="category" required onchange="get_subcategory(this.value)">
                                             <option value="">Select Products</option>
                                             <option <?php if($products['category']=='Men'){ echo 'selected'; } ?>>Men</option>
                                             <option <?php if($products['category']=='Women'){ echo 'selected'; } ?>>Women</option>
                                             <option <?php if($products['category']=='Kids'){ echo 'selected'; } ?>>Kids</option>
                                             <option <?php if($products['category']=='Toys'){ echo 'selected'; } ?>>Toys</option>
                                             <option <?php if($products['category']=='Others'){ echo 'selected'; } ?>>Others</option>
                                         </select>
                                     </div>

                                     <div class="form-group">
                                        <label>Sub Category</label>
                                         <select class="form-control" name="subcategory" required id="subcategory">
                                             <option value="">Select SubCat</option>
                                            <?php 
                                                $sub=$this->db->where('category',$products['category'])->get('category')->result_array();
                                                foreach($sub as $s)
                                                { ?>
                                                    <option <?php if($s['id']==$products['subcategory']){ echo 'selected'; } ?> value="<?= $s['id']; ?>"><?= $s['name']?></option>
                                              <?php  }
                                            ?>
                                         </select>
                                     </div>

                                     <div class="form-group">
                                        <label>Name</label>
                                         <input type="text" name="name" value="<?= $products['name']?>" class="form-control"  required="">
                                     </div>

                                     <div class="form-group">
                                        <label>Price</label>
                                         <input type="text" name="price" value="<?= $products['price']?>" class="form-control"  required="">
                                     </div>

                                      
                                       <div class="form-group">
                                        <label>Image</label>
                                         <input type="file" name="image" value="<?= $products['image']?>" class="form-control"  >
                                     </div>
                                     
                                      <div class="form-group">
                                        <label>Sort Order</label>
                                         <input type="number" value="<?= $products['sort']?>" name="sort" class="form-control" >
                                     </div>

                                     <div class="form-group">
                                        <label> Status</label>
                                         <select class="form-control" name="status">
                                             <option <?php if($products['status']=='Active'){ echo 'selected'; }?>>Active</option>
                                             <option <?php if($products['status']=='Inactive'){ echo 'selected'; }?>>Inactive</option>
                                         </select>
                                     </div>
                                     <input type="submit" name="" class="btn btn-primary" value="Submit">
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    </div>
                  <?php $this->load->view('common/foot'); ?>
        </div>
    </div>

    <?php $this->load->view('common/footer')?>

<script src="//cdnjs.cloudflare.com/ajax/libs/summernote/0.8.7/summernote.js"></script>

<script type="text/javascript">
    function get_subcategory(id='')
    {
        $=jQuery;
        $.ajax({
    type: "POST",
    url: "<?= base_url('Products/getCategory')?>",
    data: { cat : id },
    dataType: "json",
    success: function(result){
        $('#subcategory').empty();
        $("#subcategory").append('<option value="">Select SubCategory</option>');
        var opt='';
       $.each(result,function(k,v){
        opt+='<option value="'+v.id+'">'+v.name+'</option>';
       });
       $('#subcategory').append(opt);
    }
});
    }
</script>