<?php 
require 'config_db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['member_id'] == ''  or $data['mode'] == '')
{
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");
}
else
{
    
    function generateRandomString($length) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    
    function generateRandomNumber($length) {
        $characters = '0123456789';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    $mode = strip_tags(mysqli_real_escape_string($con,$data['mode']));
    $member_id = strip_tags(mysqli_real_escape_string($con,$data['member_id']));
    $email = strip_tags(mysqli_real_escape_string($con,$data['owner_email']));
    $website = strip_tags(mysqli_real_escape_string($con,$data['website']));
    $company_type = strip_tags(mysqli_real_escape_string($con,$data['company_type']));
    $logo = $data['logo'];
    
    if($mode == 1) {
        
        $join_date = strip_tags(mysqli_real_escape_string($con,$data['join_date']));
        $refer_by = strip_tags(mysqli_real_escape_string($con,$data['refer_by']));
        $company_name = strip_tags(mysqli_real_escape_string($con,$data['company_name']));
        
        $company_category = strip_tags(mysqli_real_escape_string($con,$data['company_category']));
        $owner_name = strip_tags(mysqli_real_escape_string($con,$data['owner_name']));
        $mobile_no = strip_tags(mysqli_real_escape_string($con,$data['owner_mobile']));
        
        $pan = strip_tags(mysqli_real_escape_string($con,$data['company_pan']));
        $gst = strip_tags(mysqli_real_escape_string($con,$data['gst']));
        
        $state = strip_tags(mysqli_real_escape_string($con,$data['state']));
        $district = strip_tags(mysqli_real_escape_string($con,$data['district']));
        $city = strip_tags(mysqli_real_escape_string($con,$data['city']));
        $area = strip_tags(mysqli_real_escape_string($con,$data['area']));
        $pincode = strip_tags(mysqli_real_escape_string($con,$data['pin_code']));
        
        
        $pay_status = strip_tags(mysqli_real_escape_string($con,$data['pay_status']));
        $pay_date = strip_tags(mysqli_real_escape_string($con,$data['pay_date']));
        $pay_amount = strip_tags(mysqli_real_escape_string($con,$data['pay_amount']));
        $balance = strip_tags(mysqli_real_escape_string($con,$data['balance']));
        $total = strip_tags(mysqli_real_escape_string($con,$data['total']));
        $pay_mode = strip_tags(mysqli_real_escape_string($con,$data['pay_mode']));
        $transaction_id = strip_tags(mysqli_real_escape_string($con,$data['transaction_id']));
        $business_given_amount = strip_tags(mysqli_real_escape_string($con,$data['business_given_amount']));
        $business_received_amount = strip_tags(mysqli_real_escape_string($con,$data['business_received_amount']));
        
        $logo = $data['logo'];
        
        if($logo != '' || $logo != null) {
            $image_base64 = base64_decode($logo);
            header('Content-Type: bitmap; charset=utf-8');
            $image_file_name = generateRandomString(6). '.png';
            $uploadfile = $file_upload_url   . "/" . $image_file_name;
            
            $accesspath = $file_upload_extension_url. "/";
            $savefile = $accesspath . $image_file_name;
    
            file_put_contents($uploadfile, $image_base64);
        } else {
            $savefile = '';
        }
        
        $username = strtolower($owner_name);
        $username = str_replace(" ","_",$username);
        $username = $username.generateRandomNumber(4);
        $member = "NFC".$member_id;
        
        $sql = "INSERT INTO `app_users`(`user_name`,`member_id`,`name`,`logo`,`company`,`mobile`,`password`,`type`) 
                VALUES ('$username','$member_id','$owner_name','$savefile','$company_name','$mobile_no','$mobile_no','2')";
        $result = mysqli_query($con,$sql);
        
        if($result) {
            $sql1 = "INSERT INTO `app_users_details`(`member`, `member_id`, `company_logo`, `join_date`, `referred_by`, `company_name`, `company_type`, 
            `company_category`, `owner_name`, `owner_mobile`, `owner_email`, `company_email`, `company_pan`, `gst`, `state`, `district`, `city`, `area`, `pin_code`,
            `website`, `pay_status`, `pay_date`, `pay_amount`, `balance`, `total`, `pay_mode`, `transaction_id`, `business_given_amount`, `business_received_amount`) 
            VALUES ('$member','$member_id','$savefile','$join_date','$refer_by','$company_name','$company_type','$company_category','$owner_name','$mobile_no',
            '$email','$email','$pan','$gst','$state','$district','$city','$area','$pincode','$website','$pay_status',
            '$pay_date','$pay_amount','$balance','$total','$pay_mode','$transaction_id','$business_given_amount','$business_received_amount')";
            $result1 = mysqli_query($con,$sql1);
            if($result1) {
                $returnArr = array(
    			"ResponseCode" => "200",
    			"Result" => "true",
    			"ResponseMsg" => "New Member Added Successfully",
    			"data" => []);
            } else {
                $returnArr = array(
    			"ResponseCode" => "201",
    			"Result" => "false",
    			"ResponseMsg" => "New Member Added Failure",
    			"data" => []);
            }
        } else {
            $returnArr = array(
    			"ResponseCode" => "201",
    			"Result" => "false",
    			"ResponseMsg" => "New Member Added Failure",
    			"data" => []);
        }
    }
    else if($mode == 2) {
        if($logo != '' || $logo != null) {
            $image_base64 = base64_decode($logo);
            header('Content-Type: bitmap; charset=utf-8');
            $image_file_name = generateRandomString(6). '.png';
            $uploadfile = $file_upload_url   . "/" . $image_file_name;
            
            $accesspath = $file_upload_extension_url. "/";
            $savefile = $accesspath . $image_file_name;
    
            file_put_contents($uploadfile, $image_base64);
            
            $update_customer_sql = "update app_users_details set `owner_email`='$email',`company_email`='$email',`website`='$website',
                                  `company_type`='$company_type',`company_logo`='$savefile' where member_id = '$member_id'";
            $result = $con->query($update_customer_sql);
            if($result) {
                $update_customer_sql1 = "update app_users set `logo`='$savefile' where member_id = '$member_id'";
                $result1 = $con->query($update_customer_sql1);
                if($result1) {
                    $returnArr = array(
        			"ResponseCode" => "200",
        			"Result" => "true",
        			"ResponseMsg" => "Update Member details Successfully",
        			"logo" => $savefile,
        			"data" => []);
                } else {
                    $returnArr = array(
        			"ResponseCode" => "201",
        			"Result" => "false",
        			"ResponseMsg" => "Update Member details Failure",
        			"logo" => null,
        			"data" => []);
                }
            } else {
                $returnArr = array(
    			"ResponseCode" => "201",
    			"Result" => "false",
    			"ResponseMsg" => "Update Member details Failure",
    			"logo" => null,
    			"data" => []);
            }
        } else {
            $savefile = '';
            $update_customer_sql = "update app_users_details set `owner_email`='$email',`company_email`='$email',`website`='$website',
                                  `company_type`='$company_type' where member_id = '$member_id'";
            $result = $con->query($update_customer_sql);
            $cy = $con->query("select * from app_users_details where member_id = '$member_id' and status = '1'");
            if ($row = $cy->fetch_assoc()) {
        		$savefile = $row['company_logo'];
            }
            
            if($result) {
                
                $returnArr = array(
    			"ResponseCode" => "200",
    			"Result" => "true",
    			"ResponseMsg" => "Update Member details Successfully",
    			"logo" => $savefile,
    			"data" => []);
            } else {
                $returnArr = array(
    			"ResponseCode" => "201",
    			"Result" => "false",
    			"ResponseMsg" => "Update Member details Failure",
    			"logo" => null,
    			"data" => []);
            }
        }
        
        
    }
    
   
}
echo json_encode($returnArr);