<!-- Main Content -->

<div class="page-wrapper">

    <div class="container-fluid pt-25">

    	<div class="row">

    		<div class="col-md-12">

    			<div class="alert alert-success alert-dismissible" role="alert" <?php if($this->session->flashdata('message')==''){ ?> style="display:none" <?php } ?> >

          			<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>

          			<?php echo $this->session->flashdata('message'); ?>

        		</div>

    		</div>

    	</div>

		<!-- Row -->

		<div class="row mb-10">
			<div class="col-sm-12">
				<?php if(isset($_GET) && $_GET){ ?>
					<h5>Apply Filter :</h5>
					<?php if(isset($_GET['product_name']) && $_GET['product_name']!=''){ ?>
						<p><strong>Product Name : </strong> <?php echo $_GET['product_name']; ?></p>
					<?php } ?>
					<?php if(isset($_GET['product_sku']) && $_GET['product_sku']!=''){ ?>
						<p><strong>Product Serial No. : </strong> <?php echo $_GET['product_sku']; ?></p>
					<?php } ?>
					<?php if(isset($_GET['brandid']) && $_GET['brandid']!=''){ 
						$pb = $this->common->getsData("brand",array('brandid'=>$_GET['brandid']),array('brand_name'));
					?>
						<p><strong>Brand : </strong> <?php echo $pb['brand_name']; ?></p>
					<?php } ?>
					<?php if(isset($_GET['product_category']) && $_GET['product_category']){ 
						$pc = $this->db->query("select category from category where c_id IN (".implode(',',$_GET['product_category']).")")->result_array();
						$pc = array_column($pc, 'category');
						$pc = implode(' | ', $pc);
					?>
						<p><strong>category : </strong> <?php echo $pc; ?></p>
					<?php } ?>
					<p><a href="<?php echo base_url(A.'/product-list'); ?>" class="btn btn-primary btn-sm" style="position: absolute;top: 0;right: 15px;">Clear Filter</a></p>
				<?php } ?>
			</div>
		</div>

		<div class="row">

			<div class="col-sm-12">

				<div class="panel panel-default card-view">

					<div class="panel-heading">

						<div class="pull-left">

							<h6 class="panel-title txt-dark">Product List</h6>

						</div>
						<span class="pull-right">
							<a class="btn btn-info" data-toggle="modal" data-target="#searchModal"><i class="fa fa-search"></i>  Search</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<!-- <a class="btn btn-primary" data-toggle="modal" data-target="#csvModal"><i class="fa fa-cloud-upload"></i>  Upload CSV</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	                        <a class="btn btn-warning" href="<?php //echo base_url(A.'/downloadProductCsvFormat'); ?>"><i class="fa fa-cloud-download"></i>  Download Blank CSV</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -->
	                        <!-- <a href="<?php //echo base_url(A."/downloadProductExcel"); ?>" class="btn btn-primary">Export <i class="fa fa-file-pdf-o" style="color:#fff;"></i></a> -->
	                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	                        <?php if($this->admin_type=='superadmin' || (in_array('3.1', $this->authority))){ ?>
                            	<a href="<?php echo base_url(A.'/add-product'); ?>" class="btn btn-success">Add Product <i class="fa fa-plus" style="color:#fff;"></i></a>
                            <?php } ?>
                        </span>

						<div class="clearfix"></div>

					</div>

					<div class="panel-wrapper collapse in">

						<div class="panel-body">
							<?php if($this->admin_type=='superadmin' || (in_array('3.2', $this->authority))){ ?>
								
								<div class="table-wrap">

									<div class="table-responsive">

										<table id="datable_1-1" class="table table-hover display  pb-30" >
											<thead align="center">
	                                            <tr>
	                                            	<td colspan="5"><?php echo (isset($h1)?$h1:""); ?></td>
	                                                <td colspan="4">
	                                                    <div class="col-md-8">&nbsp;</div>
	                                                    <div class="col-md-4">
	                                                        <select id="admin_length" class="pull-right form-control">
	                                                            <option value="10" <?php echo ($this->perPage==10)?'selected':""; ?>>10</option>
	                                                            <option value="20" <?php echo ($this->perPage==20)?'selected':""; ?>>20</option>
	                                                            <option value="50" <?php echo ($this->perPage==50)?'selected':""; ?>>50</option>
	                                                            <option value="100" <?php echo ($this->perPage==100)?'selected':""; ?>>100</option>
	                                                            <option value="250" <?php echo ($this->perPage==250)?'selected':""; ?>>250</option>
	                                                            <option value="500" <?php echo ($this->perPage==500)?'selected':""; ?>>500</option>
	                                                        </select>
	                                                    </div>
	                                                </td>
	                                            </tr>
	                                            <tr><td colspan="5" style="color: red;"><?php echo (isset($h2)?$h2:""); ?></td>
	                                                <td colspan="4">
	                                                </td>
	                                            </tr>
	                                        </thead>
											<thead>

								                <tr>

								                  <th>SR.No.</th>

								                  <!-- <th>Productid</th> -->

								                  <th>Name</th>

								                  <th>Image</th>

								                  <th>Serial No.</th>

								                  <th>Category</th>

								                  <th>Price</th>

								                  <th>Stock</th>

								                  <!-- <th>Shop Sales</th> -->

								                  <th>Featured products</th>
								                  <!-- <th>Shop Rewards</th> -->

								                  <th>Action</th>

								                </tr>

								            </thead>



								            <tbody>

								            	<?php //echo "<pre>"; print_r($post); ?>

								                <?php if(!empty($post)){ foreach ($post as $key => $value): 

								                $cat = explode(',',$value['product_category']);

								                $c = count($cat);

								                ?>

								                <tr id="<?php echo "products_".$value['productid']; ?>">

								                  <td><?php echo $k++; ?></td>

								                  <!-- <td><?php //echo $value['productid']; ?></td> -->

								                  <td><a href="<?php echo base_url('product-detail/'.$value['product_slug'].'/'.$value['productid']); ?>" target="_blank"><?php echo $value['product_name']; ?></a></td>

								                  <td>

								                  	<?php $image = $this->common->getsData('product_media',array('product_id'=>$value['productid'],'isThumb'=>1)); 

								                  	//print_r($image);

								                  	//echo $this->db->last_query();

								                  	?>



								                    <?php if(!empty($image)){ ?>

								                      <img src="<?php echo base_url($image['product_image']); ?>" style="height:50px; width:50px;" alt="<?php echo $value['product_name']; ?>">

								                    <?php }else{

								                      echo "No image Found!";

								                    } ?>

								                  </td>

								                  <td>
								                  	<?php 
									                	echo $value['product_sku'];
									                ?>
								                  </td>

								                  <td>

								                      <?php if(($cat) && !empty($category)){ 

								                          foreach ($category as $key => $value1) {

								                            for($cc=0;$cc<$c; $cc++){

								                              if($value1['c_id']==$cat[$cc]){

								                                echo (isset($value1['category']))?$value1['category']:'';

								                              }

								                            }

								                          }

								                        } 

								                      ?>

								                  </td>

								                  <td>$ <?php echo $value['product_price']; ?></td>

								                  <td>

								                    <strong class="grey-text text-darken-2">

								                         <?php echo $value['product_stock']; ?>

								                    </strong>

								                  </td>

								                  <!-- <td>
								                  	<?php /*if($this->admin_type=='superadmin' || (in_array('3.6', $this->authority))){ ?>
								                  		<input type="checkbox" name="best_selling" class="best_selling" value="1" data-id="<?php //echo $value['productid']; ?>" <?php //echo ($value['best_selling']==1)?'checked':""; ?> />
								                  	<?php }*/ ?>
								                  </td> -->

								                  <td>
								                  	<?php if($this->admin_type=='superadmin' || (in_array('3.5', $this->authority))){ ?>
								                  		<input type="checkbox" name="feat_prod" class="featured_products" value="1" data-id="<?php echo $value['productid']; ?>" <?php echo ($value['feat_prod']==1)?'checked':""; ?> />
								                  	<?php } ?>
								                  </td>

								                  <!-- <td><input type="checkbox" name="shop_rewards" class="shop_rewards" value="1" data-id="<?php //echo $value['productid']; ?>" <?php //echo ($value['shop_rewards']==1)?'checked':""; ?> /></td> -->

								                  <td>

								                    <!-- <a href="<?php //echo base_url(A."/sendMailToNewsletterSubsciber?productid=".$value['productid']); ?>" class="btn btn-info btn-sm btn-square mb-5" style="background-color:green !important;"><i class="fa fa-share fa-lg"></i></a> -->
								                    <?php if($this->admin_type=='superadmin' || (in_array('3.3', $this->authority))){ ?>
								                    	<a href="<?php echo base_url(A."/edit-product/".$value['productid']); ?>" class="text-inverse pr-10 mb-5" data-value="">
								                    		<button class="btn btn-info btn-icon-anim btn-square mb-5">
								                    			<i class="fa fa-pencil fa-lg"></i>
								                    		</button>
								                    		</a><!-- text-inverse pr-10 -->
								                    <?php } ?>
								                    <?php if($this->admin_type=='superadmin' || (in_array('3.4', $this->authority))){ ?>
								                    	<a href="javascript:void(0)" class="text-inverse product_delete" data-table="products" data-value="<?php echo "productid_".$value['productid']; ?>" data-id="<?php echo $value['productid']; ?>" data-image="<?php if(!empty($image)){ echo base_url($image['product_image']);} ?>">
								                    		<button class="btn btn-danger btn-icon-anim btn-square">
								                    			<i class="fa fa-trash fa-lg"></i>
								                    		</button>
								                    	</a>
								                    <?php } ?>

								                  </td>

								                </tr>

								                <?php endforeach; } ?>

								            </tbody>

										</table>

									</div>

								</div>
							<?php } ?>
						</div>
						<div class="panel-footer clearfix">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="text-center"><?php echo (isset($links))?$links:""; ?></div>
                                </div>
                            </div>
                        </div>

					</div>

				</div>	

			</div>

		</div>

		<!-- /Row -->

	</div>
