<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<!DOCTYPE html>
<html lang="en">
<?php include ("../Header/head.php"); ?>
<body class="skin-default fixed-layout">

<div id="main-wrapper">    
<?php include ("../Header/header.php"); ?>
<?php include ("../Header/left_menu.php"); ?>
<?php include ("../Header/right_sidebar.php"); ?>
        
<div class="page-wrapper">
<div class="container-fluid">
<br><br>

<?php
if ($ref_code != "MGP10000") 
{
    echo "<script type='text/javascript'>alert('You are not authorized');</script>";
    unset($_SESSION['sess_user']);
    session_destroy();
    echo "<script type='text/javascript'> document.location='https://mygoldsdnbhd.com/log-masuk/'; </script>";
} 
?>

<div class="row">
<div class="col-lg-12">
<div class="card">
    <div class="card-body">
        <form method="post" action="../Controller/Database_sql.php" enctype="multipart/form-data">
            <div class="form-body">
                <h3 class="card-title">Add New Sales Bonus</h3>
                <hr>
                <div class="row p-t-20">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">Type Of Emas</label>
                            <select class="form-control" name="emas_setting_id" required>
                                <option value="">Please Select Emas</option>
                                <?php
                                $q_emas = "SELECT id, emas_ref_code, emas_name
                                           FROM emas_setting
                                           WHERE status='1'
                                           ORDER BY emas_name ASC";
                                $result_emas = $mysqli->query($q_emas);
                                if ($result_emas ->num_rows > 0)
                                {
                                    while($emas = $result_emas -> fetch_assoc())
                                    {
                                ?>
                                <option value="<?php echo $emas['id']; ?>"><?php echo $emas['emas_name']; ?> (<?php echo $emas['emas_ref_code']; ?>)</option>
                                <?php
                                    }
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">Name Of Bonus</label>
                            <input type="text" maxlength="100" class="form-control" name="name_of_bonus" required placeholder="Bonus Kereta">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">Total Gold Weight (gram)</label>
                            <input type="number" step="0.01" class="form-control" name="total_gold_weight" required value="0.00">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">Bonus Amount (RM)</label>
                            <input type="number" step="0.01" class="form-control" name="bonus_amount" required value="0.00">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">Addon Incentive (%)</label>
                            <input type="number" step="0.01" class="form-control" name="addon_incentive_percent" required value="0.00">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">Bonus Remark</label>
                            <input type="text" maxlength="100" class="form-control" name="bonus_remark">
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-actions">
                <button name="Add_Sales_Bonus_Setting" class="btn btn-success"><i class="fa fa-check"></i> Add</button>
            </div>
        </form>
    </div>
</div>
</div>
</div>

<div class="row">
<div class="col-12">
<div class="card">
    <div class="card-body">
        <h4 class="card-title">Sales Bonus Settings</h4>
        <div class="table-responsive m-t-40">            
            <table class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>Bonus Code</th>
                        <th>Type Of Emas</th>
                        <th>Name Of Bonus</th>
                        <th>Total Gold Weight (gram)</th>
                        <th>Bonus Amount (RM)</th>
                        <th>Addon Incentive (%)</th>
                        <th>Bonus Remark</th>
                        <th>Action</th>
                    </tr>
                </thead>                                      
                <tbody>
                    <?php
                    $q = "SELECT s.*, e.emas_name, e.emas_ref_code
                          FROM `Sales_Bonus_Setting` s
                          LEFT JOIN emas_setting e ON e.id = s.emas_setting_id
                          WHERE s.status='1'
                          ORDER BY s.id DESC";
                    $result = $mysqli->query($q);
                    if ($result ->num_rows > 0)
                    {
                        while($record = $result -> fetch_assoc())
                        {    
                            $id = $record['id'];
                            $sales_bonus_ref_code = $record['sales_bonus_ref_code'];
                            $emas_setting_id = $record['emas_setting_id'];
                            $name_of_bonus = $record['name_of_bonus'];
                            $total_gold_weight = $record['total_gold_weight'];
                            $bonus_amount = $record['bonus_amount'];
                            $addon_incentive_percent = $record['addon_incentive_percent'];
                            $bonus_remark = $record['bonus_remark'];
                    ?>
                    <form method="post" onsubmit="return confirm('Do you want to proceed?');" action="../Controller/Database_sql.php" enctype="multipart/form-data">
                    <tr>
                        <td><?php echo $sales_bonus_ref_code; ?></td>
                        <td>
                            <select class="form-control" name="emas_setting_id" required>
                                <?php
                                $q_emas_update = "SELECT id, emas_ref_code, emas_name
                                                  FROM emas_setting
                                                  WHERE status='1'
                                                  ORDER BY emas_name ASC";
                                $result_emas_update = $mysqli->query($q_emas_update);
                                if ($result_emas_update ->num_rows > 0)
                                {
                                    while($emas_update = $result_emas_update -> fetch_assoc())
                                    {
                                        $selected = ($emas_update['id'] == $emas_setting_id) ? "selected" : "";
                                ?>
                                <option value="<?php echo $emas_update['id']; ?>" <?php echo $selected; ?>><?php echo $emas_update['emas_name']; ?> (<?php echo $emas_update['emas_ref_code']; ?>)</option>
                                <?php
                                    }
                                }
                                ?>
                            </select>
                        </td>
                        <td><input type="text" maxlength="100" class="form-control" name="name_of_bonus" value="<?php echo $name_of_bonus; ?>"></td>
                        <td><input type="number" step="0.01" class="form-control" name="total_gold_weight" value="<?php echo $total_gold_weight; ?>"></td>
                        <td><input type="number" step="0.01" class="form-control" name="bonus_amount" value="<?php echo $bonus_amount; ?>"></td>
                        <td><input type="number" step="0.01" class="form-control" name="addon_incentive_percent" value="<?php echo $addon_incentive_percent; ?>"></td>
                        <td><textarea maxlength="100" rows="2" class="form-control" name="bonus_remark"><?php echo $bonus_remark; ?></textarea></td>
                        <input type="hidden" class="form-control" name="id" value="<?php echo $id; ?>">
                        <td>
                            <button name="Update_Sales_Bonus_Setting" class="btn btn-success"><i class="fa fa-check"></i> Update</button>
                            <button name="Delete_Sales_Bonus_Setting" class="btn btn-danger"> Remove</button>                                   
                        </td>
                    </form> 
                    </tr>
                    <?php 
                        }
                    }
                    ?>
                </tbody>
            </table>
            </form>                                 
        </div>
    </div>
</div>                      
</div>
</div>

</div>
</div>

<?php include ("../Footer/footer.php"); ?>

</div>
    
<?php include ("../Header/all_jquery.php"); ?>

</body>

</html>
