<script type="text/javascript">
    $(document).ready(function(){
        <!-- page script -->
        $('.datatables').DataTable();
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });




    });

	function changeList(obj) {
        var id = obj.value;

        $.ajax({
            type: "POST",
            url: "<?php echo base_url() ?>setup/getState",
            data: "region=" + id,
            dataType: "json",
            success: function(content) {

                if (content.status == "success") {

                    var items = [];
                    items.push('<option>-Please Select-</option>');
                    for (var i = 0; i < content.message.length; i++) {
                        items.push('<option value="' + content.message[i].fldid + '">'
                                + content.message[i].fldname + '</option>');
                    }
                    jQuery("#stateID").empty();
                    jQuery("#stateID").append(items.join('</br>'));
                } else {
                    $("#error").html('<p>' + content.message + '</p>');
                }
            }
        });
        return false;
    }


	function editCompany(){
		alert($('#userfile').val());

		var postData = {
      'ID' : $('#ID').val(),
			'companyName' : $('#companyName').val(),
			'line1' : $('#line1').val(),
			'line2' : $('#line2').val(),
			'line3' : $('#line3').val(),
			'city' : $('#city').val(),
			'companyPostCode' : $('#companyPostCode').val(),
			'countryID' : $('#countryID option:selected').val(),
			'stateID' : $('#stateID option:selected').val(),
			'phoneNo' : $('#phoneNo').val(),
			'faxNo' : $('#faxNo').val(),
			'businessTypeID' : $('#businessTypeID option:selected').val(),
			'email' : $('#email').val(),
			'userfile' : $('#userfile').val(),
		};

		$.ajax({
			type: "POST",
			dataType: "json",
			url: "<?php echo base_url()?>setup/editCompany",
			data: postData ,
			success: function(content){
				if(content.status == "success") {
					alert('Data successfully saved');
				}
			}
		});

	}


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


	function selectParlimen(){
		
		var stateID = $('#stateID option:selected').val();
		//alert(stateID);
		//return false;
		
		$('#parlimenID').empty();
		
		$.ajax({
        	type: "POST",
            url: "<?php echo base_url(); ?>setup/getParlimen",
            data: {
            	'stateID' : stateID
			},
            dataType:'json',
            success: function(data){
            	if(data.status = 'success'){
					
                    var n = data.message.length;
                    var i = 0;
					
                    var opt = '';
					   
                    for (i=0;i<n;i++)
                    	opt=opt + '<option value="' + data.message[i].ID + '">' + data.message[i].parlimen + '</option>';

                    $('#parlimenID').append(opt);
					
            	}
            }
         });
	
	}
	
	function selectIndustry(){
	
		var industryID = $('#industryID option:selected').val();
		
		$('#businessTypeID').empty();
		
		$.ajax({
        	type: "POST",
            url: "<?php echo base_url(); ?>setup/getBusinessType",
            data: {
            	'industryID' : industryID
			},
            dataType:'json',
            success: function(data){
            	if(data.status = 'success'){
					
                    var n = data.message.length;
                    var i = 0;
					
                    var opt = '';
					   
                    for (i=0;i<n;i++)
                    	opt=opt + '<option value="' + data.message[i].ID + '">' + data.message[i].name + '</option>';

                    $('#businessTypeID').append(opt);
					
            	}
            }
         });
		
		
	}

</script>

			<div class="content-wrapper">
        		<!-- Content Header (Page header) -->
        		<section class="content-header">
          			<h1>
            			<?php echo $this->lang->line('title') ?>
            			<!--<small>advanced tables</small>-->
          			</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('updateCompany'); ?></li>
                    </ol>
        		</section>

                <!-- Main content -->
                <section class="content">
                  <div class="row">
                    <div class="col-xs-12">

                    <form action="" method="POST" enctype="multipart/form-data" action="<?php echo base_url(); ?>setup/companyEdit">

        			  <div class="box box-primary">
                        <div class="box-header with-border">
                          <h3 class="box-title"><?php echo $this->lang->line('updateCompany'); ?></h3>
                        </div><!-- /.box-header -->

                        <div class="box-body">
                            <div class="form-group col-sm-12">
                              <label class="col-sm-2 control-label"><?php echo $this->lang->line('ID');?></label>
                              <div class="col-sm-8">
                                <?php
                  $data = array(
                    'id' => 'ID',
                    'name' => 'ID',
                    'type' =>'hidden',
                    //'readonly' => 'readonly',
                    'class' => 'form-control validate[required,maxSize[128]]',);
