<?php
 	session_start(); 

	if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}



$_SESSION['_token']=bin2hex(openssl_random_pseudo_bytes(16));
	
	
	include("connect.php");

	
	//$companyname=$_POST['companyname'];
	$companyname=str_replace ("'","\'",$_POST['companyname']);
	$companyname=str_replace ('"','\"',$companyname);
	
	
	$companymastercode=str_replace ("'","\'",$_POST['companymastercode']);
	$companymastercode=str_replace ('"','\"',$companymastercode);	
	$companyfaxnumber=str_replace ("'","\'",$_POST['companyfaxnumber']);
	$companyfaxnumber=str_replace ('"','\"',$companyfaxnumber);
	$companywebsite=str_replace ("'","\'",$_POST['companywebsite']);
	$companywebsite=str_replace ('"','\"',$companywebsite);
	
	$cgstnumber=str_replace ("'","\'",$_POST['cgstnumber']);
	$cgstnumber=str_replace ('"','\"',$cgstnumber);
	
	$ccinnumber=str_replace ("'","\'",$_POST['ccinnumber']);
	$ccinnumber=str_replace ('"','\"',$ccinnumber);
	
	$cpannumber=str_replace ("'","\'",$_POST['cpannumber']);
	$cpannumber=str_replace ('"','\"',$cpannumber);
	
	$chstnumber=str_replace ("'","\'",$_POST['chstnumber']);
	$chstnumber=str_replace ('"','\"',$chstnumber);
	
	
	$contactname=str_replace ("'","\'",$_POST['contactname']);
	$contactname=str_replace ('"','\"',$contactname);
	
	$cmiddlename=str_replace ("'","\'",$_POST['cmiddlename']);
	$cmiddlename=str_replace ('"','\"',$cmiddlename);
	
	$csurname=str_replace ("'","\'",$_POST['csurname']);
	$csurname=str_replace ('"','\"',$csurname);
	
		$contactnumber=str_replace ("'","\'",$_POST['contactnumber']);
	$contactnumber=str_replace ('"','\"',$contactnumber);
	$mobilecode=$_POST['mobilecode'];
	$contactnumber=$mobilecode."".$contactnumber;
	$cmobilenumber=str_replace ("'","\'",$_POST['cmobilenumber']);
	$cmobilenumber=str_replace ('"','\"',$cmobilenumber);
	$homemobilecode=$_POST['homemobilecode'];
	$cmobilenumber=$homemobilecode."".$cmobilenumber;
	$contactemail=str_replace ("'","\'",$_POST['contactemail']);
	$contactemail=str_replace ('"','\"',$contactemail);
	$caltenateemail=str_replace ("'","\'",$_POST['caltenateemail']);
	$caltenateemail=str_replace ('"','\"',$caltenateemail);
	
	$cremarks=str_replace ("'","\'",$_POST['companynote']);
	$cremarks=str_replace ('"','\"',$cremarks);
	
	$caddress1=str_replace ("'","\'",$_POST['caddress1']);
	$caddress1=str_replace ('"','\"',$caddress1);
	
	$caddress2=str_replace ("'","\'",$_POST['caddress2']);
	$caddress2=str_replace ('"','\"',$caddress2);
	
	$ccountry=str_replace ("'","\'",$_POST['ccountry']);
	$ccountry=str_replace ('"','\"',$ccountry);
	$cpostalcode=str_replace ("'","\'",$_POST['cpostalcode']);
	$cpostalcode=str_replace ('"','\"',$cpostalcode);
	
	$cmsgappname=str_replace ("'","\'",$_POST['cmsgappname']);
	$cmsgappname=str_replace ('"','\"',$cmsgappname);
	

	
	$cmsgappid=$_POST['cmsgappid'];
	
	
	$cvideoappname=str_replace ("'","\'",$_POST['cvideoappname']);
	$cvideoappname=str_replace ('"','\"',$cvideoappname);
	
	$cvideoappid=$_POST['cvideoappid'];
	
	
	
	
	$cstate=str_replace ("'","\'",$_POST['cstate']);
	$cstate=str_replace ('"','\"',$cstate);
	
	$ccity=$_POST['ccity'];
	
	
	
	$branchmid= $_SESSION['branchid'];
	
	
	$attechment="";
	/* if(empty($_FILES['attechment']['name']) == false  )
	{
		$attechment = date("YmdHis").$_FILES['attechment']['name'];
		$path = "docs/";
		move_uploaded_file( $_FILES['attechment']['tmp_name'], $path ."/". $attechment );
	
	}
	 */
	
	$flag=1;
	{
	$companyicon="";
	 if(empty($_FILES['companyicon']['name']) == false  )
	 {
		 
		  // Get Image Dimension
		$fileinfo = @getimagesize($_FILES['companyicon']['tmp_name']);
		$width = $fileinfo[0];
		$height = $fileinfo[1];
	
		$simplephoto11=$_FILES['companyicon']['name'];
		 $ext = pathinfo($simplephoto11, PATHINFO_EXTENSION);
		
		$fileexplode1 = explode(".",$simplephoto11);
		$fileend1 = end($fileexplode1);

		$companyicon= date("YmdHis").rand(100,999).".".$fileend1;
		$path = "images";
		if($ext=='png' || $ext=='jpg' || $ext=='jpeg' || $ext=='gif' || $ext=='webp')
		{
		  move_uploaded_file( $_FILES['companyicon']['tmp_name'], $path ."/". $companyicon);
		  $flag=1;
		}
		else {
		  $_SESSION['msg1'] = "Only Upload Image";
			  $flag=0;
		}
		/* if ($width > "1920" || $height > "556") {
        
            $_SESSION['msg1']= "Image dimension should be within 1920X556";
			$flag=0;
        } */
	}
	
	$companylogo="";
	 if(empty($_FILES['companylogo']['name']) == false  )
	 {
		 
		  // Get Image Dimension
		$fileinfo = @getimagesize($_FILES['companylogo']['tmp_name']);
		$width = $fileinfo[0];
		$height = $fileinfo[1];
	
		$simplephoto11=$_FILES['companylogo']['name'];
		 $ext = pathinfo($simplephoto11, PATHINFO_EXTENSION);
		
		$fileexplode1 = explode(".",$simplephoto11);
		$fileend1 = end($fileexplode1);

		$companylogo= date("YmdHis").rand(100,999).".".$fileend1;
		$path = "images";
		if($ext=='png' || $ext=='jpg' || $ext=='jpeg' || $ext=='gif' || $ext=='webp')
		{
		  move_uploaded_file( $_FILES['companylogo']['tmp_name'], $path ."/". $companylogo);
		  $flag=1;
		}
		else {
		  $_SESSION['msg1'] = "Only Upload Image";
			  $flag=0;
		}
		/* if ($width > "1920" || $height > "556") {
        
            $_SESSION['msg1']= "Image dimension should be within 1920X556";
			$flag=0;
        } */
	}
	
	$companybanner="";
	 if(empty($_FILES['companybanner']['name']) == false  )
	 {
		 
		  // Get Image Dimension
		$fileinfo = @getimagesize($_FILES['companybanner']['tmp_name']);
		$width = $fileinfo[0];
		$height = $fileinfo[1];
	
		$simplephoto11=$_FILES['companybanner']['name'];
		 $ext = pathinfo($simplephoto11, PATHINFO_EXTENSION);
		
		$fileexplode1 = explode(".",$simplephoto11);
		$fileend1 = end($fileexplode1);

		$companybanner= date("YmdHis").rand(100,999).".".$fileend1;
		$path = "images";
		if($ext=='png' || $ext=='jpg' || $ext=='jpeg' || $ext=='gif' || $ext=='webp')
		{
		  move_uploaded_file( $_FILES['companybanner']['tmp_name'], $path ."/". $companybanner);
		  $flag=1;
		}
		else {
		  $_SESSION['msg1'] = "Only Upload Image";
			  $flag=0;
		}
		/* if ($width > "1920" || $height > "556") {
        
            $_SESSION['msg1']= "Image dimension should be within 1920X556";
			$flag=0;
        } */
	}
	
	}
	
		
	 $eve = "select * from res_company_master where companyname='$companyname'";
	$re = mysqli_query($conn, $eve);
	if(mysqli_num_rows($re) > 0)
	{
		$_SESSION['msg1'] = "company Already Inserted.";
		header('Location: company.php');
	}
	else if($flag==1)
	{	
		 $eve1 = "INSERT INTO res_company_master (companyname,branchmid,companyicon,companyfaxnumber,companymastercode,companywebsite,companylogo,companybanner,chstnumber,cpannumber,ccinnumber,cgstnumber) VALUES('$companyname','$branchmid','$companyicon','$companyfaxnumber','$companymastercode','$companywebsite','$companylogo','$companybanner','$chstnumber','$cpannumber','$ccinnumber','$cgstnumber')";
		
		if ($conn->query($eve1) === TRUE)

	{	
			$last_id = $conn->insert_id;
			$_SESSION['msg'] = "Record saved successfully";
		 $eve1 = "INSERT INTO res_contact_master (contactname, contactemail,contactnumber,cmobilenumber,contactdepartmnet,cposition,cremarks,caltenateemail,caddress1,caddress2,ccountry,cstate,ccity,cpostalcode,cmiddlename,csurname,cmsgappname,cmsgappid,cvideoappname,cvideoappid,ccompanyname,csubcategory,clinktype,clinkid) VALUES('$contactname','$contactemail','$contactnumber','$cmobilenumber','Owner','Owner','$cremarks','$caltenateemail','$caddress1','$caddress2','$ccountry','$cstate','$ccity','$cpostalcode','$cmiddlename','$csurname','$cmsgappname','$cmsgappid','$cvideoappname','$cvideoappid','$companyname','Direct','Company','$last_id')";
		
		
		$re1 = mysqli_query($conn, $eve1);
		
		/*  $delete_checkbox_array = $_POST['companynumbermid'];
		$n=0;
			foreach ($delete_checkbox_array as $one_by_one_sid) 
			{
				$companynumbermid=$one_by_one_sid;
					
				$cnvalue=$_POST['cnvalue'][$n];
				$n = $n + 1;
			
				
				if($companynumbermid==0)
				{
				}
				else
				{	
					$eve2="insert into res_companynumber_detail (companymid,companynumbermid,cnvalue) values($last_id,'$companynumbermid','$cnvalue')";
					$re2 = mysqli_query($conn, $eve2);
				}	
				
			} */
		  }
	
	else
			{
				$_SESSION['msg1'] = "Error";
			

	}
	}	
		header('Location: company.php');
	
?>