<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdmeCustomer($db,'');
include_once "includes/pagepermission.php";

//check permission - START
if(!($res_modm_prm)){
	header("Location:".admin_public_url."error.php");
}
//check permission - END

$id=$_REQUEST['id'];
if($id!="")
{
	
//check edit permission - START	
if(trim($res_modm_prm['EditPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
//check edit permission - END	

$operation="Edit";
$act="update";
$btn='Update';

$str_ed = "select * from m_customer where IsActive != '2' and cus_id = '".base64_decode($id)."' ";
$res_ed = $db->get_a_line($str_ed);

if($res_ed['code'] == 0){
	//$res_ed['code'] = mt_rand(100000, 999999);
	$str = "update m_customer set code='".base64_decode($id)."' where cus_id = '".base64_decode($id)."'";
	$db->insert($str);
}

$edit_id = $res_ed['cus_id'];

$chk='';
if($res_ed['IsActive']=='1')
{
	$chk='checked';
}

}
else
{
	
//check edit permission - START	
if(trim($res_modm_prm['AddPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
$str_ed = "select * from m_customer order by cus_id desc limit 0,1 ";
$res_code = $db->get_a_line($str_ed);
	$res_ed['code'] = $res_code['cus_id']+1; 
//check edit permission - END	
	$operation="Add";
	$act="insert";
	$btn='Save';
}
?>  

<div class="wrapper">
    <div class="container">        
		<!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-12">										
                <h4 class="page-title"><?php echo $operation; ?> Customer Information</h4>				
            </div>
        </div>
		<!-- Page-Title - END -->	
					
		<div class="row">
			<div class="col-sm-12">
				<form id="jvalidate" name="frmCustomer" role="form" class="form-horizontal" action="#" method="post" enctype="multipart/form-data" >
				<input type="hidden" name="action" value="<?php echo $act; ?>" />
				<input type="hidden" name="edit_id" id="edit_id" value="<?php echo $edit_id; ?>"  />
				
					<div class="card-box">						
						
						<div class="row">
							<div class="col-sm-6">
								<div class="form-group">
									<label>Reference Code<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" disabled name="txt_code" id="txt_code" value="<?php echo $res_ed['code']; ?>" />
								</div>
								
								<div class="form-group">
									<label>Customer Type<span class="text-danger">*</span></label>							
									<select name="sel_cus_type" id="sel_cus_type" class="form-control jsrequired" onchange="check_cus_type(this.value);">
										<option value="">Select Type</option>
										<option <?php if($res_ed['cus_type'] == "1") echo "selected"; ?> value="1">Individual</option>
										<option <?php if($res_ed['cus_type'] == "2") echo "selected"; ?> value="2">Company</option>
									</select>
								</div>
								
								<div class="form-group">
									<label>Customer Name<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" name="txt_customername" id="txt_customername" value="<?php echo $res_ed['cus_name']; ?>" />
								</div>
								
								<div class="form-group">
									<label>Address<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" name="txt_address" id="txt_address" value="<?php echo $res_ed['cus_address']; ?>" />
								</div>
								
								<div class="form-group">
									<label>Country<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" name="txt_country" id="txt_country" value="<?php echo $res_ed['cus_country']; ?>" />
								</div>
								
								<div class="form-group">
									<label>State<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" name="txt_state" id="txt_state" value="<?php echo $res_ed['cus_state']; ?>" />
								</div>
								
								<div class="form-group">
									<label>City<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" name="txt_city" id="txt_city" value="<?php echo $res_ed['cus_city']; ?>" />
								</div>
								
								<div class="form-group">
									<label>Postalcode<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" name="txt_postcode" id="txt_postcode" value="<?php echo $res_ed['cus_postcode']; ?>" onKeyPress="return isNumberKey(event,this)" />
								</div>
								
							</div>
							
							<div class="col-sm-6">
							
								<div class="form-group">
									<label>Landline No</label>							
									<input type="text" class="form-control " name="txt_landline" id="txt_landline" value="<?php echo $res_ed['cus_landline']; ?>" onKeyPress="return isNumberKey(event,this)" />
								</div>
								
								<div class="form-group">
									<label>Mobile No</label>							
									<input type="text" class="form-control " name="txt_mobile" id="txt_mobile" value="<?php echo $res_ed['cus_mobile']; ?>" onKeyPress="return isNumberKey(event,this)" />
								</div>
																
								<div class="form-group">
									<label>Email Id</label>							
									<input type="email" class="form-control" name="txt_email" id="txt_email" value="<?php echo $res_ed['cus_email']; ?>" />
								</div>
								
								<div class="form-group">
									<label>Website URL</label>							
									<input type="text" class="form-control" name="txt_websiteurl" id="txt_websiteurl" value="<?php echo $res_ed['cus_website_url']; ?>" />
								</div>
								
								<div class="form-group" id="dv_company_fields">
									<label>GST No<span class="text-danger">*</span></label>							
									<input type="text" class="form-control " name="txt_gst" id="txt_gst" value="<?php echo $res_ed['cus_gstno']; ?>" />
								</div>
								
								<div class="form-group" id="dv_company_fields1">
									<label>Company Logo</label>							
									<input id="cus_company_logo" name="cus_company_logo" type="file" class="file"  accept="image/jpeg,image/png,jpg|png" onchange="readURL_photo_preview(this);" />
									
									<?php
										$disp_img = 'NoImageAvailable.png';										
										if (!empty($res_ed['cus_company_logo']) && ($act == 'update')) { 
											if(file_exists(CAAROOTADMIN_DIR."/uploads/customer_logo/".$res_ed['cus_company_logo']))
											{
												$disp_img = $res_ed['cus_company_logo'];	
											}											
										}										
									?>
									 
									<img id="sup_preview_logo" src="uploads/customer_logo/<?php echo $disp_img; ?>" width="50px" align="absmiddle"/>
								</div>
								
								<div class="form-group" id="dv_customer_image">
									<label >Customer Image</label>										
									<input id="cus_image" name="cus_image" type="file" class="file"  accept="image/jpeg,image/png,jpg|png" onchange="disp_sup_img_preview(this);" /> 									
									<?php
										$disp_sup_img = 'NoImageAvailable.png';										
										if (!empty($res_ed['cus_image']) && ($act == 'update')) { 
											if(file_exists(CAAROOTADMIN_DIR."/uploads/customer_image/".$res_ed['cus_image']))
											{
												$disp_sup_img = $res_ed['cus_image'];	
											}											
										}										
									?>
									 
									<img id="sup_preview_img" src="uploads/customer_image/<?php echo $disp_sup_img; ?>" width="50px" align="absmiddle"/>
								</div>
								
							</div>
							
						</div>
						
						<div class="row">
							<div class="col-sm-8">&nbsp;</div>
					
							<div class="col-sm-4">						
								<div class="form-group">
									<div class="btn-group pull-right m-t-15">
										<button class="btn btn-primary waves-effect waves-light" type="button" onClick="javascript:funSubmtWithImg('frmCustomer','customer_actions.php','jvalidate','customer','customer_mng.php');" >Submit</button>	
													 
										<button class="btn btn-secondary waves-effect m-l-5" type="button" onClick="javascript:funCancel('frmCustomer','jvalidate','customer','customer_mng.php');" >Cancel</button>
									</div>
								</div>						
							</div>
							
						</div>
										
						
					</div>
					
				</form>
            </div>
        </div> <!-- end row -->	
			

<?php include "includes/footer.php";  ?> 
	 </div> <!-- container -->
</div> <!-- End wrapper -->

<?php include "includes/footer_script.php";  ?> 

<!-- Autocomplete -->
<script type="text/javascript" src="assets/plugins/autocomplete/jquery.autocomplete.min.js"></script>

<script type="text/javascript"> 

$("document").ready(function(){
    		
	<!-- autocomplete function - START -->		
	$('#txt_country').autocomplete({
		lookup: function (query, done) {				
			$.ajax({
				url  : "autocomplete_actions.php?pageaction=autocomplete_customer&srch_type=country_search&srch_keyword="+$('#txt_country').val(),
				dataType: "json",						
				success: function (data)
				{					
				 done(data);
				}
			});			
		}		
	});
	
	$('#txt_state').autocomplete({
		lookup: function (query, done) {				
			$.ajax({
				url  : "autocomplete_actions.php?pageaction=autocomplete_customer&srch_type=state_search&srch_keyword="+$('#txt_state').val(),
				dataType: "json",						
				success: function (data)
				{					
				 done(data);
				}
			});			
		}		
	});
	
	$('#txt_city').autocomplete({
		lookup: function (query, done) {				
			$.ajax({
				url  : "autocomplete_actions.php?pageaction=autocomplete_customer&srch_type=city_search&srch_keyword="+$('#txt_city').val(),
				dataType: "json",						
				success: function (data)
				{					
				 done(data);
				}
			});			
		}		
	});

	<!-- autocomplete function - END -->
	
	
	$("#dv_company_fields").hide();
	$("#dv_company_fields1").hide();
	$('#dv_customer_image').show();
	$('#txt_gst').removeClass('jsrequired');	
	
	<?php 
	if ($res_ed['cus_type'] =="2" && ($act == 'update')){
	?>
	$("#dv_company_fields").show();
	$("#dv_company_fields1").show();
	$('#dv_customer_image').hide();
	$('#txt_gst').addClass('jsrequired');	
	<?php
	}
	?>
	
	
});


function check_cus_type(cus_type){
	if(cus_type == "2"){
		$("#dv_company_fields").show();
		$("#dv_company_fields1").show();
		$('#dv_customer_image').hide();
		$('#txt_gst').addClass('jsrequired');	
	}else{
		$("#dv_company_fields").hide();
		$("#dv_company_fields1").hide();
		$('#dv_customer_image').show();
		$('#txt_gst').removeClass('jsrequired');	
	}
}


function readURL_photo_preview(input) { 
    if (input.files && input.files[0]) {				
        var reader = new FileReader();
        reader.onload = function (e) {
            $("[id*=sup_preview_logo]").attr("src", e.target.result);
        };
        reader.readAsDataURL(input.files[0]); 
    }
}

function disp_sup_img_preview(input) { 
    if (input.files && input.files[0]) {				
        var reader = new FileReader();
        reader.onload = function (e) {
            $("[id*=sup_preview_img]").attr("src", e.target.result);
        };
        reader.readAsDataURL(input.files[0]); 
    }
}

</script>



</body>
</html>
	  