<?php  $this->load->view('master/common/header.php',$data);
$base_name = $data['base_name'];
$shop = $data['shop'];
$mlm = $data['mlm'];
if($shop == 1){
    if($mlm == 1){
        $col = 'col-sm-3';
        $pvcol = 'col-sm-3';
        $dpcol = 'col-sm-3';
    }
    else
    {
        $col = 'col-sm-3';
        $pvcol = 'col-sm-3 d-none';
        $dpcol = 'col-sm-6';
    }
}
else{
    if($mlm == 1){
        $col = 'col-sm-3';
        $pvcol = 'col-sm-6';
        $dpcol = 'col-sm-3 d-none';
    }
    else
    {
        $col = 'col-sm-4';
        $pvcol = 'col-sm-4 d-none';
        $dpcol = 'col-sm-4 d-none';
    }
}
?>

    <div class="content">
        
        <!-- Start Content-->
        <div class="container-fluid">
            
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box">
                        <div class="page-title-right">
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item"><a href="javascript: void(0);"><?= $base_name;?></a></li>
                                <li class="breadcrumb-item active text-capitalize"><?= $page;?></li>
                            </ol>
                        </div>
                        <h4 class="page-title text-uppercase"><?= $page;?></h4>
                    </div>
                </div>
            </div>     
            <!-- end page title --> 

            <div class="row">
                <div class="col-md-12 col-xl-12">
                    <div class="card-box">
                        <?php 
                                                            $result = array();
                                                            foreach ($p_color as $element) {
                                                                $result[$element['size_id']][] = $element;
                                                            }

                                                        // echo '<pre>';print_r($result);echo '</pre>';
                                                    ?>
                        <div class="row">
                            <div class="col-sm-12">
                                
                                <form class="form-horizontal" action="<?= base_url('master/product/edit_content/'.$product['p_id']);?>" method="post" enctype="multipart/form-data">
                                    <input type="hidden" name="table" value="tbl_product" />
                                    <input type="hidden" name="page" value="product/edit/<?=$product['p_id'];?>" />
                                    
                                    
                                    <h4 class="header-title mb-4">Product</h4>
                                    <div class="row">
                                        <div class="col-sm-6">
                                            <div class="form-group">
                                                <label for="tcat_id">Top Category<span class="text-danger">*</span></label>
                                                <select name="tcat_id" id="tcat_id" required="" value="<?=$product['tcat_id'];?>" class="form-control" >
                                                    <option value=""> Select Top Category</option>
                                                    
                                                    <?php if (!empty($top_category)) {?>
                                                        <?php foreach ($top_category as $row) {?>
                                                            <option value="<?=$row['tcat_id'];?>" <?=($row['tcat_id']==$product['tcat_id']) ? 'selected' : '';?>><?=$row['tcat_name'];?></option>
                                                        <?php } ?>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-sm-6">
                                            <div class="form-group">
                                                <label for="mcat_id">Mid Category <span class="text-danger">*</span></label>
                                                <select name="mcat_id"  id="mcat_id" required="" value="<?=$product['mcat_id'];?>" class="form-control" >
                                                    <option value=""> Select Mid Category</option>
                                                    <?php if (!empty($mid_category)) {?>
                                                        <?php foreach ($mid_category as $row) {?>
                                                            <option value="<?=$row['mcat_id'];?>" <?=($row['mcat_id']==$product['mcat_id']) ? 'selected' : '';?>><?=$row['mcat_name'];?></option>
                                                        <?php } ?>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-sm-6">
                                            <div class="form-group">
                                                <label for="ecat_id">End Category <span class="text-danger">*</span></label>
                                                <select name="ecat_id"  id="ecat_id" required="" value="<?=$product['ecat_id'];?>" class="form-control" >
                                                    <option value=""> Select End Category</option>
                                                    <?php if (!empty($end_category)) {?>
                                                        <?php foreach ($end_category as $row) {?>
                                                            <option value="<?=$row['ecat_id'];?>" <?=($row['ecat_id']==$product['ecat_id']) ? 'selected' : '';?>><?=$row['ecat_name'];?></option>
                                                        <?php } ?>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                    
                                        <div class="col-sm-6">
                                            <div class="form-group">
                                                <label for="p_name">Product Name <span class="text-danger">*</span></label>
                                                <input type="text" name="p_name" id="p_name" required="" value="<?=$product['p_name'];?>" class="form-control" />
                                            </div>
                                        </div>  
                                        
                                        <div class="col-sm-6">
                                            <div class="form-group">
                                                <label for="tags">Product Tags <span class="text-danger">*</span></label>
                                                <div class="tags-default">
                                                    <input type="text" name="tags" id="tags" value="<?=$product['tags'];?>" data-role="tagsinput" placeholder="Add tags Like - tag1,tag2"  class="form-control" required />
                                                </div>
                                            </div>
                                        </div>  
                                        <div class="col-sm-12 mb-2">
                                            <label for="userName" class="w-100 d-block">Product Size, Old Price, Current Price, Stock, Min. Stock <span class="text-danger">*</span><b class="btn btn-xs cursor-pointer float-right btn-info" id="btnAddSize">Add More +</b></label>
                                            <div class="parent" id="valueadd">
                                                <?php
                                                $i1=0;
                                                    foreach($product_size as $row4) {
                                                        $i1++;
                                                        $i2 = $i1-1;
                                                        if( $i2 != 0 ){ $i3[] = $i2; }
                                                ?>
                                                    <div class="row mb-2 <?=($i1==1) ? '' : 'bg-light py-2 border-bottom';?>" id="ss<?=($i1==1) ? '' : $i1;?>">
                                                        <?php
                                                            if($i1!==1){
                                                        ?>
                                                        <div class="col-sm-12"><a href="javascript:void()" onclick="rem(<?=($i1==1) ? '' : $i1;?>)" class="Delete2 btn btn-danger btn-xs float-right">X</a></div>
                                                        <?php 
                                                            }
                                                        ?>
                                                    <div class="form-group <?=$col;?>" id="size1"> 
                                                        <label for="size">Product Size<span class="text-danger">*</span></label>
                                                        <select name="size[]" id="size" class="form-control" required>
                                                            <option value=""> Select Product Size</option>
                                                            <?php
                                                                $size_id='';
                                                                if(!empty($size)){
                                                                    foreach($size as $row2){
                                                                        if($row2['size_id']==$row4['size_id']){ $size_id = $row2['size_id'];}
                                                                        
                                                                        ?>
                                                                        <option value="<?=$row2['size_id'];?>" <?=($row2['size_id']==$row4['size_id']) ? 'selected' : '';?>><?=$row2['size_name'];?></option>
                                                                        <?php
                                                                    }
                                                                }
                                                            ?>
                                                        </select>
                                                    </div>
                                                    
                                                    <div class="form-group <?=$col;?>" id="color1">
                                                        <label for="size">Product Color<!--<span class="text-danger">*</span>--></label>
                                                        <!--<select name="color[0][]" class="form-control" multiple="multiple"  id="<?=($i1==1) ? 'multi_color_new' : 'color_more_'.$i2 ;?>" data-placeholder="Select Product Color">-->
                                                        <select name="color[]" class="form-control" data-placeholder="Select Product Color">
                                                            <option value=""> Select Color</option>
                                                            <?php
                                                                if(!empty($color)){
                                                                    foreach($color as $row3){
                                                                        if($row3['color_id'] == $row4['color_id']){
                                                                            // $color_id1[$row4['id']][] = $row31;
                                                                            echo '<option value="'.$row3['color_id'].'" selected>'.$row3['color_name'].'</option>';
                                                                        }    
                                                                        else{
                                                                            echo '<option value="'.$row3['color_id'].'">'.$row3['color_name'].'</option>';
                                                                        }
                                                                    }
                                                                }
                                                            ?>
                                                        </select>
                                                    </div>
                                                    
                                                    <div class="form-group <?=$col;?>" id="price1"> 
                                                        <label for="size">Product Old Price<span class="text-danger">*</span></label>
                                                        <input type="number" step="any" class="form-control" name="p_old_price[]" placeholder="Enter MRP" value="<?=$row4['p_old_price'];?>" required />
                                                    </div>
                                                    <div class="form-group <?=$col;?>" id="price2"> 
                                                        <label for="size">Product Current Price<span class="text-danger">*</span></label>
                                                        <input type="number" step="any" class="form-control" name="p_current_price[]" placeholder="Enter Selling Price" value="<?=$row4['p_current_price'];?>" required />
                                                    </div>
                                                    <div class="form-group <?=$dpcol;?>" id="price3"> 
                                                        <label for="size">Product DP Price<span class="text-danger">*</span></label>
                                                        <input type="number" step="any" class="form-control" name="p_dp_price[]" placeholder="Enter DP Price" value="<?=$row4['p_dp_price'];?>" />
                                                    </div>
                                                    <div class="form-group <?=$pvcol;?>" id="price4"> 
                                                        <label for="size">Product GST %<span class="text-danger">*</span></label>
                                                        <input type="text" class="form-control" name="p_pv[]" placeholder="Enter GST %" value="<?=$row4['p_pv'];?>" />
                                                    </div>
                                                    <div class="form-group <?=$col;?>"  id="stock1"> 
                                                        <label for="size">Product Stock QTY<span class="text-danger">*</span></label>
                                                        <input type="number" class="form-control" name="p_qty[]" placeholder="Enter Stock" value="<?=$row4['p_qty'];?>" required />
                                                    </div>
                                                    <div class="form-group <?=$col;?>"  id="stock2">
                                                        <label for="size">Product Min. Stock QTY<span class="text-danger">*</span></label>
                                                        <input type="number"  class="form-control" name="p_qty2[]" placeholder="Enter Min. Stock" value="<?=$row4['p_min_qty'];?>" required />
                                                    </div>
                                                    
                                                </div>
                                                <?php 
                                                    }
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label for="userName">Product Details </label>
                                                <textarea type="text" name="p_description" id="summernote"  class="form-control" rows="4"><?=$product['p_description'];?></textarea>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label for="userName">Product Features </label>
                                                <textarea type="text" name="p_feature" id="summernote"  class="form-control" rows="4"><?=$product['p_feature'];?></textarea>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label for="userName">Product Condition </label>
                                                <textarea type="text" name="p_condition" id="summernote"  class="form-control" rows="4"><?=$product['p_condition'];?></textarea>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-sm-12">
                                            <div class="form-group">
                                                <label for="userName">Product Return Policy </label>
                                                <textarea type="text" name="p_return_policy" id="summernote"  class="form-control" rows="4"><?=$product['p_return_policy'];?></textarea>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="form-group mb-1 w-100 d-block">
                                        <label for="" class="w-100 d-block control-label">Is Active?</label>
                                        <div class="custom-control custom-radio float-left">
        									<input class="custom-control-input" value="1" required type="radio" id="statusActive" checked name="p_is_active" <?=($product['p_is_active'] == 1) ? 'checked' : '';?> />
        									<label for="statusActive" class="custom-control-label">Yes</label>
        								</div>
        								<div class="custom-control custom-radio float-left ml-3">
        									<input class="custom-control-input" value="0" required type="radio" id="statusBlock" name="p_is_active" <?=($product['p_is_active'] == 0) ? 'checked' : '';?> />
        									<label for="statusBlock" class="custom-control-label">No</label>
        								</div>
                                    </div>
                                    
                                    <br> <br>
                                    <div class="form-group mb-1 w-100 d-block">
                                        <label for="" class="w-100 d-block control-label">Is Todays Best Deal?</label>
                                        <div class="custom-control custom-radio float-left">
        									<input class="custom-control-input" value="1" required type="radio" id="statusTodaysActive" name="todays_best_deal" <?=($product['todays_best_deal'] == 1) ? 'checked' : '';?> />
        									<label for="statusTodaysActive" class="custom-control-label">Yes</label>
        								</div>
        								<div class="custom-control custom-radio float-left ml-3">
        									<input class="custom-control-input" value="0" required type="radio" id="statusTodaysBlock" name="todays_best_deal" <?=($product['todays_best_deal'] == 0) ? 'checked' : '';?> />
        									<label for="statusTodaysBlock" class="custom-control-label">No</label>
        								</div>
                                    </div>
                                    <br> <br>
                                    
                                    <div class="form-group mb-1 w-100 d-block">
                                        <label for="" class="w-100 d-block control-label">Is Featured?</label>
                                        <div class="custom-control custom-radio float-left">
        									<input class="custom-control-input" value="1" required type="radio" id="statusFeaturedActive" name="p_is_featured" <?=($product['p_is_featured'] == 1) ? 'checked' : '';?> />
        									<label for="statusFeaturedActive" class="custom-control-label">Yes</label>
        								</div>
        								<div class="custom-control custom-radio float-left ml-3">
        									<input class="custom-control-input" value="0" required type="radio" id="statusFeaturedBlock" name="p_is_featured" <?=($product['p_is_featured'] == 0) ? 'checked' : '';?> />
        									<label for="statusFeaturedBlock" class="custom-control-label">No</label>
        								</div>
                                    </div>
                                    
                                    <div class="form-group">
                                        <button type="submit" class="btn btn-success float-right m-2">Update</button>
                                        <a href="<?php echo base_url().'master/product/'?>" class="btn float-right m-2 btn-secondary" title="">Back</a>
                                    </div>
                                
                                </form>
					        </div>
					        
                            <div class="col-sm-12">
                                <form class="form-horizontal" action="<?= base_url('master/product/edit_img');?>" method="post" enctype="multipart/form-data">
                                    <input type="hidden" name="table" value="tbl_product" />
                                    <input type="hidden" name="page" value="product/product/<?=$product['p_id'];?>" />
                                    <input type="hidden" name="p_id" value="<?=$product['p_id'];?>" />
                                    
                                    <h4 class="header-title mb-4">Product Photo  (600px X 600px)</h4>
                                    <input type="file" name="photo" class="dropify" data-default-file="<?= base_url('uploads/shop/product/'.$product['p_featured_photo']);?>" />
                                    <button type="submit" class="btn btn-success float-right my-2">Update Product Photo</button>
                                </form>
                            </div>    
                            
                            <div class="col-sm-12">
                                <label for="userName">Other Product  Photos</label>
                            </div>
                            
                            <div class="col-sm-12">
                                
                                <div class="row">
                                    <?php
                                    if (!empty($p_photo)) {?>
                                        <?php foreach ($p_photo as $row) {?>
                                            <div class="col-sm-2">
                                                <div class="control-group input-group my-2">
                                                    <div>
                                                        <input value="<?=$row['pp_id'];?>" name="pp_id" class="form-control" type="hidden">
                                                        <img src="<?=base_url('uploads/shop/product/more/').$row['photo'];?>" width="100" height="50" class="img-fluid img-thumbnail"/>
                                                    </div>
                                                    <div class="input-group-btn"> 
        									            <form class="form-horizontal" onsubmit="return delete_more_img();" action="<?= base_url('master/action/delete');?>" method="post" enctype="multipart/form-data">
                                                            <input type="hidden" name="table" value="tbl_product_photo" />
                                                            <input type="hidden" name="page" value="product/edit/<?=$product['p_id'];?>" />
                                                            <input type="hidden" name="id" value="<?=$row['pp_id'];?>" />
                                                            <input type="hidden" name="column" value="pp_id" />
                                                            
                                                            <button type="submit" class="btn btn-xs btn-danger" style="padding: 0 5px;margin-left: -20px;position: absolute;" title="Delete">&times;</button>
                										</form>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php } ?>
                                    <?php } ?>
                                </div>
				            </div>
				            <div class="col-sm-12">
				                <form class="form-horizontal" action="<?= base_url('master/product/add_more_img/'.$product['p_id']);?>" method="post" enctype="multipart/form-data">
                                <input type="hidden" name="table" value="tbl_product_photo" />
                                <input type="hidden" name="page" value="product/edit/<?=$product['p_id'];?>" />
                                <input type="hidden" name="p_id" value="<?=$product['p_id'];?>" />
                                    <div class="col-sm-12">
                                        <label for="userName">Other Product  Photo (600px X 600px)</label>
                                       	<div class="input-group control-group after-add-more">
                                          <input type="file" name="p_photo[]" class="form-control">
                                          <div class="input-group-btn"> 
                                            <button class="btn btn-primary add-more" type="button"><i class="fa fa-plus"></i> Add</button>
                                          </div>
                                        </div>
                                        
                                    </div>
                                    <div class="col-sm-12">
                                        <!-- Copy Fields -->
                                        <div class="copy hidden">
                                            <div class="control-group input-group my-2">
                                                <input type="file"  name="p_photo[]" class="form-control">
                                                <div class="input-group-btn"> 
                                                    <button class="btn btn-danger remove" type="button"><i class="glyphicon glyphicon-remove"></i> Remove</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="col-sm-12">
                                        <button type="submit" class="btn btn-success float-right my-2">Add More Photo</button>
                                    </div>
                                    
                                    
                                </div>
                            </form>
				            </div>
				        </div>
					</div>
				</div>
				<!-- /.col-md-6 -->
			</div>
			<!-- /.row -->
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php $this->load->view('master/common/footer.php'); ?>

<script>
    $('#tcat_id').change(function(){
        var tcat_id = $(this).val();
        $.post("<?php echo base_url().'master/product/get_mid_category';?>",
          {
              tcat_id: tcat_id
          },
          function(data){
            $('#mcat_id').html(data);
          });
    });
    
    $('#mcat_id').change(function(){
        var mcat_id = $(this).val();
        $.post("<?php echo base_url().'master/product/get_end_category';?>",
          {
              mcat_id: mcat_id
          },
          function(data){
            $('#ecat_id').html(data);
          });
    });
</script>

<script>
    $("#btnAddSize").click(function () {
        var count = $('.parent').find('.row').length;
        // var col2 = '<label for="size">Product Color<span class="text-danger">*</span></label><select name="color['+count+'][]" class="form-control" id="color_more_'+count+'" multiple="multiple" data-placeholder="Select Product Color"><?php if(!empty($color)){ foreach($color as $row3){ echo '<option value="'.$row3['color_id'].'">'.$row3['color_name'].'</option>';}}?></select>';
        
        var col = '<label for="size">Product Size<span class="text-danger">*</span></label><select name="size[]" class="form-control" data-placeholder="Select Product Size"><?php $size_id='';if(!empty($size)){foreach($size as $row2){if($row2['size_id']==$row4['size_id']){ $size_id = $row2['size_id'];}?><option value="<?=$row2['size_id'];?>" <?=($row2['size_id']==$row4['size_id']) ? 'selected' : '';?>><?=$row2['size_name'];?></option><?php }}?></select>';
        var col2 = '<label for="color">Product Color </label><select name="color[]" class="form-control" data-placeholder="Select Product Color"><option value=""> Select Color</option><?php if(!empty($color)){ foreach($color as $row3){ echo '<option value="'.$row3['color_id'].'">'.$row3['color_name'].'</option>';}}?></select>';
        var col3 = '<label for="p_old_price">Product Old Price<span class="text-danger">*</span></label><input type="number" step="any class="form-control" name="p_old_price[]" placeholder="Enter MRP " required />';
        var col4 = '<label for="p_current_price">Product Current Price<span class="text-danger">*</span></label><input type="number" step="any class="form-control" name="p_current_price[]" placeholder="Enter Selling Price " required />';
        var col5 = '<label for="p_dp_price">Product DP Price<span class="text-danger">*</span></label><input type="number" step="any class="form-control" name="p_dp_price[]" placeholder="Enter DP Price "  />';
        var col6 = '<label for="p_pv">Product PV<span class="text-danger">*</span></label><input type="text" class="form-control" name="p_pv[]" placeholder="Enter PV"  />';
        var col7 = '<label for="p_qty">Product Stock QTY<span class="text-danger">*</span></label><input type="number" step="any class="form-control" name="p_qty[]" placeholder="Enter Stock " required />';
        var col8 = '<label for="p_qty2">Product Min. Stock QTY<span class="text-danger">*</span></label><input type="number" step="any class="form-control" name="p_qty2[]" placeholder="Enter Min. Stock " required />';
        
        var pmd = '<div class="col-sm-12"><a href="javascript:void()" onclick="rem('+count+')" class="Delete2 btn btn-danger btn-xs float-right">X</a></div>';
        var md = '<div class="<?=$col;?>">'+col+'</div>';
        var md2 = '<div class="<?=$col;?>">'+col2+'</div>';
        var md3 = '<div class="<?=$col;?>">'+col3+'</div>';
        var md4 = '<div class="<?=$col;?>">'+col4+'</div>';
        var md5 = '<div class="<?=$dpcol;?>">'+col5+'</div>';
        var md6 = '<div class="<?=$pvcol;?>">'+col6+'</div>';
        var md7 = '<div class="<?=$col;?>">'+col7+'</div>';
        var md8 = '<div class="<?=$col;?>">'+col8+'</div>';
        var row = '<div class="row bg-light py-2 border-bottom mb-2" id="ss'+count+'">'+pmd+''+md+''+md2+''+md3+''+md4+''+md5+''+md6+''+md7+''+md8+'</div>';
        $("#valueadd").append(row);
        // multicolor(count);
        // default_value(count);
    });
    
   /* $('#valueadd').delegate('a.Delete2', 'click', function () {
        $('.Delete2').closest('.parent').find('.row').not(':first').last().fadeOut("slow").remove();
    return false;
    });
    */
    
function rem(id)
{
  $("#ss"+id).fadeOut("slow").remove();  
}
</script>

<script>
$("#statusTodaysActive").click(function(){
    if ($("#statusTodaysActive").is(":checked")) {
        $("#statusFeaturedBlock").prop('checked','true');
    }
});
$("#statusFeaturedActive").click(function(){
    if ($("#statusFeaturedActive").is(":checked")) {
        $("#statusTodaysBlock").prop('checked','true');
    }
});
</script>

<script type="text/javascript">
    $(document).ready(function() {
      $(".add-more").click(function(){ 
          var html = $(".copy").html();
          $(".after-add-more").after(html);
      });


      $("body").on("click",".remove",function(){ 
          $(this).parents(".control-group").remove();
      });
    });
</script>

<script type="text/javascript">
	function delete_more_img() {
		if (confirm("Are You Sure You Want TO Delete Product Other Image ?")) {
            return true;
		}
		return false;
	}
</script>


<script>
    function multicolor(id){
            var width = $("#color1").width();
        $("#color_more_"+id).multiselect({
                buttonWidth: width+'px'
            });
            $("#color_more_"+id).addClass('multi_color');
    }
</script>

<script>
//     function default_value(id){
//         var $trWithEmpty  = $("#ss"+id).children("div").filter(function () {
//             return $(this).find('.form-control').val();
//         });

// console.log($trWithEmpty);


//     }
</script>

<script>
        $(function() {
            var width = $("#color1").width();
            $("#multi_color_new").multiselect({
                buttonWidth: width+'px'
            });
            $("#multi_color_new").addClass('multi_color');
        });
</script>

<?php
    foreach($i3 as $cr){
        echo '<script>multicolor('.$cr.');</script>';
    }
?>

                                                    