<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Chandrani Pearls Gold Billing</title>
<link rel="icon" type="image/jpg" href="dist/img/favicon.png?v=2">
  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  
  <!-- DataTables -->
  <link rel="stylesheet" href="plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/datatables-buttons/css/buttons.bootstrap4.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
  <link rel="stylesheet" href="dist/css/custom.css">
  <style>
#tooltip
{
    text-align: center;
    color: #FFF;
    background: #777777;
    position: absolute;
    z-index: 100;
    padding: 8px 15px;
	font-size:12px;
	font-family: 'Roboto', sans-serif;
}
 
    #tooltip:after /* triangle decoration */
    {
        width: 0;
        height: 0;
        border-left: 10px solid transparent;
        border-right: 10px solid transparent;
        border-top: 10px solid #777777;
        content: '';
        position: absolute;
        left: 50%;
        bottom: -10px;
        margin-left: -10px;
    }
 
        #tooltip.top:after
        {
            border-top-color: transparent;
            border-bottom: 10px solid #777777;
            top: -20px;
            bottom: auto;
        }
 
        #tooltip.left:after
        {
            left: 10px;
            margin: 0;
        }
 
        #tooltip.right:after
        {
            right: 10px;
            left: auto;
            margin: 0;
        }
.fulldiv {
    float: left;
    width: 100%;
    height: 100%;
    top: 0;
    bottom: 0;
    background-color: rgba(38, 53, 68, 0.80);
    background: rgba(38, 53, 68, 0.80);
    z-index: 1500;
    position: fixed;
}
	  .message_maindiv_new {
    width: 430px;
    height: auto;
    margin: 0px auto;
}
	  .message_div_new {
    float: left;
    width: 100%;
    height: auto;
    padding: 3%;
    background-color: #FFFFFF;
    margin-top: 300px;
}
	  .message_title {
    float: left;
    width: 100%;
    height: auto;
    text-align: center;
    font-size: 20px;
    color: #000000;
    margin-bottom: 20px;
    text-transform: capitalize;
	  }
	  
	  .bg-maroon{
	  background-color:#d5042d !important;
	  }
	.truncate-text {
  max-width: 250px; /* Adjust the max-width as needed */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
	  td {
  white-space: normal;
  word-wrap: break-word;
}
</style>
<script>
$( function()
{
    var targets = $( '[rel~=tooltip]' ),
        target  = false,
        tooltip = false,
        title   = false;
 
    targets.bind( 'mouseenter', function()
    {
        target  = $( this );
        tip     = target.attr( 'title' );
        tooltip = $( '<div id="tooltip"></div>' );
 
        if( !tip || tip == '' )
            return false;
 
        target.removeAttr( 'title' );
        tooltip.css( 'opacity', 0 )
               .html( tip )
               .appendTo( 'body' );
 
        var init_tooltip = function()
        {
            if( $( window ).width() < tooltip.outerWidth() * 1.5 )
                tooltip.css( 'max-width', $( window ).width() / 2 );
            else
                tooltip.css( 'max-width', 340 );
 
            var pos_left = target.offset().left + ( target.outerWidth() / 2 ) - ( tooltip.outerWidth() / 2 ),
                pos_top  = target.offset().top - tooltip.outerHeight() - 20;
 
            if( pos_left < 0 )
            {
                pos_left = target.offset().left + target.outerWidth() / 2 - 20;
                tooltip.addClass( 'left' );
            }
            else
                tooltip.removeClass( 'left' );
 
            if( pos_left + tooltip.outerWidth() > $( window ).width() )
            {
                pos_left = target.offset().left - tooltip.outerWidth() + target.outerWidth() / 2 + 20;
                tooltip.addClass( 'right' );
            }
            else
                tooltip.removeClass( 'right' );
 
            if( pos_top < 0 )
            {
                var pos_top  = target.offset().top + target.outerHeight();
                tooltip.addClass( 'top' );
            }
            else
                tooltip.removeClass( 'top' );
 
            tooltip.css( { left: pos_left, top: pos_top } )
                   .animate( { top: '+=10', opacity: 1 }, 50 );
        };
 
        init_tooltip();
        $( window ).resize( init_tooltip );
 
        var remove_tooltip = function()
        {
            tooltip.animate( { top: '-=10', opacity: 0 }, 50, function()
            {
                $( this ).remove();
            });
 
            target.attr( 'title', tip );
        };
 
        target.bind( 'mouseleave', remove_tooltip );
        tooltip.bind( 'click', remove_tooltip );
    });
});
</script>
	
	</head>
	<body class="hold-transition sidebar-mini">
		
		
<div class="wrapper">
  <!-- Navbar -->
    <?php include ("topheader.php"); ?>
  <!-- /.navbar -->
  <!-- Main Sidebar Container -->
    <?php include ("sidemenu.php"); ?>
	<?php
	$Get_Cat_Disc=$Mainclass->dbh->prepare("SELECT * FROM gold_discount_master WHERE Active='Yes' ORDER BY SLN desc");
	$Get_Cat_Disc->execute();
	$Disc_res=$Get_Cat_Disc->get_result();
	$Disc_Arr = $Disc_res->fetch_all(MYSQLI_ASSOC);
	$Get_Cat_Disc->close();
	
	$collection_ids = [];
foreach ($Disc_Arr as $career) {
    if ($career['Offer_On'] == "Collection" && !empty($career['Collections'])) {
        $collection_ids = array_merge($collection_ids, explode(",", $career['Collections']));
    }
}
$collection_ids = array_unique(array_filter($collection_ids)); // Remove duplicates and empty values
	
	
$collection_names_map = [];
if (!empty($collection_ids)) {
    // Dynamically create placeholders for the IN clause
    $placeholders = implode(',', array_fill(0, count($collection_ids), '?'));
    $sql = "SELECT SLN, collection_name FROM gold_collection_master WHERE active='Yes' AND SLN IN ($placeholders)";
    $Get_Collections = $Mainclass->dbh->prepare($sql);
    
    // Dynamically create a type string for bind_param
    $types = str_repeat('i', count($collection_ids));
    $Get_Collections->bind_param($types, ...$collection_ids);
    
    $Get_Collections->execute();
    $collections_result = $Get_Collections->get_result();
    while ($ft_collection = $collections_result->fetch_assoc()) {
        $collection_names_map[$ft_collection['SLN']] = $ft_collection['collection_name'];
    }
    $Get_Collections->close();
}	
	
	$category_ids = [];
foreach ($Disc_Arr as $career) {
    if ($career['Offer_On'] == "Category" && !empty($career['Product_Category'])) {
        $category_ids = array_merge($category_ids, explode(",", $career['Product_Category']));
    }
}
$category_ids = array_unique(array_filter($category_ids)); // Remove duplicates and empty values
	
$category_names_map = [];
if (!empty($category_ids)) {
    // Dynamically create placeholders for the IN clause
    $placeholders1 = implode(',', array_fill(0, count($category_ids), '?'));
    $sql = "SELECT SLN, Category FROM gold_product_category_master WHERE SLN IN ($placeholders1)";
    $Get_categories = $Mainclass->dbh->prepare($sql);
    
    // Dynamically create a type string for bind_param
    $types = str_repeat('i', count($category_ids));
    $Get_categories->bind_param($types, ...$category_ids);
    
    $Get_categories->execute();
    $category_result = $Get_categories->get_result();
    while ($ft_categories = $category_result->fetch_assoc()) {
        $category_names_map[$ft_categories['SLN']] = $ft_categories['Category'];
    }
    $Get_categories->close();
}	
	
	?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1></h1>
                </div>
                <div class="col-sm-6">
                    
                </div>
            </div>
        </div>
    </section>
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
			<div class="col-12">
      <div class="card">
                        <div class="card-header">
        <h4>View Discount
            <a href="create_category_discount.php"><button type="button" class="btn btn-custom" style="float: right; background-color: #ffd700; color: #062e50;"><i class="fa-solid fa-circle-plus fa-beat"></i>   ADD DISCOUNT</button></a>
        </h4>
    </div>

                        <div class="card-body">
                            <table id="example1" class="table table-bordered table-striped">
                            <thead>
                            <tr>
                            <th>SLN</th>
                            <th>Offer Name</th> 
                            <th>Offer From</th> 
                            <th>Offer Till</th> 
                            <th>Dealers</th>
                            <th>Offer On</th>
                            <th>Collection/Categories</th>
                            <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>
								<?php
								$i=1;								
								foreach($Disc_Arr as $Career)
								{									
									if($Career['Offer_On']=="Collection")
									{
										$collection_ids_for_career = explode(",", $career['Collections']);
										$collection_names_for_career = [];
										foreach ($collection_ids_for_career as $id) {
											if (isset($collection_names_map[$id])) {
												$collection_names_for_career[] = $collection_names_map[$id];
											}
										}
										$Offer_Data = implode(", ", $collection_names_for_career);										
									}
									if($Career['Offer_On']=="Category")
									{										
										$category_ids_for_career = explode(",", $career['Product_Category']);
										$category_names_for_career = [];
										foreach ($category_ids_for_career as $id) {
											if (isset($category_names_map[$id])) {
												$category_names_for_career[] = $category_names_map[$id];
											}
										}
										$Offer_Data = implode(", ", $category_names_for_career);
									}
								?>
                            <tr>
                            <td><?php echo $i;?></td>
                            <td><?php echo $Career['Offer_Name'];?></td> 
                            <td><?php echo date('d-M-Y',strtotime($Career['From_Date']));?></td>
                            <td><?php echo date('d-M-Y',strtotime($Career['To_Date']));?></td>
                            <td><?php echo $Career['Dealer_Code'];?></td>
                            <td><?php echo $Career['Offer_On'];?></td>
                            <td><?php echo $Offer_Data;?></td>
							<td>
								<a href="edit_category_discount.php?e=<?php echo base64_encode($Career['SLN']);?>" style="color:#0055ff; cursor:pointer;margin-left:10px;" title="Edit Discount"><i class="fa-solid fa-pen"></i></a>
								<i class="fa-solid fa-trash" style="color:#ff3300; cursor:pointer;margin-left:10px;" title="Delete Discount" onClick="delete_offer('<?php echo $Career['SLN'];?>')"></i>
								</td>
                            </tr>
								<?php
									$i++;
								}
								?>
                                </tbody>
				  </table>
                        </div>
                    </div> 
				</div>
            <!-- /.card -->
			</div>
			<!--/.row-->    
        
      </div>
      <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

   <?php include ("footer.php"); ?>
  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->
<!-- jQuery -->
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>-->
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- DataTables  & Plugins -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="plugins/jszip/jszip.min.js"></script>
<script src="plugins/pdfmake/pdfmake.min.js"></script>
<script src="plugins/pdfmake/vfs_fonts.js"></script>
<script src="plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.colVis.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
<!-- Page specific script -->
<script>
  $(function () {
    $("#example1").DataTable({
      "responsive": true,
		"lengthChange": false,
		"autoWidth": false,
		"searching": false,
		"columnDefs": [
    { 
      "targets": 4, // The column index (e.g., 2 for the third column)
      "className": "truncate-text" // The CSS class to apply
    },
	{ 
      "targets": 6, // The column index (e.g., 2 for the third column)
      "className": "truncate-text" // The CSS class to apply
    }
  ]
     // "buttons": ["copy", "csv", "excel", "pdf", "print", "colvis"]
    }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
    $('#example2').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false,
      "responsive": true,
    });
  });
</script>
	
	<script>
		function delete_offer(per)
		{
			$.ajax({
				url:'ajax_delete_category_discount.php',
				type:'POST',
				data:{per:per},
				success:function(res)
				{
					Swal.fire({
							  icon: 'error',
							  title: '',
							  text: 'Discount Deleted Successfully.',  
							   showConfirmButton:false,
							   timer:2000,
							   toast:true,
							   animation:true,
							   position:'top-center',
						}).then(function() {  
						window.location.href="view-gold-category-discount.php";
						});
				}
			});
		}
		</script>	
		
	
</body>
</html>