<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/header.php');

// Fetch current commission settings
$settings_query = mysqli_query($ahk_conn, "SELECT * FROM `settings` WHERE `id`=1");
$settings = mysqli_fetch_assoc($settings_query);

// Update commission if form submitted
if(isset($_POST['dist_com'])){
    // List of all commission fields
    $fields = ['airtal','vi','jio','bsnl','airtel_digital_tv_dth','tata_sky_dth','dishtv_dth','Videocon_dth','Sun_Direct_dth','google_play','user_commission'];

    $update_set = [];
    foreach($fields as $field){
        if(isset($_POST[$field]) && $_POST[$field] !== $settings[$field]){
            $value = mysqli_real_escape_string($ahk_conn, $_POST[$field]);
            $update_set[] = "`$field`='$value'";
        }
    }

    if(!empty($update_set)){
        $update_sql = "UPDATE `settings` SET ".implode(',', $update_set)." WHERE id=1";
        $update = mysqli_query($ahk_conn, $update_sql);

        if($update){
            $alert_type = "success";
            $alert_msg = "Commission Updated Successfully";
        } else {
            $alert_type = "error";
            $alert_msg = "Commission Not Updated";
        }
    } else {
        $alert_type = "info";
        $alert_msg = "No changes detected";
    }
}
?>

<div class="page-wrapper">
    <div class="page-content">
        <h6 class="mb-3 text-uppercase">Set Commission</h6>
        <div class="card">
            <div class="card-body">
                <form action="" method="POST" class="row g-3">
                    <?php
                    $labels = [
                        'airtal'=>'Airtel Commission',
                        'vi'=>'VI Commission',
                        'bsnl'=>'BSNL Commission',
                        'jio'=>'Jio Commission',
                        'airtel_digital_tv_dth'=>'Airtel DTH Commission',
                        'tata_sky_dth'=>'Tata Sky DTH Commission',
                        'dishtv_dth'=>'Dish TV DTH Commission',
                        'Videocon_dth'=>'Videocon D2H Commission',
                        'Sun_Direct_dth'=>'Sun Direct DTH Commission',
                        'google_play'=>'Google Play Commission',
                        'user_commission'=>'All Service Commission'
                    ];

                    foreach($labels as $key=>$label){
                        $value = isset($settings[$key]) ? $settings[$key] : '';
                        echo '<div class="col-md-3">
                                <label class="form-label">'.$label.'</label>
                                <input type="text" name="'.$key.'" value="'.$value.'" class="form-control">
                              </div>';
                    }
                    ?>
                    <div class="col-12">
                        <button type="submit" name="dist_com" class="btn btn-primary">Save</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include('footer.php'); ?>

<script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/ahkweb/assets/js/app.js"></script>

<?php if(isset($alert_type) && isset($alert_msg)): ?>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        Swal.fire("<?php echo $alert_msg; ?>", '', "<?php echo $alert_type; ?>");
        <?php if($alert_type == 'success'): ?>
            setTimeout(() => { window.location = ''; }, 1200);
        <?php endif; ?>
    });
</script>
<?php endif; ?>
