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

$heading = "Manage Services";
$record_type = "Services";
$script_name = "manage-services.php";
$redirect_script_name = "manage-services.php";
$table_name = "manage_services_tbl";

####################################################

if ($action == "add_record") {
    $maxRes = mysqli_fetch_array(mysqli_query($dbconn, "select max(faqOrder) from `$table_name`"));
    $faqOrder = $maxRes[0] + 1;
    $sql = db_query("insert into $table_name set question='$questions',faqOrder='$faqOrder', answer='$answers', category_id='$category_id'");
    echo $sql;
    exit;
    // $_SESSION['session_message'] = 'New ' . $record_type . ' has been added successfully';
    // header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?category_id=" . $category_id);
    // exit;
}

if ($action == "update_record") {

    $update = updateTable($table_name, " question='$question', answer='$answer' where slno='$recordID'");
    echo $update;
    exit;
    // $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    // header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?category_id=" . $category_id);
    // 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 faqOrder='$val' where slno='$key1'");
        }
    }
    $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully  </div>';

    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?category_id=" . $category_id);
    exit;
}


if (isset($_POST['active'])) {
    if (!empty($_POST['arr_id'])) {
        $catid = $_POST['arr_id'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'Y' WHERE slno = '$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 . "?category_id=" . $category_id);
    exit;
}

if (isset($_POST['inactive'])) {
    if (!empty($_POST['arr_id'])) {
        $catid = $_POST['arr_id'];
        foreach ($catid as $id) {
            // echo "UPDATE $table_name SET status = 'N'  WHERE slno = '$id'";
            // exit;
            mysqli_query($dbconn, "UPDATE $table_name SET status = 'N'  WHERE slno = '$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 . "?category_id=" . $category_id);
    exit;
}


if (isset($_POST['delete'])) {
    if (!empty($_POST['arr_id'])) {
        $catid = $_POST['arr_id'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "DELETE FROM $table_name WHERE slno='$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 . "?category_id=" . $category_id);
    exit;
}

admin_header("");
//echo  "select * from $table_name where category_id='$category_id'";

$qry = db_query("select * from $table_name where category_id='$category_id' order by slno");
?>
<?php
$res = getResult($table_name, " where slno='$_GET[recordID]'");
?>
<div class="content-wrapper">

    <div class="page-header">
        <h3 class="page-title">
            <?= $heading; ?> | <?= get_cat_name_company($category_id) ?>
        </h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="nav-item d-none d-lg-flex">
                    <a class="nav-link" href="<?= SITE_ADMIN_URL; ?>/manage-case-study.php">
                        <span class="btn btn-primary">Go Back</span>
                    </a>
                </li>
            </ol>
        </nav>
    </div>
    <div class="card">
        <div class="card-body">

            <div class="page-header">
                <h3 class="page-title"><a class="addNewService" href="#" data-toggle="modal" data-target="#modalAdd">Add
                        New Service</a></h3>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb" id="breadcrumb">
                        <li class="breadcrumb-item"><a href="<? admin_url ?>welcome.php">Admin</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Add New Servie</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="">
                                <?php if ($category_id != null) { ?>
                                <div class="form-group" style="display:none;">
                                    <label>Category</label>
                                    <input class="typeahead" type="text" name=""
                                        value="<?= get_category_name($category_id) ?>" placeholder="Category Name">
                                </div>
                                <?php } ?>
                                <div class="form-group">
                                    <label>Service Name *</label>
                                    <input class="typeahead" type="text" name="question" value="<?= $res['question'] ?>"
                                        placeholder="Service Name" required>
                                </div>
                                <div class="form-group">
                                    <label>Button Name</label>
                                    <input class="typeahead" type="text" name="answer" value="<?= $res['answer'] ?>"
                                        placeholder="Button Name">
                                </div>
                                <?php
                                if ($_GET[recordID] != Null) {
                                ?>
                                <button type="submit" class="btn btn-primary mr-2" name="add_record">Update
                                    Service</button>
                                <input type="hidden" name="recordID" value="<?= $res['slno']; ?>">
                                <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="row">
                <div class="col-12">
                    <form method="Post" action="">
                        <div class="table-responsive">
                            <table id="order-listing" class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th>
                                            <div class="form-check mt-0">
                                                <label class="form-check-label">
                                                    <input type="checkbox" class="form-check-input" name="check_all"
                                                        id="check_all" value="1" onclick="checkall(this.form)">
                                                    <i class="input-helper"></i>
                                                </label>
                                            </div>
                                        </th>

                                        <?php if ($category_id != 0) {  ?>
                                        <!-- <th>Company Name</th><?php } ?> -->
                                        <th>Service Name</th>
                                        <th>Order
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sl = $start + 1;
                                    $counter = 1;
                                    while ($arr = mysqli_fetch_array($qry)) { ?>
                                    <tr>
                                        <td><input type="checkbox" name="arr_id[]" value="<?= $arr['slno'] ?>"></td>

                                        <!-- <?php if ($category_id != 0) {  ?>
		  					<td><?= get_cat_name_company($category_id) ?></td><?php } ?> -->
                                        <td>
                                            <p class="user-dcs"><span><?= $arr['question']; ?></span></p>

                                            <a
                                                href="<?= SITE_ADMIN_URL; ?>/manage-servicedata.php?slno=<?= $arr['slno']; ?>&frmData=<?= $sl; ?>&category_id=<?= $category_id; ?>">Manage
                                                Data <i class="icon-sm fa fa-edit mr-2"></i></a>

                                        </td>
                                        <td>
                                            <p class="rec-date-dcs">
                                                <input name="order_<?= $arr['slno']; ?>" class="form-control order"
                                                    type="text" size="2" value="<?= $arr['faqOrder']; ?>">
                                            </p>
                                        </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>
                                            <p class="download-dcs">
                                                <!-- <a href="<?= $script_name; ?>?category_id=<?= $category_id ?>&id=edit_record&recordID=<?= $arr['slno']; ?>" -->

                                                <a href="javascript:void();" class="editB" data-toggle="modal"
                                                    data-target="#moadlUpdate" data-id="<?= $arr['slno']; ?>"
                                                    data-catid="<?= $arr['category_id']; ?>"
                                                    data-question="<?= $arr['question']; ?>"
                                                    data-answer="<?= $arr['answer']; ?>">
                                                    <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>
                                            </p>
                                        </td>
                                    </tr>
                                    <?php $sl++;
                                        $counter++;
                                    } ?>

                                </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="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> -->
                                <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>

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


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

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

            <div class="modal-content">

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

                    <h5 class="modal-title">Add Service Heading</h5>
                    <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>

                </div>

                <form action="" id="myFormAdd" class="form_submit" 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>Service Name *<sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" value=""
                                    name="questions" placeholder="" required>
                            </div>

                            <div class="col-md-12 mb-2">
                                <label>Button Name *<sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" value=""
                                    name="answers" placeholder="" required>

                            </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="action" value="add_record">


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

                    </div>

                </form>

            </div>

        </div>
    </div>


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

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

            <div class="modal-content">

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

                    <h5 class="modal-title">Update Service Heading</h5>
                    <button type="button" class="btn-close" data-dismiss="modal" aria-label="Close"></button>

                </div>

                <form action="" id="myFormUpdate" class="form_submit" 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>Service Name *<sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" value="" name="question"
                                    placeholder="" required>
                            </div>

                            <div class="col-md-12 mb-2">
                                <label>Button Name *<sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" value="" name="answer"
                                    placeholder="" required>



                            </div>


                        </div>

                    </div>

                    <div class="modal-footer">



                        <button type="submit" class="btn btn-primary mr-2" name="update_record">Submit</button>
                        <input type="hidden" name="recordID" value="">
                        <input type="hidden" name="action" value="update_record">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

                    </div>

                </form>

            </div>

        </div>
    </div>




    <script language="javascript">
    checked = false;

    function checkall(frm1) {
        var aa = frm1;
        if (checked == false) {
            checked = true
        } else {
            checked = false
        }
        for (var i = 0; i < aa.elements.length; i++) {
            aa.elements[i].checked = checked;
        }
    }

    function select_chk() {
        var chks = document.getElementsByName('arr_id[]');
        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>
    var sll = 2;
    $('#add').click(function() {
        // alert(sll);

        $('#dynamicField').append('<div class="row" id="dataLoad' + sll +
            '"><div class="col-md-9"><div class="form-group"><input class="form-control form-control-sm" type="text" name="data[]" placeholder="Content Line ' +
            sll +
            '" required></div></div><div class="col-md-3"><div class="form-group"><button type="button" name="remove" id="' +
            sll + '" class="btn btn-danger btn_remove">X</button></div></div></div>'
        );
        sll++;
    });

    $(document).on('click', '.btn_remove', function() {
        var button_id = $(this).attr("id");
        $('#dataLoad' + button_id + '').remove();
    });
    </script>

    <script>
    $(document).ready(function() {

        //Add Modal
        $("#myFormAdd").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 Added Successfully</div>'
                        );
                        $('#myFormAdd')[0].reset();
                        $('#modalAdd').modal('hide');
                        setTimeout(function() {
                            window.location.reload();
                        }, 3000);
                    } else {

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

                    }
                }
            });
        });

  //Update Modal
        $('.editB').click(function() {

            $('input[name=recordID]').val($(this).data('id'));
            $('input[name=question]').val($(this).data('question'));
            $('input[name=answer]').val($(this).data('answer'));

            $("#myFormUpdate").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 Updated Successfully</div>'
                        );
                        $('#myFormUpdate')[0].reset();
                        $('#moadlUpdate').modal('hide');
                        setTimeout(function() {
                            window.location.reload();
                        }, 3000);
                    } else {

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

                    }
                }
            });
        });


        });


    });
    </script>

    <script>
    $(".uploadImage").on("change", function(e) {
        var ext = this.value.split('.').pop().toLowerCase();
        var el = $(this).closest('.fullUpload')
        if ($.inArray(ext, ['jpg', 'jpeg', 'png', 'png', 'psd', 'pdf']) == -1) {
            _el.find(".error_msg").text("Not an Image...");
        } else {
            el.find(".error_msg").text("");
            el.find(".processLine").addClass("file_uploading");
            setTimeout(function() {
                el.find(".processLine").addClass("file_uploaded");
            }, 3000);

            var uploadedFile = URL.createObjectURL(e.target.files[0]);
            setTimeout(function() {
                el.find(".fileUploadedView .imageHolder").html('<img src="' + uploadedFile + '">');
                el.find(".fileUploadedView").addClass("show");
                // $("#fileUploadedView").append('<img src="https://www.wpscrm.in/beta2023/assets/sitepanel_new/img/xlsx.png">').addClass("show");
            }, 2500);

        }

        $(".file_remove").on("click", function(e) {
            var el = $(this).closest('.fullUpload')
            el.find(".fileUploadedView").removeClass("show");
            el.find(".fileUploadedView").find("img").remove();
            el.find(".processLine").removeClass("file_uploading");
            el.find(".processLine").removeClass("file_uploaded");
            el.find('.uploadImage').val('');
        });
    });
    </script>

    <?php
    exit;
    ?>