<?php 


?>
<div id="page-wrapper">

            <div class="container-fluid">

                <!-- Page Heading -->
                <div class="row">
                    <div class="col-lg-12">
                        <h3 class="page-header">
                            Add Agent
                        </h3>
                        
                    </div>
                </div>
                <!-- /.row -->

                	
				<?php $attributes = array('id' => 'addnewuser','method'=>'post');
                        echo form_open('user/insert_agent', $attributes);
                        ?>
						<div class="row">
                    <div class="col-lg-6">

                        
                        <div id="status_div">
                        
                        <?php if($this->session->flashdata('err')) { ?>
                        <div class="alert-info">  <?php echo $this->session->flashdata('err');?></div>
                        <?php } ?>
                        </div>

                            <div class="form-group">
                                <label>Name</label>
                                <input class="form-control" type="text" name="name" value="<?php  if(isset($name)){ echo $name;} ?>"  id="name" required="">
                                 <label class="error">
                                  <?php if(form_error('name')) { echo form_error('name'); } ?>
                                </label>
                            </div>
							<div class="form-group">
                                <label>Email</label>
                                <input class="form-control" type="text" name="email" value="<?php  if(isset($email)){ echo $email;} ?>"  id="email" required="">
                                 <label class="error">
                                  <?php if(form_error('email')) { echo form_error('email'); } ?>
                                </label>
                            </div>
							<div class="form-group">
                                <label>Password</label>
                                <input class="form-control" type="password" name="password" value="<?php  if(isset($password)){ echo $password;} ?>"  id="password" required="" onchange="this.setCustomValidity(this.validity.patternMismatch ? this.title : ''); if(this.checkValidity()) form.confirm_password.pattern = this.value;">
                                <label class="error">
                                  <?php if(form_error('password')) { echo form_error('password'); } ?>
                                </label>
                            </div>
                            <div class="form-group">
                                <label>Rural/Urban</label>
                                <select class="form-control" id="sel1" name="sel1" required="">
									<option value="0">Select any</option>
									<option value="1">Rural</option>
									<option value="2">Urban</option>
									</select>
                                <label class="error">
                                  <?php if(form_error('place_category')) { echo form_error('place_category'); } ?>
                                </label>
                            </div>
							<div class="form-group" id="drp_sec">
                                    <label>Select Type</label>
                                  <select class="form-control" id="sel2" onchange="get_type(this.value)"  name="sel2" required="">
									<option value="0">Select any</option>
									<option value="muncipality">Municipality</option>
									<option value="panchayath">Panchayath</option>
									<option value="corporation">Corporation</option>
									</select>
                                </label>
                            </div>
														

                           
							                          
                            

                            

                    </div>
                    <div class="col-lg-6">
						<div class="form-group">
                                <label>User Name</label>
                                <input class="form-control" type="text" name="username" value="<?php  if(isset($username)){ echo $username;} ?>"  id="username" required="">
                                <label class="error">
                                  <?php if(form_error('username')) { echo form_error('username'); } ?>
                                </label>
                            </div>
							<div class="form-group">
                                <label>Confirm Password</label>
                                <input class="form-control" type="password" name="confirm_password" value="<?php  if(isset($confirm_password)){ echo $confirm_password;} ?>"  id="confirm_password" required="" onchange="this.setCustomValidity(this.validity.patternMismatch ? this.title : '');">
                                <label class="error">
                                  <?php if(form_error('password')) { echo form_error('password'); } ?>
                                </label>
                            </div>
						<div class="form-group">
                                    <label>Select District</label>                                  
                                    <?php $array=array(''=>'Select District');
                                    foreach($district_all as $value ){
                                        $array[$value['id']] = $value['name'];
                                    }
                                    echo form_dropdown("district_id", $array, '' ,'class="form-control" id="district_id" onchange="clear_dropBox()"');
                                     ?>
                                    <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
							<div class="form-group" id="muncipality_drop">
                                    <label>Select Muncipality</label>
                                  <select id="muncipality_data" class="form-control" name='sel_muncipality'></select>
								  
                                    <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
							
							<div class="form-group" id="block_drop">
                                    <label>Select Block</label>
                                  <select id="block_data" class="form-control" onchange="getPanch(this.value)" name='sel_block'></select>
                                     <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
							<div class="form-group" id="panchayath_drop">
                                    <label>Select Panchayath</label>
                                  <select id="panchayath_data" class="form-control" name='sel_panchayath'></select>
                                     <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
							<div class="form-group" id="corporation_drop">
                                    <label>Select Corporation</label>
                                   <select id="corporation_data" class="form-control"  name='sel_corporation'></select>
                                    <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
					<div>
                </div>
                <!-- /.row -->

            </div>
			
            <!-- /.container-fluid -->

        </div>
		<input type="submit" name="submit" class="btn btn-primary" value="SUBMIT"/>
            </form>
        <!-- /#page-wrapper -->

     </div>
    <!-- /#wrapper -->

    <!-- jQuery -->
    <script src="<?php echo base_url()?>js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="<?php echo base_url()?>js/bootstrap.min.js"></script>
    <script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.validate.js"></script>
    <script>
    $(document).ready(function() {
       $('#status_div').delay(5000).fadeOut(3600);
      });
       
    </script>
    <script language='JavaScript' type='text/JavaScript'>
    $(document).ready(function() {	
	var $taluk_sec = $('#taluk_sec');
	$("#muncipality_drop").hide();
	$("#panchayath_drop").hide();
	$("#corporation_drop").hide();
	$("#block_drop").hide();
	$taluk_sec.hide();		
		$("#addnewuser").validate({ 
			rules : {    
				password			: 	{ required: true, minlength: 6,maxlength: 50}, 
				confirm_password	: 	{ required: true, equalTo: "#password" }, 
				name 				: 	'required',    
				username			: 	{ required: true, minlength: 6,maxlength: 50},
				sel1				:	'required',
				sel2				:	'required',
				district_id			:	'required',
				email				: 	{required: true,email: true}
			}, 
			messages : {
				password     		: 	'Please specify your Password',
				confirm_password 	: 	'Please specify your Confirm Password Again',
				name 				:	'Please specify your Name',
				username 			:	'Please specify your username',
				sel1				:	'Please select Rural/Urban',
				sel2				:	'Please select Type',
				district_id			:	'Please select District',
				email				: 	'Please enter a valid email address',

			}
		});
		
    });
