<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);

// $ex1 = mysqli_real_escape_string($con,$data['example']);

$si1 = $data['shop_id'];
$sc1 = $data['shop_code'];
$sh1 = $data['shop_name'];
$ow1 = $data['owner'];
$mo1 = $data['mobile'];
$em1 = $data['email'];
$ma1 = $data['manager_name'];
$ma2 = $data['manager_mobile'];

$bu1 = $data['business_type'];
$bu2 = $data['business_category'];
$ad1 = $data['address1'];
$ad2 = $data['address2'];
$ci1 = $data['city'];
$pi1 = $data['pin_code'];
$la1 = $data['latitude'];
$lo1 = $data['longitude'];

$gs1 = $data['gst_no'];
$fs1 = $data['fssai_no'];
$dr1 = $data['drug_no'];
$ot1 = $data['others'];

$ac1 = $data['account_holder_name'];
$ac2 = $data['account_no'];
$if1 = $data['ifsc_code'];
$ba1 = $data['bank_name'];
$br1 = $data['branch_name'];

$de1 = $data['subscription'];
$st2 = $data['status'];

$date = date('Y-m-d H:i:s');


if($si1 == '' or $sh1 == '' or $sc1 == '' or $ow1 == '' or $mo1 == '' or $em1 == '' or $ma1 == '' or $ma2 == '' or $bu1 == '' or $bu2 == '' or $ad1 == '' or $ad2 == '' 
    or $ci1 == '' or $pi1 == '' or $ac1 == '' or $ac2 == '' or $if1 == '' or $ba1 == '' or $br1 == '')
{
	$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something went wrong! Try again!");
}
else 
{
	$count = $con->query("select * from shop_details where shop_id ='$si1' and shop_code = '$sc1' and owner_mobile = '$mo1' and status = '1'")->num_rows;
	if($count == 0)
	{
	    if($bu2 == "Food & Drinks" || $bu2 == "Grocery")
	    {
	        $bu2 = "food";
	        $save_mer = $con->query("insert into shop_details(`shop_id`,`shop_code`,`shop_name`,`owner`,`owner_mobile`,`email`,`manager`,`manager_mobile`,`busi_type`,`busi_category`,
	        `address1`,`address2`,`city`,`pincode`,`latitude`,`longitude`,`gst_no`,`doc_type`,`doc_no`,`ifsc_code`,`bank_account_no`,`account_holder_name`,`bank_name`,`branch_name`,
	        `subscription`,`status`,`date_created`)
	        	values('$si1','$sc1','$sh1','$ow1','$mo1','$em1','$ma1','$ma2','$bu1','$bu2','$ad1','$ad2','$ci1','$pi1','$la1','$lo1','$gs1','FSSAI Certificate','$fs1','$if1','$ac2',
	        	'$ac1','$ba1','$br1','$de1','$st2','$date')");
	    }
	    else if($bu2 == "Medicines")
	    {
	        $save_mer = $con->query("insert into shop_details(`shop_id`,`shop_code`,`shop_name`,`owner`,`owner_mobile`,`email`,`manager`,`manager_mobile`,`busi_type`,`busi_category`,
	        `address1`,`address2`,`city`,`pincode`,`latitude`,`longitude`,`gst_no`,`doc_type`,`drug_no`,`ifsc_code`,`bank_account_no`,`account_holder_name`,`bank_name`,`branch_name`,
	        `subscription`,`status`,`date_created`)
	        	values('$si1','$sc1','$sh1','$ow1','$mo1','$em1','$ma1','$ma2','$bu1','$bu2','$ad1','$ad2','$ci1','$pi1','$la1','$lo1','$gs1','DRUG Certificate','$dr1','$if1','$ac2',
	        	'$ac1','$ba1','$br1','$de1','$st2','$date')");
	    }
	    else if($bu2 == "Materials" || $bu2 == "Gift Shop" || $bu2 == "Pet Supplies")
	    {
	        $save_mer = $con->query("insert into shop_details(`shop_id`,`shop_code`,`shop_name`,`owner`,`owner_mobile`,`email`,`manager`,`manager_mobile`,`busi_type`,`busi_category`,
	        `address1`,`address2`,`city`,`pincode`,`latitude`,`longitude`,`gst_no`,`doc_type`,`others`,`ifsc_code`,`bank_account_no`,`account_holder_name`,`bank_name`,`branch_name`,
	        `subscription`,`status`,`date_created`)
	        	values('$si1','$sc1','$sh1','$ow1','$mo1','$em1','$ma1','$ma2','$bu1','$bu2','$ad1','$ad2','$ci1','$pi1','$la1','$lo1','$gs1','Other Certificate','$ot1','$if1','$ac2',
	        	'$ac1','$ba1','$br1','$de1','$st2','$date')");
	    }
	    else
	    {
	        $save_mer = false;
	    }
	   
	   if($save_mer)
	   {
	       $hash_pass = md5($mo1);
	       $save_mer_de = $con->query("insert into shop_settings(`shop_id`,`shop_code`,`mobile_no`,`user_name`,`password`,`hash_password`,`min_order_value`,`tax_value`,`shop_status`,`shop_message`,
	       `shop_time`,`start_time`,`end_time`)values ('$si1','$sc1','$mo1','$ow1','$mo1','$hash_pass','99','5','1','','Default','10:00','22:00')");
	   }
	   else
	   {
	       $save_mer_de = false;
	   }
	   
	   $chek = $con->query("SELECT 
                shop_details.shop_name,
                shop_settings.shop_id,
                shop_settings.shop_code
                FROM shop_settings
                INNER JOIN shop_details ON
                shop_settings.shop_id = shop_details.shop_id
                WHERE shop_settings.mobile_no = '$mo1' and shop_settings.password='$mo1'");
                
        if($chek->num_rows !=0)
        {
            $c = $chek->fetch_assoc();
        }
        else
        {
            $c = [];
        }
        
        if($save_mer_de)
            $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Merchant Added","merchant"=>$c);
        else
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Some Error Occured!");
	}
	else 
	{
		$returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Retry!");
	}
}
echo json_encode($returnArr);
mysqli_close($con);
?> 