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

if ($userdata['usertype'] == "mainadmin" || $userdata['usertype'] == "wluser") {
if (isset($_REQUEST['type']) && !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;
}
}

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;
$sms_objc = json_decode($row['smsapi']);

if (isset($_POST['update'])) {
// Initialize $response as an object
$response = new stdClass();
$response->api_key = '';
$response->api_url = $_POST['api_url'];
$response->api_key = $_POST['api_key'];
$response->senderid = $_POST['senderid'];
$response->route = '2';
$sms_json = json_encode($response);

$sql = "UPDATE settings SET smsapi = :smsapi WHERE id = :id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":smsapi", $sms_json);
$stmt->bindParam(":id", $row['id']);

if ($stmt->execute()) {
$success = 1;
} else {
$invalid = 1;
}
} else {
$invalid = 0;
$success = 0;
}
?>
<div class="main-panel">
<div class="content-wrapper">
    
<div class="row">
<div class="col-md-12 stretch-card">
        
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">SMS API 凭证</h6>
</div>
<div class="card-body">
<?php if ($success == 1) {
echo '<div class="alert alert-success" role="alert"><strong>成功！</strong> SMS API 凭证已更新！</div>';
} ?>
<?php if ($invalid == 1) {
echo '<div class="alert alert-danger" role="alert">更新 SMS API 凭证失败！</div>';
} ?>

<form class="user" action="" method="POST">
<div class="form-group row">
<div class="col-sm-4 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-primary">API URL（不带 https://）</label>
<input required type="text" class="form-control" placeholder="API URL" name="api_url" value="<?php echo $sms_objc->{'api_url'}; ?>">
</div>
<div class="col-sm-4 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-primary">API 密钥</label>
<input required type="text" class="form-control" placeholder="输入 API 密钥" name="api_key" value="<?php echo $sms_objc->{'api_key'}; ?>">
</div>
<div class="col-sm-4 mb-3 mb-sm-0">
<label class="m-0 font-weight-bold text-primary">API 发送者 ID</label>
<input required type="text" class="form-control" placeholder="API 发送者 ID" name="senderid" value="<?php echo $sms_objc->{'senderid'}; ?>" maxlength="12">
</div>
</div>
<div class="form-group row">
<div class="col-sm-12">
<button class="btn btn-primary bg-primary text-white btn-block" name="update" type="submit">更新 SMS API 凭证</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>

</div>
<?php
}
}
}
require_once('../database/footer.php');
?>