<?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
date_default_timezone_set("Asia/Kuala_Lumpur");
$default_from_date = date('Y-m-01');
$default_to_date = date('Y-m-t');

if (isset($_POST['Date_Selection']))
{
    $from_date = $_POST['from_date'];
    $to_date = $_POST['to_date'];
}
else
{
    $from_date = $default_from_date;
    $to_date = $default_to_date;
}

$from_date_display = date('d-m-Y', strtotime($from_date));
$to_date_display = date('d-m-Y', strtotime($to_date));
?>

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <form method="post" action="" enctype="multipart/form-data">
                    <div class="form-body">
                        <h3 class="card-title">Date Selection</h3>
                        <hr>
                        <div class="row p-t-20">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="control-label">From</label>
                                    <input type="date" class="form-control" name="from_date" value="<?php echo $from_date; ?>" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="control-label">To</label>
                                    <input type="date" class="form-control" name="to_date" value="<?php echo $to_date; ?>" required>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-actions">
                        <button name="Date_Selection" class="btn btn-success"><i class="fa fa-check"></i> Submit</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">View Gold Sales From <?php echo $from_date_display; ?> To <?php echo $to_date_display; ?></h4>
        <h6 class="card-subtitle">Export data to Copy, CSV, Excel, PDF & Print</h6>
        <div class="table-responsive m-t-40">
            <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th style="display:none;">ID</th>
                        <th>Sales ID</th>
                        <?php if ($level == "1") { ?>
                        <th>User Detail</th>
                        <?php } ?>
                        <th>Gold Detail</th>
                        <th>Item Code</th>
                        <th>Weight (gram)</th>
                        <th>Customer Detail</th>
                        <th>Commission</th>
                        <th>Attachment</th>
                        <th>Remark</th>
                        <th>Sales Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if ($level == "1")
                    {
                        $q = "SELECT *
                              FROM gold_sales
                              WHERE status='1'
                                AND DATE(created_date) BETWEEN '$from_date' AND '$to_date'
                              ORDER BY id DESC";
                    }
                    else
                    {
                        $q = "SELECT *
                              FROM gold_sales
                              WHERE user_ref_code='$ref_code'
                                AND status='1'
                                AND DATE(created_date) BETWEEN '$from_date' AND '$to_date'
                              ORDER BY id DESC";
                    }

                    $result = $mysqli->query($q);
                    if ($result && $result->num_rows > 0)
                    {
                        while($record = $result->fetch_assoc())
                        {
                            $id = $record['id'];
                            $gold_sale_ref_code = $record['gold_sale_ref_code'];
                            $emas_name = $record['emas_name'];
                            $emas_ref_code = $record['emas_ref_code'];
                            $item_code = $record['item_code'];
                            $gold_weight = number_format((float)$record['gold_weight'], 2);
                            $customer_full_name = $record['customer_full_name'];
                            $customer_email = $record['customer_email'];
                            $customer_phone_number = $record['customer_phone_number'];
                            $customer_address = $record['customer_address'];
                            $commission_amount = number_format((float)$record['commission_amount'], 2);
                            $sale_remark = $record['sale_remark'];
                            $created_date = $record['created_date'];
                            $created_date_display = date('d-m-Y', strtotime($created_date));
                            $sale_user_ref_code = $record['user_ref_code'];
                            $sale_user_name = "-";
                            $sale_user_email = "-";
                            $sale_user_phone_number = "-";
                            $sale_user_nick_name = "-";

                            if ($level == "1")
                            {
                                $sale_user_ref_code_esc = $mysqli->real_escape_string($sale_user_ref_code);
                                $sql_sale_user = "SELECT name, last_name, email, phone_number, nick_name
                                                  FROM user
                                                  WHERE ref_code='$sale_user_ref_code_esc'
                                                  LIMIT 1";
                                $result_sale_user = $mysqli->query($sql_sale_user);
                                if ($result_sale_user && $result_sale_user->num_rows > 0)
                                {
                                    $row_sale_user = $result_sale_user->fetch_assoc();
                                    $sale_user_name = trim(($row_sale_user['name'] ?? '') . ' ' . ($row_sale_user['last_name'] ?? ''));
                                    $sale_user_email = $row_sale_user['email'] ?? '-';
                                    $sale_user_phone_number = $row_sale_user['phone_number'] ?? '-';
                                    $sale_user_nick_name = $row_sale_user['nick_name'] ?? '-';
                                }
                            }
                    ?>
                    <tr>
                        <td style="display:none;"><?php echo $id; ?></td>
                        <td><?php echo htmlspecialchars($gold_sale_ref_code, ENT_QUOTES); ?></td>
                        <?php if ($level == "1") { ?>
                        <td>
                            <?php echo htmlspecialchars($sale_user_ref_code, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($sale_user_name, ENT_QUOTES); ?><br>
                            Username: <?php echo htmlspecialchars($sale_user_nick_name, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($sale_user_email, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($sale_user_phone_number, ENT_QUOTES); ?>
                        </td>
                        <?php } ?>
                        <td><?php echo htmlspecialchars($emas_name, ENT_QUOTES); ?><br><?php echo htmlspecialchars($emas_ref_code, ENT_QUOTES); ?></td>
                        <td><?php echo htmlspecialchars($item_code, ENT_QUOTES); ?></td>
                        <td><?php echo $gold_weight; ?></td>
                        <td>
                            <?php echo htmlspecialchars($customer_full_name, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($customer_email, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($customer_phone_number, ENT_QUOTES); ?><br>
                            <?php echo nl2br(htmlspecialchars($customer_address, ENT_QUOTES)); ?>
                        </td>
                        <td>RM <?php echo $commission_amount; ?></td>
                        <td>
                            <?php
                            for ($i = 1; $i <= 3; $i++)
                            {
                                $attachment = $record['attachment_' . $i];
                                if ($attachment != "")
                                {
                            ?>
                            <a href="../Controller/uploads/<?php echo htmlspecialchars($attachment, ENT_QUOTES); ?>" download style="color: blue;">
                                <?php echo htmlspecialchars($attachment, ENT_QUOTES); ?>
                            </a><br>
                            <?php
                                }
                            }
                            ?>
                        </td>
                        <td><?php echo nl2br(htmlspecialchars($sale_remark, ENT_QUOTES)); ?></td>
                        <td><?php echo $created_date_display; ?></td>
                    </tr>
                    <?php
                        }
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
</div>
</div>

</div>
</div>

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

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

</body>
</html>
