<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;

$heading = "Manage Popup Forms";
$record_type = "Popup Forms";
$script_name = "manage_forms.php";
$table_name = "forms_tbl";
####################################################

if ($id == "add_record" && $_POST['name'] != "") {


    $form_id =  htmlentitiesDecode($form_id);
    $maxRes = mysqli_fetch_array(mysqli_query($dbconn, "select max(tiltOrder) from $table_name"));
    $tiltOrder = $maxRes[0] + 1;

    $sql = db_query("insert into $table_name set name='$name',form_id='$form_id',tiltOrder='$tiltOrder',added_on=now()");
    echo $sql;
    exit;
    
  
    //$_SESSION['session_message'] = '<div class="btn btn-success">New ' . $record_type . ' has been added successfully </div>';
    // header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&pagesize=" . $pagesize);
    // exit;
}

if ($id == "update_record") {

    $form_id =  htmlentitiesDecode($form_id);
    $update = updateTable($table_name, " name='$name',form_id='$form_id', added_on=now() where id='$recordID'");
    //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully  </div>';
    echo $update;
    exit;
}

if ($action == "Update") {
    $input = $_POST;
    $part = array_splice($input, 0, 2);
  
    while (list($key, $val) = each($input)) {
        if (substr($key, 0, 6) == "order_") {
            $key1 = substr($key, 6, strlen($key) - 6);
  
            $qry1 = db_query("update $table_name set tiltOrder='$val' where id='$key1'");
        }
    }
    //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully  </div>';
  
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }
  
  if (isset($_POST['active'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'Y' WHERE id = '$id'");
        }
        //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }
  
  if (isset($_POST['inactive'])) {
    if (!empty($_POST['cat_arr'])) {
        $catid = $_POST['cat_arr'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'N'  WHERE id = '$id'");
        }
        //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
    } else {
        //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }
  
  if (isset($_POST['delete'])) {
    if (!empty($_POST['cat_arr'])) {
        $arr_id = $_POST['cat_arr'];
        foreach ($arr_id as $id) {
            mysqli_query($dbconn, "DELETE FROM $table_name WHERE id = '$id'");
        }
        // $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
    } else {
        //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
    }
    header("Location: ".SITE_ADMIN_URL."/".$script_name);
    exit;
  }
########################################################
admin_header("");
$qry= db_query("select * from $table_name");
?>
<?php
 $res=getResult($table_name," where id='$_GET[recordID]'");
?>
<div class="content-wrapper">

    <!-- 
    <div class="page-header">
        <h3 class="page-title">Add New Tilt Scroller</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?admin_url?>welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Add New Tiltt Scroller</li>
            </ol>
        </nav>
    </div>
    <div class="row">
        <div class="col-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">






                    <form class="forms-sample" method="POST" action="" enctype="multipart/form-data">
                        <div class="row">
                            <div class="col-lg-4">
                                <div class="form-group">
                                    <label>Tilt Heading</label>
                                    <input class="typeahead" type="text" name="name"
                                        value="<?=$res['name']?>" placeholder="Tilt Heading" required>
                                </div>
                            </div>
        
                        </div>



                        <?php
          if($_GET[recordID] != Null) {
            ?>
                        <button type="submit" class="btn btn-primary mr-2" name="add_record">Update Heading</button>
                        <input type="hidden" name="recordID" value="<?=$res['id'];?>">
                        <input type="hidden" name="id" value="update_record">
                        <?php
          } else {
            ?>
                        <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                        <input type="hidden" name="id" value="add_record">
                        <?php
          }
          ?>
                        <button class="btn btn-light"
                            onclick="javascript: window.location.href = '<?=SITE_ADMIN_URL;?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>'">Cancel</button>

                    </form>
                </div>
            </div>
        </div>
    </div> -->



    <div class="page-header">
        <h3 class="page-title">
            <?=$heading;?>

        </h3>
        <nav aria-label="breadcrumb">

            <ol class="breadcrumb">


                <li class="nav-item d-none d-lg-flex">

                    <a class="nav-link tiltData" href="#" data-toggle="modal" data-action="add_record"
                        data-target="#edtPop">
                        <span class="btn btn-info">+ Add New <?=$record_type;?></span>
                    </a>
                </li>
                <!-- <li class="breadcrumb-item"><a href="welcome.php">Admin</a></li>
                <li class="breadcrumb-item active" aria-current="page">Manage Tilt Scroller Heading</li> -->


            </ol>
        </nav>
    </div>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">
                    <form action="" method="post">
                        <div class="table-responsive">
                            <table id="order-listing" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th class="_reab">
                                            <div class="form-check mt-0">
                                                <label class="form-check-label">
                                                    <input type="checkbox" class="form-check-input">
                                                    <i class="input-helper"></i>
                                                </label>
                                            </div>
                                        </th>
                                        <th>S.No.</th>
                                        <th>Form Name </th>
                                        <th>Order</th>
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) { ?>
                                    <tr>
                                        <td>
                                            <div class="form-check mt-0"><label class="form-check-label"><input
                                                        type="checkbox" class="form-check-input" name="cat_arr[]"
                                                        value="<?= $arr['id'] ?>"><i class="input-helper"></i></label>
                                            </div>
                                        </td>
                                        <td><?=$sl?></td>
                                        <td>
                                            <p class="ordern"><?=$arr['name'];?></p>
                                        </td>
                                        <td>
                                            <input name="order_<?= $arr['id']; ?>" class="form-control order"
                                                type="text" size="2" value="<?= $arr['tiltOrder']; ?>">
                                        </td>

                                        <td>
                                            <p class="status-dcs"><?php if ($arr['status'] == 'Y') { ?><label
                                                    class="badge badge-success badge-pill">Active</label><?php } else if ($arr['status'] == 'N') { ?>
                                                <label class="badge badge-danger badge-pill">In-Active</label>
                                                <?php } ?>
                                            </p>
                                        </td>

                                        <td class="download-dcs">


                                            <a href="#" class="tiltData" data-toggle="modal" data-target="#edtPop"
                                                data-action="update_record" data-name="<?= $arr['name']; ?>"
                                                data-formid="<?= $arr['form_id']; ?>" data-id="<?= $arr['id']; ?>"><svg
                                                    xmlns="http://www.w3.org/2000/svg" width="24" height="24"
                                                    viewBox="0 0 24 24" fill="none" stroke="currentColor"
                                                    stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
                                                    class="feather feather-edit-3">
                                                    <path d="M12 20h9"></path>
                                                    <path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z">
                                                    </path>
                                                </svg></a>
                                        </td>
                                    </tr>

                                    <?php  $sl++; }?>

                                </tbody>
                            </table>
                            <div class="btn-grp">
                                <button type="submit" name="active" onclick="return select_chk()"
                                    class="btn btn-success btn-icon-text btn-sm">
                                    <i class="fas fa-toggle-on mr-1"></i>Activate
                                </button>
                                <button type="submit" name="inactive" onclick="return select_chk()"
                                    class="btn btn-warning btn-icon-text btn-sm">
                                    <i class="far fa-times-circle mr-1"></i>Deactivate
                                </button>

                                <button type="submit" name="action" value="Update"
                                    class="btn btn-default btn-icon-text btn-sm">
                                    <i class="far fa-check-square mr-1"></i>Update Order
                                </button>
                                <button type="submit" name="delete" onclick="return select_chk()"
                                    class="btn btn-danger btn-icon-text btn-sm">
                                    <i class="fas fa-trash-alt mr-1"></i>Delete
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal" id="edtPop" tabindex="-1" role="dialog">

    <div class="modal-dialog" role="document">

        <div class="modal-content">

            <div class="modal-header p-2">

                <h5 class="modal-title">Popup Forms</h5>

                <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                    <span aria-hidden="true">&times;</span>

                </button>

            </div>

            <form action="" class="form_submit" id="myForm" enctype="form-data/multipart" method="post">

                <div class="modal-body p-2">

                    <div class="form-group row">


                        <div class="col-md-12 mb-2">

                            <label>Form Name<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text" value="" name="name"
                                    placeholder="" required>

                            </div>

                        </div>

                        <div class="col-md-12 mb-2">

                            <label>Form ID<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <textarea class="form-control" name="form_id" rows="4"></textarea>

                            </div>

                        </div>

                    </div>

                </div>

                <div class="modal-footer">


                    <button type="submit" class="btn btn-primary mr-2" name="add_record">Submit</button>
                    <input type="hidden" name="id" value="">
                    <input type="hidden" name="recordID" value="">

                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

                </div>

            </form>

        </div>

    </div>

</div>





<script language="javascript">
function select_chk() {
    var chks = document.getElementsByName('cat_arr[]');
    var hasChecked = false;
    for (var i = 0; i < chks.length; i++) {
        if (chks[i].checked) {
            hasChecked = true;
            break;
        }
    }
    if (hasChecked == false) {
        alert("Please Select At Least One.");
        return false;
    }
}
</script>
<?php
admin_footer();
?>
<script>
$(document).ready(function() {


    $(document).on("click", ".tiltData", function() {

        $('input[name=id]').val($(this).data('action'));
        $('input[name=recordID]').val($(this).data('id'));
        $('input[name=name]').val($(this).data('name'));
        $('textarea[name=form_id]').val($(this).data('formid'));

        
    });


    $("#myForm").submit(function(event) {
        event.preventDefault(); // Prevent the default form submission
        var formData = $(this).serialize(); // Serialize the form data
        $.ajax({
            url: '', // Empty URL to call the same page
            type: 'POST',
            data: formData, // Send the serialized form data
            success: function(response) {
                if (response == 1) {
                    $('.breadcrumb').prepend(
                        '<div class="alert alert-success"><strong>Success!</strong> Data Saved successfully</div>'
                    );
                    $('#myForm')[0].reset();
                    $('#edtPop').modal('hide');
                    setTimeout(function() {
                        window.location.reload();
                    }, 3000);
                } else {

                    $('.breadcrumb').prepend(
                        '<div class="alert alert-danger"><strong>Error!</strong> Something went wrong.</div>'
                    );
                    $('#myForm')[0].reset();
                    $('#edtPop').modal('hide');
                    setTimeout(function() {
                        window.location.reload();
                    }, 3000);

                }
            }
        });
    });

});
</script>