<?php $page_title="Settings API";
    include("includes/header.php");
    require("includes/lb_helper.php");
    require("language/language.php");
    require 'vendor/autoload.php';
	
    if(!isset($_SESSION['admin_type'])){
        if($_SESSION['admin_type'] == 0){
            session_destroy();
            header( "Location:index.php");
            exit;
        }
    }
	
	$tableName="tbl_settings";
    
    $qry="SELECT * FROM tbl_settings where id='1'";
    $result=mysqli_query($mysqli,$qry);
    $settings_data=mysqli_fetch_assoc($result);

    if(isset($_POST['submit_general'])){

        if($_FILES['bg_login']['name']!=""){

            if($settings_data['bg_login']!=""){
                unlink('images/'.$settings_data['bg_login']);
            }
			
            $file = $_FILES['bg_login'];
            $tmpName = $file['tmp_name'];
            $originalName = basename($file['name']);
            $uploadPath = 'images/' . $originalName;
			
            $ext = pathinfo($_FILES['bg_login']['name'], PATHINFO_EXTENSION);
            $bg_login=rand(0,99999)."_bg_login.".$ext;
            $resizedPath='images/'.$bg_login;
			
            if (move_uploaded_file($tmpName, $uploadPath)) {
                if (resizeImage($uploadPath, $resizedPath, 1920,1080)) {
					unlink($uploadPath);
                } else {
                    $_SESSION['msg']="24";
                    $_SESSION['class'] = "error";
                    header( "Location:settings_api.php");
                    exit;
                }
            } else {
                $_SESSION['msg']="23";
                $_SESSION['class'] = "error";
                header( "Location:settings_api.php");
                exit;
            }

        } else {
            $bg_login = $settings_data['bg_login'];
        }

        if($_FILES['bg_main']['name']!=""){

            if($settings_data['bg_main']!=""){
                unlink('images/'.$settings_data['bg_main']);
            }
			
            $file = $_FILES['bg_main'];
            $tmpName = $file['tmp_name'];
            $originalName = basename($file['name']);
            $uploadPath = 'images/' . $originalName;
			
            $ext = pathinfo($_FILES['bg_main']['name'], PATHINFO_EXTENSION);
            $bg_main=rand(0,99999)."_bg_main.".$ext;
            $resizedPath='images/'.$bg_main;
			
            if (move_uploaded_file($tmpName, $uploadPath)) {
                if (resizeImage($uploadPath, $resizedPath, 1920,1080)) {
					unlink($uploadPath);
                } else {
                    $_SESSION['msg']="24";
                    $_SESSION['class'] = "danger";
                    header( "Location:settings_api.php");
                    exit;
                }
            } else {
                $_SESSION['msg']="23";
                $_SESSION['class'] = "danger";
                header( "Location:settings_api.php");
                exit;
            }
		
        } else {
            $bg_main = $settings_data['bg_main'];
        }
		
        if($_FILES['bg_setting']['name']!=""){

            if($settings_data['bg_setting']!=""){
                unlink('images/'.$settings_data['bg_setting']);
            }
			
            $file = $_FILES['bg_setting'];
            $tmpName = $file['tmp_name'];
            $originalName = basename($file['name']);
            $uploadPath = 'images/' . $originalName;
			
            $ext = pathinfo($_FILES['bg_setting']['name'], PATHINFO_EXTENSION);
            $bg_setting=rand(0,99999)."_bg_setting.".$ext;
            $resizedPath='images/'.$bg_setting;
			
            if (move_uploaded_file($tmpName, $uploadPath)) {
                if (resizeImage($uploadPath, $resizedPath, 1920,1080)) {
					unlink($uploadPath);
                } else {
                    $_SESSION['msg']="24";
                    $_SESSION['class'] = "danger";
                    header( "Location:settings_api.php");
                    exit;
                }
            } else {
                $_SESSION['msg']="23";
                $_SESSION['class'] = "danger";
                header( "Location:settings_api.php");
                exit;
            }
		
        } else {
            $bg_setting = $settings_data['bg_setting'];
        }
		
        if($_FILES['bg_search']['name']!=""){
			
            if($settings_data['bg_search']!=""){
                unlink('images/'.$settings_data['bg_search']);
            }
			
            $file = $_FILES['bg_search'];
            $tmpName = $file['tmp_name'];
            $originalName = basename($file['name']);
            $uploadPath = 'images/' . $originalName;
			
            $ext = pathinfo($_FILES['bg_search']['name'], PATHINFO_EXTENSION);
            $bg_search=rand(0,99999)."_bg_setting.".$ext;
            $resizedPath='images/'.$bg_search;
			
            if (move_uploaded_file($tmpName, $uploadPath)) {
                if (resizeImage($uploadPath, $resizedPath, 1920,1080)) {
					unlink($uploadPath);
                } else {
                    $_SESSION['msg']="24";
                    $_SESSION['class'] = "danger";
                    header( "Location:settings_api.php");
                    exit;
                }
            } else {
                $_SESSION['msg']="23";
                $_SESSION['class'] = "danger";
                header( "Location:settings_api.php");
                exit;
            }
		
        } else {
            $bg_search = $settings_data['bg_search'];
        }
        
        $data = array(
		    'bg_login' => $bg_login,
			'bg_main'  =>  $bg_main,
			'bg_setting' => $bg_setting,
			'bg_search' => $bg_search,
			'bg_color' => $_POST['bg_color'],
        );
        $settings_edit=Update('tbl_settings', $data, "WHERE id = '1'");
        
        $_SESSION['msg']="11";
        $_SESSION['class'] = "success";
        header( "Location:settings_api.php");
        exit;
        
    } else if(isset($_POST['countdown_update_submit'])){
        
        $data = array(
            'countdown_status'  =>  !empty($_POST['countdown_status']) ? 1 : 0,
            'countdown_time'  =>  trim($_POST['countdown_time'])
        );
        
        $settings_edit = Update('tbl_settings', $data, "WHERE id = '1'");
        
        $_SESSION['msg']="11";
        $_SESSION['class']='success'; 
        header("Location:settings_api.php?status=2");
        exit;
        
    } else if(isset($_POST['check_auth_update_submit'])){
        
        $data = array(
            'check_auth'  =>  trim($_POST['check_auth_time'])
        );
        
        $settings_edit = Update('tbl_settings', $data, "WHERE id = '1'");
        
        $_SESSION['msg']="11";
        $_SESSION['class']='success'; 
        header("Location:settings_api.php?status=2");
        exit;
        
    }
