<?php
require("header.php")
?>


<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <!-- <h1>Add User</h1> -->
                </div>
                <div class="col-sm-6">

                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="container-fluid">
            <div class="row">

                <div class="col-md-12">
                    <!-- general form elements -->
                    <div class="card card-secondary">
                        <div class="card-header">
                            <h3 class="card-title">Telegram</h3>
                            
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item">Phonebook</li>
                                <li class="breadcrumb-item active">Telegram</li>
                            </ol>
                        </div>

                        <!-- /.card-header -->
                        <!-- form start -->
                        <section class="content">
                            <div class="container-fluid">
                                <div class="row">
                                    <!-- left column -->
                                    <div class="col-md-12">
                                        <!-- general form elements -->
                                        <div class="card shadow ">

                                            <!-- /.card-header -->
                                            <!-- form start -->
                                            <form action="add_telegram_code.php" method="POST">
                                                <div class="card-body">

                                                    <div class="row">
                                                        <div class="col-5">
                                                            <div class="form-group">
                                                                <label>Number</label>
                                                                <input type="text" required class="form-control" placeholder="Number" name="number">
                                                            </div>
                                                        </div>
                                                        <div class="col-6">
                                                            <div class="form-group">
                                                                <label>UserName</label>
                                                                <input type="text" required class="form-control" placeholder="Name" name="username">
                                                            </div>
                                                        </div>



                                                        <div class="col-1" style="margin-top: 23px;">
                                                            <button type="submit" class="btn btn-danger mt-2" style="background: #343a40;border:none">Submit</button>
                                                        </div>

                                                    </div>

                                                </div>

                                            </form>
                                        </div>
                                        <!-- /.card -->


                                    </div>

                                </div>
                                <!-- /.row -->
                            </div><!-- /.container-fluid -->
                        </section>

                        <section class="content">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-12">
                                        <div class="card shadow">
                                            <div class="card-header" style="background: #343a40; ">
                                                <h3 class="card-title text-white text-bold text-uppercase"> View All
                                                    Telegram
                                                </h3>
                                            </div>
                                            <!-- /.card-header -->
                                            <div class="card-body">
                                                <div id="example2_wrapper" class="dataTables_wrapper dt-bootstrap4">
                                                    <div class="row">
                                                        <div class="col-sm-12 col-md-6"></div>
                                                        <div class="col-sm-12 col-md-6"></div>
                                                    </div>
                                                    <div class="row">
                                                        <div class="col-sm-12">

                                                            <table id="example2" class="table table-bordered table-hover dataTable dtr-inline display" aria-describedby="example2_info">
                                                                <thead>
                                                                    <tr>
                                                                        <th>Date &amp; time</th>
                                                                        <th>Number</th>
                                                                        <th>UserName</th>
                                                                        <th>Telegram Channel</th>
                                                                        <th>Telegram Group</th>
                                                                        <th>Action</th>

                                                                    </tr>
                                                                </thead>
                                                                <tbody>
                                                                    <?php
                                                                    include('connection.php');

                                                                    $sql = "SELECT * FROM telegram ";
                                                                    $res = mysqli_query($conn, $sql);
                                                                    while ($row = mysqli_fetch_assoc($res)) {


                                                                    ?>
                                                                        <tr>
                                                                            <td><?php echo $row['date_time'] ?></td>
                                                                            <td><?php echo $row['number'] ?></td>
                                                                            <td><?php echo $row['username'] ?></td>
                                                                            <td>
                                                                                <a href="#" class="btn btn-outline-primary btn-block btn-sm" data-toggle="modal" data-target="#addChannelModal<?php echo $row['number']; ?>">
                                                                                    <i class="nav-icon fas fa-edit info"></i> Add Channel
                                                                                </a>
                                                                            </td>
                                                                            <td><button type="button" class="btn btn-outline-primary btn-block btn-sm" data-toggle="modal" data-target="#editContactModal<?php echo $row['number']; ?>"><i class="fa fa-book"></i> Add
                                                                                    Group</button></td>

                                                                            <td>
                                                                                <a href="delete_contact.php?id=<?php echo $row['id']; ?>" class="btn btn-danger btn-sm">Delete</a>
                                                                            </td>
                                                                        </tr>
                                                                        <!-- moadal start  -->
                                                                        <div class="modal fade" id="addChannelModal<?php echo $row['number']; ?>">
                                                                            <div class="modal-dialog modal-lg">
                                                                                <div class="modal-content">
                                                                                    <div class="modal-header">
                                                                                        <h4 class="modal-title"> Telegram Channel(<?php echo $row['username']; ?>) </h4>
                                                                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                                            <span aria-hidden="true">×</span>
                                                                                        </button>
                                                                                    </div>
                                                                                    <div class="modal-body">
                                                                                        <form action="add_telegram_channel.php" method="POST">
                                                                                            <input type="hidden" class="form-control" value="<?php echo $row['username']; ?>" name="username">
                                                                                            <input type="hidden" class="form-control" value="<?php echo $row['number']; ?>" name="number">

                                                                                            <?php
                                                                                            // Retrieve data from telegram_channel table based on the 'number'
                                                                                            $tele_channel_query = "SELECT * FROM telegram_channel WHERE number = '{$row['number']}'";
                                                                                            $tele_channel_result = mysqli_query($conn, $tele_channel_query);

                                                                                            // Loop through the retrieved data
                                                                                            $i = 0;
                                                                                            while ($tele_channel_row = mysqli_fetch_assoc($tele_channel_result)) {
                                                                                                $i++;
                                                                                            ?>
                                                                                                <div class="row">
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>Add Channel</label>
                                                                                                            <input type="text" required class="form-control mb-1" placeholder="Enter Channel Name" name="tele_channel[]" value="<?php echo $tele_channel_row['tele_channel']; ?>">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>Channel URL</label>
                                                                                                            <input type="text" required class="form-control mb-1" name="channel_url[]" placeholder="Enter Channel Url" value="<?php echo $tele_channel_row['channel_url']; ?>">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                </div>
                                                                                            <?php
                                                                                            }
                                                                                            ?>

                                                                                            <?php for ($j = $i; $j < 10; $j++) { // Display empty input fields for the remaining slots 
                                                                                            ?>
                                                                                                <div class="row">
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>Add Channel</label>
                                                                                                            <input type="text" required class="form-control mb-1" placeholder="Enter Channel Name" name="tele_channel[]">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>Channel URL</label>
                                                                                                            <input type="text" required class="form-control mb-1" name="channel_url[]" placeholder="Enter Channel Url">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                </div>
                                                                                            <?php } ?>
                                                                                            <div class="modal-footer justify-content-between">
                                                                                                <button type="button" class="btn btn-dark btn-sm" data-dismiss="modal">Close</button>
                                                                                                <button type="submit" class="btn btn-dark btn-sm" name="add"> <i class="fa fa-book"></i> Add Data</button>
                                                                                            </div>
                                                                                        </form>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                        <!-- moadal end -->

                                                                        <!-- 2 moadal start -->


                                                                        <div class="modal fade" id="editContactModal<?php echo $row['number']; ?>" tabindex="-1" role="dialog" aria-labelledby="editContactModalLabel" aria-hidden="true">
                                                                            <div class="modal-dialog modal-lg" role="document">
                                                                                <div class="modal-content">
                                                                                    <div class="modal-header">
                                                                                        <h5 class="modal-title" id="editContactModalLabel">Add Group </h5>
                                                                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                                            <span aria-hidden="true">&times;</span>
                                                                                        </button>
                                                                                    </div>
                                                                                    <div class="modal-body">

                                                                                        <form action="add_telegram_group.php" method="POST">
                                                                                            <input type="hidden" class="form-control" value="<?php echo $row['username']; ?>" name="username">
                                                                                            <input type="hidden" class="form-control" value="<?php echo $row['number']; ?>" name="number">

                                                                                            <?php
                                                                                            // Retrieve data from telegram_channel table based on the 'number'
                                                                                            $tele_group_query = "SELECT * FROM telegram_group WHERE number = '{$row['number']}' LIMIT 10";
                                                                                            $tele_group_result = mysqli_query($conn, $tele_group_query);

                                                                                            // Loop through the retrieved data
                                                                                            $i = 0;
                                                                                            while ($tele_group_row = mysqli_fetch_assoc($tele_group_result)) {
                                                                                                $i++;
                                                                                            ?>
                                                                                                <div class="row">
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>Add Group</label>
                                                                                                            <input type="text" required class="form-control mb-1" placeholder="Enter group Name" name="tele_group[]" value="<?php echo $tele_group_row['tele_group']; ?>">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>Group URL</label>
                                                                                                            <input type="text" required class="form-control mb-1" name="group_url[]" placeholder="Enter group Url" value="<?php echo $tele_group_row['group_url']; ?>">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                </div>
                                                                                            <?php
                                                                                                if ($i >= 10) {
                                                                                                    break; // Stop after displaying 10 rows
                                                                                                }
                                                                                            }
                                                                                            ?>

                                                                                            <?php for ($j = $i; $j < 10; $j++) { // Display empty input fields for the remaining slots 
                                                                                            ?>
                                                                                                <div class="row">
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>Add group</label>
                                                                                                            <input type="text" required class="form-control mb-1" placeholder="Enter group Name" name="tele_group[]">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                    <div class="col-12 col-md-6 col-lg-6">
                                                                                                        <div class="form-group">
                                                                                                            <label>group URL</label>
                                                                                                            <input type="text" required class="form-control mb-1" name="group_url[]" placeholder="Enter group Url">
                                                                                                        </div>
                                                                                                    </div>
                                                                                                </div>
                                                                                            <?php } ?>
                                                                                            <div class="modal-footer justify-content-between">
                                                                                                <button type="button" class="btn btn-dark btn-sm" data-dismiss="modal">Close</button>
                                                                                                <button type="submit" class="btn btn-dark btn-sm" name="add"> <i class="fa fa-book"></i> Add Data</button>
                                                                                            </div>
                                                                                        </form>



                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                        <!-- moadal  end -->



                                                                    <?php } ?>
                                                                    <!-- Add more rows as needed -->
                                                                </tbody>

                                                            </table>


                                                            <!-- /.modal-content -->
                                                        </div>
                                                    </div>

                                                </div>
                                            </div>
                                            <!-- /.card-body -->
                                        </div>

                                    </div>
                                    <!-- /.col -->
                                </div>

                            </div>

                        </section>
                    </div>
                    <!-- /.card -->




                </div>

            </div>

            <!-- /.col -->
        </div>
        <!-- /.row -->

        <!-- /.container-fluid -->
    </section>
    <!-- /.content -->
</div>
<!-- for searching -->
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>

<script src="https://code.jquery.com/jquery-3.5.1.js"></script>

<script>
    $().ready(function() {
        $("#gnrt_pass").click(function() {
            var randomstring = Math.random().toString(36).slice(-8);
            $("input[name=password]").val(randomstring);
        })
    })
</script>
<!-- searching end -->

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<script>
    <?php if (isset($_GET['error'])) { ?>
        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: '<?php echo $_GET['error']; ?>',
        });
    <?php } ?>

    <?php if (isset($_GET['success'])) { ?>
        Swal.fire({
            icon: 'success',
            title: 'Success',
            text: '<?php echo $_GET['success']; ?>',
        });
    <?php } ?>
</script>
<!-- pagination -->


<?php
require("footer.php")
?>