<?php
include 'include/db.php';
if (isset($_REQUEST['commission_type']) && $_REQUEST['commission_type'] != "") {

    $comm_type = $_REQUEST['commission_type'];
    $joining_comm_amount = $_REQUEST['joining_comm_amount'];
    $stipulated_day = $_REQUEST['stipulated_day'];
    $stipulated_comm_amount = $_REQUEST['stipulated_comm_amount'];
    $negated_comm_amount = $_REQUEST['negated_comm_amount'];

    /**************** update code ***************/
    if (isset($_REQUEST['id']) && $_REQUEST['id'] != '') {
        $id = $_REQUEST['id'];
        if ($_REQUEST['commission_type'] == "joining") {
            $comm_query = "UPDATE commission_master commission_master set 
                            direct_comm_amount='$joining_comm_amount'
                            WHERE id=$id";
        } else if ($_REQUEST['commission_type'] == "pairing") {
            $comm_query = "UPDATE commission_master commission_master set 
                            stipulated_day='$stipulated_day',
                            stipulated_comm_amount='$stipulated_comm_amount',
                            negated_comm_amount='$negated_comm_amount'
                            WHERE id=$id";
        }

        $insert_query = mysqli_query($conn, $comm_query);
        if ($insert_query) {
            echo "<script>
                alert('commission updated successfully!');
                window.location.href ='manage_commission.php';
                </script>";
        } else {
            echo mysqli_error($conn);
        }
    }
    /**************** update code ***************/
    /*************insert code ****************/
     else {
        if ($_REQUEST['commission_type'] == "joining") {

            $comm = "select * from commission_master where commission_type='$comm_type'";
            $res = mysqli_query($conn, $comm);
            if(mysqli_num_rows($res) == 0)
            {
                $comm_query = "insert into commission_master(commission_type,direct_comm_amount,created_at)
                values('$comm_type','$joining_comm_amount',NOW())";
                $insert_query = mysqli_query($conn, $comm_query);
                if ($insert_query) {
                    echo "<script>
                        alert('commission added successfully!');
                        window.location.href = 'manage_commission.php';
                        </script>";
                } else {
                    echo mysqli_error($conn);
                }
            } 
            else
            {
                echo "<script>
                alert('Joining Commission already added!');
                window.location.href = 'manage_commission.php';
                </script>";
            }          
        } 
        else if ($_REQUEST['commission_type'] == "pairing") 
        {
            $comm = "select * from commission_master where commission_type='$comm_type'";
            $res = mysqli_query($conn, $comm);
            if(mysqli_num_rows($res) == 0)
            {
            $comm_query = "insert into commission_master(commission_type,stipulated_day,stipulated_comm_amount,negated_comm_amount,created_at)
                                values('$comm_type','$stipulated_day','$stipulated_comm_amount','$negated_comm_amount',NOW())";
                                $insert_query = mysqli_query($conn, $comm_query);
                                if ($insert_query) {
                                    echo "<script>
                                        alert('commission added successfully!');
                                        window.location.href = 'manage_commission.php';
                                        </script>";
                                } else {
                                    echo mysqli_error($conn);
                                }                    
            } 
            else
            {
                echo "<script>
                alert('Pairing Commission already added!');
                window.location.href = 'manage_commission.php';
                </script>";
            }                     
        }

       
    }

     /*************insert code ****************/
    
}