//akmal 20160608 //error when company not created. 
//suggestion to return false. 					
                  if(isset($company) ){
                    echo form_input($data, $company[0]->ID);
                  }
                  else{
                    echo form_input($data, set_value('ID'));
                  }
                                ?>
                              </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('companyName');?></label>
                              <div class="col-sm-8">
                                <?php
                									$data = array(
                										'id' => 'companyName',
                										'name' => 'companyName',
                										//'readonly' => 'readonly',
                										'class' => 'form-control validate[required,maxSize[128]]',);
                									if(isset($company)){
                										echo form_input($data, $company[0]->companyName);
                									}
                									else{
                										echo form_input($data, set_value('companyName'));
                									}
                                ?>
                              </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('companyAddress');?></label>
                              <div class="col-sm-8">
                                <?php
                                	$data = array(
										'id' => 'line1',
										'name' => 'line1',
                                    	'class' => 'form-control validate[required,maxSize[128]]',);
                                    	if(isset($company)){
											echo form_input($data, $company[0]->line1);
										}
										else{
											echo form_input($data, set_value('line1'));
										}
                                ?>
                              </div>
                            </div>
                          </div>

                          <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(
										'id' => 'line2',
										'name' => 'line2',
                                    	'class' => 'form-control validate[required,maxSize[128]]',);
                                    	if(isset($company)){
											echo form_input($data, $company[0]->line2);
										}
										else{
											echo form_input($data, set_value('line2'));
										}
                                ?>
                              </div>
                            </div>
                          </div>

                          <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(
										'id' => 'line3',
										'name' => 'line3',
                                    	'class' => 'form-control',);
                                    	if(isset($company)){
											echo form_input($data, $company[0]->line3);
										}
										else{
											echo form_input($data, set_value('line3'));
										}
                                ?>
                              </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('city');?></label>
                              <div class="col-sm-3">
                                <?php
									$data = array(
										'name' => 'city',
										'id' => 'city',
										'class' => 'form-control',);
										if(isset($company)){
											echo form_input($data, $company[0]->city);
										}
										else{
											echo form_input($data, set_value('city'));
										}
                                ?>
                              </div>
                              <label class="col-sm-2 control-label"><?php echo $this->lang->line('companyPostCode');?></label>
                              <div class="col-sm-3">
                                <?php
                                	$data = array(
										'name' => 'companyPostCode',
										'id' => 'companyPostCode',
										'class' => 'form-control',);
										if(isset($company)){
											echo form_input($data, $company[0]->companyPostCode);
										}
										else{
											echo form_input($data, set_value('companyPostCode'));
										}
                                 ?>
                              </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('country');?></label>
                              <div class="col-sm-3">
                                <?php
									$preselcat = $company[0]->fldcountry;
									$options[$company[0]->countryID] = $company[0]->fldcountry;
										foreach ($country as $row) {
											$options[$row->fldid] = $row->fldcountry;
											}
											$js = 'onChange="changeList(this)" class="form-control select2" id="countryID"';
												echo form_dropdown('countryID', $options, $preselcat, $js);
								?>
                              </div>
                              <label class="col-sm-2 control-label"><?php echo $this->lang->line('state');?></label>
                              <div class="col-sm-3">
                                <?php
									$preselcat2 = $company[0]->fldname;
									$options2[$company[0]->companyStateID] = $company[0]->fldname;
									if ($state != '') {
										foreach ($state as $row) {
											$options2[$row->fldid] = $row->fldname;
											}
										}
										$js = 'id=stateID class="form-control select2" onchange="selectParlimen()"';
											echo form_dropdown('stateID', $options2, $preselcat2, $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('parlimen');?></label>
                              <div class="col-sm-3">
                                	<select name="parlimenID" id="parlimenID" class="form-control select2">
                                    	
                                        <?php
											if($company[0]->parlimenID != 0){
												echo"<option value='".$company[0]->parlimenID."'>".$company[0]->parlimen."</option>";
										
												foreach($parlimen as $parlimens){
													if($parlimens->ID != $company[0]->parlimenID){
														echo"<option value='".$parlimens->ID."'>".$parlimens->parlimen."</option>";
													}
												}
											}
											else{
										
												echo"<option value=''>--Please Select--</option>";
												
												foreach($parlimenAll as $parlimens){
													echo"<option value='".$parlimens->ID."'>".$parlimens->parlimen."</option>";
												}
											
											}
										?>
                                    </select>
                              </div>
                              <label class="col-sm-2 control-label"><?php echo $this->lang->line('phoneNo');?></label>
                              <div class="col-sm-3">
                                <?php
									$data = array(
										'id' => 'phoneNo',
    									'class' => 'form-control',
    									'name' => 'phoneNo',);
										if(isset($company)){
											echo form_input($data, $company[0]->phoneNo);
										}
										else{
											echo form_input($data, set_value('phoneNo'));
										}

								?>
                              </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('faxNo');?></label>
                              <div class="col-sm-3">
                                <?php
									$data = array(
										'id' => 'faxNo',
    									'class' => 'form-control',
    									'name' => 'faxNo',);
										if(isset($company)){
											echo form_input($data, $company[0]->faxNo);
										}
										else{
											echo form_input($data, set_value('faxNo'));
										}
								?>
                              </div>

                              <label class="col-sm-2 control-label"><?php echo $this->lang->line('email');?></label>
                              <div class="col-sm-3">
                                <?php
									$data = array(
										'id' => 'email',
    									'class' => 'form-control',
    									'name' => 'email',);
										if(isset($company)){
											echo form_input($data, $company[0]->email);
										}
										else{
											echo form_input($data, set_value('email'));
										};
								?>
                              </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('typeOfIndustry');?></label>
                              <div class="col-sm-3">
                              		<select name="industryID" id="industryID" class="form-control select2" onchange="selectIndustry()">
                                    	
                                        <?php
											if($company[0]->industryID != 0 || $company[0]->industryID != ''){
												echo"<option value='".$company[0]->industryID."'>".$company[0]->industryName."</option>";
										
												foreach($businessIndustry as $industry){
													if($industry->ID != $company[0]->industryID){
														echo"<option value='".$industry->ID."'>".$industry->name."</option>";
													}
												}
											}
											else{
										
												echo"<option value=''>--Please Select--</option>";
										
												foreach($businessIndustry as $industry){
													echo"<option value='".$industry->ID."'>".$industry->name."</option>";
												}
											
											}
										?>
                                    </select>
                              </div><!-- /.form-group -->
                              <label class="col-sm-2 control-label"><?php echo $this->lang->line('typeOfBusiness');?></label>
                              <div class="col-sm-3">
                              	<select name="businessTypeID" id="businessTypeID" class="form-control select2">
                                    	
                                        <?php
											if($company[0]->businessTypeID != 0){
												echo"<option value='".$company[0]->businessTypeID."'>".$company[0]->businessName."</option>";
										
												foreach($businessType as $bussType){
													if($bussType->ID != $company[0]->businessTypeID){
														echo"<option value='".$bussType->ID."'>".$bussType->name."</option>";
													}
												}
											}
											else{
										
												echo"<option value=''>--Please Select--</option>";
										
												foreach($businessType as $bussType){
													echo"<option value='".$bussType->ID."'>".$bussType->name."</option>";
												}
											
											}
										?>
                                  </select>
                              </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('uploadLogo'),'uploadLogo');?></label>
                              <div class="col-sm-3">
                              <input type="hidden" name="companyfile" value="<?php echo $company[0]->uploadLogo; ?>">
                              <?php
                                $data = array(
									'name' => 'userfile',
                                    'id' => 'userfile',
                                    'type ' => 'file',
                                    'value' => '');

                                if(isset($company)){
                      				echo form_upload($data, set_value($company[0]->uploadLogo));
                    			}
                    			else{
                      				echo form_upload($data, set_value('userfile'));
                    			};
										            //echo form_upload($data, set_value($company[0]->userfile));
                              ?>
                              </div>
                              
                              <label class="col-sm-2 control-label"><?php echo $this->lang->line('startDate');?></label>
                              <div class="col-sm-3">
                              	<input type="text" id="incorporateDate" name="incorporateDate" class="form-control datetimepicker" value="<?php if($this->session->userdata('incorporateDate') != "0000-00-00"){echo date("d-m-Y", strtotime($company[0]->incorporateDate));}?>" />
                              </div>

                            </div>
                          </div>
                          
                          <div class="box-body">
                            <div class="form-group col-sm-12">
                              <label class="col-sm-2 control-label"></label>
                              <div class="col-sm-3">
                              <?php if (isset($company)) { ?>
                              		<div class="pic">
                                    	<a class="fancybox" href="<?php echo base_url().'dist/companyProfile/'.$company[0]->uploadLogo; ?>">
											                 <img class="img-polaroid" src="<?php echo base_url().'dist/companyProfile/'.$company[0]->uploadLogo; ?>" width="50%"/>
                                    	</a>
									</div>
                              <?php } ?>
                              </div>

                            </div>
                          </div>

                          <div class="box">
                        	<div class="box-header with-border">
                            	<div class="box-title pull-right">
                            		<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'=>'mysubmit',
                                            'class'=>'btn btn-primary');
                                        $js='onclick="return confirm(\'Press OK to continue...\')"';
                                            //$js='onclick="submitForm()"';
                                            echo form_submit($data,$this->lang->line('update'), $js);
                                        ?>
								</div>
                        		</div>
                            </div>
                          </div><!-- /.box-header -->


                      </div>

        </form>


                      </div><!-- /.box -->
                    </div><!-- /.col -->
                  </div><!-- /.row -->
                </section><!-- /.content -->

              </div><!-- /.content-wrapper -->
