<?php
require_once("../layouts/members/head.php");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

?>
<?php if(in_array($user_data['user_id'], ['1'])){ ?>
<?php
if(isset($_POST['siteupdate']) && $user_data['user_id']>0){
$protocol = strip_tags($_POST['protocol']);
$baseurl = strip_tags($_POST['baseurl']);
$server_name = $_SERVER['SERVER_NAME'];
if ($baseurl == $server_name) {
$brand = strip_tags($_POST['brand'] ?? 'FLX');
$title = strip_tags($_POST['title'] ?? 'FLX'); 
$mobile = strip_tags($_POST['mobile']);
$email = strip_tags($_POST['email']);
$address = strip_tags($_POST['address']);
$gatewayService = strip_tags($_POST['gateway_service']);
$whatsaapsms = strip_tags($_POST['whatsaapsms']);
$whatsaapsmsReport = strip_tags($_POST['whatsaapsmsReport']);
$emailsms = strip_tags($_POST['emailsms']);
$self_registertion = strip_tags($_POST['self_registertion']);
$user_charge = is_valid_amount($_POST['user_charge']);
$distributor_charge = is_valid_amount($_POST['distributor_charge']);
$master_charge = is_valid_amount($_POST['master_charge']);
$self_script = $_POST['self_script'];

if(!empty($protocol) && !empty($baseurl) && !empty($brand) && !empty($title) && !empty($mobile) && !empty($email) && !empty($address) && !empty($distributor_charge) 
&& !empty($master_charge) && !empty($user_charge) && !empty($gatewayService) && !empty($whatsaapsms) && !empty($whatsaapsmsReport) && !empty($emailsms) && !empty($self_registertion) ){

$insData = array();
$insData['protocol'] = $protocol;
$insData['baseurl'] = $baseurl;
$insData['brand'] = $brand;
$insData['title'] = $title;
$insData['mobile'] = $mobile;
$insData['email'] = $email;
$insData['address'] = $address;
$insData['gateway'] = $gatewayService;
$insData['whatsaapsms'] = $whatsaapsms;
$insData['whatsaapsmsReport'] = $whatsaapsmsReport;
$insData['emailsms'] = $emailsms;
$insData['self_registertion'] = $self_registertion;
$insData['user_charge'] = $user_charge;
$insData['distributor_charge'] = $distributor_charge;
$insData['master_charge'] = $master_charge;
$insData['self_script'] = $self_script;

if(db_update("sitedata", $insData, " user_id='".$user_data['user_id']."' ")){
awal_alert_msg("Web Updated Successfully!",'success','');  
}else{
awal_alert_msg("No Any Changes!",'info');  
}

}else{
awal_alert_msg("Please Fill All Valid Details!",'error');
}

}else{
awal_alert_msg("Please update with Valid URL. Expected: $server_name",'error');
}
}
?>


<?php
if (isset($_POST['logoupdate']) && $user_data['user_id'] > 0) {

    if (isset($_FILES['logo']['name']) && $_FILES['logo']['error'] == 0) {

        $allowed_extensions = array("jpg","jpeg","png","gif");
        $logo_ext = strtolower(pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION));
        $file_size = $_FILES['logo']['size']; // size in bytes

        if (in_array($logo_ext, $allowed_extensions)) {
            if ($file_size <= 150 * 1024) {  // 15KB limit
                $logo = 'data:image/' . $logo_ext . ';base64,' . base64_encode(file_get_contents($_FILES['logo']['tmp_name']));
                $insData = array();
                $insData['logo'] = $logo;

                if (db_update("sitedata", $insData, " user_id='".$user_data['user_id']."' ")) {
                    awal_alert_msg("Logo Upload Successfully!", 'success', '');
                } else {
                    awal_alert_msg("No Any Changes!", 'info');
                }
            } else {
                awal_alert_msg("File size must be less than 15KB!", 'error');
            }
        } else {
            awal_alert_msg("Please Upload Valid File!", 'error');
        }
    } else {
        awal_alert_msg("File Not Found!", 'error');
    }
}
?>


