<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>JKRoyalty | Upload Report Youtube</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="../../bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../../cloudflare/ajax/libs/font-awesome-4.7.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="../../cloudflare/ajax/libs/ionicons-2.0.1/css/ionicons.min.css">
  <!-- DataTables -->
  <link rel="stylesheet" href="../../plugins/datatables/dataTables.bootstrap.css">
  <!-- Select2 -->
  <link rel="stylesheet" href="../../plugins/select2/select2.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="../../dist/css/skins/_all-skins.min.css">
  
  <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>-->
  <script src="../../plugins/jQuery/jquery-2.2.3.min.js"></script>
</head>
<body>
<div class="form-group">
	<div align="center" style=" margin-top:300px; "><b>Proses Input Data ....</b></div>
	<div align="center" class="progress" style=" margin-left:300px; height:25px; width:50%">
    	<div class="progress-bar progress-bar-striped progress-bar-animated" id="progress-bar" style="height:25px; width:100%; padding:0px">
        	<span id="progess-info"></span>
        </div>
    </div>
</div>

<?php
session_start();
ini_set('max_execution_time', 0);
include "../../conf/conn.php";
error_reporting(E_ALL ^ E_NOTICE); 
require_once '../../conf/excel_reader2.php';

//jika tombol import ditekan 
if($_GET['page']=='two')
{
	
	
	$query_grh = mysql_query("SELECT * FROM ttk_grh ORDER BY group_id");
    while($data_grh = mysql_fetch_array($query_grh))
    {
	
		//---------------------------------------------------------- SEQUENCE 1 ----------------------------------------------------------------
		//NWR
		$no = 1;
		$qy_nwr = mysql_query("SELECT * FROM ttk_nwr WHERE group_id = '$data_grh[group_id]' ORDER BY transaction_sequence");
		$jumlahbaris = mysql_num_rows($qy_nwr);
    	while($dt_nwr = mysql_fetch_array($qy_nwr))
    	{
			
			//PROGRESS BAR
			$barisXls = $jumlahbaris-1;
			$k = $no-1;
			$percent = @(intval($k/$barisXls * 100))."%";
			// mengupdate progress bar

			echo '<script language="javascript">
			document.getElementById("progress-bar").style.width="'.$percent.'";
			document.getElementById("progess-info").innerHTML="'.($no-1).' record ('.$percent.')";
			</script>';
			//AKHIR PROGRESS BAR


			$query_sequence = "INSERT INTO ttk_sequence (group_id, 
	  		record_type,
      		transaction_sequence, 
	  		record_sequence, 
	  		seq_record_type)values('$dt_nwr[group_id]', 
	  		'$dt_nwr[record_type]',
      		'$dt_nwr[transaction_sequence]', 
	  		'', 
	  		'')";
      		mysql_query($query_sequence);
			
			
			//SPU
			$qy_spu = mysql_query("SELECT * FROM ttk_spu WHERE group_id = '$dt_nwr[group_id]' AND transaction_sequence = '$dt_nwr[transaction_sequence]'");
    		while($dt_spu = mysql_fetch_array($qy_spu))
    		{
      
	  			$query_sequence = "INSERT INTO ttk_sequence (group_id, 
	  			record_type,
      			transaction_sequence, 
	  			record_sequence, 
	  			seq_record_type)values('$dt_spu[group_id]', 
	  			'$dt_spu[record_type]',
      			'$dt_spu[transaction_sequence]', 
	  			'', 
	  			'$dt_spu[seq_spu]')";
      			mysql_query($query_sequence);
			}
			
			
			//SPT
			$qy_spt = mysql_query("SELECT * FROM ttk_spt WHERE group_id = '$dt_nwr[group_id]' AND transaction_sequence = '$dt_nwr[transaction_sequence]'");
    		while($dt_spt = mysql_fetch_array($qy_spt))
    		{
      
	  			$query_sequence = "INSERT INTO ttk_sequence (group_id, 
	  			record_type,
      			transaction_sequence, 
	  			record_sequence, 
	  			seq_record_type)values('$dt_spt[group_id]', 
	  			'$dt_spt[record_type]',
      			'$dt_spt[transaction_sequence]', 
	  			'', 
	  			'$dt_spt[seq_spt]')";
      			mysql_query($query_sequence);
			}


			//SWR
			$qy_swr = mysql_query("SELECT * FROM ttk_swr WHERE group_id = '$dt_nwr[group_id]' AND transaction_sequence = '$dt_nwr[transaction_sequence]'");
    		while($dt_swr = mysql_fetch_array($qy_swr))
    		{
      
	  			$query_sequence = "INSERT INTO ttk_sequence (group_id, 
	  			record_type,
      			transaction_sequence, 
	  			record_sequence, 
	  			seq_record_type)values('$dt_swr[group_id]', 
	  			'$dt_swr[record_type]',
      			'$dt_swr[transaction_sequence]', 
	  			'', 
	  			'$dt_swr[seq_swr]')";
      			mysql_query($query_sequence);


				//SWT
				$qy_swt = mysql_query("SELECT * FROM ttk_swt WHERE group_id = '$dt_swr[group_id]' AND transaction_sequence = '$dt_swr[transaction_sequence]' 
									AND seq_swt = '$dt_swr[seq_swr]'");
				$dt_swt = mysql_fetch_array($qy_swt);
				

					$query_sequence = "INSERT INTO ttk_sequence (group_id, 
	  				record_type,
      				transaction_sequence, 
	  				record_sequence, 
	  				seq_record_type)values('$dt_swt[group_id]', 
	  				'$dt_swt[record_type]',
      				'$dt_swt[transaction_sequence]', 
	  				'', 
	  				'$dt_swt[seq_swt]')";
      				mysql_query($query_sequence);


					//PWR
					$qy_pwr = mysql_query("SELECT * FROM ttk_pwr WHERE group_id = '$dt_swr[group_id]' AND transaction_sequence = '$dt_swr[transaction_sequence]' 
										AND seq_pwr = '$dt_swr[seq_swr]'");
					$dt_pwr = mysql_fetch_array($qy_pwr);
					

						$query_sequence = "INSERT INTO ttk_sequence (group_id, 
	  					record_type,
      					transaction_sequence, 
	  					record_sequence, 
	  					seq_record_type)values('$dt_pwr[group_id]', 
	  					'$dt_pwr[record_type]',
      					'$dt_pwr[transaction_sequence]', 
	  					'', 
	  					'$dt_pwr[seq_pwr]')";
      					mysql_query($query_sequence);
					
					
					
				

			}

		$no++;
    	}
		//-------------------------------------------------------- SEQUENCE 1 -----------------------------------------------------------------

		
		//-------------------------------------------------------- SEQUENCE 2 -----------------------------------------------------------------
		$no = 1;
		$qysequence = mysql_query("SELECT * FROM ttk_sequence WHERE group_id = '$data_grh[group_id]' GROUP BY transaction_sequence ORDER BY transaction_sequence");
		$jumlahbaris = mysql_num_rows($qysequence);
    	while($dt_sequence = mysql_fetch_array($qysequence))
    	{
			
			//PROGRESS BAR
			$barisXls = $jumlahbaris-1;
			$k = $no-1;
			$percent = @(intval($k/$barisXls * 100))."%";
			// mengupdate progress bar

			echo '<script language="javascript">
			document.getElementById("progress-bar").style.width="'.$percent.'";
			document.getElementById("progess-info").innerHTML="'.($no-1).' record ('.$percent.')";
			</script>';
			//AKHIR PROGRESS BAR


			$qy_sequence = mysql_query("SELECT * FROM ttk_sequence WHERE group_id = '$dt_sequence[group_id]' 
										AND transaction_sequence = '$dt_sequence[transaction_sequence]' ORDER BY no ASC");
    		while($data_sequence = mysql_fetch_array($qy_sequence))
    		{
				
				//membuat transaction_sequence
				$query=mysql_query("SELECT max(record_sequence) as max_kode FROM ttk_sequence WHERE transaction_sequence = '$data_sequence[transaction_sequence]' 
									ORDER BY no DESC LIMIT 1");
				$data = mysql_fetch_array($query);
				$transactionsequence = $data['max_kode'];
				//$no = substr($transactionsequence, -8, 8);
				$nomor = $transactionsequence;
				//$no += 1;
		
				if($nomor == '')
				{
					$noterurut = '0';	
				}
				elseif($nomor != '')
				{
					$nomor = (int) $transactionsequence;
					$noterurut = $nomor + 1;
				}

				$newTransactionSequence = sprintf("%08s", $noterurut);


				mysql_query("UPDATE ttk_sequence SET record_sequence = '$newTransactionSequence' WHERE no = '$data_sequence[no]'");

				
			}	

		$no++;	
		}	
		//-------------------------------------------------------- SEQUENCE 2 -----------------------------------------------------------------
		

	} //Akhir GRH



	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=aksi_upload_metadata_cwr_tiktok_3.php?page=three">';
	exit;

}
else
{
	/*
	echo '<script>alert("Proses import gagal !!!");
	window.location.href="../../index.php?page=tiktok_cwr"</script>';
	*/

	//sebelum redirect, daftarkan session pesan
    $_SESSION['gagal'] = 'Proses import gagal !!!';
	echo '<META HTTP-EQUIV="Refresh" Content="0; URL=../../index.php?page=tiktok_cwr">';
	exit;

}
 
?>

<!-- jQuery 2.2.3 -->
<script src="../../plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="../../bootstrap/js/bootstrap.min.js"></script>
<!-- Select2 -->
<script src="../../plugins/select2/select2.full.min.js"></script>
<!-- DataTables -->
<script src="../../plugins/datatables/jquery.dataTables.min.js"></script>
<script src="../../plugins/datatables/dataTables.bootstrap.min.js"></script>
<!-- SlimScroll -->
<script src="../../plugins/slimScroll/jquery.slimscroll.min.js"></script>
<!-- FastClick -->
<script src="../../plugins/fastclick/fastclick.js"></script>
<!-- AdminLTE App -->
<script src="../../dist/js/app.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../../dist/js/demo.js"></script>

</body>
</html>