<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdmeSupplier($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_supplier where IsActive != '2' and sup_id = '".base64_decode($id)."' ";
$res_ed = $db->get_a_line($str_ed);

if($res_ed['code'] == 0){
	
	$str = "update m_supplier set code='".base64_decode($id)."' where sup_id = '".base64_decode($id)."'";
	$db->insert($str);
}

$edit_id = $res_ed['sup_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	
}
//check edit permission - END
$str_ed = "select * from m_supplier order by sup_id desc limit 0,1 ";
$res_code = $db->get_a_line($str_ed);
	$res_ed['code'] = $res_code['sup_id']+1; 
	$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; ?> Supplier Information</h4>				
            </div>
        </div>
		<!-- Page-Title - END -->	
					
		<div class="row">
			<div class="col-sm-12">
				<form id="jvalidate" name="frmSupplier" 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>Supplier Type<span class="text-danger">*</span></label>							
									<select name="sel_sup_type" id="sel_sup_type" class="form-control jsrequired" onchange="check_sup_type(this.value);">
										<option value="">Select Type</option>
										<option <?php if($res_ed['sup_type'] == "1") echo "selected"; ?> value="1">Individual</option>
										<option <?php if($res_ed['sup_type'] == "2") echo "selected"; ?> value="2">Company</option>
									</select>
								</div>
								
								<div class="form-group">
									<label>Supplier Name<span class="text-danger">*</span></label>							
									<input type="text" class="form-control jsrequired" name="txt_suppliername" id="txt_suppliername" value="<?php echo $res_ed['sup_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['sup_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['sup_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['sup_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['sup_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['sup_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['sup_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['sup_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['sup_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['sup_website_url']; ?>" />
								</div>
								
								
								
								<div class="form-group" id="dv_company_fields1">
									<label>Company Logo</label>							
									<input id="sup_company_logo" name="sup_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['sup_company_logo']) && ($act == 'update')) { 											
											if(file_exists(CAAROOTADMIN_DIR."/uploads/supplier_logo/".$res_ed['sup_company_logo']))
											{
												$disp_img = $res_ed['sup_company_logo'];	
											}											
										}										
									?>
									 
									<img id="sup_preview_logo" src="uploads/supplier_logo/<?php echo $disp_img; ?>" width="50px" align="absmiddle"/>
								</div>
								
								<div class="form-group" id="dv_supplier_image">
									<label >Supplier Image</label>										
									<input id="sup_image" name="sup_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['sup_image']) && ($act == 'update')) { 
											if(file_exists(CAAROOTADMIN_DIR."/uploads/supplier_image/".$res_ed['sup_image']))
											{
												$disp_sup_img = $res_ed['sup_image'];	
											}											
										}										
									?>
									 
									<img id="sup_preview_img" src="uploads/supplier_image/<?php echo $disp_sup_img; ?>" width="50px" align="absmiddle"/>
								</div>

								
								<div class="form-group">
									<label>Supplier Of <span class="text-danger">*</span></label>
									<div class="radio-info radio-inline">
										
										<label class="radio-inline">
											<input type="radio" name="supplier_type" value="1" <?php if($res_ed['sup_product'] == "1") echo "checked"; ?>> Raw Material 
										</label>
										</div>
									<div class="radio-info radio-inline">
										
										<label class="radio-inline">
											<input type="radio" name="supplier_type" value="2" <?php if($res_ed['sup_product'] == "2") echo "checked"; ?>> Product
										</label>
									</div>
								</div>

								
								<div class="form-group">
									<label> Payment Type <span class="text-danger">*</span></label>	
									<div class="radio-info radio-inline">
										<input class="rbtn_sup_paym_type_list jsrequired" type="radio" value="1" id="rbtn_sup_paym_type_gst" name="rbtn_sup_pay_type" <?php if($res_ed['sup_payment_type'] == "1") echo "checked"; ?> >
										<label for="rbtn_sup_paym_type_gst"> GST Supplier </label>
									</div>	
																	
									<div class="radio-info radio-inline">
										<input class="rbtn_sup_paym_type_list jsrequired" type="radio" value="2" id="rbtn_sup_paym_type_nongst" name="rbtn_sup_pay_type" <?php if($res_ed['sup_payment_type'] == "2") echo "checked"; ?>>
										<label for="rbtn_sup_paym_type_nongst" > Non-GST Supplier </label>
									</div>
																		
								</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['sup_gstno']; ?>" />
								</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('frmSupplier','supplier_actions.php','jvalidate','supplier','supplier_mng.php');" >Submit</button>	
													 
										<button class="btn btn-secondary waves-effect m-l-5" type="button" onClick="javascript:funCancel('frmSupplier','jvalidate','supplier','supplier_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_supplier&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_supplier&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_supplier&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_supplier_image').show();
	$('#txt_gst').removeClass('jsrequired');	
	$('#rbtn_sup_paym_type_gst').iCheck('disable');	
	
	<?php 
	if ($res_ed['sup_type'] =="2" && ($act == 'update')){
	?>
	$("#dv_company_fields").show();
	$("#dv_company_fields1").show();
	$('#dv_supplier_image').hide();
	$('#rbtn_sup_paym_type_nongst').iCheck('disable');	
	$('#txt_gst').addClass('jsrequired');	
	<?php
	}
	?>
	
	
});


function check_sup_type(sup_type){
	if(sup_type == "2"){
		$("#dv_company_fields").show();
		$("#dv_company_fields1").show();
		$('#dv_supplier_image').hide();
		$('#rbtn_sup_paym_type_gst').iCheck('enable');
		$('#rbtn_sup_paym_type_gst').iCheck('check');
		$('#rbtn_sup_paym_type_nongst').iCheck('uncheck');
		$('#rbtn_sup_paym_type_nongst').iCheck('disable');
		$('#txt_gst').addClass('jsrequired');
	}else{
		$("#dv_company_fields").hide();
		$("#dv_company_fields1").hide();	
		$('#dv_supplier_image').show();
		$('#rbtn_sup_paym_type_nongst').iCheck('enable');
		$('#rbtn_sup_paym_type_gst').iCheck('uncheck');
		$('#rbtn_sup_paym_type_nongst').iCheck('check');
		$('#rbtn_sup_paym_type_gst').iCheck('disable');	
		$('#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>
	  