<link href="<?php echo site_url();?>assets/css/fSelect.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="<?php echo site_url();?>assets/css/fSelect.js"></script>
<script>
(function($) {
    $(function() {
        $('.test').fSelect();
    });
})(jQuery);
</script>
<div id="page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h3 class="page-header">Heading Master</h3>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div class="row">
                <div class="col-lg-12">
                       <!--    <a href="<?php echo site_url();?>admin/Company/user_add" class="btn btn-info"> Add User</a>-->
                    <div class="panel panel-default">
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <?php $success_message = $this->session->flashdata('success_message');
                            if (!empty($success_message)) {?>
                                <div class="alert alert-success">
                                      <a class="close" data-dismiss="alert" href="components-popups.html#" aria-hidden="true">×</a>
                                      <strong><?php echo $this->session->flashdata('success_message');  ?></strong>
                                </div> 
                            <?php }  ?>
                            <?php $failure_message = $this->session->flashdata('failure_message');
                            if (!empty($failure_message)) {?>
                                <div class="alert alert-warning">
                                      <a class="close" data-dismiss="alert" href="components-popups.html#" aria-hidden="true">×</a>
                                      <strong><?php echo $this->session->flashdata('failure_message');  ?></strong>
                                </div> 
                            <?php }  ?>
							
									<?php
								//	echo $_GET['id'];
									
					if($_GET['id'])
					{
				//	    echo $_GET['id'];
					$id=$_GET['id'];
					$sql="select * from headingmaster where heading_no='$id'";
			//		echo $sql;
					$r=$this->db->query($sql);
					$rr=$r->result_array();
			//		print_r($rr);
			
			/* $master_product=$rr[0]['master_product'];
								 $sql = "SELECT * FROM product where id='$master_product'";
									$record = $this->db->query($sql);
									$master_product1=$record->row('product'); */
				$heading=$rr[0]['heading'];

					?>
			 <form role="form" action="<?php echo site_url();?>admin/HeadingMaster/update_headingMaster" method="post" role="form" class="form-horizontal">
						<input type="hidden" value="<?=$id?>" name="id">

						<div class="form-group">
							<label class="control-label col-md-1">Heading :<font color="red">*</font></label>
							<div class="col-md-3">
							<input type="text" required name="heading" value="<?php echo set_value('heading')?set_value('heading'):$rr[0]['heading']?>" class="form-control">
							</div>
							<label class="control-label col-md-2">Master Product :<font color="red">*</font></label>
							<div class="col-md-3">
							<select required name="master_product[]" multiple="multiple" class="test form-control">
							<?php
							$sql3="select DISTINCT master_product from headingmaster where heading='$heading' and IsActive=0";
							$r3=$this->db->query($sql3);
							$rr3=$r3->result_array();
				//	print_r($rr3);
						//	$i=0;
								foreach($rr3 as $r5)
								{
									//pri
								$master_product=$r5['master_product'];
								 $sql = "SELECT DISTINCT * FROM product where id='$master_product' and IsActive=0";
									$record = $this->db->query($sql);
									$master_product1=$record->row('product');
									$master_product_id=$record->row('id');
									?>
							<option selected value="<?=$master_product_id?>"><?php echo $master_product1;?></option>
							<?php
								
					
								}
								 $sqlq = "SELECT * FROM product WHERE IsActive='0'";
							//	 echo  $sqlq;
								 $recordq = $this->db->query($sqlq);
								$product_info=$recordq->result_array();
							foreach($product_info as $rr2)
							{
								$p_id=$rr2['id'];
								$sqlqcc="select count(*) as count from headingmaster where master_product='$p_id' and heading='$heading'";
								$rcc=$this->db->query($sqlqcc);
								$rrc=$rcc->row('count');
								if($rrc <=0)
								{
								?>
								
									<option value="<?=$rr2['id']?>"><?=$rr2['product']?></option>
								<?php
								}
							//	$i++;
							}
							
						//	$i++;
								
								
							?>
							</select>
							</div>
							
							
							<div class="col-md-2">
								<button id="signupSubmit" type="submit" class="btn btn-warning btn-block">UPDATE</button>
							</div>
						</div>
						
					
					</form>	
				<?php
					}
					else
					{
						
					?>

					<form role="form" action="<?php echo site_url();?>admin/HeadingMaster/save_headingMaster" method="post" role="form" class="form-horizontal">
				

						<div class="form-group">
							<label class="col-md-1">Heading :<font color="red">*</font></label>
							<div class="col-md-3">
							<input type="text" required name="heading" value="<?php echo set_value('heading')?set_value('heading'):'';?>" class="form-control">
							</div>
								<label class="control-label col-md-2">Master Product :<font color="red">*</font></label>
							<div class="col-md-3">
							<select required name="master_product[]"  multiple="multiple" class="test form-control">
							<?php
							foreach($product_info as $rr2)
							{
								?>
									<option value="<?=$rr2['id']?>"><?=$rr2['product']?></option>
								<?php
							}
							?>
							</select>
							</div> 
							<div class="col-md-2">
								<button id="signupSubmit" type="submit" class="btn btn-warning btn-block">Submit</button>
							</div>
						</div>
						
					
					</form>
					<?php
					}
					?>
           <div style="overflow-x:auto;">
              
     <table id="example1" class="table table-striped table-bordered responsive">
        <thead>
            <tr>
				<th>Sr.No</th>
				<th>Heading</th>		
				<th>Master Product</th>
				<th>Edit</th>                
				<th>Delete</th>                
            </tr>
        </thead>
        <tbody>
			  
