<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>Add New Ticket</title>
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/jquery.dataTables.css">
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/dataTables.bootstrap4.css">
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/responsive.dataTables.css">
	<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); 

	$action=$_REQUEST['action'];

	$csadded=$_SESSION['murgibhai'];
	$gcustid=$_REQUEST['custid'];
	$getlog=f(q("SELECT * FROM tbl_login WHERE l_uid='$csadded'"));
				$ltype=$getlog['l_type'];

				if($ltype=='3'){

					$getu=f(q("SELECT * FROM adduser WHERE custid='$getlog[l_read]'"));

				}

	if($action=='save'){

		if($psave==1){
		
		$ctype=$_POST['ctype'];
		$custid=$_POST['custid'];
		$strtdt=$_POST['strtdt'];
		$pticket=$_POST['pticket'];
		$pdet=$_POST['pdet'];
		$stat='PENDING';	
		$csadded=$_SESSION['murgibhai'];	
		$txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
		$suser='na';
		$sdate=date('Y-m-d');
		$sdet='NA';

		
		

		$res=mysqli_query($con,"INSERT INTO tbl_ticket SET ctype='$ctype',custid='$custid',strtdt='$strtdt',pticket='$pticket',pdet='$pdet',stat='$stat',suser='$suser',sdate='$sdate',sdet='$sdet'");

		//echo "INSERT INTO tbl_ticket SET ctype='$ctype',custid='$custid',strtdt='$strtdt',pticket='$pticket',pdet='$pdet',stat='$stat',suser='$suser',sdate='$sdate',sdet='$sdet'";
			

			if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Saved Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Updating data','','error')
             </script>

				<? }

		}else{

			?>

				<script type="text/javascript">
			Swal.fire('Permission Denied','','error')
			 </script>

			<?php
		}

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

		if($pupdate==1){

		$ctype=$_POST['ctype'];
		$custid=$_POST['custid'];
		$strtdt=$_POST['strtdt'];
		$pticket=$_POST['pticket'];
		$pdet=$_POST['pdet'];
		$stat='PENDING';	
		$csadded=$_SESSION['murgibhai'];	
		$txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
		$suser='na';
		$sdate=date('Y-m-d');
		$sdet='NA';
		$cid=$_REQUEST['cid'];

		
		

		$res=mysqli_query($con,"UPDATE tbl_ticket SET ctype='$ctype',custid='$custid',strtdt='$strtdt',pticket='$pticket',pdet='$pdet',stat='$stat',suser='$suser',sdate='$sdate',sdet='$sdet' WHERE tkt_id='$cid'");
			

			if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Updated Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Updating data','','error')
             </script>

				<? }

		}else{

			?>

				<script type="text/javascript">
			Swal.fire('Permission Denied','','error')
			 </script>

			<?

		}



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

			$cid=$_REQUEST['cid'];
			$edit=f(q("SELECT * FROM tbl_ticket WHERE tkt_id='$cid'"));


	}

	


	?>
	<? include "ajax.php"; 
		$currentdt=date("Y-m-d");
		$thirtyDaysUnix = strtotime('+30 days', strtotime($currentdt));
		$fetdt=date("Y-m-d",$thirtyDaysUnix);

	?>
	<script type="text/javascript">
		function setdt(oldDate){

			//var nextday=new Date(oldDate.getFullYear(),oldDate.getMonth(),oldDate.getDate()+30);
			 var tt = oldDate;

    var date = new Date(tt);
    var newdate = new Date(date);

    newdate.setDate(newdate.getDate() + 30);
    
    var dd = newdate.getDate();
    var mm = newdate.getMonth() + 1;
    var y = newdate.getFullYear();

    var someFormattedDate = y + '-' + mm + '-' + dd;
    
    var formattedDate = someFormattedDate.toISOString().substr(0, 10)
    document.getElementById('enddt').value = formattedDate;

		}


		
	</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>Customer Ticket</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">Add new Ticket</li>
								</ol>
							</nav>
						</div>
						<div class="col-md-6 col-sm-12 text-right">
							<!-- <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">Select Type</label>
							<div class="col-sm-12 col-md-10">
								<select name="ctype" id="lnarid" class="form-control">
									<option value="1" <? if($edit['ptype']=='1'){?> selected <? } ?>>Cable</option>
									<!-- <option value="0" <? if($edit['ptype']=='0'){?> selected <? } ?>>Internet</option> -->
									
								</select>
							</div>
						</div>
					
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Search Customer</label>
							<div class="col-sm-12 col-md-10">
								<input class="form-control" type="text" name="serch" id="srch" placeholder="Acno / VC / STB / Phone Number / Username / IP Address" value="<?php if($gcustid==''){ echo $edit['custid']; }else{ echo $gcustid; }?>" required <? if($ltype!='3'){?>onblur="getcustp()"<? } ?>>
								<? if($action=='edit'){ ?> <script type="text/javascript">getcustp();</script> <? } ?>
							</div>
						</div>
						<div class="form-group row">
							<input type="hidden" name="custid" id="custid">
							<div class="col-md-12"><span id="cname"></span><br></div>
							<div class="col-md-12"><span id="cadd"></span><br></div>
							<div class="col-md-12"><span id="caddr"></span><br></div>
							<div class="col-md-12"><span id="cphone"></span><br></div>
							<div class="col-md-12"><span id="cacno"></span><br></div>
							<div class="col-md-12"><span id="cvc"></span><br></div>
							<div class="col-md-12"><span id="cstb"></span><br></div>
							<div class="col-md-12"><span id="cprice"></span><br></div>
							<div class="col-md-12"><span id="cexpiry"></span><br></div>
							<div class="col-md-12"><span id="cpack"></span><br></div>


							<?
							if($gcustid!=''){ ?>

								<script type="text/javascript">
									getcustp();

								</script>

						<?	}

						?>

						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Ticket Date</label>
							<div class="col-sm-12 col-md-3">
								<input class="form-control" type="date" placeholder="dd-mm-yyyy" id="strdt" name="strtdt"  value="<? if($action!='edit'){?><?=$currentdt?><?}else{ echo $edit['pdate']; }?>">
							</div>
							<div class="col-md-2"></div>
							<label class="col-sm-12 col-md-2 col-form-label">Problem Type</label>
							<div class="col-sm-12 col-md-3">

								<select name="pticket" class="custom-select2 form-control" required>
									<option value="">Choose</option>
								<?
									$gett=mysqli_query($con,"SELECT * FROM tbl_problems ORDER BY pname");
									while($arrp=mysqli_fetch_array($gett)){ ?>

										<option value="<?=$arrp['pname']?>" <? if($edit['pname']==$arrp['pname']){?> selected <? } ?>><?=$arrp['pname']?></option>



							<?		}

								?>
							</select>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Problem Details</label>
							<div class="col-sm-12 col-md-10">
								<textarea class="form-control" name="pdet"><?=$edit['pdet']?></textarea>
							</div>
						</div>
						
							<script type="text/javascript">
								function setacno(){


			document.getElementById('srch').value="<?=$getu['custid']?>";
			getcustp();

		}

		<? if($ltype=='3'){?>
		setacno();

	<? } ?>
							</script>

						

						
						
						<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> -->
							<button type="button" class="btn btn-primary btn-sm" onclick="getcustp()"><i class="fa fa-save"></i> Search</button>
						</div>
						<div class="pull-right">
													
							<!-- <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> -->
							<button type="submit" id="submt" class="btn btn-primary btn-sm" ><i class="fa fa-save"></i> Add Now</button>
						</div>
					</div>
					</form>
					<div class="collapse collapse-box" id="basic-form1" >
						
					</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>