<?php 
include_once('../header.php');
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_currency_setup.php');
if(!check_login()){
	header("Location: " . WEB_URL . "logout.php");
	die();
}
if(isset($_SESSION['login_type']) && $_SESSION['login_type'] !== "super"){
  header("Location: ".WEB_URL."logout.php");
  die();
}

$button_text = $_data['save_button_text'];
$form_url = WEB_URL . "setting/currency_setup.php";

if(isset($_POST['txtSymbol'])){
	if(!isset($_POST['hdnSpid'])){
		$sql="INSERT INTO `currencies`(`symbol`,`name`, `exchange_rate`) VALUES ('$_POST[txtSymbol]','$_POST[txtName]'," . (float)$_POST["txtExchange"] . ")";	
    mysqli_query($link,$sql);
		mysqli_close($link);
		$url = WEB_URL . 'setting/currency_setup.php?m=add';
		header("Location: $url");
	} else {
		$sql_update="UPDATE `currencies` set symbol = '$_POST[txtSymbol]', `name` = '$_POST[txtName]', `exchange_rate` = " . (float)$_POST["txtExchange"] ." where cid = " . (int)$_POST['hdnSpid'] ;	
    mysqli_query($link,$sql_update);
		mysqli_close($link);
		$url = WEB_URL . 'setting/currency_setup.php?m=up';
		header("Location: $url");
	}
	$success = "block";
}

if(isset($_GET['spid']) && $_GET['spid'] != ''){
	$result_location = mysqli_query($link,"SELECT * FROM currencies where cid = '" . (int)$_GET['spid'] . "'");
	if($row = mysqli_fetch_array($result_location)){
		$symbol = $row['symbol'];
		$name = $row['name'];
		$button_text = $_data['update_button_text'];
		$form_url = WEB_URL . "setting/currency_setup.php?id=".$_GET['spid'];
		$hval = $row['cid'];
	}	
}
$default_currency_id_query =  $link->query("SELECT `default_currency_id` FROM `settings` LIMIT 1");
$default_currency_id = mysqli_fetch_assoc($default_currency_id_query)["default_currency_id"];
$foreign_currency_id_query =  $link->query("SELECT `foreign_currency_id` FROM `settings`");
$foreign_currency_id = mysqli_fetch_assoc($foreign_currency_id_query)["foreign_currency_id"];
?>
<!-- Content Header (Page header) -->

<section class="content-header">
  <h1><?= $_data['text_1'];?> </h1>
  <ol class="breadcrumb">
    <li><a href="<?= WEB_URL?>dashboard.php"><i class="fa fa-dashboard"></i> <?= $_data['home_breadcam'];?></a></li>
    <li class="active"><?= $_data['setting'];?></li>
    <li class="active"><?= $_data['text_1'];?></li>
  </ol>
</section>
<!-- Main content -->
<section class="content">
<!-- Full Width boxes (Stat box) -->
     
      <!-- /.box-body -->
