<?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 Upah Simpan Gold 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>Upah Simpan ID</th>
                        <?php if ($level == "1") { ?>
                        <th>User Detail</th>
                        <?php } ?>
                        <th>Gold Detail</th>
                        <th>Item Code</th>
                        <th>Weight (gram)</th>
                        <th>Total Price</th>
                        <th>Customer Detail</th>
                        <th>Upah Simpan</th>
                        <th>Attachment</th>
                        <th>Remark</th>
                        <th>Upah Simpan Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    if ($level == "1")
                    {
                        $q = "SELECT *
                              FROM upah_simpan_gold
                              WHERE status='1'
                                AND DATE(created_date) BETWEEN '$from_date' AND '$to_date'
                              ORDER BY id DESC";
                    }
                    else
                    {
                        $q = "SELECT *
                              FROM upah_simpan_gold
                              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'];
                            $upah_simpan_gold_ref_code = $record['upah_simpan_gold_ref_code'];
                            $emas_name = $record['emas_name'];
                            $emas_ref_code = $record['emas_ref_code'];
                            $emas_price = number_format((float)$record['emas_price'], 2);
                            $emas_upah_simpan = number_format((float)$record['emas_upah_simpan'], 2);
                            $item_code = $record['item_code'];
                            $gold_weight = number_format((float)$record['gold_weight'], 2);
                            $total_price = number_format((float)$record['total_price'], 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'];
                            $upah_simpan_amount = number_format((float)$record['upah_simpan_amount'], 2);
                            $upah_simpan_remark = $record['upah_simpan_remark'];
                            $created_date = $record['created_date'];
                            $created_date_display = date('d-m-Y', strtotime($created_date));
                            $upah_simpan_user_ref_code = $record['user_ref_code'];
                            $upah_simpan_user_name = "-";
                            $upah_simpan_user_email = "-";
                            $upah_simpan_user_phone_number = "-";
                            $upah_simpan_user_nick_name = "-";

                            if ($level == "1")
                            {
                                $upah_simpan_user_ref_code_esc = $mysqli->real_escape_string($upah_simpan_user_ref_code);
                                $sql_upah_simpan_user = "SELECT name, last_name, email, phone_number, nick_name
                                                         FROM user
                                                         WHERE ref_code='$upah_simpan_user_ref_code_esc'
                                                         LIMIT 1";
                                $result_upah_simpan_user = $mysqli->query($sql_upah_simpan_user);
                                if ($result_upah_simpan_user && $result_upah_simpan_user->num_rows > 0)
                                {
                                    $row_upah_simpan_user = $result_upah_simpan_user->fetch_assoc();
                                    $upah_simpan_user_name = trim(($row_upah_simpan_user['name'] ?? '') . ' ' . ($row_upah_simpan_user['last_name'] ?? ''));
                                    $upah_simpan_user_email = $row_upah_simpan_user['email'] ?? '-';
                                    $upah_simpan_user_phone_number = $row_upah_simpan_user['phone_number'] ?? '-';
                                    $upah_simpan_user_nick_name = $row_upah_simpan_user['nick_name'] ?? '-';
                                }
                            }
                    ?>
                    <tr>
                        <td style="display:none;"><?php echo $id; ?></td>
                        <td><?php echo htmlspecialchars($upah_simpan_gold_ref_code, ENT_QUOTES); ?></td>
                        <?php if ($level == "1") { ?>
                        <td>
                            <?php echo htmlspecialchars($upah_simpan_user_ref_code, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($upah_simpan_user_name, ENT_QUOTES); ?><br>
                            Username: <?php echo htmlspecialchars($upah_simpan_user_nick_name, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($upah_simpan_user_email, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($upah_simpan_user_phone_number, ENT_QUOTES); ?>
                        </td>
                        <?php } ?>
                        <td>
                            <?php echo htmlspecialchars($emas_name, ENT_QUOTES); ?><br>
                            <?php echo htmlspecialchars($emas_ref_code, ENT_QUOTES); ?><br>
                            Price: RM <?php echo $emas_price; ?><br>
                            Upah Simpan: <?php echo $emas_upah_simpan; ?>%
                        </td>
                        <td><?php echo htmlspecialchars($item_code, ENT_QUOTES); ?></td>
                        <td><?php echo $gold_weight; ?></td>
                        <td>RM <?php echo $total_price; ?></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 $upah_simpan_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($upah_simpan_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>
