<?php $this->load->view('header');?>

	<!-- BEGIN CONTENT -->
	<ul class="nav nav-tabs" id="myTab" role="tablist">
		<li class="nav-item">
			<a class="nav-link <?php echo (($this->uri->segment(2) == 'index') ? '': 'active');?>" id="tab1-tab" data-toggle="tab" href="#tab1" role="tab" aria-controls="tab1" aria-selected="true"><?php echo $this->lang->line('hf_buy_package');?></a>
		</li>
		<li class="nav-item">
			<a class="nav-link" id="tab2-tab" data-toggle="tab" href="#tab2" role="tab" aria-controls="tab2" aria-selected="false"><?php echo $this->lang->line('hf_topup_package');?></a>
		</li>
		<li class="nav-item">
			<a class="nav-link <?php echo (($this->uri->segment(2) == 'index') ? 'active': '');?>" id="tab3-tab" data-toggle="tab" href="#tab3" role="tab" aria-controls="tab3" aria-selected="false"><?php echo $this->lang->line('hf_topup_package_history');?></a>
		</li>
	</ul>
	<div class="tab-content" id="myTabContent">
		<div class="tab-pane fade <?php echo (($this->uri->segment(2) == 'index') ? '': 'show active');?>" id="tab1" role="tabpanel" aria-labelledby="tab1-tab">
			<div class="row no-gutters">
				<div class="col-12 bg-red rounded-lg rounded-top-0 p-3">
					<div class="row no-gutters">
						<div class="col-12 rounded-lg border-dark-blue bg-red p-3 text-white">
							<div class="d-flex justify-content-center mb-3">
								<h2><?php echo $this->lang->line('hf_buy_package');?></h2>
							</div>
							<div class="row no-gutters bg-dark-blue rounded-lg">
								<div class="col-12 p-3">
									<span><?php echo $this->lang->line('buy_current_package');?>:</span> <span class="font-weight-bold"><?php echo $this->lang->line(get_package($package_type));?></span><br />
									<span><?php echo $this->lang->line('wallet_1');?>:</span> <span class="font-weight-bold"><?php echo (isset($v_unit) ? $v_unit : '0.00');?></span><br />
									<span><?php echo $this->lang->line('wallet_4');?>:</span> <span class="font-weight-bold"><?php echo (isset($v_unit4) ? $v_unit4 : '0.00');?></span>
								</div>
							</div>
							<!-- form -->
							<form id="buy-form">
								<div class="form-group row pt-5">
									<label for="package_type" class="col-sm-3 col-form-label"><?php echo $this->lang->line('buy_package');?></label>
									<div class="col-sm-9">
										<select id="package_type" name="package_type" class="form-control custom-select">
											<?php
												$package_arr = array();
												$num = 0;
												$lowest_package = 0;
												$current_package_price = 0;
												$dropdown_list = '';
												if($package->num_rows() > 0) {
													foreach($package->result() as $row) {
														$package_arr[$row->package_id] = $row->package_price;
														
														if($row->package_id == $package_type) {
															$current_package_price = $row->package_price;
														}
														
														if($row->package_id > $package_type) {
															if($num == 0) {
																$lowest_package = $row->package_price;
																$num++;
															}
														
															if($row->package_id > 1) {
																$dropdown_list .= '<option value="' . $row->package_id . '" data-value="' . $row->package_price . '">' . $row->package_name . '</option>';
															}
														}
													}
												}
												
												if( ! empty($dropdown_list)) {
													echo $dropdown_list;
												}
												else {
													echo '<option value="0">' . $this->lang->line('buy_please_select') . '</option>';
												}
											?>
										</select>
									</div>
								</div>
								<div class="form-group row pt-2">
									<label for="diff_amount" class="col-sm-3 col-form-label"><?php echo $this->lang->line('buy_need_to_topup');?></label>
									<div class="col-sm-9">
										<?php
											$diff_amount = 0;
											if(isset($package_arr[$package_type])) {
												$diff_amount = ($lowest_package - $package_arr[$package_type]);
												if($diff_amount < 0) {
													$diff_amount = 0;
												}
											}
											else {
												$diff_amount = $lowest_package;
											}
										?>
										<label id="diff_amount" class="col-form-label"><?php echo $diff_amount;?></label>
									</div>
								</div>
								<div class="form-group row pt-2">
									<label for="transaction_amount" class="col-sm-3 col-form-label"><?php echo $this->lang->line('wallet_1');?></label>
									<div class="col-sm-9">
										<input type="text" class="form-control" id="transaction_amount" name="transaction_amount" placeholder="<?php echo $this->lang->line('buy_fill_in_amount_to_use_message_1');?>">
									</div>
								</div>
								<div class="form-group row pt-2">
									<label for="transaction_amount_4" class="col-sm-3 col-form-label"><?php echo $this->lang->line('wallet_4');?></label>
									<div class="col-sm-9">
										<input type="text" class="form-control" id="transaction_amount_4" name="transaction_amount_4" placeholder="<?php echo $this->lang->line('buy_fill_in_amount_to_use_message_2');?>">
									</div>
								</div>
								<div class="form-group row pt-2">
									<label for="security_password" class="col-sm-3 col-form-label"><?php echo $this->lang->line('buy_security_password');?></label>
									<div class="col-sm-9">
										<input type="password" class="form-control" id="security_password" name="security_password" placeholder="<?php echo $this->lang->line('buy_security_password_fill_in_message');?>">
									</div>
								</div>
								
								<div class="row pt-3">
									<div class="col-sm-4">&nbsp;</div>
									<div class="col-sm-4">
										<input type="hidden" id="current_package" name="current_package" value="<?php echo (isset($package_arr[$package_type]) ? $package_arr[$package_type] : 0);?>" />
										<input type="hidden" id="<?php echo $this->security->get_csrf_token_name(); ?>" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>" />
										<button onclick="buy_submit()" type="button" class="col-12 btn btn-warning"><?php echo $this->lang->line('buy_buy_now');?></button>
									</div>
									<div class="col-sm-4">&nbsp;</div>
								</div>
							</form>
						</div>
					</div>
				</div>	
			</div>
		</div>
		<div class="tab-pane fade" id="tab2" role="tabpanel" aria-labelledby="tab2-tab">
			<div class="row no-gutters">
				<div class="col-12 bg-red rounded-lg rounded-top-0 p-3">
					<div class="row no-gutters">
						<div class="col-12 rounded-lg border-dark-blue bg-red p-3 text-white">
							<div class="d-flex justify-content-center mb-3">
								<h2><?php echo $this->lang->line('hf_topup_package');?></h2>
							</div>
							<div class="row no-gutters bg-dark-blue rounded-lg">
								<div class="col-12 p-3">
									<span><?php echo $this->lang->line('buy_current_package');?>:</span> <span class="font-weight-bold"><?php echo $this->lang->line(get_package($package_type));?></span><br />
									<span><?php echo $this->lang->line('wallet_1');?>:</span> <span class="font-weight-bold"><?php echo (isset($v_unit) ? $v_unit : '0.00')?></span><br />
									<span><?php echo $this->lang->line('buy_current_balance');?>:</span> <span class="font-weight-bold"><?php echo (isset($balance) ? $balance : '0.00')?></span>
								</div>
							</div>
							<!-- form -->
							<form id="topup-package-form">
								<div class="form-group row pt-5">
									<label for="transaction_amount" class="col-sm-3 col-form-label"><?php echo $this->lang->line('buy_topup_amount');?></label>
									<div class="col-sm-9">
										<label class="col-form-label"><?php $topup_diff = ($current_package_price - $balance); echo (($topup_diff > 0) ? $topup_diff : 0);?></label>
									</div>
								</div>
								<div class="form-group row pt-2">
									<label for="security_password" class="col-sm-3 col-form-label"><?php echo $this->lang->line('buy_security_password');?></label>
									<div class="col-sm-9">
										<input type="password" class="form-control" id="security_password_2" name="security_password" placeholder="<?php echo $this->lang->line('buy_security_password_fill_in_message');?>">
									</div>
								</div>
								
								<div class="row pt-3">
									<div class="col-sm-4">&nbsp;</div>
									<div class="col-sm-4">
										<input type="hidden" id="<?php echo $this->security->get_csrf_token_name(); ?>" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>" />
										<button onclick="topup_package_submit()" type="button" class="col-12 btn btn-primary"><?php echo $this->lang->line('buy_topup_package');?></button>
									</div>
									<div class="col-sm-4">&nbsp;</div>
								</div>
							</form>
						</div>
					</div>
				</div>		
			</div>
		</div>
		<div class="tab-pane fade <?php echo (($this->uri->segment(2) == 'index') ? 'show active': '');?>" id="tab3" role="tabpanel" aria-labelledby="tab3-tab">
			<div class="row no-gutters">
				<div class="col-12 bg-red rounded-lg rounded-top-0 p-3">
					<div class="row no-gutters">
						<div class="col-12 rounded-lg border-dark-blue bg-red p-3 text-white">
							<div class="d-flex justify-content-center mb-3">
								<h2><?php echo $this->lang->line('hf_topup_package_history');?></h2>
							</div>
							<?php 
							if($list['query']->num_rows() > 0) {
								$i = 1;
								foreach($list['query']->result() as $row) {
									$border = (($list['query']->num_rows() > $i) ? 'border-bottom' : '');
							?>
							<div class="row no-gutters col-12 p-3 <?php echo $border;?>">
								<div class="col-6 text-left">
									<?php echo $this->lang->line('buy_date');?>: <?php echo date('Y-m-d H:i:s', $row->transaction_date);?><br />
									<?php echo $this->lang->line('buy_amount');?> (<?php echo $this->lang->line('wallet_1');?>): <span class="-danger">-<?php echo number_format($row->credit_amount, 2, '.', ',');?></span><br />
								</div>
								<div class="col-6 text-right">
									<?php echo $this->lang->line('buy_platform_amount');?>: <span class="-success">+<?php echo number_format($row->transaction_amount, 2, '.', ',');?></span><br />
									<?php echo $this->lang->line('topup_status');?>: <?php echo (($row->status == 1) ? '<span class="badge badge-success">' . $this->lang->line('topup_confirmed') . '</span>' : (($row->status == 2) ? '<span class="badge badge-danger">' . $this->lang->line('topup_cancelled') . '</span>' : '<span class="badge badge-primary">' . $this->lang->line('topup_pending') . '</span>'));?>
								</div>
							</div>
							<?php $i++; } } else {?>
							<div class="row no-gutters col-12 p-3">
								<div class="col-12 text-center"><?php echo $this->lang->line('message_no_data_found');?></div>
							</div>
							<?php }?>
							<div class="row no-gutters col-12 p-3">
								<div class="col-12 text-center pagination"><?php echo $list['pagination'];?></div>
							</div>
						</div>
					</div>
				</div>
			</div>	
		</div>
	</div>

	<div class="py-4">&nbsp;</div>
	<!-- END CONTENT -->

<?php $this->load->view('footer');?>