<?php 
$sql = "SELECT * FROM headingmaster where IsActive=0 group by heading";
        $record = $this->db->query($sql);
        $headingMaster_info= $record->result_array();
foreach($headingMaster_info as $r)
{
	$heading=$r['heading'];
	/* $master_product2=$r['master_product'];
	$sql1 = "SELECT * FROM product where id='$master_product2'";
	$record1 = $this->db->query($sql1);
	$master_product11=$record1->row('product'); */
?>							
					<tr class="gradeX">
					<td><?php echo $r['id']?></td>
					<td><?php echo $r['heading']?></td>
					<td>
					<?php
					$sqlc = "SELECT DISTINCT master_product FROM headingmaster
					where IsActive=0 and heading='$heading'";
					$recordc = $this->db->query($sqlc);
					$category_infoc= $recordc->result_array();
					foreach($category_infoc as $c)
					{
						$master_product2=$c['master_product'];
						$sql1 = "SELECT * FROM product where id='$master_product2'";
						$record1 = $this->db->query($sql1);
						$master_product11=$record1->row('product');
								
					
					 echo ' '.$master_product11;
					}
					?>
					</td>
					<td><a href="<?php echo site_url();?>admin/HeadingMaster/add_headingMaster?id=<?=$r['heading_no']?>" title="Edit Product" class="btn btn-primary btn-circle"><i class="fa fa-pencil" aria-hidden="true"></i></td>
					<td><a href="<?php echo site_url();?>admin/HeadingMaster/delete_headingMaster/<?php echo $r['heading_no']?>" title="Delete Product" class="btn btn-danger btn-circle"><i class="fa fa-trash" aria-hidden="true"></i></td>
					</tr>
<?php
}

?>			
             
        </tbody>
    </table>
	</div>
	
	    <link href="<?php echo site_url();?>assets/css/jquery.dataTables.min.css" rel="stylesheet">
	    <link href="https://cdn.datatables.net/buttons/1.4.2/css/buttons.dataTables.min.css" rel="stylesheet">
    
	<script src="//code.jquery.com/jquery-1.12.4.js"></script> 
	<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script> 
	<script src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script> 
	<script src="//cdn.datatables.net/buttons/1.4.2/js/buttons.flash.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script> 
	<script src="//cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script> 
	<script src="//cdn.datatables.net/buttons/1.4.2/js/buttons.html5.min.js"></script> 
	<script src="//cdn.datatables.net/buttons/1.4.2/js/buttons.print.min.js"></script> 

	
	<script>
$(document).ready(function() {
    $('#example1').DataTable( {
        dom: 'Bfrtip',
		"paging": false,
        buttons: [
             'csv', 'excel'
        ]
    } );
} );			
	</script>
                            
                          
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->            
        </div>
        <!-- /#page-wrapper -->

<!-- DataTables JavaScript -->
   <script type="text/javascript">
$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'T<"clear">lfrtip',
        tableTools: {
            "sSwfPath": "../js/datatables/extras/TableTools/media/swf/copy_csv_xls_pdf.swf"
        }
    } );
} );

</script>
<script>
function doconfirm()
{
    job=confirm("Are you sure to delete permanently?");
    if(job!=true)
    {
        return false;
    }
}
</script>
