<?php include("_config/dbconfig.php");
include("_includes/head.php");

$full_name      = '';
$father_name    = '';
$email          = '';
$password       = '';
$contact        = '';
$mobile         = '';
$gender         = '';
$address        = '';
$cnic           = '';
$join_date      = '';
$agent_type     = '';


if(count($_POST)>0 && isset($_POST['add_form'])){

        $full_name      = $_POST['full_name'];
        $father_name    = $_POST['father_name'];
        $email          = $_POST['email'];
        $password       = ($_POST['password']);
        $contact        = $_POST['contact'];
        $mobile         = $_POST['mobile'];
        $gender         = $_POST['gender'];
        $address        = $_POST['address'];
        $cnic           = $_POST['cnic'];
        $join_date      = $_POST['join_date'];
        
        $agent_type      = $_POST['agent_type'];
         
        $check = $obj_agents_class->checkAgent($email);
        
        if($check==''){

            if(isset($_FILES['profile_img']) && !empty($_FILES['profile_img']['name'])){
                // settings
                $thumb_img = $_FILES['profile_img'];
                $max_file_size = 1024*3000; // 900kb 3mb
                $valid_exts = array('jpeg', 'jpg', 'png', 'gif');
                // thumbnail sizes
                $sizes = array(900 => 400);
                // Path
                $path = 'uploads/profile/';
                    // get file extension
                    $ext = strtolower(pathinfo($thumb_img['name'], PATHINFO_EXTENSION));

                    if (in_array($ext, $valid_exts)) {
                            /* resize image */
                            $uniqId  = "profile_".uniqid(5).".".$ext;
                            $fPath 	 = 'uploads/profile/'.$uniqId;
                            $filename = $uniqId;
                            $thumb_name = $uniqId;
                            //$res = $image->imageupload($thumb_img['tmp_name'], $uniqId, $path);
                            $aa = "uploads/profile/".$uniqId;
                            move_uploaded_file($thumb_img['tmp_name'], $aa);
                    } else {
                      $msg = 'Unsupported file';
                    }
                }
                
  
    $obj_agents_class->insert($full_name, $father_name, $email, $password, $contact,$mobile, $address, $gender,  $cnic, $join_date, $filename,$agent_type);
            echo "<script> window.location= 'agents.php'</script>";
           
        }else {
            $error = 'Email already exists!';
        }
}


if(count($_POST)>0 && isset($_POST['update_form'])){
        
        $full_name      = $_POST['full_name'];
        $father_name    = $_POST['father_name'];
        $email          = $_POST['email'];
        $password       = ($_POST['password']);
        $contact        = $_POST['contact'];
        $mobile         = $_POST['mobile'];
        $gender         = $_POST['gender'];
        $address        = $_POST['address'];
        $cnic           = $_POST['cnic'];
        $join_date      = $_POST['join_date'];
        $agent_id      = $_POST['agent_id'];
        $agent_type    = $_POST['agent_type'];
        
        
        if(isset($_FILES['profile_img']) && !empty($_FILES['profile_img']['name'])){
                // settings
                $thumb_img = $_FILES['profile_img'];
                $max_file_size = 1024*3000; // 900kb 3mb
                $valid_exts = array('jpeg', 'jpg', 'png', 'gif');
                // thumbnail sizes
                $sizes = array(900 => 400);
                // Path
                $path = 'uploads/profile/';
                    // get file extension
                    $ext = strtolower(pathinfo($thumb_img['name'], PATHINFO_EXTENSION));

                    if (in_array($ext, $valid_exts)) {
                            /* resize image */
                            $uniqId  = "profile_".uniqid(5).".".$ext;
                            $fPath 	 = 'uploads/profile/'.$uniqId;
                            $filename = $uniqId;
                            $thumb_name = $uniqId;
                            //$res = $image->imageupload($thumb_img['tmp_name'], $uniqId, $path);
                            $aa = "uploads/profile/".$uniqId;
                            move_uploaded_file($thumb_img['tmp_name'], $aa);
                    } else {
                        $msg = 'Unsupported file';
                    }
                }else {
                        $filename = $_POST['old_img'];
                }
        
        $obj_agents_class->update($agent_id, $full_name, $father_name, $password,  $contact,$mobile, $address, $gender,  $cnic, $join_date, $filename, $agent_type);
        echo "<script> window.location= 'agents.php'</script>";
        die();
}

 
    if(isset($_GET['edit']) && !empty($_GET['edit'])){
            $id_        = base64_decode($_GET['edit']);
            $agent      = $obj_agents_class->getAgent($id_);
            
            $full_name      = $agent['name'];
            $father_name    = $agent['father_name'];
            $email          = $agent['email'];
            $password       = ($agent['password']);
            $contact        = $agent['contact_no'];
            $mobile         = $agent['mobile_no'];
            $gender         = $agent['gender'];
            $address        = $agent['address'];
            $cnic           = $agent['cnic'];
            $join_date      = $agent['join_date'];
            
            $profile_img    = $agent['profile_img'];
            $agent_type    = $agent['agent_type'];
             
            /*echo "<pre>";
            print_r($agent);
            echo '</pre>';*/
            
            $page_title = "Edit Agent";
            echo '<title>Edit Agent</title>'; 
       }
       
    else if(isset($_GET['view']) && !empty($_GET['view'])){
            $id_        = base64_decode($_GET['view']);
            $agent      = $obj_agents_class->getAgent($id_);

            $full_name      = $agent['name'];
            $father_name    = $agent['father_name'];
            $email          = $agent['email'];
            $password       = ($agent['password']);
            $contact        = $agent['contact_no'];
            $mobile         = $agent['mobile_no'];
            $gender         = $agent['gender'];
            $address        = $agent['address'];
            $cnic           = $agent['cnic'];
            $join_date      = $agent['join_date'];
            $profile_img    = $agent['profile_img'];
            $agent_type    = $agent['agent_type'];
?>    
<?php             $page_title = "View Agent";
            echo '<title>View Agent</title>';
       }       
       
       else { 
           $page_title = "Add Agent";
           echo '<title>Add Agent </title>'; 
       }
       
       
       $UserRoles = $obj_user_class->getUserRole();