<div id="searchModal" class="modal fade" role="dialog">
	<div class="modal-dialog">

	  <!-- Modal content-->
	  <div class="modal-content">
	    <div class="modal-header">
	      <button type="button" class="close" data-dismiss="modal">&times;</button>
	      <h4 class="modal-title">Search Product</h4>
	    </div>
	    <form action="<?php echo base_url(A."/product-list"); ?>" method="get" enctype="multipart/form-data">
	      <div class="modal-body">
	          <div class="form-group">
	          	<label>Product Name</label>
	            <input type="text" name="product_name" class="form-control" placeholder="Enter Product Name" />
	          </div>
	          <div class="form-group">
	          	<label>Product Serial No.</label>
	            <input type="text" name="product_sku" class="form-control" placeholder="Enter Product Serial No." />
	          </div>
	          <div class="form-group">
	          	<label>Product Category</label>
	            <div class="col-sm-12" style="overflow:scroll; height:300px;border: 1px solid #ddd;"><br>
                	<ul class="tristate" style="list-style-type:disc;margin-left:20px;line-height: 35px;">
                        <?php if(isset($allcategory) && !empty($allcategory)){ 
                        	echo postMenu($allcategory);
                        } ?>
                    </ul>
                </div>
	          </div>
	      </div>
	      <div class="modal-footer">
	        <input type="submit" class="btn btn-success" value="Submit">
	      </div>
	    </form>
	  </div>

	</div>
