<?php

include_once("../include/main.inc.php");

check_session('MyCpanel', 'index.php');

################ Paging Starts From Here ##################

$start = (intval($start) < 0 or $start == "") ? 0 : $start;

$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;



$heading = "Manage Admin/Agents";

$record_type = "Admin/Agents";

$script_name = "agents.php";

$table_name = "admin_details";



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 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."?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 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."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);

    exit;

}



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

admin_header("");

?>

<style>

    span.frm-error {

        color: red;

        font-size: 12px;

        float: right;

    }

</style>

<div class="content-wrapper">

    <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" href="#" data-toggle="modal" data-target="#addBox">

                        <span class="btn btn-primary">+ Add New Admin/Agent</span>

                    </a>

                </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>Name</th>

                                        <th>User Type</th>

                                        <th>login Detail</th>

                                        <th>Mobile</th>

                                        <th>Order</th>

                                        <th>Status</th>

                                        <th>Actions</th>

                                    </tr>

                                </thead>

                                <tbody>

                                    <?php

                                    $sl = $start + 1;

                                    $qry = mysqli_query($dbconn, "SELECT a.*, (SELECT count(order_id) FROM orders_tbl1 AS o WHERE o.agentId=a.slno AND o.order_status='ACTIVE' GROUP BY o.agentId) AS project FROM admin_details AS a ORDER BY a.slno desc");

                                    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['slno'] ?>"><i class="input-helper"></i>

                                                    </label>

                                                </div>

                                            </td>

                                            <td>

                                                <?= $sl ?>

                                            </td>

                                            <td><p class="ordern"><?= $arr['name']; ?></p></td>

                                            <td><p><?= strtoupper($arr['user_type']); ?></p></td>

                                            <td><p>User Id: <?= $arr['user_id']; ?>, Password: <?= $arr['pwd']; ?></p></td>

                                            <td><p class="productid-dcs"><?= $arr['mobile']; ?></p></td>

                                            <td><p class="productid-dcs"><?= isset($arr['project'])?'<a class="text-white bg-info p-1 rounded" href="'.SITE_ADMIN_URL.'/manage-orders.php?f_keyword=&f_payment=&f_order=ACTIVE&f_agent='.$arr['slno'].'">Assigned Order: '.$arr['project'].'</a>':'0'; ?></p></td>

                                            <td>

                                                <p class="status-dcs">

                                                    <?= ($arr['status'] == 'Y') ? '<label class="badge badge-success badge-pill">Active</label>' : '<label class="badge badge-danger badge-pill">In-Active</label>'; ?>

                                                </p>

                                            </td>

                                            <td>

                                                <p class="download-dcs">

                                                    <a class="edtBtn" href="#" data-toggle="modal" data-target="#edtPop" data-id="<?= $arr['slno']; ?>" data-name="<?= $arr['name']; ?>" data-mobile="<?= $arr['mobile']; ?>" data-email="<?= $arr['email']; ?>" data-type="<?= $arr['user_type']; ?>" data-user="<?= $arr['user_id']; ?>" data-password="<?= $arr['pwd']; ?>">

                                                        <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++;

                                    } ?>



                                </tbody>

                            </table>

                            <div class="btn-grp">

                                <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>

                                </div>

                            </div>

                    </form>

                </div>

            </div>

        </div>

    </div>

