<?php 
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));
require "../inc/header.php";
Session::CheckSession(); 
$msg = Session::get('msg');
Session::set("msg", NULL); 
if(!empty($_POST)){
    $error_ = 0;
    if(empty($_POST['erp_sys_user_id'])){
        Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Please enter code !</div>');
    }else{
        $id = $_POST['user_id'];
        $cntA = $commonFunctions->getRowData('sys_erp_adm_user',' WHERE id NOT IN('.$id.') AND erp_sys_user_id = "'.$_POST['erp_sys_user_id'].'" ');
        if(@count($cntA) > 1){
           $error_ = 1;
           Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Customer code already exists !</div>');
        }
    }

    if(!empty($_POST['erp_sys_user_id']) and $error_ == 0){
        if($_POST['action'] == "add"){
            $column = $fileName = "";
            if($_FILES["erp_sys_user_logo"]){
                $target_dir = "../uploads/admin-setting/users/";
                $target_file = $target_dir . basename($_FILES["erp_sys_user_logo"]["name"]);
                $uploadOk = 1;
                $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                @getimagesize($_FILES["erp_sys_user_logo"]["tmp_name"]);
                if(move_uploaded_file($_FILES["erp_sys_user_logo"]["tmp_name"], $target_file)){
                    $column = " ,erp_sys_user_logo";
                    $fileName = " , '".$_FILES["erp_sys_user_logo"]["name"]."'";
                }
            }
            $rand = rand(0,999999999999999);
            $commonFunctions->addData('global_contacts'," (cont_random_num,global_user_name_first,global_user_name_middle,global_user_name_last,global_user_email_personal,global_user_mobile1,global_user_mobile2,global_user_mobile_work,global_org_name,global_user_job_title,global_org_phone_work,global_user_org_phone_work_ext,global_org_fax,global_user_email_work,global_user_id_sm_linkedin,global_user_id_sm_twitter,global_user_id_sm_facebook,global_user_id_sm_instagram,global_user_id_msngr_skype,global_user_id_msngr_whatsapp,global_user_id_msngr_imo,global_user_id_msngr_telegram,global_user_id_msngr_signal,global_user_id_msngr_botim,global_user_id_messanger_viber) VALUES ('".$rand."','".$_POST['erp_sys_user_name_first']."','".$_POST['erp_sys_user_name_middle']."','".$_POST['erp_sys_user_name_last']."','".$_POST['erp_sys_user_email_personal']."','".$_POST['erp_sys_user_mobile1']."','".$_POST['erp_sys_user_mobile2']."','".$_POST['erp_sys_user_mobile_work']."','".$_POST['erp_sys_org_name']."','".$_POST['erp_sys_user_job_title']."','".$_POST['erp_sys_org_phone_work']."','".$_POST['erp_sys_user_org_phone_work_ext']."','".$_POST['erp_sys_org_fax']."','".$_POST['erp_sys_user_email_work']."','".$_POST['erp_sys_user_id_sm_linkedin']."','".$_POST['erp_sys_user_id_sm_twitter']."','".$_POST['erp_sys_user_id_sm_facebook']."','".$_POST['erp_sys_user_id_sm_instagram']."','".$_POST['erp_sys_user_id_msngr_skype']."','".$_POST['erp_sys_user_id_msngr_whatsapp']."','".$_POST['erp_sys_user_id_msngr_imo']."','".$_POST['erp_sys_user_id_msngr_telegram']."','".$_POST['erp_sys_user_id_msngr_signal']."','".$_POST['erp_sys_user_id_msngr_botim']."','".$_POST['erp_sys_user_id_messanger_viber']."' ) ",'');

            $commonFunctions->addData('sys_erp_adm_user'," (erp_sys_user_name_first,erp_sys_user_name_middle,erp_sys_user_name_last,erp_sys_user_id,erp_sys_user_email_personal,erp_sys_user_mobile1,erp_sys_user_mobile2,erp_sys_user_mobile_work,erp_sys_org_name,erp_sys_user_job_title,erp_sys_org_phone_work,erp_sys_user_org_phone_work_ext,erp_sys_org_fax,erp_sys_user_email_work,erp_sys_user_id_sm_linkedin,erp_sys_user_id_sm_twitter,erp_sys_user_id_sm_facebook,erp_sys_user_id_sm_instagram,erp_sys_user_id_msngr_skype,erp_sys_user_id_msngr_whatsapp,erp_sys_user_id_msngr_imo,erp_sys_user_id_msngr_telegram,erp_sys_user_id_msngr_signal,erp_sys_user_id_msngr_botim,erp_sys_user_id_messanger_viber,erp_sys_user_secutiry_permission,erp_sys_user_roleid,erp_sys_user_isActive".$column.") VALUES ('".$_POST['erp_sys_user_name_first']."','".$_POST['erp_sys_user_name_middle']."','".$_POST['erp_sys_user_name_last']."','".$_POST['erp_sys_user_id']."','".$_POST['erp_sys_user_email_personal']."','".$_POST['erp_sys_user_mobile1']."','".$_POST['erp_sys_user_mobile2']."','".$_POST['erp_sys_user_mobile_work']."','".$_POST['erp_sys_org_name']."','".$_POST['erp_sys_user_job_title']."','".$_POST['erp_sys_org_phone_work']."','".$_POST['erp_sys_user_org_phone_work_ext']."','".$_POST['erp_sys_org_fax']."','".$_POST['erp_sys_user_email_work']."','".$_POST['erp_sys_user_id_sm_linkedin']."','".$_POST['erp_sys_user_id_sm_twitter']."','".$_POST['erp_sys_user_id_sm_facebook']."','".$_POST['erp_sys_user_id_sm_instagram']."','".$_POST['erp_sys_user_id_msngr_skype']."','".$_POST['erp_sys_user_id_msngr_whatsapp']."','".$_POST['erp_sys_user_id_msngr_imo']."','".$_POST['erp_sys_user_id_msngr_telegram']."','".$_POST['erp_sys_user_id_msngr_signal']."','".$_POST['erp_sys_user_id_msngr_botim']."','".$_POST['erp_sys_user_id_messanger_viber']."','".$_POST['erp_sys_user_secutiry_permission']."','".$_POST['erp_sys_user_roleid']."','".$_POST['erp_sys_user_isActive']."'".$fileName." ) ",'users.php?state=form');
        }
        if($_POST['action'] == "edit"){
            $column = $fileName = "";
            if($_FILES["erp_sys_user_logo"]){
                $target_dir = "../uploads/admin-setting/users/";
                $target_file = $target_dir . basename($_FILES["erp_sys_user_logo"]["name"]);
                $uploadOk = 1;
                $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
                @getimagesize($_FILES["erp_sys_user_logo"]["tmp_name"]);
                if(move_uploaded_file($_FILES["erp_sys_user_logo"]["tmp_name"], $target_file)){
                    $column = " ,erp_sys_user_logo";
                    $fileName = " ,erp_sys_user_logo = '".$_FILES["erp_sys_user_logo"]["name"]."' ";
                }
            }
            $commonFunctions->updateData('global_contacts'," WHERE id = '".$_POST['user_id']."' ", " global_user_name_first = '".$_POST['erp_sys_user_name_first']."', global_user_name_middle='".$_POST['erp_sys_user_name_middle']."', global_user_name_last='".$_POST['erp_sys_user_name_last']."' , global_user_email_personal='".$_POST['erp_sys_user_email_personal']."' , global_user_mobile1='".$_POST['erp_sys_user_mobile1']."', global_user_mobile2='".$_POST['erp_sys_user_mobile2']."', global_user_mobile_work='".$_POST['erp_sys_user_mobile_work']."' , global_org_name='".$_POST['erp_sys_org_name']."' , global_user_job_title='".$_POST['erp_sys_user_job_title']."', global_org_phone_work='".$_POST['erp_sys_org_phone_work']."', global_user_org_phone_work_ext='".$_POST['erp_sys_user_org_phone_work_ext']."', global_org_fax='".$_POST['erp_sys_org_fax']."', global_user_email_work='".$_POST['erp_sys_user_email_work']."', global_user_id_sm_linkedin='".$_POST['erp_sys_user_id_sm_linkedin']."', global_user_id_sm_twitter='".$_POST['erp_sys_user_id_sm_twitter']."', global_user_id_sm_facebook='".$_POST['erp_sys_user_id_sm_facebook']."', global_user_id_sm_instagram='".$_POST['erp_sys_user_id_sm_instagram']."', global_user_id_msngr_skype='".$_POST['erp_sys_user_id_msngr_skype']."', global_user_id_msngr_whatsapp='".$_POST['erp_sys_user_id_msngr_whatsapp']."', global_user_id_msngr_imo='".$_POST['erp_sys_user_id_msngr_imo']."', global_user_id_msngr_telegram='".$_POST['erp_sys_user_id_msngr_telegram']."', global_user_id_msngr_signal='".$_POST['erp_sys_user_id_msngr_signal']."', global_user_id_msngr_botim='".$_POST['erp_sys_user_id_msngr_botim']."', global_user_id_messanger_viber='".$_POST['erp_sys_user_id_messanger_viber']."'",'');

            $commonFunctions->updateData('sys_erp_adm_user'," WHERE id = '".$_POST['user_id']."' ", " erp_sys_user_name_first = '".$_POST['erp_sys_user_name_first']."', erp_sys_user_name_middle='".$_POST['erp_sys_user_name_middle']."', erp_sys_user_name_last='".$_POST['erp_sys_user_name_last']."' , erp_sys_user_id='".$_POST['erp_sys_user_id']."' , erp_sys_user_email_personal='".$_POST['erp_sys_user_email_personal']."' , erp_sys_user_mobile1='".$_POST['erp_sys_user_mobile1']."', erp_sys_user_mobile2='".$_POST['erp_sys_user_mobile2']."', erp_sys_user_mobile_work='".$_POST['erp_sys_user_mobile_work']."' , erp_sys_org_name='".$_POST['erp_sys_org_name']."' , erp_sys_user_job_title='".$_POST['erp_sys_user_job_title']."', erp_sys_org_phone_work='".$_POST['erp_sys_org_phone_work']."', erp_sys_user_org_phone_work_ext='".$_POST['erp_sys_user_org_phone_work_ext']."', erp_sys_org_fax='".$_POST['erp_sys_org_fax']."', erp_sys_user_email_work='".$_POST['erp_sys_user_email_work']."', erp_sys_user_id_sm_linkedin='".$_POST['erp_sys_user_id_sm_linkedin']."', erp_sys_user_id_sm_twitter='".$_POST['erp_sys_user_id_sm_twitter']."', erp_sys_user_id_sm_facebook='".$_POST['erp_sys_user_id_sm_facebook']."', erp_sys_user_id_sm_instagram='".$_POST['erp_sys_user_id_sm_instagram']."', erp_sys_user_id_msngr_skype='".$_POST['erp_sys_user_id_msngr_skype']."', erp_sys_user_id_msngr_whatsapp='".$_POST['erp_sys_user_id_msngr_whatsapp']."', erp_sys_user_id_msngr_imo='".$_POST['erp_sys_user_id_msngr_imo']."', erp_sys_user_id_msngr_telegram='".$_POST['erp_sys_user_id_msngr_telegram']."', erp_sys_user_id_msngr_signal='".$_POST['erp_sys_user_id_msngr_signal']."', erp_sys_user_id_msngr_botim='".$_POST['erp_sys_user_id_msngr_botim']."', erp_sys_user_id_messanger_viber='".$_POST['erp_sys_user_id_messanger_viber']."', erp_sys_user_secutiry_permission='".$_POST['erp_sys_user_secutiry_permission']."', erp_sys_user_roleid='".$_POST['erp_sys_user_roleid']."', erp_sys_user_isActive='".$_POST['erp_sys_user_isActive']."'".$fileName,'users.php?state=list');
        }
    }else{
        echo "<script>location.href='users.php?state=form';</script>";
    }
}
?>    
    

