<?php
include 'inc/header.php';

if (isset($_SESSION['user_role']) && $_SESSION['user_role'] == 'user') {
    echo "<script>window.location.href='index.php'</script>";
    exit;
}
?>

<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">

<div class="card shadow mb-4">
    <div class="card-header py-3 d-flex justify-content-between">
        <h6 class="m-0 font-weight-bold text-secondary">Product Materials List</h6>

        <a href="assign_product_material.php" class="btn btn-primary btn-sm">
            + Assign Material
        </a>
    </div>

    <div class="card-body">
        <table id="productMaterialTable" class="display table table-bordered" width="100%">
            <thead>
            <tr>
                <th>ID</th>
                <th>Product</th>
                <th>Material</th>
                <th>Type</th>
                <th>SQFT Value</th>
                <th>LNFT Value</th>
                <th>Status</th>
                <th>Created</th>
                <th>Actions</th>
            </tr>
            </thead>

            <tbody>
            <?php
            $sql = "
            SELECT pm.*, 
                   p.name AS product_name,
                   m.name AS material_name
            FROM product_materials pm
            LEFT JOIN products p ON p.id = pm.product_id
            LEFT JOIN stone_materials m ON m.id = pm.material_id
            ORDER BY pm.id DESC
            ";

            $res = mysqli_query($conn, $sql);

            while ($row = mysqli_fetch_assoc($res)) {
            ?>
                <tr data-id="<?= $row['id'] ?>">
                    <td><?= $row['id'] ?></td>

                    <td><?= htmlspecialchars($row['product_name']) ?></td>
                    <td><?= htmlspecialchars($row['material_name']) ?></td>

                    <td>
                        <span class="badge badge-info">
                            <?= strtoupper($row['calc_type']) ?>
                        </span>
                    </td>

                    <td><?= $row['sqft_value'] ?></td>
                    <td><?= $row['lnft_value'] ?></td>

                    <td>
                        <span class="badge badge-<?= $row['status']=='active' ? 'success' : 'danger' ?>">
                            <?= ucfirst($row['status']) ?>
                        </span>
                    </td>

                    <td>
                        <?= date('d M Y h:i a', strtotime($row['created_at'])) ?>
                    </td>

                    <td>
                        <a href="edit_product_material.php?id=<?= $row['id'] ?>" class="btn btn-sm btn-info">
                            Edit
                        </a>

                        <button class="btn btn-sm btn-danger delete-btn">
                            Delete
                        </button>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
    </div>
</div>

<?php include 'inc/footer.php'; ?>

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
$(function () {

    const table = $('#productMaterialTable').DataTable();

    toastr.options = {
        closeButton: true,
        progressBar: true,
        positionClass: "toast-top-right",
        timeOut: "3000"
    };

    /* DELETE */
    $('#productMaterialTable').on('click', '.delete-btn', function () {

        let row = $(this).closest('tr');
        let id = row.data('id');

        Swal.fire({
            title: 'Are you sure?',
            text: "This assignment will be deleted!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {

            if (result.isConfirmed) {

                $.post('delete_product_material.php', { id: id }, function (res) {

                    if (res == 1) {
                        toastr.success('Deleted successfully');
                        table.row(row).remove().draw();
                    } else {
                        toastr.error('Delete failed');
                    }

                });

            }
        });

    });

});
</script>