<?php
    include "../../db/connection.php";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SkyBots</title>

    <?php include "../support_header.php"; ?>
    <link rel="stylesheet" href="../../assets/custom_css/other_styles.css">
</head>
<body>
    <div class="wrapper">
        <div class="app-content-header">
            <div class="row">
                <div class="col-6"><div class="heading">Brands</div></div>
                <div class="col-6 text-right">
                    <a href="brand_save.php" class="btn btn-primary mr-2">+ Brand</a>
                    <a class="btn btn-secondary" href="../dashboard.php">Back</a>
                </div>
            </div>
        </div>
        <div class="app-content">
            <div class="card">
                <div class="datatableContainer">
                    <table id="datatable-response" class="dataTable">
                        <thead>
                            <tr>
                                <th>S.No</th>
                                <th>Brands</th>
                                <th>Added Time</th>
                                <th class="text-center">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                                $sql = "SELECT id, brand_id, brand, added_time  
                                        FROM brand 
                                        WHERE deletion = 0";
                                $res = mysqli_query($conn, $sql);
                                if ($res && mysqli_num_rows($res) > 0) {
                                    $sno = 1;
                                    while ($row = mysqli_fetch_assoc($res)) {
                            ?>
                                        <tr>
                                            <td><?php echo $sno; ?></td>
                                            <td><?php echo $row['brand']; ?></td>
                                            <td><?php echo $row['added_time']; ?></td>
                                            <td>
                                                <div class="action-dot"><i class="fa fa-ellipsis-v"></i>
                                                    <div class="menu-box">
                                                        <ul class="p-0 m-0">
                                                            <li class="menu-item"><a href="brand_save.php?brand_id=<?php echo $row['brand_id']; ?>">Edit</a></li>
                                                            <li class="menu-item"><a href="javascript:void(0)" data-id="<?php echo $row['brand_id']; ?>" id="deletemenu">Delete</a></li>
                                                        </ul>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                            <?php
                                        $sno++;
                                    }
                                }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <?php include "../support_footer.php"; ?>

    <script>
        $('#datatable-response').DataTable({
            responsive: true,
            autoWidth: false,
            scrollX: false,
            dom: '<"top d-flex justify-content-between"lBf>rt<"bottom d-flex justify-content-between"ip><"clear">',
            buttons: ['copy', 'csv', 'excel', 'pdf', 'print', 'colvis'],
            columnDefs: [
                { responsivePriority: 1, targets: -1 }
            ]
        });        
    </script>
    
    <script>
        $(document).on("click","#deletemenu",function(){
            var id=$(this).data("id");
            window.parent.showModal('deleteModal');
            window.parent.$('#delete-id').val(id);
            window.parent.$("#deleteModal .alertTitle").html("Are you sure to Delete?");
            window.parent.$('#deleteModal .yesBtn').off('click').on('click', function() { 
                delbtn(id);
            });
        });
        
        function delbtn(id) {
            var type="delete_brand";
            $.ajax({
                type: "POST",
                url: "query_function.php",
                data: {type:type,id:id},
                async: false,
                success: function(response) { 
                    if(response==11) {
                        window.parent.$("#successModal .alertTitle").html("Successfully Deleted!");
                        window.parent.showModal('successModal');
                        window.parent.$('#successModal').off('hidden.bs.modal').on('hidden.bs.modal', function () {
                            window.parent.location.reload();
                        });
                    } else {
                        window.parent.$("#warningModal .alertTitle").html("Not Deleted!");
                        window.parent.showModal('warningModal');
                    }
                }
            });
            return false;
        }
    </script>

</body>
</html>