<?php include("include/config.php");?>
<?php include('PHPMailer/PHPMailer/PHPMailerAutoload.php');?>
<?php define("title","Collaborator Registration");?>
<?php include("include/head.php");?>
<?php include("include/header.php");?>

<?php
   // $edit_status=0;
    if(isset($_REQUEST['eid']) && !empty($_REQUEST['eid'])) {
	
       // $edit_status=1;
        $getdata = mysqli_query($conn,"SELECT full_name,username,role_id,password FROM `user_list` WHERE id='".$_REQUEST['eid']."' AND del_action='N'");
		$data = mysqli_fetch_array($getdata);
    }
    else {
        $query=mysqli_query($conn,"SELECT role_id FROM user_list ORDER BY role_id DESC LIMIT 0,1 ");
        $code_data = mysqli_fetch_array($query);
        $code = $code_data['agent_id']+101;
        $tp_code= "EMP_".$code;
    }

    if(isset($_POST['submit_data'])) {
        
        $create_message=array();
        $full_name=$_POST['full_name'];
        $user_name=$_POST['username'];
        $role_name=$_POST['role_id'];
        $password=$_POST['password'];
        $pass=md5($password);

        // $insert=mysqli_query($conn,"INSERT INTO agent_list SET agent_name='".$agent_name."',  agent_mobile='".$agent_mobile."', agent_email='".$agent_email."',organization_name='".$organization_name."', agent_type='".$agent_type."', country_id='".$country_id."', state_id='".$state_id."' ");
        // $id = mysqli_insert_id($conn );

        $instuser = mysqli_query($conn,"INSERT INTO user_list SET full_name='".$full_name."', username='".$user_name."', role_id='".$role_name."',password='".$pass."', is_active='1'");
        $user_id = mysqli_insert_id($conn );
            
        if($instuser){
            $create_message=array("status"=>1,"message"=>"User Added Successfully !!");
            echo "<script>window.location.href='user_management_list.php'</script>";
        }else{
            $create_message=array("status"=>0,"message"=>"Something Went Wrong !!");
        }
        return $create_message;
    }

    if(isset($_POST['update_data'])) {

        $create_message=array();
        $full_name=$_POST['full_name'];
        $user_name=$_POST['username'];
        $role_name=$_POST['role_id'];
        $password=$_POST['password'];
        
        $update=mysqli_query($conn,"UPDATE user_list SET full_name='".$full_name."',password='".$password."', username='".$user_name."', role_id='".$role_name."' WHERE id='".$_REQUEST['eid']."' AND del_action ='N'");
        $id = mysqli_insert_id($conn );  

        if($update){
            $create_message=array("status"=>1,"message"=>"User Updated Successfully !!");
            echo "<script>window.location.href='user_management_list.php'</script>";
        }else{
            $create_message=array("status"=>0,"message"=>"Something Went Wrong !!");
        }
        return $create_message;
    }
?>

<div class="content-page">
    <div class="content">
        <div class="page-title-box">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="d-flex d-title-box-block align-items-center justify-content-between">
                            <?php if($edit_status==1){?>
                                <h4 class="page-title">Update User</h4>
                                <?php } else { ?>
                                <h4 class="page-title">Add User</h4>
                                <?php } ?>
                                <div class="page-title-right">
                                <ol class="breadcrumb m-0">
                                    <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                                    <?php if($edit_status==1){?>
                                    <li class="breadcrumb-item active">Update User</li>
                                    <?php } else { ?>
                                    <li class="breadcrumb-item active">Add User</li>
                                    <?php } ?>
                                </ol>
                            </div>
                         </div>
                     </div>
                 </div>
             </div>
        </div>
        
        <div class="container-fluid">
            <div class="row">
                <div class="col-xl-12">
                    <div class="card-box form-boxi">
                        <fieldset>
                            <legend class="px-2">User Details</legend>
                            <div class="profile-inner-bg mt-0 mb-3">
                                <form action="" method="post">
                                    <div class="row">
                                        <div class="col-lg-6">
                                            <div class="form-group">
                                                <label class="col-form-label">Full Name</label>
                                                <span>*</span>
                                                <input type="text" value="<?=$data['full_name']?>" class="form-control" required="" placeholder="Enter your Full Name" name="full_name">
                                            </div>
                                        </div>
                                        <div class="col-lg-6">
                                            <div class="form-group">
                                                <label class="col-form-label">User Name</label>
                                                <span>*</span>
                                                <input type="email" value="<?=$data['username']?>" class="form-control" required="" placeholder="Enter your Email Id" name="username">
                                            </div>
                                        </div>
										<div class="col-lg-6">
                                            <div class="form-group">
                                            <label class="col-form-label">Select Role</label>
                                            <span>*</span>
                                                <select class="form-control select2" name="role_id" id="role_id">
                                                    <option>Select Role</option>
                                                    <?php
                                                        $getrole = mysqli_query($conn,"SELECT id, role_name FROM role_master WHERE status='1' AND id > '8' ORDER BY role_name ASC");
                                                        while($role = mysqli_fetch_array($getrole))
                                                        {?>
                                                            <option value="<?=$role['id']?>" <?php if($role['id']==$data['role_id']) {echo "selected";} ?> ><?=$role['role_name']?></option>
                                                        <?php    
                                                        }
                                                    ?>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-lg-6">
                                            <div class="form-group">
                                                <label class="col-form-label">Password</label>
                                                <span>*</span>
                                                <input type="password" value="<?=$data['password']?>" class="form-control" required="" placeholder="Enter Password" name="password">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-12 text-right">
                                        <div class="form-group">
                                            <?php
                                            if($_REQUEST['eid']!='') {
                                            ?>
                                                <input type="submit" class="btn btn-primary btn-rounded w-md waves-effect waves-light" name="update_data" value="Update">
                                            <?php
                                            }
                                            else {
                                            ?>
                                                <input type="submit" class="btn btn-primary btn-rounded w-md waves-effect waves-light" name="submit_data" value="Submit">
                                            <?php
                                            }
                                            ?>
                                        </div>
                                    </div>
                                </form>
                            </div>   
                        </fieldset>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php include("include/footer.php");?>
<?php include("include/js.php");?>
<script>
    function getState(cId)
    {
        $.ajax({
            url:"ajax/get_district.php",
            type:"POST",
            data:"country_id="+cId,
            success:function(data){
                $('#state_id').html(data);
            }
        });
    }
</script>

<script>
    $('.mobile-no').mask('0000000000');
</script>