</div>

<div id="csvModal" class="modal fade" role="dialog">
	<div class="modal-dialog">
	  <div class="modal-content">
	    <div class="modal-header">
	      <button type="button" class="close" data-dismiss="modal">&times;</button>
	      <h4 class="modal-title">Select Your CSV File</h4>
	    </div>
	    <form action="<?php echo base_url(A."/importCsv"); ?>" method="post" enctype="multipart/form-data">
	      <div class="modal-body">
	        <p></p>
	          <div class="form-group">
	            <input type="file" name="csv">
	          </div>
	        <p></p>
	      </div>
	      <div class="modal-footer">
	        <input type="submit" class="btn btn-success" value="Submit">
	        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
	      </div>
	    </form>
	  </div>

	</div>
</div>

<script type="text/javascript">

	var site_url = "<?php echo base_url(); ?>";

	var urlLink = "<?php echo A; ?>";



	$('.best_selling').change(function(){

	    var td = this.checked ? '1' : '0';

	    var pid = $(this).attr('data-id');

	    

	    $.ajax({

	        url: site_url+urlLink+"/setBestSellingProducts",

	        type: "POST",

	        data: "productid="+pid+"&best_selling="+td,

	        success: function(data){ 

	        	alert('Product Updated'); 

	        }

	    });

	});



	$('.featured_products').change(function(){

	    var td = this.checked ? '1' : '0';

	    var pid = $(this).attr('data-id');

	    

	    $.ajax({

	        url: site_url+urlLink+"/setFeatProdProducts",

	        type: "POST",

	        data: "productid="+pid+"&feat_prod="+td,

	        success: function(data){  

	        	alert('Product Updated');

	        }

	    });

	});



	$('.shop_rewards').change(function(){

	    var td = this.checked ? '1' : '0';

	    var pid = $(this).attr('data-id');

	    

	    $.ajax({

	        url: site_url+urlLink+"/setShopRewardsProducts",

	        type: "POST",

	        data: "productid="+pid+"&shop_rewards="+td,

	        success: function(data){  

	        	alert('Product Updated');

	        }

	    });

	});


	$(".product_delete").on("click",function(){

	    var id = $(this).attr('data-id');

	    var pimage = $(this).attr('data-image');

	    var ok = confirm("Are you sure you want to delete?");

	    if (ok) {

	        $.ajax({

	            url: site_url+urlLink+'/deleteProduct',

	            type:'POST',

	            data:'id='+id+'&pimage='+pimage,

	            success: function(data){

	                if(data==1)

	                {

	                    location.reload();

	                }

	            },

	        });

	    }

	});

</script>

<script type="text/javascript">
    var path = "<?php echo base_url(A); ?>";
    $("#admin_length").on("change",function(){
        var length = $(this).val();
        $.ajax({
            url: path+"/AdminSearchLength",
            type:"POST",
            data:"length="+length,
            success: function(result){
                if(result==1){
                    window.location.reload();
                }
            }
        });
    });
</script>

<style>
	li 
	{
		list-style: none!important;
	}
	ul.tristate
	{
		margin-left: 0!important;
	}
</style>