?>

<?php include("_includes/header.php"); ?>

<div class="col-sm-12 right_form">
        <div class="title">
            <div class="row">
                <div class="col-md-8 col-sm-12">
                    <h1><?php echo $page_title; ?></h1>
                </div>
            </div>
        </div>
        
        
           
         <ol class="breadcrumb white">
	          <li><a href="dashboard.php">Dashboard</a></li>
              <li><a href="agents.php">Agents</a></li>
          	  <li class="active">Add Agent</li>
          </ol>
        
        
</div>


<div class="col-sm-12 form_inner">
    <form class="col-sm-8 form-box" id="agent_form" method="post" action="" enctype="multipart/form-data">
                <div class="row">
					<div class="col-md-6 col-sm-6 form-group">
						<label for="FirstName">First Name</label>
                                                <input type="text" class="form-control required" name="full_name" value="<?= $full_name?>" >
					</div>
					<div class="col-md-6 col-sm-6 form-group">
						<label for="LastName">Last Name</label>
                                                <input type="text" class="form-control agent_form required" name="father_name" value="<?= $father_name?>" >
					</div>
                    <div class="clearfix"></div>
                    
                    
                    
                                    <?php if(isset($_GET['edit']) || isset($_GET['view']) ) {?> 

					<div class="col-md-6 col-sm-6 form-group">
						<label for=" ">Email</label>
                                                <input type="email" class="form-control required" name="email" readonly value="<?= $email ?>">
                                                <?php if(isset($error) && !empty($error)){?>
                                                <label id="full_name-error" class="error" for="full_name"><?= $error ?></label>
                                                <?php } ?>
					</div>
                                        
                                        <div class="col-md-6 col-sm-6 form-group">
						<label for="">Password</label>
                                                <input type="text" class="form-control required" name="password" value="<?= $password ?>"  >
					</div>                                        
                                        
                                        <?php }else {?>
                    
                                        <div class="col-md-6 col-sm-6 form-group">
						<label for="FatherName">Email</label>
                                                <input type="email" class="form-control required" name="email" value="<?= $email ?>">
                                                <?php if(isset($error) && !empty($error)){?>
                                                <label id="full_name-error" class="error" for="full_name"><?= $error ?></label>
                                                <?php } ?>
					</div>
                                        
                                        <div class="col-md-6 col-sm-6 form-group">
						<label for="FatherName">Password</label>
                                                <input type="text" class="form-control required" name="password" value="<?php echo $obj_user_class->generatePassword(); ?>" >
					</div>
                                        <?php } ?>
                    
                                        <div class="clearfix"></div>
                    
					<div class="col-md-4 col-sm-6 form-group">
						<label for="ContactNum">Contact Number</label>
                                                <input class="form-control digits  " type="text" maxlength="15" name="contact" value="<?= $contact?>">
					</div>
                    
                    
					<div class="col-md-4 col-sm-6 form-group">
						<label for="MobNum">Mobile Number</label>
                                                <input class="form-control required digits" type="text" maxlength="15" name="mobile" value="<?= $mobile ?>">
					</div>
                                        
                                        <div class="col-md-4 col-sm-6 form-group">
                                                <label for="Gender">Gender</label>
                                                
                                                <?php  if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
                                                <select class="form-control required" name="gender" >
                                                    <?php if($gender == "Male"){?>
                                                        <option value="Male" selected="selected" >Male</option>
                                                    <?php } else {?>
                                                        <option value="Female" selected="selected">Female</option>
                                                    <?php } ?>
                                                </select>
                                                <?php } else {?>
                                                <select class="form-control required" name="gender" >
                                                    <option value="">Select</option>
                                                    <option value="Male" <?php if($gender == "Male") echo 'selected="selected"';?> >Male</option>
                                                    <option value="Female" <?php if($gender == "Female") echo 'selected="selected"';?> >Female</option>
                                                </select>
                                                <?php } ?>
                                        </div>

                                        <div class="clearfix"></div>
                    
					<div class="col-md-12 col-sm-12 form-group">
						<label for="Address">Address</label>
                                                <input class="form-control" type="text" name="address" value="<?= $address?>" >
					</div>
                                        
			
                                        <div class="col-md-6 col-sm-6 form-group">
						<label for="CNIC">CNIC No.</label>
                                                <input class="form-control required digits" maxlength="13" minlength="13" type="text" name="cnic" value="<?= $cnic?>" >
					</div>
					  
                    <div class="col-md-6 col-sm-6 form-group">
                    	<div class="date_calendar">
                            <label for="Date">Join Date</label>
                            <input class="form-control txt_field date calendar_icon" name="join_date" id="#basic_example_1" type="text" placeholder="Date" value="<?= $join_date?>">
                    	</div>
                    </div>

                                        
                                        
                    <div class="clearfix"></div>

                   <div class="col-md-6 col-sm-12 form-group">
                           <label for="Address">Profile Image </label>
    <?php if(isset($_GET['edit']) && !empty($_GET['edit'])){ ?>
                <?php   if(!empty($profile_img)) ?>
                           <br><img src="uploads/profile/<?= $profile_img ?>" width="100" id="old_img" />
                           <input class="form-control" type="hidden" name="old_img" value="<?= $profile_img ?>">
                           <input class="form-control" type="file" name="profile_img" value="" id="profile_img" onchange="thumbImg();"  >
    <?php } 
            
        else  if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
                <?php   if(!empty($profile_img)) ?>
                           <br><img src="uploads/profile/<?= $profile_img ?>" width="100" id="old_img" />
    <?php }    
    else {?>
                           <input class="form-control" type="file" name="profile_img" value="" id="profile_img" onchange="thumbImg();"   >
    <?php } ?>        
                           
                   </div>
                    
                    
            <div class="col-md-6 col-sm-6 form-group">
                <div class="date_calendar">
                    <label for="Date">Account Type</label>
                    <select class="form-control required" name="agent_type" >
                        <option value="">Select</option>
                        <?php foreach($UserRoles as $urs){ 
                            $ssll = '';
                            if($agent_type== $urs['utId']){$ssll = "selected='selected'";}
                            ?>
                            <option value="<?php echo $urs['utId'];?>" <?= $ssll ?>><?php echo $urs['ut_title'];?></option>
                        <?php } ?>
                        
                    </select>
                </div>
            </div>
                     
                                        
    <?php if(isset($_GET['edit']) && !empty($_GET['edit'])){ ?>
                    <div class="col-sm-12 form-group">
                        <input type="hidden" name="agent_id" value="<?= $id_ ?>" >
                        <button type="submit" class="btn btn-default" name="update_form"> Submit </button>
                    </div>
    <?php } else if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
                                            
            <div class="col-sm-12 form-group"><a href="agents.php">
                <button type="button" class="btn btn-default" name="add_form"> Back </button>
                </a>
            </div>                            
    <?php  } else { ?>
                                        
            <div class="col-sm-12 form-group">
                <button type="submit" class="btn btn-default" name="add_form"> Submit </button>
            </div>
    <?php } ?>
                                        
		</div>
            </form>
		</div>

<?php include("_includes/footer.php"); ?>

<script>
	$(document).ready(function(){
                   $("#agent_form").validate();
               });

function thumbImg(){
	
		var imgg = $("#profile_img").val();
		arr = imgg.split(".");
		imgg = arr[1].toLowerCase();
		
		if(imgg == "jpg" || imgg =="jepg"  ||  imgg =="png"  || imgg =="gif"){
                    $("#old_img").fadeOut(300);                    
                }
		else {
				alert('Please select the valid file or format!');
				$("#thumb_img").focus();
				$("#thumb_img").val('');
			}
	}	
        
        
</script>

<?php if(isset($_GET['view']) && !empty($_GET['view'])){ ?>
<script>
$(document).ready(function(){ 
        $('.form-control').attr('readonly', 'readonly'); 
   });               

</script>


<?php } ?>


<script>
    $('.date').datepicker(); 
</script>