<?php
    ob_start();
    include_once("../include/main.inc.php");
    check_session('MyCpanel', 'index.php');
?>

<?php

@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;
$referurl = $_SERVER['HTTP_REFERER'];
$heading = "Manage eKYC Link";
$record_type = "Documents";
$script_name = "manage-ekyclink.php";
$redirect_script_name = "manage-ekyclink.php";
$table_name = "ekyclink_tbl";
####################################################

if ($id == "add_record") {
    $bussinesstype = ($user_type == 'Organization')?$bussinesstype:'';
    $sql = db_query("insert into $table_name set catID='$catid', brand='$brand', validity='$validity', user_type='$user_type',org_type='$bussinesstype', ekyc_link='$link', status=1");
    $succee_msg = 'New ' . $record_type . ' has been added successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
}


if (isset($recordID) && !empty($recordID)) {
    $ebussinesstype = ($euser_type == 'Organization')?$ebussinesstype:'';
    updateTable($table_name, "catID='$ecatid', brand='$ebrand', validity='$evalidity', user_type='$euser_type',org_type='$ebussinesstype', ekyc_link='$elink' where link_id='$recordID'");

    $_SESSION['session_message'] = $record_type . ' has been updated successfully';

    header("Location:" . $referurl);

    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&category_id=" . $category_id . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;

}



