<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>Package Master</title>
	<link rel="stylesheet" type="text/css" href="src/plugins/datatables/media/css/jquery.dataTables.css">
	<link rel="stylesheet" type="text/css" href="src/plugins/datatables/media/css/dataTables.bootstrap4.css">
	<link rel="stylesheet" type="text/css" href="src/plugins/datatables/media/css/responsive.dataTables.css">
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); 

	$action=$_REQUEST['action'];
	if($action=='save'){

		if($psave==1){
		$pname=$_POST['pname'];
		$details=$_POST['details'];
		$base_rate=$_POST['base_rate'];
		$prc=$_POST['prc'];
		$ptype=$_POST['ptype'];
		$pmso=$_POST['pmso'];
		$pnochannel=$_POST['pnochannel'];

		if($pnochannel==''){

			$pnochannel=0;
		}
		
		if($base_rate==''){
			$base_rate=0;
		}

		$res=mysqli_query($con,"INSERT INTO tbl_pack SET pname='$pname',details='$details',base_rate='$base_rate',prc='$prc',ptype='$ptype',pmso='$pmso',pnochannel='$pnochannel',pdt=CURDATE()");

		//echo "INSERT INTO tbl_pack SET pname='$pname',details='$details',prc='$prc',ptype='$ptype',pmso='$pmso',pnochannel='$pnochannel',pdt=CURDATE()";

	}else{

		echo "<script>alert('You are not Authorized to perform this Action.');</script>";
	}
	



	}elseif($action=='update'){
		if($pupdate==1){
		$pname=$_POST['pname'];
		$details=$_POST['details'];
		$base_rate=$_POST['base_rate'];
		$prc=$_POST['prc'];
		$ptype=$_POST['ptype'];
		$pmso=$_POST['pmso'];
		$pnochannel=$_POST['pnochannel'];
		$cid=$_REQUEST['cid'];
		
		if($base_rate==''){
			$base_rate=0;
		}

	$res=mysqli_query($con,"UPDATE tbl_pack SET pname='$pname',details='$details',base_rate='$base_rate',prc='$prc',ptype='$ptype',pmso='$pmso',pnochannel='$pnochannel' WHERE pid='$cid'");
		
		}else{

			echo "<script>alert('You are not Authorized to perform this Action.');</script>";
		}

	}elseif($action=='edit'){
		$cid=$_REQUEST['cid'];
		$edit=f(q("SELECT * FROM tbl_pack WHERE pid='$cid'"));



	}elseif($action=='delete'){

		if($pdelete==1){

		$cid=$_REQUEST['cid'];

		$res=mysqli_query($con,"DELETE FROM tbl_pack WHERE pid='$cid'");
		}else{

			echo "<script>alert('You are not Authorized to perform this Action.');</script>";
		}
	}

	


	?>
	<script type="text/javascript">

		function setvis(valss){

			if(valss=='2'){
				document.getElementById('noch').style.visibility='visible';

			}
			if(valss=='3'){
				document.getElementById('noch').style.visibility='visible';

			}

			if(valss=='1'){
				document.getElementById('noch').style.visibility='hidden';

			}
			if(valss=='4'){
				document.getElementById('noch').style.visibility='hidden';

			}
			if(valss=='0'){
				document.getElementById('noch').style.visibility='hidden';

			}


		}

		function calculateWithGST() {
			var baseRate = parseFloat(document.getElementById('base_rate').value) || 0;
			var gstRate = 0.18; // 18% GST
			var priceWithGst = baseRate + (baseRate * gstRate);
			document.getElementById('prc').value = priceWithGst.toFixed(2);
		}

	</script>
	<div class="main-container">
		<div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
			<div class="min-height-200px">
				<div class="page-header">
					<div class="row">
						<div class="col-md-6 col-sm-12">
							<div class="title">
								<h4>Package Master</h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="index.php">Home</a></li>
									<li class="breadcrumb-item active" aria-current="page">Package Master</li>
								</ol>
							</nav>
						</div>
						<div class="col-md-6 col-sm-12 text-right">
							<a href="import-channel-master.php" class="btn btn-success btn-sm" style="margin-right: 5px;">
								<i class="fa fa-upload"></i> Import Packages
							</a>
							<!-- <div class="dropdown">
								<a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-toggle="dropdown">
									January 2018
								</a>
								<div class="dropdown-menu dropdown-menu-right">
									<a class="dropdown-item" href="#">Export List</a>
									<a class="dropdown-item" href="#">Policies</a>
									<a class="dropdown-item" href="#">View Assets</a>
								</div>
							</div> -->
						</div>
					</div>
				</div>
				<!-- Default Basic Forms Start -->
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<small style="color: red;"><?=$msg?></small>
					<form action="<?=$_SERVER['PHP_SELF']?>?action=<? if($action=='edit'){?>update&cid=<?=$cid?><? }else{?>save<?} ?>" method="POST">
					
					<div class="clearfix" style="min-height: 30px;"></div>
					
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Name</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="pname" placeholder="enter type name" value="<?=$edit['pname']?>" required>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Details</label>
							<div class="col-sm-12 col-md-10">
								<textarea class="form-control" name="details"><?=$edit['details']?></textarea>
								
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Type</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="ptype" onchange="setvis(this.value)">
									<option value="0" >Choose</option>
									<!-- <option value="1" <? if($edit['ptype']=='1'){?> selected <? } ?>>Internet Package</option> -->
									<option value="2" <? if($edit['ptype']=='2'){?> selected <? } ?>>Cable Package</option>
									<option value="3" <? if($edit['ptype']=='3'){?> selected <? } ?>>Broadcaster Bouquet</option>
									<option value="4" <? if($edit['ptype']=='4'){?> selected <? } ?>>Channel</option>
									<!-- <option value="0">Choose</option>
									<option value="0">Choose</option> -->


								</select>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Base Price (No GST)</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="number" name="base_rate" id="base_rate" placeholder="enter base price without GST" step=0.01 value="<?=isset($edit['base_rate']) ? $edit['base_rate'] : 0?>" onchange="calculateWithGST()">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Price (with 18% GST)</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="number" name="prc" id="prc" placeholder="price with GST will appear here" step=0.01 value="<?=$edit['prc']?>" readonly>
								<small class="text-muted">Auto-calculated from Base Price + 18% GST</small>
							</div>
						</div>
						<div class="form-group row" style="visibility: hidden;" id="noch">
							<label class="col-sm-12 col-md-2 col-form-label">No Of Channels</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="number" name="pnochannel" placeholder="enter no of channel" value="<?=$edit['pnochannel']?>" >
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Mso</label>
							<div class="col-sm-12 col-md-10">
								<select class="form-control" name="pmso">
									<option value="0" >Choose if Applicable</option>
									<?
											$mso=mysqli_query($con,"SELECT * FROM tbl_mso order by m_name");
											while($arr=mysqli_fetch_array($mso)){
									?>
										<option value="<?=$arr['m_id']?>"><?=$arr['m_name']?></option>


									<? } ?>
								</select>
							</div>
						</div>
						
						
						<div class="clearfix">
						<div class="pull-left">
							<!-- <h4 class="text-blue">Default Basic Forms</h4>
							<p class="mb-30 font-14">All bootstrap element classies</p> -->
						</div>
						<div class="pull-right">
							<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> Save</button>
							<!-- <a href="#basic-form1" class="btn btn-primary btn-sm scroll-click" rel="content-y"  data-toggle="collapse" role="button"><i class="fa fa-code"></i> Source Code</a> -->
						</div>
					</div>
					</form>
					<div class="collapse collapse-box" id="basic-form1" >
						
					</div>
				</div>
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<div class="clearfix mb-20">
						<div class="pull-left">
							<h5 class="text-blue">Package Master List</h5>
						</div>
					</div>
					<div class="row">
						<table class="stripe hover multiple-select-row data-table-export nowrap">
							<thead>
								<tr>
									<th class="table-plus datatable-nosort">Name</th>
									<th class="table-plus datatable-nosort">Details</th>
									<th class="table-plus datatable-nosort">Type</th>
									<th class="table-plus datatable-nosort">Price</th>
									
									<th>Action</th>	
								</tr>
							</thead>
							<tbody>
								<?
									$getc=mysqli_query($con,"SELECT * FROM tbl_pack ORDER BY pname");
									while($arr=mysqli_fetch_array($getc)){
								?>
								<tr>
									<td class="table-plus"><?=$arr['pname']?></td>
									<td class="table-plus"><?=$arr['details']?></td>
									<td class="table-plus"><? if($arr['ptype']=='1'){echo "Internet Package";}elseif($arr['ptype']=='2'){ echo "Cable Package"; }elseif($arr['ptype']=='3'){ echo "Broadcaster Bouquet"; }elseif($arr['ptype']=='4'){ echo "Channel"; } ?></td>
									<td class="table-plus"><?=$arr['prc']?></td>
									
									<td>
									<a href="<?=$_SERVER['PHP_SELF']?>?action=edit&cid=<?=$arr['pid']?>">EDIT</a><br>
									<a href="<?=$_SERVER['PHP_SELF']?>?action=delete&cid=<?=$arr['pid']?>" onclick="return confirm('Are you sure you want to delete this item?');">DELETE</a><br>
									

									</td>
								</tr>
								<? } ?>	
							</tbody>
						</table>
					</div>
				</div>
				

			</div>
			<?php include('include/footer.php'); ?>
		</div>
	</div>
	<?php include('include/script.php'); ?>

	<script src="src/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.responsive.js"></script>
	<script src="src/plugins/datatables/media/js/responsive.bootstrap4.js"></script>
	<!-- buttons for Export datatable -->
	<script src="src/plugins/datatables/media/js/button/dataTables.buttons.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.print.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.html5.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.flash.js"></script>
	<script src="src/plugins/datatables/media/js/button/pdfmake.min.js"></script>
	<script src="src/plugins/datatables/media/js/button/vfs_fonts.js"></script>
	<script>
		$('document').ready(function(){
			$('.data-table').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
			});
			$('.data-table-export').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
				dom: 'Bfrtip',
				buttons: [
				'copy', 'csv', 'pdf', 'print'
				]
			});
			var table = $('.select-row').DataTable();
			$('.select-row tbody').on('click', 'tr', function () {
				if ($(this).hasClass('selected')) {
					$(this).removeClass('selected');
				}
				else {
					table.$('tr.selected').removeClass('selected');
					$(this).addClass('selected');
				}
			});
			var multipletable = $('.multiple-select-row').DataTable();
			$('.multiple-select-row tbody').on('click', 'tr', function () {
				$(this).toggleClass('selected');
			});
		});
	</script>
</body>
</html>