<?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 Documents";
$record_type = "Documents";
$script_name = "manage-documents.php";
$redirect_script_name = "manage-documents.php";
$table_name = "documents_tbl";
####################################################

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



if (isset($recordID) && !empty($recordID)) {
    $ebussinesstype = ($euser_type == 'Organization')?$ebussinesstype:'';
    updateTable($table_name, "user_type='$euser_type',org_type='$ebussinesstype', documents='$edocuments' where document_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 document_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("");
$qry = db_query("select * from ".$table_name);
?>
<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 Document</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>User type</th>
                                        <th>Organization Type</th>
                                        <th>Documents</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sl = $start + 1;
                                    while ($arr = mysqli_fetch_array($qry)) { ?>
                                    <tr>
                                        <td><input type="checkbox" name="arr_id[]" value="<?= $arr['document_id'] ?>"></td>
                                        <td><?= $sl ?></td>
                                        <td><?= $arr['user_type'] ?></td>
                                        <td><?= $arr['org_type'] ?></td>
                                        <td><?= $arr['documents'] ?> </td>
                                        <td>
                                            <a href="#" class="edtRcd" data-toggle="modal" data-target="#updateBox" data-id="<?= $arr['document_id'] ?>" data-user="<?= $arr['user_type'] ?>" data-org="<?= $arr['org_type'] ?>" data-docs="<?= $arr['documents'] ?>"><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 Document</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-6">
                            <div class="form-group">
                                <label>User type</label>
                                <select name="user_type" class="form-control">
                                    <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</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-12">
                            <div class="form-group">
                                <label>Required Documents <small class="text-danger">(Must be seperate by 'comma')</small></label>
                                <textarea class="form-control form-control-sm" type="text" name="documents" placeholder="Required Documents" required></textarea>
                            </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 Price/Variants</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-6">
                            <div class="form-group">
                                <label>User type</label>
                                <select name="euser_type" class="form-control">
                                    <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</label>
                                <select class="form-control" name="ebussinesstype" 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-12">
                            <div class="form-group">
                                <label>Required Documents <small class="text-danger">(Must be seperate by 'comma')</small></label>
                                <textarea class="form-control form-control-sm" type="text" name="edocuments" placeholder="Required Documents" required></textarea>
                            </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'));
            $('textarea[name=edocuments]').val($(this).data('docs'));
        });
    });
</script>