<script type="text/javascript">
    $(document).ready(function(){
        <!-- page script -->
        $('.datatables').DataTable();
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });
    });
</script>
<script>
jQuery(document).ready(function() {
	//$('select').select2();
	$.each($(".select2-container"), function (i, n) {
            $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
            $(n).prepend($(n).next());
            $(n).delay(500).queue(function () {
                $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                $(this).dequeue();
            });
        });
	jQuery("#content").find("input").keyup(function() {
		if (jQuery(this).attr('id') == "role") {
			jQuery(this).val(jQuery(this).val().toUpperCase());
		}
	});
});
   function ajaxCheck(id){
       var res;
        $.ajax({
            type: "POST",
            async: false,
            dataType : 'json',
            url: "<?php echo base_url() ?>admin/checkuserid",
            data: {
                userid : id
            },
            success: function(data) {
                   if(data == 'exist'){
                            res = "exist";

                    }
            }
	});
        return res;
   }
    function checkuserid(field, rules, i, options){
      var res = ajaxCheck(field.val());
      if(res=="exist"){
        return "This ID is used";
      }
    }

    function conf_update_user() {
        $("#frm_submit_insert_user").submit();
    }

    function clear_form(form) {
    $(form).find(':input').each(function(){
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                if(!$(this).is(':disabled'))
                    $(this).val('');
            break;
            case 'checkbox':
            case 'radio':
                if(!$(this).is(':disabled')){
                    $(this).attr('checked', false);
                    if($(this).hasClass('uni'))
                        $(this).parent('span').removeClass('checked');
                }
            break;
        }
    });
    return false;
}

	function back(){
		window.history.go(-1);
	}
	
	
	function changeDate(){

		if(document.getElementById('typeOfPack1').checked){
			var typeOfPk = 1;
		}
		else{
			var typeOfPk = 2;
		}
		alert(typeOfPk);
		var typeOfPack = typeOfPk;
		var numberOfPeriod = $('#numberOfPeriod').val();
		var startDate = $('#startDate').val();
		var endDate = $('#endDate').val();
		var userMasterID = "<?php echo $userMasterID ?>";
		
		//alert(userMasterID);
		//return false;
		
		var postData = {
			'typeOfPackage' : typeOfPack,
			'numberOfPeriod' : numberOfPeriod,
			'startDate' : startDate,
			'endDate' : endDate,
			'userMasterID' : userMasterID,
		};
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "<?php echo base_url()?>admin/changeDate",
			data: postData ,
			success: function(content){
				if(content.status == "success") {
					//alert(content.message.startDt);
					//alert(content.message.lastDt);
					$('#startDate').val(content.message.startDt);
					$('#endDate').val(content.message.lastDt);
					//window.location.href = "<?php //echo base_url()?>admin/userEdit/<?php //echo $userMasterID; ?>";
				}
			}
		});
	}
	
	/*function changeDate2(){
		
		var numberOfPeriod = $('#numberOfPeriod').val();
		var startDate = $('#startDate').val();
		var endDate = $('#endDate').val();
		var userMasterID = "<?php echo $userMasterID ?>";
		
		//alert(userMasterID);
		//return false;
		
		var postData = {
			'numberOfPeriod' : numberOfPeriod,
			'startDate' : startDate,
			'endDate' : endDate,
			'userMasterID' : userMasterID,
		};
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "<?php echo base_url()?>admin/changeDate",
			data: postData ,
			success: function(content){
				if(content.status == "success") {
					//alert(content.message.startDt);
					//alert(content.message.lastDt);
					$('#startDate').val(content.message.startDt);
					$('#endDate').val(content.message.lastDt);
					//window.location.href = "<?php echo base_url()?>admin/userEdit/<?php echo $userMasterID; ?>";
				}
			}
		});
		
	}*/
	

</script>