if ($id == "delete_record") {
    if (!empty($_POST['arr_id'])) {
        $catid = $_POST['arr_id'];
        foreach ($catid as $id) {
            mysqli_query($dbconn, "delete from $table_name WHERE link_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 . "?start=" . $start . "&category_id=" . $category_id . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;

}

admin_header("");

$cat = mysqli_query($dbconn, "SELECT catID, displayName FROM category_tbl WHERE parentID=1 AND `status`='Y'");
$category = $brand = [];
while($ct = mysqli_fetch_array($cat)){
    $category[$ct['catID']] = $ct['displayName'];
}

$brands = mysqli_query($dbconn, "SELECT slno, brandName FROM brand_tbl WHERE `status`='Y'");
while($brd = mysqli_fetch_array($brands)){
    $brand[$brd['slno']] = $brd['brandName'];
}
?>
<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 link</span>
                    </a>
                </li>
            </ol>
        </nav>
        
    </div>

    <div class="card">
        <div class="card-body">
            <div class="row">
                <div class="col-12">
                    <div class="table-responsive">
                        <form method="post" id="detailsID">
                            <table id="order-listing" class="table">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>S.No.</th>
                                        <th>Product</th>
                                        <th>User type</th>
                                        <th>Brand</th>
                                        <th>Validity</th>
                                        <th>Link</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sl = $start + 1;
                                    $qry = mysqli_query($dbconn, "SELECT e.*, displayName FROM ekyclink_tbl AS e LEFT JOIN category_tbl AS c on c.catID=e.catID ORDER BY link_id DESC ");
                                    while ($arr = mysqli_fetch_array($qry)) { ?>
                                    <tr>
                                        <td><input type="checkbox" name="arr_id[]" value="<?= $arr['link_id'] ?>"></td>
                                        <td><?= $sl ?></td>
                                        <td><?= $arr['displayName'] ?></td>
                                        <td><?= $arr['user_type'] ?> <?= ($arr['user_type'] == 'Organization')?'('.$arr['org_type'].')':''; ?></td>
                                        <td><?= $arr['brand'] ?> </td>
                                        <td><?= $arr['validity'] ?> Year</td>
                                        <td><?= $arr['ekyc_link'] ?></td>
                                        <td>
                                            <a href="#" class="edtRcd" data-toggle="modal" data-target="#updateBox" data-id="<?= $arr['link_id'] ?>" data-user="<?= $arr['user_type'] ?>" data-org="<?= $arr['org_type'] ?>" data-brand="<?= $arr['brand'] ?>" data-validity="<?= $arr['validity'] ?>" data-catid="<?= $arr['catID'] ?>" data-link="<?= $arr['ekyc_link'] ?>"><i class="icon-sm fa fa-edit mr-2"></i></a>
                                        </td>
                                    </tr>
                                    <?php $sl++; } ?>
                                </tbody>
                            </table>
                            <div class="btn-grp">
                                <button type="submit" class="btn btn-danger btn-icon-text btn-sm" onclick="select_chk();">
                                    <i class="fas fa-trash-alt mr-1"></i>Delete
                                </button>
                                <input type="hidden" name="id" value="delete_record">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" 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 eKYC Link</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body p-2">
                <form class="forms-sample" method="POST" action="">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>Product<sup class="text-danger">*</sup></label>
                                <select name="catid" class="form-control" required>
                                    <option value="">Select Product</option>
                                    <?php                                        
                                        foreach($category as $cid => $cname){
                                            echo '<option value="'.$cid.'">'.$cname.'</option>';
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>User type<sup class="text-danger">*</sup></label>
                                <select name="user_type" class="form-control" required>
                                    <option value="Individuals">Individuals</option>
                                    <option value="Organization">Organization</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group" style="display:none;" id="orgType">
                                <label>Organization Type<sup class="text-danger">*</sup></label>
                                <select class="form-control" name="bussinesstype" id="type">
                                    <option value="Proprietorship">Proprietorship </option>
                                    <option value="Partnership">Partnership</option>
                                    <option value="LLP">LLP</option>
                                    <option value="PrivateLimited">Private Limited</option>
                                    <option value="PublicLimited">Public Limited</option>
                                    <option value="NGO / Trust">NGO / Trust</option>
                                    <option value="HUF">HUF</option>
                                    <option value="Government / PSU">Government / PSU </option>
                                    <option value="Bank">Bank</option>
                                    <option value="AOP">AOP</option>
                                    <option value="BOI">BOI</option>
                                    <option value="AJP">AJP</option>
                                    <option value="LocalAuthority">Local Authority</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Brand<sup class="text-danger">*</sup></label>
                                <select class="form-control" name="brand" required>
                                    <option value="">Select Brand </option>
                                    <?php
                                        foreach($brand as $bid => $bname){
                                            echo '<option value="'.$bname.'">'.$bname.'</option>';
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Validity<sup class="text-danger">*</sup></label>
                                <select class="form-control" name="validity" required>
                                    <option value="">Select Validity</option>
                                    <option value="1">1 Year</option>
                                    <option value="2">2 Year</option>
                                    <option value="3">3 Year</option>
                                    <option value="4">4 Year</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>eKYC Link<sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="link" placeholder="eKYC Link" required>
                            </div>
                        </div>
                    </div>
                
                    <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                    <input type="hidden" name="id" value="add_record">
                    <button class="btn btn-light" data-dismiss="modal" type="button">Cancel</button>
                </form>
            </div>                
        </div>
    </div>
</div>

<div class="modal fade" id="updateBox" 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 eKYC Link</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body p-2">
            <form class="forms-sample" method="POST" action="">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>Product<sup class="text-danger">*</sup></label>
                                <select name="ecatid" class="form-control" required>
                                    <option value="">Select Product</option>
                                    <?php                                        
                                        foreach($category as $cid => $cname){
                                            echo '<option value="'.$cid.'">'.$cname.'</option>';
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>User type<sup class="text-danger">*</sup></label>
                                <select name="euser_type" class="form-control" required>
                                    <option value="Individuals">Individuals</option>
                                    <option value="Organization">Organization</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group" style="display:none;" id="eorgType">
                                <label>Organization Type<sup class="text-danger">*</sup></label>
                                <select class="form-control" name="ebussinesstype">
                                    <option value="Proprietorship">Proprietorship </option>
                                    <option value="Partnership">Partnership</option>
                                    <option value="LLP">LLP</option>
                                    <option value="PrivateLimited">Private Limited</option>
                                    <option value="PublicLimited">Public Limited</option>
                                    <option value="NGO / Trust">NGO / Trust</option>
                                    <option value="HUF">HUF</option>
                                    <option value="Government / PSU">Government / PSU </option>
                                    <option value="Bank">Bank</option>
                                    <option value="AOP">AOP</option>
                                    <option value="BOI">BOI</option>
                                    <option value="AJP">AJP</option>
                                    <option value="LocalAuthority">Local Authority</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Brand<sup class="text-danger">*</sup></label>
                                <select class="form-control" name="ebrand" required>
                                    <option value="">Select Brand </option>
                                    <?php
                                        foreach($brand as $bid => $bname){
                                            echo '<option value="'.$bname.'">'.$bname.'</option>';
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Validity<sup class="text-danger">*</sup></label>
                                <select class="form-control" name="evalidity" required>
                                    <option value="">Select Validity</option>
                                    <option value="1">1 Year</option>
                                    <option value="2">2 Year</option>
                                    <option value="3">3 Year</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>eKYC Link<sup class="text-danger">*</sup></label>
                                <input class="form-control form-control-sm" type="text" name="elink" placeholder="eKYC Link" required>
                            </div>
                        </div>
                    </div>
                
                    <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
                    <input type="hidden" name="recordID" value="">
                    <button class="btn btn-light" data-dismiss="modal" type="button">Cancel</button>
                </form>
            </div>                
        </div>
    </div>
</div>



<script language="javascript">
    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>
    $(document).ready(function(){
        $('select[name=user_type]').change(function(){
            if($(this).val() == 'Organization'){
                $('#orgType').show();
            }else{
                $('#orgType').hide();
            }
        });

        $('select[name=euser_type]').change(function(){
            if($(this).val() == 'Organization'){
                $('#eorgType').show();
            }else{
                $('#eorgType').hide();
            }
        });


        $('.edtRcd').click(function(){
            $('input[name=recordID]').val($(this).data('id'));
            $('select[name=euser_type]').val($(this).data('user'));
            if($(this).data('user') == 'Organization'){
                $('#eorgType').show();
            }else{
                $('#eorgType').hide();
            }
            $('select[name=ebussinesstype]').val($(this).data('org'));
            $('select[name=ecatid]').val($(this).data('catid'));
            $('select[name=evalidity]').val($(this).data('validity'));
            $('select[name=ebrand]').val($(this).data('brand'));
            $('input[name=elink]').val($(this).data('link'));
        });
    });
</script>