function clear_dropBox()
{
	$("#sel2").val(0);
	$("#block_drop").hide();
	$("#muncipality_drop").hide();
	$("#panchayath_drop").hide();
}
function get_type(tid)
{
	var district = $("#district_id").val();
	$("#muncipality_drop").hide();
	if(district== '' || district== null || district == undefined)
	{
		return false;
	}
	if(tid == 'muncipality'){$("#muncipality_drop").show(); getlocation = 'getlocation';$("#block_drop").hide();}
	if(tid == 'panchayath'){$("#panchayath_drop").show();$("#block_drop").show(); getlocation = 'block';}
	if(tid == 'corporation'){$("#corporation_drop").show(); getlocation = 'getlocation';$("#block_drop").hide();}
	var toAppend;
	//toAppend = '<option>Any</option>';
	$.ajax({
            type: "POST",
            dataType:"json",
            url: "<?php echo site_url('user/getLocationDetails')?>",
            data: "district_id="+district+"&type="+tid+"&action="+getlocation,
            success: function(data){
			//console.log(data)
            if(data.location.length >0)
				{
					var $muncipality 	= $('#muncipality_data');             
					var $panchayath 	= $('#panchayath_data');             
					var $corporation 	= $('#corporation_data');             
					var $block 			= $('#block_data');             
					$muncipality.empty();
					$panchayath.empty();
					$corporation.empty();
					$block.empty();
					$("#muncipality_data").append("<option value='0'>Any</option>");
					$("#block_data").append("<option value='0'>Any</option>");
					$("#corporation_data").append("<option value='0'>Any</option>");
					$.each(data,function(key, value)
						{
							toAppend = '';
							$.each(value,function(k, v)
								{
									if(tid == 'muncipality')
										{
											$("#panchayath_drop").hide();
											$("#corporation_drop").hide();
											$("#muncipality_data").append('<option value=' + v.id + '>' + v.name + '</option>');
											//toAppend += '<option value=' + v.id + '>' + v.name + '</option>';
											//$("#muncipality_data").append(toAppend);
										}
									if(tid == 'panchayath')
										{
											$("#muncipality_drop").hide();
											$("#corporation_drop").hide();
											$("#panchayath_drop").hide();
											
											$("#block_data").append('<option value=' + v.id + '>' + v.name + '</option>');
										}
									if(tid == 'corporation')
										{
											$("#muncipality_drop").hide();
											$("#panchayath_drop").hide();
											$("#corporation_data").append('<option value=' + v.id + '>' + v.name + '</option>');
										}
								})
						});
				}
			else
				{
					$("#muncipality_drop").hide();
					$("#panchayath_drop").hide();
					$("#corporation_drop").hide();
				}	
            }
        });

}
function getPanch(bid)
{
	var $panchayath 	= $('#panchayath_data');
	$panchayath.empty();
	 $.ajax({
            type: "POST",
            dataType:"json",
            url: "<?php echo site_url('user/getLocationDetails')?>",
            data: "district_id="+bid+"&type=panchayath&action=getlocation",
            success: function(data){
				console.log(data)
            if(data.location.length >0)
				{
					console.log(data)
					$("#panchayath_drop").show();
					$.each(data,function(key, value)
						{
							$.each(value,function(k, v)
								{
									$("#panchayath_data").append('<option value=' + v.id + '>' + v.name + '</option>');
								})
						});
				}
			else	
				{
					$("#panchayath_drop").hide();
				}	
            }
        });
}
function get_taluk(id)
{
	var $taluk_sec = $('#taluk_sec');
	$taluk_sec.hide();
	//+"&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>"
	 $.ajax({
            type: "POST",
            dataType:"json",
            url: "<?php echo site_url('user/getTaluk')?>",
            data: "district_id="+id+"&type=taluk",
            success: function(data){
				comsole.log(data)
            if(data.taluk_all.length >0)
				{
					comsole.log(data)
					var $taluk = $('#taluk');             
					$taluk.empty();
					$taluk_sec.show();
					$.each(data,function(key, value)
						{
							$.each(value,function(k, v)
								{
									$("#taluk").append('<option value=' + v.id + '>' + v.name + '</option>');
								})
						});
				}	
            }
        });
}
</script>



<style type="text/css">
.error
{
  color:rgb(238, 57, 57) !important ;
}
</style>
</body>

</html>