<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
    	<h1>
            <?php echo $this->lang->line('title') ?>
        </h1>
        <ol class="breadcrumb">
        	<li><a href="#"><i class="fa fa-home"></i> <?php echo $this->lang->line('home'); ?></a></li>
            <li><a href="#"><?php echo $this->lang->line('module'); ?></a></li>
            <li class="active"><?php echo $this->lang->line('title'); ?></li>
            <li class="active"><?php echo $this->lang->line('edituser'); ?></li>
        </ol>
    </section>


		<section class="content">
		<form method="post" enctype="multipart/form-data" action="<?php echo base_url(); ?>admin/userEdit">

			<div class="row">
				<div class="col-xs-12">

				<div class="box box-primary">
                	<div class="box-header with-border">
                		<h3  class="box-title">
                            <h3 class="pull-left"><?php echo $this->lang->line('edituser'); ?></h3>
                            <div style="margin-top:10px;" class="pull-right">
                      			<!--<button type="button" class="btn btn-primary btn-danger pull-right" onClick="clear_form('#validate');">Clear form</button>-->
                    		</div>
                    	</h3>
                    </div>



					<?php
					$hidden = array('order_time' => date('Y-m-d H-i-s'));
					$attrib = array('id'=>'validate');
					echo form_open_multipart('', $attrib, $hidden);?>

			<div class="row">
					<div class="col-md-12">

						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"></label>
								<div class="col-sm-8">
                            	<?php
								$data = array(
									'name'=>'fldid',
									'id'=>'fldid',
									'type'=>'hidden',
									'readonly'=>'readonly',
									'class'=>'form-control validate[required, funcCall[checkuserid], minSize[3]]',);
									echo form_input($data, $umaster->fldid); ?>

								</div>
							</div>
						</div>

						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo form_label($this->lang->line('userid'),'userid');?></label><span class="help-inline">Required, min size = 3</span>
								<div class="col-sm-8">
                            	<?php
								$data = array(
									'name'=>'userid',
									'id'=>'userid',
									//'readonly'=>'readonly',
									'class'=>'form-control validate[required, funcCall[checkuserid], minSize[3]]',);
									echo form_input($data, $umaster->userid); ?>

								</div>
							</div>
						</div>

						<!-- <div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo $this->lang->line('password1');?></label><span class="help-inline">Required, min size = 5, max size = 10</span>
								<div class="col-sm-8">
								<?php
								$data = array(
									'name'=>'password1',
									'id'=>'password1',
									'type'=>'password',
									'class'=>'form-control validate[required,minSize[5],maxSize[10]]',);
									echo form_input($data, $umaster->password); ?>
								</div>
							</div>
						</div>

						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo form_label($this->lang->line('password'),'password');?></label><span class="help-inline">Required, equals Password</span>
								<div class="col-sm-8">
								<?php
								$data = array(
									'name'=>'password',
									'id'=>'password',
									'type'=>'password',
									'class'=>'form-control validate[required,equals[password1]]',);
									echo form_input($data, $umaster->password); ?>

								</div>
							</div>
						</div> -->

						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo form_label($this->lang->line('username'),'username');?></label>
								<div class="col-sm-8">
								<?php
								$data = array(
									'name'=>'username',
									'id'=>'username',
									'class'=>'form-control validate[maxSize[150]]',);
									echo form_input($data, $umaster->username);
									?>
								</div>
							</div>
						</div>




						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo form_label($this->lang->line('phone_mobile'),'phone_mobile');?></label><span class="help-inline"><?php echo $this->lang->line('example') ?>: +601-9339 3995</span>
								<div class="col-sm-8">
								<?php
								$data = array(
									'name'=>'phone_mobile',
									'class'=>'form-control validate[maxSize[30]]',);
									echo form_input($data, $umaster->phone_mobile); ?>
								</div>
							</div>
						</div>

						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo form_label($this->lang->line('email'),'email');?></label><span class="help-inline"><?php echo $this->lang->line('example') ?>: sps@salihin.com.my</span>
								<div class="col-sm-8">
								<?php
								$data = array(
									'name'=>'email',
									'class'=>'form-control validate[custom[email]]',);
									echo form_input($data, $umaster->email); ?>


								</div>
							</div>
						</div>

						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo form_label($this->lang->line('language'),'language');?></label>
								<div class="col-sm-8">
								<?php
								$presellang = $umaster->language;
								$option = '';
								$option[NULL] = $this->lang->line('select');
								foreach($language as $row){
									$option[$row->code] = $row->description;
								}
								$js = "class='form-control'";
									echo form_dropdown('language', $option, $presellang, $js);
								?>
								</div>
							</div>
						</div>

            <div class="box-body">
              <div class="form-group col-sm-12">
                <label class="col-sm-2 control-label"><?php echo form_label($this->lang->line('company'),'company');?></label>
                <div class="col-sm-8">
                <?php
                $preselcomp = $umastercomp->companyID;

                // var_dump($preselcomp);
                // exit();

                $option = '';
                $option[NULL] = $this->lang->line('select');
                foreach($companyList as $row){
                  $option[$row->ID] = $row->companyName;
                }
                $js = "class='form-control'";
                  echo form_dropdown('companyID', $option, $preselcomp, $js);
                ?>
                </div>
              </div>
            </div>
            
            			<div class="box-body">
							<div class="form-group col-sm-12">
                            	<label class="col-sm-2 control-label"><?php echo $this->lang->line('paymentBy');?></label>
								<div class="col-sm-3">
                                    <input type="radio" id="typeOfPack1" name="typeOfPack"
                                    <?php if ($typeOfPack == 1) echo "checked";?>
                                    value="1" onclick="changeDate()">&nbsp;Monthly &nbsp;&nbsp;&nbsp;&nbsp;
                                    <input type="radio" id="typeOfPack2" name="typeOfPack"
                                    <?php if ($typeOfPack == 2) echo "checked";?>
                                    value="2" onclick="changeDate()">&nbsp;Yearly

								</div>
								<label class="col-sm-2 control-label"><?php echo $this->lang->line('numberOfPeriod');?></label>
								<div class="col-sm-3">
								<?php
								$data = array(
									'id'=>'numberOfPeriod',
									'name'=>'numberOfPeriod',
									'class'=>'form-control validate[required]',);
									$js='onblur="changeDate()"';
									echo form_input($data, $userExpiry, $js); ?>

								</div>
							</div>
						</div>
                        
                        <div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo $this->lang->line('startDate');?></label>
								<div class="col-sm-3">
								<input type="text" class="form-control datetimepicker" id="startDate" name="startDate" placeholder="" value="<?php echo date("d-m-Y", strtotime($startDate)); ?>" onblur="changeDate2()" readonly="readonly">
								</div>
                                <label class="col-sm-2 control-label"><?php echo $this->lang->line('endDate');?></label>
								<div class="col-sm-3">
								<input type="text" class="form-control datetimepicker" id="endDate" name="endDate" placeholder="" value="<?php echo date("d-m-Y", strtotime($endDate)); ?>" readonly="readonly">
								</div>
							</div>
						</div>

						<div class="box-body">
							<div class="form-group col-sm-12">
								<label class="col-sm-2 control-label"><?php echo $this->lang->line('userfile');?></label>
								<div class="col-sm-8">
                <input type="hidden" name="userfile" value="<?php echo $umaster->imageFile; ?>">
								<?php
								$data = array(
									'name'=>'userfile',
									'id'=>'userfile',
									'type ' => 'file',
									'value'=>'');
									echo form_upload($data, set_value('userfile')); ?>
								</div>

								<div class="span5">
									<div id="gallery">
										<div class="pic" align="center">
										<?php
										$path = parse_url(base_url(). 'dist/img/profile/' . $umaster->imageFile, PHP_URL_PATH);
										//To get the dir, use: dirname($path)
										//echo $_SERVER['DOCUMENT_ROOT'] . $path;
										if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $path)) {
											$image = base_url(). 'dist/img/profile/' .'default-user.png';
										}else{
											$image = base_url(). 'dist/img/profile/' . $umaster->imageFile;
										}
										?>
										<a class="fancybox" href="<?php echo $image; ?>">
											<img class="img-polaroid" style="width:50px;" src="<?php echo $image; ?>" />
										</a>
										</div>
									</div>
								</div>
							</div>
						</div>

						<div class="box-body">

								<div class="btn pull-right">
                                <button type="button" onclick="back()" class="btn btn-primary" ><?php echo $this->lang->line('back'); ?></button>
								<?php
									$data = array(
										'name'=>'submit',
										'class'=>'btn btn-primary');
									$js='onclick="return confirm(\'Press OK to continue...\')"';
									echo form_submit($data,$this->lang->line('update'),$js);
								?>
								</div>
						</div>
						</div>

						</div> <!--content np -->
					</div> <!--block -->
				 <!--span12-->

			</div> <!--row fluid-->

			</div>
			</form>
	</section><!--content class-->
</div> <!--wrap-->

