<?php
require_once('../database/header.php');
?>

<div class="main-panel">
<div class="content-wrapper">

<?php
// error_reporting(E_ALL);
// ini_set('display_errors', 1);

require_once('../database/config.php');

// Auto-add commission columns if they don't exist
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN payin_commission VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN payout_commission VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN rspay_payin_base VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN rspay_payout_base VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN payin_fixed VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN payout_fixed VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN rspay_payin_fixed VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN rspay_payout_fixed VARCHAR(50) DEFAULT '0'");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN rspay_merchant_id VARCHAR(100) DEFAULT NULL");
} catch (Exception $e) {}
try {
    $conn->exec("ALTER TABLE settings ADD COLUMN rspay_api_key VARCHAR(255) DEFAULT NULL");
} catch (Exception $e) {}

if($userdata['usertype']=="mainadmin" || $userdata['usertype']=="wluser"){

	
	
if(isset($_REQUEST['type']) AND !empty($_REQUEST['siteid']) ) {
$stmt = $conn->prepare("select * from settings WHERE id = ?");
$stmt->execute([$_REQUEST['siteid']]);
$row=$stmt->fetch();
	
$utmt = $conn->prepare("select * from loginusers WHERE id = ?");
$utmt->execute([$row['users']]);
$usr=$utmt->fetch();
	
if($userdata['usertype']=="mainadmin"){
$access = 1;
}else{
if($row['users']==$userdata['id']) {
$access = 1;	
}else{
$access = 0;	
}
}	
	


//echo $access;		
	
if($row['id']=="" || $access==0){
echo '<img class="img-fluid" src="../bootstrap/img/cloud.png">
<script>
setTimeout(
function(){
window.location = "index.php" 
},
1000);
</script>
';		
}else{
		
	
$usrrow = $row;	

if(isset($_POST['update']) AND 
!empty($_POST['webname'])  AND 
!empty($_POST['copyright'])  AND 
!empty($_POST['webtag'])  AND 
!empty($_POST['webnote'])  AND 
!empty($_POST['webemail'])  AND 
!empty($_POST['webmob1'])  AND
!empty($_POST['address1'])  AND 
!empty($_POST['timings'])  AND 
!empty($_POST['weburl']) 
 ){
	 


$sql = "UPDATE `settings` SET webname=:webname, copyright=:copyright, webtag=:webtag, webnote=:webnote, webemail=:webemail, webmob1=:webmob1, webmob2=:webmob2, address1=:address1, address2=:address2, address3=:address3, timings=:timings, weburl=:weburl, payin_commission=:payin_commission, payout_commission=:payout_commission, rspay_payin_base=:rspay_payin_base, rspay_payout_base=:rspay_payout_base, payin_fixed=:payin_fixed, payout_fixed=:payout_fixed, rspay_payin_fixed=:rspay_payin_fixed, rspay_payout_fixed=:rspay_payout_fixed, rspay_merchant_id=:rspay_merchant_id, rspay_api_key=:rspay_api_key WHERE id=:id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":webname", $_REQUEST['webname']);
$stmt->bindParam(":copyright", $_REQUEST['copyright']);
$stmt->bindParam(":webtag", $_REQUEST['webtag']);
$stmt->bindParam(":webnote", $_REQUEST['webnote']);
$stmt->bindParam(":webemail", $_REQUEST['webemail']);
$stmt->bindParam(":webmob1", $_REQUEST['webmob1']);
$stmt->bindParam(":webmob2", $_REQUEST['webmob2']);
$stmt->bindParam(":address1", $_REQUEST['address1']);
$stmt->bindParam(":address2", $_REQUEST['address2']);
$stmt->bindParam(":address3", $_REQUEST['address3']);
$stmt->bindParam(":timings", $_REQUEST['timings']);
$stmt->bindParam(":weburl", $_REQUEST['weburl']);
$stmt->bindParam(":payin_commission", $_REQUEST['payin_commission']);
$stmt->bindParam(":payout_commission", $_REQUEST['payout_commission']);
$stmt->bindParam(":rspay_payin_base", $_REQUEST['rspay_payin_base']);
$stmt->bindParam(":rspay_payout_base", $_REQUEST['rspay_payout_base']);
// Bind Fixed Fees
$stmt->bindParam(":payin_fixed", $_REQUEST['payin_fixed']);
$stmt->bindParam(":payout_fixed", $_REQUEST['payout_fixed']);
$stmt->bindParam(":rspay_payin_fixed", $_REQUEST['rspay_payin_fixed']);
$stmt->bindParam(":rspay_payout_fixed", $_REQUEST['rspay_payout_fixed']);
$stmt->bindParam(":rspay_merchant_id", $_REQUEST['rspay_merchant_id']);
$stmt->bindParam(":rspay_api_key", $_REQUEST['rspay_api_key']);
$stmt->bindParam(":id", $row['id']);

if($stmt->execute()){
$success = 1;	
} else {
$invalid = 1;

}

} else {
$invalid = 0;
$success = 0;
}
?>  


<div class="row">
<div class="col-md-12 stretch-card">
  
<!-- DataTales Example -->    
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-dark">网站设置</h6>
</div>

<div class="card-body">
              
<?php if(isset($success) && $success==1){ echo '<div class="alert alert-success" role="alert"><strong>'.strtoupper($_POST['webname']).'!</strong> 更新成功！</div> <script> setTimeout( function(){ window.location = "" },1000); </script>';} ?>
<?php if(isset($invalid) && $invalid==1){ echo '<div class="alert alert-danger" role="alert">数据未插入！</div>';} ?>

<form class="user" action="" method="POST">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">公司</h6> 
<input required="required" type="text" class="form-control"  placeholder="Company Name" name="webname" value="<?php echo $usrrow['webname'];?>" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>

<div class="col-sm-6">
<btn-dark6 class="m-0 font-weight-bold text-dark">版权</h6> 
<input required="required" type="text" class="form-control" name="copyright" value="<?php echo $usrrow['copyright'];?>" placeholder="Copyright Name" >
</div>
</div>

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">网站标题</h6> 
<textarea required="required" type="text" name="webtag" class="form-control"  placeholder="Website Title" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();"><?php echo $usrrow['webtag'];?></textarea>
</div>

<div class="col-sm-6">
<btn-dark6 class="m-0 font-weight-bold text-dark">通知</h6> 
<textarea required="required" type="text" class="form-control" placeholder="Website Notifications" name="webnote"><?php echo $usrrow['webtag'];?></textarea>
</div>
</div>
                

<div class="form-group row">
<div class="col-sm-3 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">电子邮件 ID</h6> 
<input required="required" type="text" class="form-control" value="<?php echo $usrrow['webemail'];?>" placeholder="Email ID" name="webemail">
</div>
                  
<div class="col-sm-3 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">手机号码 1</h6> 
<input required="required" type="text" class="form-control" placeholder="Mobile Number 1" name="webmob1" value="<?php echo $usrrow['webmob1'];?>" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>

<div class="col-sm-3  mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">手机号码 2</h6> 
<input type="text" class="form-control" placeholder="Mobile Number 1" name="webmob2" value="<?php echo $usrrow['webmob2'];?>" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>

<div class="col-sm-3  mb-3 mb-sm-0">
 <btn-dark6 class="m-0 font-weight-bold text-dark">营业时间</h6> 
<input required type="text" class="form-control" placeholder="Timings" name="timings" value="<?php echo $usrrow['timings'];?>" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();">
</div>
</div>
                           
<div class="form-group row">
<div class="col-sm-4 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">地址行 1</h6> 
<textarea required="required" type="text" class="form-control" placeholder="Address 1" name="address1" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();"><?php echo $usrrow['address1'];?></textarea>
</div>

<div class="col-sm-4 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">地址行 2</h6> 
<textarea type="text" class="form-control" placeholder="Address 2" name="address2" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();"><?php echo $usrrow['address2'];?></textarea>
</div>

<div class="col-sm-4 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">地址行 3</h6> 
<textarea type="text" class="form-control" placeholder="Address 3" name="address3" onkeyup="this.value = this.value.toUpperCase();" onblur="this.value = this.value.toUpperCase();"><?php echo $usrrow['address3'];?></textarea>
</div>
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">您的利润：代收佣金 (%)</h6> 
<input type="text" class="form-control"  placeholder="Payin Fee %" name="payin_commission" value="<?php echo $usrrow['payin_commission'] ?? '0';?>">
</div>

<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">您的利润：代付佣金 (%)</h6> 
<input type="text" class="form-control"  placeholder="Payout Fee %" name="payout_commission" value="<?php echo $usrrow['payout_commission'] ?? '0';?>">
</div>
</div>

<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">RSPay 基础代收费率 (%)</h6> 
<input type="text" class="form-control"  placeholder="Base Payin Rate %" name="rspay_payin_base" value="<?php echo $usrrow['rspay_payin_base'] ?? '0';?>">
</div>

<div class="col-sm-6 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">RSPay 基础代付费率 (%)</h6> 
<input type="text" class="form-control"  placeholder="Base Payout Rate %" name="rspay_payout_base" value="<?php echo $usrrow['rspay_payout_base'] ?? '0';?>">
</div>
</div>

<h6 class="text-primary font-weight-bold mt-4 mb-3">固定费用设置</h6>
<div class="form-group row">
    <div class="col-sm-3">
        <label>您的代收固定费</label>
        <input type="text" class="form-control" name="payin_fixed" value="<?php echo $usrrow['payin_fixed'] ?? '0';?>">
    </div>
    <div class="col-sm-3">
        <label>您的代付固定费</label>
        <input type="text" class="form-control" name="payout_fixed" value="<?php echo $usrrow['payout_fixed'] ?? '0';?>">
    </div>
    <div class="col-sm-3">
        <label>RSPay 基础代收固定费</label>
        <input type="text" class="form-control" name="rspay_payin_fixed" value="<?php echo $usrrow['rspay_payin_fixed'] ?? '0';?>">
    </div>
    <div class="col-sm-3">
        <label>RSPay 基础代付固定费</label>
        <input type="text" class="form-control" name="rspay_payout_fixed" value="<?php echo $usrrow['rspay_payout_fixed'] ?? '0';?>">
    </div>
</div>

<h6 class="text-danger font-weight-bold mt-4 mb-3">RSPay 网关凭证（用于自动代付）</h6>
<div class="form-group row">
    <div class="col-sm-6">
        <label>RSPay 商户 ID</label>
        <input type="text" class="form-control" name="rspay_merchant_id" value="<?php echo $usrrow['rspay_merchant_id'] ?? '';?>" placeholder="Enter Merchant ID">
    </div>
    <div class="col-sm-6">
        <label>RSPay API 密钥</label>
        <input type="text" class="form-control" name="rspay_api_key" value="<?php echo $usrrow['rspay_api_key'] ?? '';?>" placeholder="Enter Secret Key">
    </div>
</div>
				
<div class="form-group row">
<div class="col-sm-4 mb-3 mb-sm-0">
<btn-dark6 class="m-0 font-weight-bold text-dark">网站网址 ( example.com ) </h6> 
<input required="required" type="text" class="form-control"  placeholder="Website Url example.com"  name="weburl" value="<?php echo $usrrow['weburl'];?>">
</div>	

<div class="col-sm-2">
<btn-dark6 class="m-0 font-weight-bold text-dark">更新</h6> 
<button class="btn btn-info bg-info text-white btn-block" name="update" type="submit">提交</button>
</div>
</div>
	   
</div>
</form>

</div>
</div>
<?php 
}
}
}
?>
</div>
</div>


        <!-- /.container-fluid -->
        
      <!-- End of Main Content -->
<?php
require_once('../database/footer.php');
?>