<!-- Page-Title -->
<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box">
            <div class="row">
                <div class="col">
                    <h4 class="page-title">Users</h4>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Settings</a></li>
                        <li class="breadcrumb-item"><a href="javascript:void(0);">User Management</a></li>
                        <li class="breadcrumb-item"><a href="javascript:void(0);">Users<?php echo (!empty($_GET['state']) and $_GET['state'] == "form")?" Form ":" List "; ?></a></li>
                    </ol>
                </div><!--end col-->
                <?php if($_GET['state'] != "list"){ ?>
                    <a href="users.php?state=list"><button type="button" class="btn btn-sm btn-outline-secondary clear_form">Back to Users List</button></a> 
                <?php } ?>
            </div><!--end row-->                                                              
        </div><!--end page-title-box-->
    </div><!--end col-->
</div><!--end row-->
<!-- end page title end breadcrumb -->
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title">
                    <div class="form-row">
                        <div class="form-group searchForm col-md-6">
                            <a class="btn btn-soft-primary" href="users.php?state=form" role="button" style="float: left;"><i class="fas fa-plus mr-2"></i> Add New User</a>
                        </div>    
                        <div class="form-group searchForm col-md-6">
                            <?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): ?>
                            <form action="" id="searchForm" method="GET">
                                <div class="form-row">
                                    <div class="form-group searchForm col-md-8">
                                        <input type="text" name="search" id="search" placeholder="Search" class="form-control" value="<?php echo @$_GET['search']; ?>">
                                        <input type="hidden" name="state" value="<?php echo @$_GET['state']; ?>">
                                        <input type="hidden" name="page" value="<?php echo @$_GET['page']; ?>">
                                    </div>
                                    <div class="form-group searchForm col-md-4">
                                        <button type="submit" id="searchbtn" class="btn btn-primary"> <i class="fa fa-search"></i> Search</button>
                                        <button type="button" id="clearbtn" class="btn btn-danger"> <i class="fa fa-trash"></i> Clear</button>
                                    </div>
                                </div>
                            </form>
                            <?php endif; ?>
                        </div>
                    </div>
                </h4>
                <?php echo !empty($msg)?$msg:''; ?>        
                <?php echo !empty($msg1)?$msg1:''; ?>        
            </div>
    
            <div class="card-body">
                <div class="row">
                    <div class="col-md-12">
                        <div class="main-card mb-3 ">
                            <!-- <div class="card-body"> -->
                                <?php if(!empty($_GET['state']) and $_GET['state'] != "list"):
                                    if(!empty($_GET['user_id'])){
                                        $editData = $commonFunctions->getRowData('sys_erp_adm_user'," WHERE id = '".$_GET['user_id']."' ");
                                    }
                                    ?>
                                    <form enctype="multipart/form-data" action="users.php?state=form&action=add" method="post" name="formsoc" autocomplete="off">      
                                        <div class="form-row">
                                            <input type="hidden" name="action" value="<?php echo !empty($_GET['user_id'])?'edit':'add';?>">
                                            <input type="hidden" name="user_id" value="<?php echo @$_GET['user_id'];?>">
                                            <input type="hidden" name="erp_sys_user_isActive" value="1">
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_name_first" class="">First Name</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_name_first" id="erp_sys_user_name_first" value="<?php echo @$editData['erp_sys_user_name_first'];?>" autofocus="autofocus" class="form-control" placeholder="First Name">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_name_middle" class="">Middle Name</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_name_middle" id="erp_sys_user_name_middle" value="<?php echo @$editData['erp_sys_user_name_middle'];?>" autofocus="autofocus" class="form-control" placeholder="Middle Name">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_name_last" class="">Last Name</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_name_last" id="erp_sys_user_name_last" value="<?php echo @$editData['erp_sys_user_name_last'];?>" autofocus="autofocus" class="form-control" placeholder="Last Name">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_name" class="">Company Name</label>
                                                <select class="form-control" name="erp_sys_org_name">
                                                    <option value="0">Select</option>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_job_title" class="">Job Title</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_job_title" id="erp_sys_user_job_title" value="<?php echo @$editData['erp_sys_user_job_title'];?>" autofocus="autofocus" class="form-control" placeholder="Job Title">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_phone_work" class="">Work Phone</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_phone_work" id="erp_sys_org_phone_work" value="<?php echo @$editData['erp_sys_org_phone_work'];?>" autofocus="autofocus" class="form-control" placeholder="Work Phone">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_org_phone_work_ext" class="">Work Phone - Ext</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_org_phone_work_ext" id="erp_sys_user_org_phone_work_ext" value="<?php echo @$editData['erp_sys_user_org_phone_work_ext'];?>" autofocus="autofocus" class="form-control" placeholder="Work Phone - Ext">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_org_fax" class="">Work Fax</label>
                                                <input type="text" maxlength="128" name="erp_sys_org_fax" id="erp_sys_org_fax" value="<?php echo @$editData['erp_sys_org_fax'];?>" autofocus="autofocus" class="form-control" placeholder="Work Fax">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_mobile1" class="">Mobile 1</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_mobile1" id="erp_sys_user_mobile1" value="<?php echo @$editData['erp_sys_user_mobile1'];?>" autofocus="autofocus" class="form-control" placeholder="Mobile">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_mobile2" class="">Mobile 2</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_mobile2" id="erp_sys_user_mobile2" value="<?php echo @$editData['erp_sys_user_mobile2'];?>" autofocus="autofocus" class="form-control" placeholder="Mobile">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_email_work" class="">Work eMail</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_email_work" id="erp_sys_user_email_work" value="<?php echo @$editData['erp_sys_user_email_work'];?>" autofocus="autofocus" class="form-control" placeholder="Work eMail">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_mobile_work" class="">Work Mobile</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_mobile_work" id="erp_sys_user_mobile_work" value="<?php echo @$editData['erp_sys_user_mobile_work'];?>" autofocus="autofocus" class="form-control" placeholder="Work Mobile">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_email_personal" class="">Personal eMail</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_email_personal" id="erp_sys_user_email_personal" value="<?php echo @$editData['erp_sys_user_email_personal'];?>" autofocus="autofocus" class="form-control" placeholder="Personal eMail">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_sm_linkedin" class="">LinkedIn</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_sm_linkedin" id="erp_sys_user_id_sm_linkedin" value="<?php echo @$editData['erp_sys_user_id_sm_linkedin'];?>" autofocus="autofocus" class="form-control" placeholder="LinkedIn">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_sm_twitter" class="">Twitter</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_sm_twitter" id="erp_sys_user_id_sm_twitter" value="<?php echo @$editData['erp_sys_user_id_sm_twitter'];?>" autofocus="autofocus" class="form-control" placeholder="Twitter">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_sm_facebook" class="">Facebook</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_sm_facebook" id="erp_sys_user_id_sm_facebook" value="<?php echo @$editData['erp_sys_user_id_sm_facebook'];?>" autofocus="autofocus" class="form-control" placeholder="Facebook">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_sm_instagram" class="">Instagram</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_sm_instagram" id="erp_sys_user_id_sm_instagram" value="<?php echo @$editData['erp_sys_user_id_sm_instagram'];?>" autofocus="autofocus" class="form-control" placeholder="Instagram">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_msngr_skype" class="">Skype</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_msngr_skype" id="erp_sys_user_id_msngr_skype" value="<?php echo @$editData['erp_sys_user_id_msngr_skype'];?>" autofocus="autofocus" class="form-control" placeholder="Skype">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_msngr_whatsapp" class="">Whatsapp Number</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_msngr_whatsapp" id="erp_sys_user_id_msngr_whatsapp" value="<?php echo @$editData['erp_sys_user_id_msngr_whatsapp'];?>" autofocus="autofocus" class="form-control" placeholder="Whatsapp Number">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_msngr_imo" class="">IMO Number </label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_msngr_imo" id="erp_sys_user_id_msngr_imo" value="<?php echo @$editData['erp_sys_user_id_msngr_imo'];?>" autofocus="autofocus" class="form-control" placeholder="IMO Number">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_msngr_telegram" class="">Telegram Number </label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_msngr_telegram" id="erp_sys_user_id_msngr_telegram" value="<?php echo @$editData['erp_sys_user_id_msngr_telegram'];?>" autofocus="autofocus" class="form-control" placeholder="Telegram Number">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_msngr_signal" class="">Signal Number </label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_msngr_signal" id="erp_sys_user_id_msngr_signal" value="<?php echo @$editData['erp_sys_user_id_msngr_signal'];?>" autofocus="autofocus" class="form-control" placeholder="Signal Number">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_msngr_botim" class="">Botim Number</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_msngr_botim" id="erp_sys_user_id_msngr_botim" value="<?php echo @$editData['erp_sys_user_id_msngr_botim'];?>" autofocus="autofocus" class="form-control" placeholder="Botim Number">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id_messanger_viber" class="">Viber Number</label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id_messanger_viber" id="erp_sys_user_id_messanger_viber" value="<?php echo @$editData['erp_sys_user_id_messanger_viber'];?>" autofocus="autofocus" class="form-control" placeholder="Viber Number">
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_secutiry_permission" class="">Permission</label>
                                                <select name="erp_sys_user_secutiry_permission" class="form-control">
                                                    <option value="">Select</option>
                                                </select>
                                            </div>

                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_id" class="">UserName <span class="error">*</span></label>
                                                <input type="text" maxlength="128" name="erp_sys_user_id" id="erp_sys_user_id" value="<?php echo @$editData['erp_sys_user_id'];?>" autofocus="autofocus" class="form-control" placeholder="UserName" required="">
                                            </div>
                                            <!-- <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_secutiry_password" class="">Password <span class="error">*</span></label>
                                                <input type="password" maxlength="128" name="erp_sys_user_secutiry_password" id="erp_sys_user_secutiry_password" value="" autofocus="autofocus" class="form-control" placeholder="Password" required="">
                                            </div> -->
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_roleid">Select User Type <span class="error">*</span></label>
                                                <?php $optionData = $commonFunctions->getData('sys_erp_adm_user_roles',' WHERE isActive = 0 '); ?>
                                                <select class="form-control" name="erp_sys_user_roleid" id="erp_sys_user_roleid" required="">
                                                  <?php foreach ($optionData as $key) { ?>
                                                    <option value="<?php echo $key['id'];?>" <?php echo (!empty(@$editData['erp_sys_user_roleid']) and $key['id'] == $editData['erp_sys_user_roleid'])?"selected":""; ?> ><?php echo $key['role'];?></option>
                                                  <?php } ?>
                                                </select>
                                            </div>
                                            <div class="position-relative form-group col-md-4">
                                                <label for="erp_sys_user_logo" class=""> User Photo (Avatar) </label>
                                                <input type="file" name="erp_sys_user_logo" >
                                            </div>

                                            <div class="position-relative form-group col-md-12">
                                                <button type="submit" class="btn btn-success">Create</button>
                                                <a href="users.php?state=list"><button type="button" class="btn btn-danger">Back to Users List</button></a>
                                            </div>
                                        </div>
                                    </form>
                                <?php endif; ?>
                                <?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): 

                                    $targetpage = "users.php?state=list";

                                    $limit = 50;
                                    $total_pages = $commonFunctions->getData('sys_erp_adm_user','');
                                    $total_pages = count($total_pages);
                                    $stages = 3;
                                    $page = @$_GET['page'];
                                    if($page){
                                        $start = ($page - 1) * $limit; 
                                    }else{
                                        $start = 0; 
                                    } 
                                    
                                    $search_con = "";
                                    if(!empty($_GET['search'])){
                                        $countryArr = $commonFunctions->getIDs('sys_erp_adm_countries','rowid'," WHERE label LIKE '%".$_GET['search']."%' ");
                                        $sys_erp_adm_countries_statesArr = $commonFunctions->getIDs('sys_erp_adm_countries_states','rowid'," WHERE label LIKE '%".$_GET['search']."%' ");
                                        $country_ids = @$countryArr["ids"];
                                        $state_ids = @$sys_erp_adm_countries_statesArr["ids"];
                                        $search_con = " AND erp_sys_user_name_first LIKE '%".$_GET['search']."%' OR erp_sys_user_name_middle LIKE '%".$_GET['search']."%' OR erp_sys_user_name_last LIKE '%".$_GET['search']."%' OR erp_sys_user_email_personal LIKE '%".$_GET['search']."%' OR erp_sys_user_mobile1 LIKE '%".$_GET['search']."%' OR erp_sys_org_phone_work LIKE '%".$_GET['search']."%' OR erp_sys_user_email_work LIKE '%".$_GET['search']."%' OR erp_sys_org_name LIKE '%".$_GET['search']."%' ";
                                    }
                                    $where = " WHERE id ".$search_con." ORDER BY id DESC LIMIT $start, $limit ";
                                   
                                    // Initial page num setup
                                    if ($page == 0){ $page = 1; }
                                    $prev = $page - 1;  
                                    $next = $page + 1;  
                                    $lastpage = ceil($total_pages/$limit);  
                                    $LastPagem1 = $lastpage - 1;    
                                    $paginate = '';
                                    if($lastpage > 1)
                                    {       
                                        $paginate .= "<div class='paginate'>";
                                        // Previous

                                        if ($page > 1){
                                            $paginate.= "<a href='$targetpage&page=$prev'>Previous</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Previous</span>";   }
                                        // Pages    
                                        if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
                                        {   
                                            for ($counter = 1; $counter <= $lastpage; $counter++)
                                            {
                                                if ($counter == $page){
                                                    $paginate.= "<span class='current'>$counter</span>";
                                                }else{
                                                    $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                            }
                                        }
                                        elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
                                        {
                                            // Beginning only hide later pages
                                            if($page < 1 + ($stages * 2))   
                                            {
                                                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // Middle hide some front and some back
                                            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // End only hide early pages
                                            else
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}
                                                }
                                            }       
                                        }       
                                                // Next
                                        if ($page < $counter - 1){ 
                                            $paginate.= "<a href='$targetpage&page=$next'>Next</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Next</span>";
                                            }
                                        $paginate.= "</div>";   
                                    }
                                    $resultData = $commonFunctions->getData('sys_erp_adm_user',$where);
                                    ?>
                                    <div class="form-row1 table-rep-plugin">
                                        <div class="table-responsive1 mb-0" data-pattern="priority-columns">
                                            <table style="width: 100% !important;" class="table datatable_t table-hover table-striped table-bordered">
                                                <thead>
                                                    <tr>
                                                        <th  class="text-center">SL</th>
                                                        <th  class="text-center">Name</th>
                                                        <th  class="text-center">User Name</th>
                                                        <th  class="text-center">eMail Address</th>
                                                        <th  class="text-center">Mobile</th>
                                                        <th  class="text-center">Status</th>
                                                        <th  class="text-center">Last Login</th>
                                                        <th  class="text-center">User Type</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php $i = 1; foreach ($resultData as $rw) { ?>
                                                        <tr onclick="redirect_('users-card.php?user_id=<?php echo $rw['id'];?>')" >
                                                            <td><?php echo $i++; ?></td>
                                                            <td><?php echo $rw['erp_sys_user_name_first']." ".$rw['erp_sys_user_name_middle']." ".$rw['erp_sys_user_name_last'];?></td>
                                                            <td><?php echo $rw['erp_sys_user_id'];?></td>
                                                            <td><?php echo $rw['erp_sys_user_email_personal'];?></td>
                                                            <td><?php echo $rw['erp_sys_user_mobile1'];?></td>
                                                            <td>
                                                                <?php if ($rw['erp_sys_user_isActive'] == '0') { ?>
                                                                    <span class="badge badge-lg badge-info text-white">Active</span>
                                                                <?php }else{ ?>
                                                                    <span class="badge badge-lg badge-danger text-white">Deactive</span>
                                                                <?php } ?>
                                                            </td>
                                                            <td><span class="badge badge-lg badge-secondary text-white"><?php echo $users->formatDate($rw['last_logged_date']); ?></span></td>
                                                            <td><?php echo "<span class='badge badge-lg badge-info text-white'>".$commonFunctions->getName($rw['erp_sys_user_roleid'],'sys_erp_adm_user_roles','role','id')."</span>"; ?>
                                                            </td>
                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    <?php echo "<center>".$paginate."</center>";?>
                                <?php endif; ?>
                            <!-- </div> -->
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div> <!-- end col -->
</div> <!-- end row -->

    
<?php require "../inc/footer.php"; ?>
<script type="text/javascript">
$("#clearbtn").click(function(){
    $("#search").val('');
    $("#searchForm").submit();
});
function redirect_(uri){
    window.location.href = uri;
}
</script>