<div class="row">
  <div class="col-md-8 mb-2">
    <div class="card text-danger">
      <div class="card-header bg-primary text-white py-2">
        <div class="d-flex align-items-center">
          <span class="mr-auto"><?=file_name()?></span>
        </div>
      </div>
      <div class="p-3">
      <form class="row text-dark" method="POST" action="">
        <div class="col-md-4 mb-3">
          <label>Brand</label>
          <input type="text" class="form-control" placeholder="Enter Brand" name="brand" value="<?=$site_data['brand']?>" required>
        </div>
        <div class="col-md-4 mb-3">
          <label>Title</label>
          <input type="text" class="form-control" placeholder="Enter Title" name="title" value="<?=$site_data['title']?>" required>
        </div>
        <div class="col-md-4 mb-3">
          <label>Mobile Number</label>
          <input type="number" class="form-control" placeholder="Enter Mobile Number" name="mobile" onkeypress="if(this.value.length==10) return false;" value="<?=$site_data['mobile']?>" required>
        </div>
        <div class="col-md-4 mb-3">
          <label>Email Address</label>
          <input type="text" class="form-control" placeholder="Enter Email Address" name="email" value="<?=$site_data['email']?>" required>
        </div>
        <div class="col-md-4 mb-3">
          <label>Address</label>
          <input type="text" class="form-control" placeholder="Enter Address" name="address" value="<?=$site_data['address']?>" required>
        </div>
        <div class="col-md-4 mb-3">
          <label>Protocol & Domain</label>
          <div class="input-group input-group-alt">
            <div class="input-group-prepend">
              <select name="protocol" class="form-control">
                <option <?=$site_data['protocol']=="http://" ? 'selected':''?> value="http://">http://</option>
                <option <?=$site_data['protocol']=="https://" ? 'selected':''?> value="https://">https://</option>
              </select>
            </div>
            <input type="text" class="form-control" name="baseurl" placeholder="<?= htmlspecialchars($_SERVER['SERVER_NAME'], ENT_QUOTES, 'UTF-8') ?>" value="<?=$site_data['baseurl']?>" required>
          </div>
        </div>
        <div class="col-md-3 mb-3">
          <label for="self_registertion">Self Registration</label>
          <select class="form-control" id="self_registertion" name="self_registertion" required>
              <option value="active" <?= $site_data['self_registertion'] === 'active' ? 'selected' : '' ?>>Active</option>
              <option value="inactive" <?= $site_data['self_registertion'] === 'inactive' ? 'selected' : '' ?>>Deactive</option>
          </select>
        </div>
        
        <div class="col-md-3 mb-3">
          <label for="gatewayService">Gateway Service</label>
          <select class="form-control" id="gatewayService" name="gateway_service" required>
              <option value="active" <?= $site_data['gateway'] === 'active' ? 'selected' : '' ?>>Active</option>
              <option value="inactive" <?= $site_data['gateway'] === 'inactive' ? 'selected' : '' ?>>Deactive</option>
          </select>
        </div>

        <div class="col-md-3 mb-3">
          <label for="emailsms">Email SMS</label>
          <select class="form-control" id="emailsms" name="emailsms" required>
              <option value="active" <?= $site_data['emailsms'] === 'active' ? 'selected' : '' ?>>Active</option>
              <option value="inactive" <?= $site_data['emailsms'] === 'inactive' ? 'selected' : '' ?>>Deactive</option>
          </select>
        </div>
        
        <div class="col-md-3 mb-3">
          <label for="whatsaapsms">Whatsapp SMS <sup style="color:red; font-weight:bold;">Login|Register|Forgot</sup></label>
          <select class="form-control" id="whatsaapsms" name="whatsaapsms" required>
              <option value="active" <?= $site_data['whatsaapsms'] === 'active' ? 'selected' : '' ?>>Active</option>
              <option value="inactive" <?= $site_data['whatsaapsms'] === 'inactive' ? 'selected' : '' ?>>Deactive</option>
          </select>
        </div>
        <div class="col-md-3 mb-3">
          <label for="whatsaapsmsReport">Whatsapp SMS <sup style="color:red; font-weight:bold;">All Services</sup></label>
          <select class="form-control" id="whatsaapsmsReport" name="whatsaapsmsReport" required>
              <option value="active" <?= $site_data['whatsaapsmsReport'] === 'active' ? 'selected' : '' ?>>Active</option>
              <option value="inactive" <?= $site_data['whatsaapsmsReport'] === 'inactive' ? 'selected' : '' ?>>Deactive</option>
          </select>
        </div>
        <div class="col-md-3 mb-3">
          <label>Retailer Charge<sup style="color:red; font-weight:bold;"> Free : 00</sup></label>
          <input type="text" class="form-control" placeholder="Set Retailer Charge" name="user_charge" value="<?=$site_data['user_charge']?>" required>
        </div>
        <div class="col-md-3 mb-3">
          <label>Distributor Charge</label>
          <input type="text" class="form-control" placeholder="Set Distributor Charge" name="distributor_charge" value="<?=$site_data['distributor_charge']?>" required>
        </div>
        <div class="col-md-3 mb-3">
          <label>Master Distributor Charge</label>
          <input type="text" class="form-control" placeholder="Set Master Distributor Charge" name="master_charge" value="<?=$site_data['master_charge']?>" required>
        </div>
        <div class="col-md-12 mb-3">
          <label>Chat Script</label>
          <textarea type="text" class="form-control" placeholder="Enter Chat Script" name="self_script" style="height:100px" required><?=$site_data['self_script']?></textarea>
        </div>
        <div class="col-md-4 mb-3">
          <button type="submit" class="btn btn-primary btn-block" name="siteupdate">Submit</button>
        </div>
      </form>
     </div>
    </div>
  </div>



  <div class="col-md-4 mb-2">
    <div class="card text-danger">
      <div class="card-header bg-primary text-white py-2">
        <div class="d-flex align-items-center">
          <span class="mr-auto">Brand Logo</span>
        </div>
      </div>
      <div class="p-3">
       <form class="row text-dark" method="POST" action="" enctype="multipart/form-data">
        <div class="col-md-12 mb-3">
          <label>Logo</label>
          <input type="file" class="form-control" name="logo" required>
        </div>
        <div class="col-md-4 mb-3">
          <button class="btn btn-primary btn-block" name="logoupdate">Upload</button>
        </div>
       </form>
       <img src="<?=$site_data['logo']?>" class="logo">
      </div>
    </div>
  </div>




</div>
<?php 
}else{
require_once("404.php");
} 
?>

<?php
require_once("../layouts/members/foot.php");
data_table('#dataTable');
?>