<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
    
    function generateRandomString($length) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
    }
    
    $filesource = mysqli_real_escape_string($con, trim($request->form_value->fileSource));
    $closetime = mysqli_real_escape_string($con, trim($request->form_value->close_time));
    $opentime = mysqli_real_escape_string($con, trim($request->form_value->open_time));
    $add1 = mysqli_real_escape_string($con, trim($request->form_value->shop_address1));
    $add2 = mysqli_real_escape_string($con, trim($request->form_value->shop_address2));
    $add3 = mysqli_real_escape_string($con, trim($request->form_value->shop_address3));
    $binfo = mysqli_real_escape_string($con, trim($request->form_value->shop_business_info));
    $btype = mysqli_real_escape_string($con, trim($request->form_value->shop_business_type));
    $mainshop = mysqli_real_escape_string($con, trim($request->form_value->main_shop));
    $subshop = mysqli_real_escape_string($con, trim($request->form_value->sub_shop));
    
    $email = mysqli_real_escape_string($con, trim($request->form_value->shop_email));
    $gst = mysqli_real_escape_string($con, trim($request->form_value->shop_gst));
    $latitude = mysqli_real_escape_string($con, trim($request->form_value->shop_latitude));
    $longitude = mysqli_real_escape_string($con, trim($request->form_value->shop_longitude));
    $shopname = mysqli_real_escape_string($con, trim($request->form_value->shop_name));
    $manager = mysqli_real_escape_string($con, trim($request->form_value->shop_manager));
    $managermobile = mysqli_real_escape_string($con, trim($request->form_value->shop_manager_mobile));
    $owner = mysqli_real_escape_string($con, trim($request->form_value->shop_owner));
    $ownermobile = mysqli_real_escape_string($con, trim($request->form_value->shop_owner_mobile));
    $shoptime = mysqli_real_escape_string($con, trim($request->form_value->shop_time));
    $pincode = mysqli_real_escape_string($con, trim($request->form_value->shop_pincode));
    $doctype = mysqli_real_escape_string($con, trim($request->form_value->shop_doctype));
    $docno = mysqli_real_escape_string($con, trim($request->form_value->shop_docno));
    
    $city = mysqli_real_escape_string($con, trim($request->form_value->shop_city));
    $bcat = mysqli_real_escape_string($con, trim($request->form_value->shop_business_category));
    $shop_id = rand(1000000,9999999);
    $shop_code = generateRandomString(10);
    $type = 'img';
    
    if($shopname == "" ||  $owner == "" || $ownermobile == "" || $manager == "" || $managermobile == "" || $email == "" || $bcat == "" || $binfo == null ||  $btype == "")
    {
        $rows = array('status'=>false,'message' => 'Fields Missing');
        echo json_encode($rows);
    }
    else
    {
        if($shoptime == 'Default')
        {
            $opentime  = '10:00';
            $closetime = '22:00';
        }
        
        if($mainshop == 'Yes')
        {
            $subshop = '';
            $mainshop = 0;
        }
        else
        {
            if($subshop != "")
            {
                $sc_sql = "SELECT shop_id FROM shop_details WHERE shop_name = '$subshop'";
                $sc_result = mysqli_query($con,$sc_sql);
                if($rows = $sc_result->fetch_assoc())
                {
                    $subshop = $rows['shop_id'];
                }
            }
            $mainshop = 1;
        }
        
        if($filesource != '' || $filesource != null)
        {
    
            // $url = "/home/d5awyvzt87ne/public_html/shop.iconxtechnologies.in";
            $url = $file_upload_url;
            
            $city_path = strtolower($city);
            if (!file_exists($url . $city_path)) {
                mkdir($url . "/" . $city_path, 0777, true);
                $city_url = $url. "/" .$city_path;
            }
            else
            {
                $city_url = $url. "/" .$city_path;
            }
            
            $bcat_path = strtolower($bcat);
            if (!file_exists($city_url . $bcat_path)) {
                mkdir($city_url . "/" . $bcat_path, 0777, true);
                $cat_url = $city_url. "/" .$bcat_path;
            }
            else
            {
                $cat_url = $city_url. "/" .$bcat_path;
            }
            
            $shop_path = strtolower($shop_id);
            if (!file_exists($cat_url . $shop_path)) {
                mkdir($cat_url . "/" . $shop_path, 0777, true);
                $type_url = $cat_url. "/" .$shop_path;
            }
            else
            {
                $type_url = $cat_url. "/" .$shop_path;
            }
            
            $type_path = strtolower($type);
            if (!file_exists($type_url . $type_path)) {
                mkdir($type_url . "/" . $type_path, 0777, true);
                $upload_url = $type_url. "/" .$type_path;
            }
            else
            {
                $upload_url = $type_url. "/" .$type_path;
            }
            
            $image_parts = explode(";base64,", $filesource);
            $image_type_aux = explode("image/", $image_parts[0]);
            $image_type = $image_type_aux[1];
            $image_base64 = base64_decode($image_parts[1]);
            $image_file_name = uniqid(). '.png';
            $uploadfile = $upload_url  . "/" . $image_file_name;
            
            $accesspath = $file_upload_extension_url. "/" .$city_path. "/" .$bcat_path. "/" .$shop_path. "/";
            $savefile = $accesspath . $type_path. "/" . $image_file_name;

            file_put_contents($uploadfile, $image_base64);
    
        }
        else
        {
            $savefile = '';
        }
        
        $city = strtolower($city);
        $bcat = strtolower($bcat);
        
        $d=strtotime("+1 Months");
        $expiry_date = date("Y-m-d h:i:sa", $d);
                
        $sql = "INSERT INTO `shop_details`(`shop_id`,`shop_code`,`shop_name`, `shop_logo`, `main_shop`, `sub_shop`, `owner`, `owner_mobile`, `manager`, `manager_mobile`, 
        `email`, `address1`, `address2`, `area`, `city`, `pincode`, `busi_category`, `busi_type`, `busi_info`, `doc_type`, `doc_no`, `gst_no`, `latitude`, `longitude`,
        `expiry_date`) VALUES 
        ('$shop_id','$shop_code', '$shopname', '$savefile','$mainshop','$subshop','$owner','$ownermobile','$manager','$managermobile',
        '$email','$add1','$add2','$add3','$city','$pincode','$bcat','$btype','$binfo','$doctype','$docno','$gst','$latitude','$longitude',
        '$expiry_date')";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $hash_password = md5($ownermobile);
            $sql1 = "INSERT INTO `shop_settings`(`one_key`, `one_hash`, `shop_id`, `shop_code`, `mobile_no`, `user_name`, `password`, `hash_password`,
            `min_order_value`, `free_deli_value`, `tax_value`, `shop_status`, `shop_message`, `shop_time`, `start_time`, `end_time`, `privacy_policy`, 
            `about_us`, `contact_us`, `terms`, `access_path`) VALUES 
            ('','','$shop_id','$shop_code','$ownermobile','$owner','$ownermobile','$hash_password','100','299','5','1','','$shoptime','$opentime','$closetime','','','','','$accesspath')";
            $result1 = mysqli_query($con,$sql1);
            if($result1)
            {
                $rows = array('status'=>true,'message' => 'Shop Added');
                echo json_encode($rows);
            }
            else
            {
                $rows = array('status'=>false,'message' => 'Something went wrong');
                echo json_encode($rows);
            }
        }
        else
        {
            $rows = array('status'=>false,'message' => 'Something went wrong');
            echo json_encode($rows);
        }
    
    }

}
else
{
    $rows = array('status'=>false,'message' => 'Something Error');
    echo json_encode($rows);
}

?>