<?php
$delinfo = 'none';
$addinfo = 'none';
$msg = "";
if(isset($_GET['delid']) && $_GET['delid'] != '' && $_GET['delid'] > 0){
	$sqlx= "DELETE FROM `currencies` WHERE cid = ".$_GET['delid'];
	mysqli_query($link,$sqlx); 
	$delinfo = 'block';
}
if(isset($_GET['m']) && $_GET['m'] == 'add'){
	$addinfo = 'block';
	$msg = $_data['text_5'];
}
if(isset($_GET['m']) && $_GET['m'] == 'up'){
	$addinfo = 'block';
	$msg = $_data['text_6'];
}
?>      
      <!-- Main content -->
      <section class="content">
      <!-- Full Width boxes (Stat box) -->
      <div class="row">
        <div class="col-xs-12">
          <div class="alert alert-danger alert-dismissable" style="display:<?= $delinfo; ?>">
            <button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="fa fa-close"></i></button>
            <h4><i class="icon fa fa-ban"></i> <?= $_data['delete_text'];?> !</h4>
            <?= $_data['text_7'];?> </div>
          <div class="alert alert-success alert-dismissable" style="display:<?= $addinfo; ?>">
            <button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="fa fa-close"></i></button>
            <h4><i class="icon fa fa-check"></i> <?= $_data['success'];?> !</h4>
            <?= $msg; ?> </div>
            <div align="right" style="margin-bottom:1%;"> 
              <a class="btn btn-success" data-toggle="tooltip" href="javascript:;" onClick="$('#add_currency').modal('show');" data-original-title="Add Currency"><i class="fa fa-plus"></i></a>
            </div>
          <div class="box box-success">
            <div class="box-header">
              <h3 class="box-title"><?= $_data['text_4'];?></h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
            
              <table class="table sakotable table-bordered table-striped dt-responsive">
                <thead>
                  <tr>
                    <th><?= $_data['text_3'];?></th>
					          <th><?= $_data['text_33'];?></th>
                    <th>Exchange Rate</th>
                    <th><?= $_data['action_text'];?></th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                    $result = mysqli_query($link,"SELECT * FROM currencies order by name ASC ");
                    while($row = mysqli_fetch_array($result)){ ?>
                  <tr>
                    <td><?= $row['symbol']; ?></td>
                    <td><?= $row['name']; ?> <span class="badge"><?= $row["cid"]  === $default_currency_id ? "Default Currency" : "" ?></span></td>
                    <td><?= $row['exchange_rate']; ?></td>
                    <td><a class="btn btn-warning ams_btn_special" data-toggle="tooltip" href="javascript:;" onClick="$('#edit_currency<?= $row['cid']; ?>').modal('show');"  data-original-title="<?= $_data['edit_text'];?>"><i class="fa fa-pencil"></i></a> <a class="btn btn-danger ams_btn_special" data-toggle="tooltip" onclick="deleteCurrency(<?= $row['cid']; ?>);" href="javascript:;" data-original-title="<?= $_data['delete_text'];?>"><i class="fa fa-trash-o"></i></a></td>
                  </tr>
                  <div id="edit_currency<?= $row['cid']; ?>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                      <div class="modal-content">
                        <div class="modal-header green_header">
                          <button aria-label="Close" data-dismiss="modal" class="close" type="button"><span aria-hidden="true"><i class="fa fa-close"></i></span></button>
                          <h3 class="modal-title">Edit Currency</h3>
                        </div>
                        <div class="modal-body model_view" align="left">&nbsp;
                          <div class="modal-body">
                            <div class="row">
                              <div class="col-xs-12">
                                <form action="<?= $form_url; ?>" method="post" >
                                  <div class="box-body">
                                    <div class="form-group">
                                      <label for="txtSymbol"><?= $_data['text_3'];?> : <span class="errorStar">*</span></label>
                                      <input type="text" required name="txtSymbol" value="<?= $row['symbol']; ?>" id="txtSymbol" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                      <label for="txtName"><?= $_data['text_33'];?> : <span class="errorStar">*</span></label>
                                      <input type="text" required name="txtName" value="<?= $row['name']; ?>" id="txtName" class="form-control" />
                                    </div>
                                    <div class="form-group">
                                      <label for="txtExchange">Exchange Rate : <span class="errorStar">*</span></label>
                                      <input type="text" required name="txtExchange" value="<?= $row['exchange_rate']; ?>" id="txtExchange" class="form-control" />
                                    </div>
                                    <div class="form-group pull-right">
                                      <button type="submit" name="submit" class="btn btn-success" ><i class="fa fa-save"></i> <?= $button_text; ?></button>&nbsp;
                                    </div>
                                  </div>
                                  <input type="hidden" name="hdnSpid" value="<?= $row['cid']; ?>"/>
                                </form>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <!-- /.modal-content -->
                    </div>
                  </div>
                  <?php } mysqli_close($link);$link = NULL; ?>
                </tbody>
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
          <div id="add_currency" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header green_header">
                    <button aria-label="Close" data-dismiss="modal" class="close" type="button"><span aria-hidden="true"><i class="fa fa-close"></i></span></button>
                    <h3 class="modal-title">Add Currency</h3>
                  </div>
                  <div class="modal-body model_view" align="left">&nbsp;
                    <div class="modal-body">
                          <form onSubmit="return validateMe();" action="<?= $form_url; ?>" method="post" enctype="multipart/form-data">
                            <div class="box-body">
                              <div class="form-group">
                                <label for="txtSymbol"> <?= $_data['text_3'];?> : </label>
                                <input type="text" name="txtSymbol" value="<?= $row['symbol']; ?>" id="txtSymbol" class="form-control" />
                              </div>
                              <div class="fSET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));orm-group">
                                <label for="txtName"> <?= $_data['text_33'];?> : </label>
                                <input type="text" name="txtName" value="<?= $row['name']; ?>" id="txtName" class="form-control" />
                              </div>
                              <div class="form-group">
                                <label for="txtExchange"> Exchange Rate : </label>
                                <input type="text" name="txtExchange" value="<?= $row['exchange_rate']; ?>" id="txtExchange" class="form-control" />
                              </div>
                              <div class="form-group pull-right">
                                <button type="submit" name="submit" class="btn btn-success" ><i class="fa fa-save"></i> <?= $button_text; ?></button>&nbsp;
                              </div>
                            </div>
                          </form>
                    </div>
                  </div>
                </div>
                <!-- /.modal-content -->
              </div>
            </div>
            
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    </div>
    <!-- /.box -->
  </div>
</div>
</div>
<!-- /.row -->
<script type="text/javascript">
  function deleteCurrency(Id){
  	var iAnswer = confirm("<?= $_data['confirm_delete']; ?>");
	if(iAnswer){
		window.location = '<?= WEB_URL;?>setting/currency_setup.php?delid=' + Id;
	}
  }
  function validateMe() {
  	if($("#txtSymbol").val() == ''){
		alert('<?= $_data['required_1']; ?>');
		$("#txtSymbol").focus();
		return false;
	} else if($("#txtName").val() == ''){
		alert('<?= $_data['required_2']; ?>');
		$("#txtName").focus();
		return false;
	} else {
		return true;
	}
  }
  </script>
<?php include_once('../footer.php'); ?>