?>

<!-- Start: main -->
<main id="nsofts_main">
    <div class="nsofts-container">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb align-items-center">
                <li class="breadcrumb-item d-inline-flex"><a href="dashboard.php"><i class="ri-home-4-fill"></i></a></li>
                <li class="breadcrumb-item d-inline-flex active" aria-current="page"><?php echo (isset($page_title)) ? $page_title : "" ?></li>
            </ol>
        </nav>
            
        <div class="card">
            <div class="card-body p-0">                    
                <div class="nsofts-setting">
                    <div class="nsofts-setting__sidebar">
                        <a class="d-inline-flex align-items-center text-decoration-none fw-semibold mb-4">
                            <span class="ps-2 lh-1"><?php echo (isset($page_title)) ? $page_title : "" ?></span>
                        </a>
                        <div class="nav flex-column nav-pills" id="nsofts_setting" role="tablist" aria-orientation="vertical">
                            <button class="nav-link active" id="nsofts_setting_1" data-bs-toggle="pill" data-bs-target="#nsofts_setting_content_1" type="button" role="tab" aria-controls="nsofts_setting_1" aria-selected="true">
                                <i class="ri-list-settings-line"></i>
                                <span>General</span>
                            </button>
                            
                            <button class="nav-link" id="nsofts_setting_2" data-bs-toggle="pill" data-bs-target="#nsofts_setting_content_2" type="button" role="tab" aria-controls="nsofts_setting_2" aria-selected="false">
                                <i class="ri-skip-right-fill"></i>
                                <span>Next Episode</span>
                            </button>
                            
                            <button class="nav-link" id="nsofts_setting_3" data-bs-toggle="pill" data-bs-target="#nsofts_setting_content_3" type="button" role="tab" aria-controls="nsofts_setting_3" aria-selected="false">
                                <i class="ri-pass-valid-line"></i>
                                <span>Check Auth</span>
                            </button>

                        </div>
                    </div>
                    <div class="nsofts-setting__content">
                        <div class="tab-content">
                        
                            <!--General Settings-->
                            <div class="tab-pane fade show active" id="nsofts_setting_content_1" role="tabpanel" aria-labelledby="nsofts_setting_1" tabindex="0">
                                <form action="" name="settings_general" method="POST" enctype="multipart/form-data">
                                    <h4 class="mb-4">General Settings</h4>
                                    
                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-2 col-form-label">Backgrounds Login</label>
                                        <div class="col-sm-10">
                                            <div class="row">
                                                <div class="col-md-5">
                                                    <input type="file" class="form-control-file" name="bg_login" value="fileupload" accept=".png, .jpg, .jpeg, .PNG, .JPG, .JPEG" onchange="fileValidation(this,'#loginPreview')" id="fileupload">
                                                    <span class="badge text-bg-dark">( Recommended resolution: 1920x1080 )</span>
                                                </div>
                                                <div class="col-md-3">
                                                    <?php if($settings_data['bg_login']!='' AND file_exists('images/'.$settings_data['bg_login'])) { ?>
                                                        <div class="fileupload_img" id="loginPreview">
                                                            <img  type="image" src="images/<?=$settings_data['bg_login']?>" style="width: 50px;height: 50px"   alt="image" />
                                            <a href="javascript:void(0)" class="btn btn-outline-danger rounded-pill me-2 btn-icon image_delete" data-id="bg_login" data-table="<?=$tableName ?>" data-bs-toggle="tooltip" data-bs-placement="top" title="Delete">
                                                <i class="ri-delete-bin-line"></i>
                                            </a>
                                                        </div>
                                                    <?php }else{ ?>
                                                        <div class="fileupload_img" id="loginPreview">
                                                            <img type="image" src="assets/images/300x300.jpg" style="width: 50px; height: 50px"  alt="image" />
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-2 col-form-label">Backgrounds Main</label>
                                        <div class="col-sm-10">
                                            <div class="row">
                                                <div class="col-md-5">
                                                    <input type="file" class="form-control-file" name="bg_main" value="fileupload" accept=".png, .jpg, .jpeg, .PNG, .JPG, .JPEG" onchange="fileValidation(this,'#mainPreview')" id="fileupload">
                                                    <span class="badge text-bg-dark">( Recommended resolution: 1920x1080 )</span>
                                                </div>
                                                <div class="col-md-3">
                                                    <?php if($settings_data['bg_main']!='' AND file_exists('images/'.$settings_data['bg_main'])) { ?>
                                                        <div class="fileupload_img" id="mainPreview">
                                                            <img  type="image" src="images/<?=$settings_data['bg_main']?>" style="width: 50px;height: 50px"   alt="image" />
                                            <a href="javascript:void(0)" class="btn btn-outline-danger rounded-pill me-2 btn-icon image_delete" data-id="bg_main" data-table="<?=$tableName ?>" data-bs-toggle="tooltip" data-bs-placement="top" title="Delete">
                                                <i class="ri-delete-bin-line"></i>
                                            </a>
                                                        </div>
                                                    <?php }else{ ?>
                                                        <div class="fileupload_img" id="mainPreview">
                                                            <img type="image" src="assets/images/300x300.jpg" style="width: 50px; height: 50px"  alt="image" />
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-2 col-form-label">Backgrounds Setting</label>
                                        <div class="col-sm-10">
                                            <div class="row">
                                                <div class="col-md-5">
                                                    <input type="file" class="form-control-file" name="bg_setting" value="fileupload" accept=".png, .jpg, .jpeg, .PNG, .JPG, .JPEG" onchange="fileValidation(this,'#settingPreview')" id="fileupload">
                                                    <span class="badge text-bg-dark">( Recommended resolution: 1920x1080 )</span>
                                                </div>
                                                <div class="col-md-3">
                                                    <?php if($settings_data['bg_setting']!='' AND file_exists('images/'.$settings_data['bg_setting'])) { ?>
                                                        <div class="fileupload_img" id="settingPreview">
                                                            <img  type="image" src="images/<?=$settings_data['bg_setting']?>" style="width: 50px;height: 50px"   alt="image" />
                                            <a href="javascript:void(0)" class="btn btn-outline-danger rounded-pill me-2 btn-icon image_delete" data-id="bg_setting" data-table="<?=$tableName ?>" data-bs-toggle="tooltip" data-bs-placement="top" title="Delete">
                                                <i class="ri-delete-bin-line"></i>
                                            </a>
                                                        </div>
                                                    <?php }else{ ?>
                                                        <div class="fileupload_img" id="settingPreview">
                                                            <img type="image" src="assets/images/300x300.jpg" style="width: 50px; height: 50px"  alt="image" />
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-2 col-form-label">Backgrounds Search</label>
                                        <div class="col-sm-10">
                                            <div class="row">
                                                <div class="col-md-5">
                                                    <input type="file" class="form-control-file" name="bg_search" value="fileupload" accept=".png, .jpg, .jpeg, .PNG, .JPG, .JPEG" onchange="fileValidation(this,'#searchPreview')" id="fileupload">
                                                    <span class="badge text-bg-dark">( Recommended resolution: 1920x1080 )</span>
                                                </div>
                                                <div class="col-md-3">
                                                    <?php if($settings_data['bg_search']!='' AND file_exists('images/'.$settings_data['bg_search'])) { ?>
                                                        <div class="fileupload_img" id="searchPreview">
                                                            <img  type="image" src="images/<?=$settings_data['bg_search']?>" style="width: 50px;height: 50px"   alt="image" />
                                            <a href="javascript:void(0)" class="btn btn-outline-danger rounded-pill me-2 btn-icon image_delete" data-id="bg_search" data-table="<?=$tableName ?>" data-bs-toggle="tooltip" data-bs-placement="top" title="Delete">
                                                <i class="ri-delete-bin-line"></i>
                                            </a>
                                                        </div>
                                                    <?php }else{ ?>
                                                        <div class="fileupload_img" id="searchPreview">
                                                            <img type="image" src="assets/images/300x300.jpg" style="width: 50px; height: 50px"  alt="image" />
                                                        </div>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-2 col-form-label">Backgrounds Color</label>
                                        <div class="col-sm-5">
                                          <div class="col-sm-4 input-group mb-3">
                                            <div class="input-group-prepend">
                                              <span class="input-group-text">
                                                <input type="color" id="colorPicker" value="<?php echo $settings_data['bg_color']; ?>">
                                              </span>
                                            </div>
                                            <input type="text" name="bg_color" id="bg_color" value="<?php echo $settings_data['bg_color']; ?>"  class="form-control" required="required">
                                            </div>
                                        </div>
                                    </div>

                                    <button type="submit" name="submit_general" class="btn btn-primary" style="min-width: 120px;">Save</button>
                                </form>
                            </div>
                            
                            <!--App Update-->
                            <div class="tab-pane fade" id="nsofts_setting_content_2" role="tabpanel" aria-labelledby="nsofts_setting_2" tabindex="0">
                                <form action="" name="settings_app_update" method="POST" enctype="multipart/form-data">
                                    <h4 class="mb-4">Episode Countdown Next</h4>
                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-2 col-form-label">ON/OFF</label>
                                        <div class="col-sm-10">
                                            <div class="nsofts-switch d-flex align-items-center">
                                                <input type="checkbox" id="countdown_status" name="countdown_status" value="true" class="nsofts-switch__label" <?php if($settings_data['countdown_status']==true){ echo 'checked'; }?>/>
                                                <label for="countdown_status" class="nsofts-switch__label"></label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-3 col-form-label">Time seconds Countdown</label>
                                        <div class="col-sm-2">
                                            <input type="number" min="1" name="countdown_time" id="countdown_time" required="" class="form-control" value="<?php echo $settings_data['countdown_time'];?>">
                                        </div>
                                    </div>
                                    <button type="submit" name="countdown_update_submit" class="btn btn-primary" style="min-width: 120px;">Save</button>
                                </form>
                            </div>
                            
                            <!--App Check Auth-->
                            <div class="tab-pane fade" id="nsofts_setting_content_3" role="tabpanel" aria-labelledby="nsofts_setting_3" tabindex="0">
                                <form action="" name="settings_app_check_auth" method="POST" enctype="multipart/form-data">
                                    <h4 class="mb-4">Check Auth Access</h4>
                                    <div class="mb-3 row">
                                        <label for="" class="col-sm-3 col-form-label">Time seconds check auth <i class="ri-question-fill" data-bs-toggle="tooltip" data-bs-placement="top" title="<?php echo $client_lang["25"]; ?>"></i></label>
                                        <div class="col-sm-2">
                                            <input type="number" min="1" name="check_auth_time" id="check_auth_time" required="" class="form-control" value="<?php echo $settings_data['check_auth'];?>">
                                        </div>
                                    </div>
                                    <button type="submit" name="check_auth_update_submit" class="btn btn-primary" style="min-width: 120px;">Save</button>
                                </form>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>
<!-- End: main -->
    
<?php include("includes/footer.php");?>
<script type="text/javascript">

    $(document).ready(function(event) {
        
        $(document).on("click", ".btn_token", function(e) {
            var el = document.getElementById('access_token');
			el.select();
			el.setSelectionRange(0, 99999); // For mobile devices
            var successful = copyToClipboard(el);
            if (successful) {
                $.notify('Copied!', { position:"top right",className: 'success'} );
            } else {
                $.notify('Whoops, not copied!', { position:"top right",className: 'error'} );
            }
        });
        
        $(document).on("click", ".btn_generate", function(e) {
            var el = document.getElementById('access_token');
            var successful = generateToken(el);
            $.notify('Generate Token!', { position:"top right",className: 'success'} );
        });
		
        const colorPicker = document.getElementById('colorPicker');
        const hexCodeInput = document.getElementById('bg_color');
        function updateHexCode() {
                const hexCode = colorPicker.value;
                hexCodeInput.value = hexCode.toUpperCase();
        }
        colorPicker.addEventListener('input', updateHexCode);
        updateHexCode();


	
    });
</script>