</div>



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

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

        <div class="modal-content">

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

                <h5 class="modal-title">Add New Admin/Agent</h5>

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

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

                </button>

            </div>

            <form action="form_ajax.php" class="form_submit" enctype="form-data/multipart">

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

                    <div class="form-group row">

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

                            <label>User Type<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <select class="typeahead" name="agent_type" required>

                                    <option value="">Select user type</option>

                                    <option value="admin">ADMIN</option>

                                    <option value="agent">AGENT</option>

                                </select>

                            </div>

                        </div>

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

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

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text" name="agent_name" placeholder="Enter Name" required>

                            </div>

                        </div>



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

                            <label>login User id<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text" name="agent_id" placeholder="Enter Login id" required>

                            </div>

                        </div>



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

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

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text" name="agent_password" placeholder="Enter Password" required>

                            </div>

                        </div>

                        <div class="col-md-6">

                            <label>User Mobile<sup class="text-danger">*</sup></label>

                            <div id="bloodhound">

                                <input class="form-control form-control-sm" type="tel" maxlength="10" name="agent_mobile" placeholder="Enter Mobile No" required>

                            </div>

                        </div>

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

                            <label>User Email<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="email" name="agent_email" placeholder="Enter Email" required>

                            </div>

                        </div>

                    </div>

                </div>

                <div class="modal-footer">

                    <input type="hidden" name="add_agent" value="add_agent">

                    <button type="submit" class="btn btn-primary">Save changes</button>

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

                </div>

            </form>

        </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">Update Agent</h5>

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

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

                </button>

            </div>

            <form action="form_ajax.php" class="form_submit" enctype="form-data/multipart">

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

                <div class="form-group row">

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

                            <label>User Type<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <select class="typeahead" name="eagent_type" required>

                                    <option value="">Select user type</option>

                                    <option value="admin">ADMIN</option>

                                    <option value="agent">AGENT</option>

                                </select>

                            </div>

                        </div>

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

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

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text" name="eagent_name" placeholder="Enter Name" required>

                            </div>

                        </div>



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

                            <label>login User id<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text" name="eagent_id" placeholder="Enter Login id" required>

                            </div>

                        </div>



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

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

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="text" name="eagent_password" placeholder="Enter Password" required>

                            </div>

                        </div>

                        <div class="col-md-6">

                            <label>User Mobile<sup class="text-danger">*</sup></label>

                            <div id="bloodhound">

                                <input class="form-control form-control-sm" type="tel" maxlength="10" name="eagent_mobile" placeholder="Enter Mobile No" required>

                            </div>

                        </div>

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

                            <label>User Email<sup class="text-danger">*</sup></label>

                            <div id="the-basics">

                                <input class="form-control form-control-sm" type="email" name="eagent_email" placeholder="Enter Email" required>

                            </div>

                        </div>

                    </div>

                </div>

                <div class="modal-footer">

                    <input type="hidden" name="edit_agent" value="">

                    <button type="submit" class="btn btn-primary">Save changes</button>

                    <button type="button" class="btn btn-secondary" 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(){

        $('.edtBtn').click(function(e){

            e.preventDefault();

            $('input[name=eagent_name]').val($(this).data('name'));

            $('select[name=eagent_type]').val($(this).data('type'));

            $('input[name=eagent_email]').val($(this).data('email'));

            $('input[name=eagent_id]').val($(this).data('user'));

            $('input[name=eagent_password]').val($(this).data('password'));

            $('input[name=eagent_mobile]').val($(this).data('mobile'));

            $('input[name=edit_agent]').val($(this).data('id'));

        });



        $('.form_submit').submit(function(e){

            e.preventDefault();

            let frm = $(this);

            let lnk = frm.attr('action');

            frm.find('input, textarea, select, button').prop('readonly', true);

            $('.frm-error').remove();

            $.ajax({

            url: lnk,

            data: new FormData(this),

            type:'post',

            dataType: 'JSON',

            processData: false,

            cache: false,

            contentType: false,

            success: function(r){

                if(r.success == true){

                if(r.alert){alert(alert());}

                if(r.msg){frm.before(r.msg);}

                if(r.rlink){window.location.href = r.rlink;}

                if(r.reload){location.reload();}

                }else{

                frm.find('input, textarea, select, button').prop('readonly', false);

                $.each(r.message, function(k,v){

                    $('input[name='+k+'],select[name='+k+'],textarea[name='+k+']').parent().before(v);

                });

                }

            }

            });

        });

    });

</script>