<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['mobile'] == '' && $data['deli_code'] == '')
{
 $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{
    function generateRandomString($name,$mobile) 
    {
        $characters = strtoupper($name).$mobile;
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < 8; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
    
    $mobile =  $data['mobile'];
    $delicode =  $data['deli_code'];
    $stage =  $data['stage'];
    if($stage == 1)
    {
        $name =  $data['name'];
        $lastname =  $data['last_name'];
        $email =  $data['email'];
        $selfie =  $data['selfie'];
        $del_code = str_replace('R', '', $delicode);
        
        $che = $con->query("select * from assist_settings where mobile_no ='$mobile'")->num_rows;
        if($che == 0)
        {
            $d_code = strtolower($del_code);
            if (!file_exists($file_assist_upload_url . $d_code)) {
                mkdir($file_assist_upload_url . "/" . $d_code, 0777, true);
                $upload_url = $file_assist_upload_url. "/" .$d_code;
            }
            else
            {
                $upload_url = $file_assist_upload_url. "/" .$d_code;
            }
        
            $selfie_image=base64_decode($selfie);
            header('Content-Type: bitmap; charset=utf-8');
            $image_file_name = uniqid(). '.png';
            
            $savefile = $file_assist_upload_extension_url . "/" . $d_code. "/" . $image_file_name;
            
            $uploadfile = $upload_url  . "/" . $image_file_name;
            file_put_contents($uploadfile, $selfie_image);
            
            $q1 = $con->query("INSERT INTO `assist_settings`(`deli_code`, `mobile_no`, `assigned_orders`, `accepted_orders`, `rejected_orders`, `my_referrals`, `success_referrals`, 
                    `deli_reviews`, `vaccinate_status`, `status`, `admin_status`) VALUES ('$delicode','$mobile','0','0','0','0','0','0','0','1','0')");
                    
            $result = $con->query("INSERT into assist_details(mobile_no,deli_code,name,last_name,email,selfie_photo,stage_status) VALUES ('$mobile','$delicode','$name','$lastname','$email','$savefile','2')");
            if($result && $q1)
            $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Success");
        }
        else
        {
            $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Mobile Already Registered");
        }
    }
    else if($stage == 2)
    {
        $address1 =  $data['address1'];
        $address2 =  $data['address2'];
        $city =  $data['city'];
        $pincode =  $data['pincode'];

        $sql = "update assist_details set address1='".$address1."', address2='".$address2."', city='".$city."', pincode='".$pincode."',stage_status='3' where mobile_no='".$mobile."' and deli_code='".$delicode."'";
        $result = $con->query($sql);
        if($result)
        $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>$sql);
            
    }
    else if($stage == 3)
    {
        $pan_no =  $data['pan_no'];
        $aadhar_no =  $data['aadhar_no'];
        $dl_no =  $data['dl_no'];
        $vehicle_no =  $data['vehicle_no'];
        $vehicle_model =  $data['vehicle_model'];
        $pan =  $data['pan_image'];
        $dl =  $data['dl_image'];
        
        $del_code = str_replace('R', '', $delicode);
        
        $d_code = strtolower($del_code);
        if (!file_exists($file_assist_upload_url . $d_code)) {
            mkdir($file_assist_upload_url . "/" . $d_code, 0777, true);
            $upload_url = $file_assist_upload_url. "/" .$d_code;
        }
        else
        {
            $upload_url = $file_assist_upload_url. "/" .$d_code;
        }
        
        header('Content-Type: bitmap; charset=utf-8');
        
        if($pan != null)
        {
            $pan_image=base64_decode($pan);
            $image_file_name_pan = uniqid(). '.png';
            $savefile_pan = $file_assist_upload_extension_url . "/" . $d_code. "/" . $image_file_name_pan;
            $uploadfile_pan = $upload_url  . "/" . $image_file_name_pan;
            file_put_contents($uploadfile_pan, $pan_image);
        }
        if($dl != null)
        {
            $dl_image=base64_decode($dl);
            $image_file_name_dl = uniqid(). '.png';
            $savefile_dl = $file_assist_upload_extension_url . "/" . $d_code. "/" . $image_file_name_dl;
            $uploadfile_dl = $upload_url  . "/" . $image_file_name_dl;
            file_put_contents($uploadfile_dl, $dl_image);
        }
        
        $result = $con->query("update assist_details set pan_card='".$pan_no."',aadhar_card='".$aadhar_no."',driving_license='".$dl_no."',
        vehicle_no='".$vehicle_no."',vehicle_model='".$vehicle_model."',pan_image='".$savefile_pan."',dl_image='".$savefile_dl."',stage_status='4' where mobile_no='".$mobile."' and deli_code='".$delicode."'");
        if($result)
        $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Success");
            
    }
    else if($stage == 4)
    {
        $accountname =  $data['account_name'];
        $accountno =  $data['account_no'];
        $ifsccode =  $data['ifsc_code'];
        $bankname =  $data['bank_name'];
        $branchname =  $data['branch_name'];
        $bankproof =  $data['bank_proof'];
        
        $del_code = str_replace('R', '', $delicode);
        
        $d_code = strtolower($del_code);
        if (!file_exists($file_assist_upload_url . $d_code)) {
            mkdir($file_assist_upload_url . "/" . $d_code, 0777, true);
            $upload_url = $file_assist_upload_url. "/" .$d_code;
        }
        else
        {
            $upload_url = $file_assist_upload_url. "/" .$d_code;
        }
    
        if($dl != null)
        {
            $bank_image=base64_decode($bankproof);
            header('Content-Type: bitmap; charset=utf-8');
            $image_file_name = uniqid(). '.png';
            $savefile = $file_assist_upload_extension_url . "/" . $d_code. "/" . $image_file_name;
            $uploadfile = $upload_url  . "/" . $image_file_name;
            file_put_contents($uploadfile, $bank_image);
        }

        $result = $con->query("update assist_details set account_holder_name='".$accountname."',bank_account_no='".$accountno."',ifsc_code='".$ifsccode."',bank_name='".$bankname."',branch_name='".$branchname."',
        bank_proof='".$savefile."',stage_status='5' where mobile_no='".$mobile."' and deli_code='".$delicode."'");
        if($result)
        $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Success");
            
    }
    else if($stage == 5)
    {
        $workhours =  $data['work_hours'];
        $starttime =  $data['start_time'];
        $endtime =  $data['end_time'];
        $shirtsize =  $data['shirt_size'];
        $feesamount =  $data['fees_amount'];
        $dueamount =  $data['due_amount'];
        
        $ch = $con->query("select * from assist_details where mobile_no ='".$mobile."'");
        if($row = $ch->fetch_assoc())
        {
            if($row['name'] != null)
            {
                $aname = $row['name'];
            }
        }
        
        $invite_code = generateRandomString($aname,$mobile);

        $result = $con->query("update assist_details set work_hours='".$workhours."', start_time='".$starttime."', end_time='".$endtime."', shirt_size='".$shirtsize."', fees_amount='".$feesamount."',
        due_amount='".$dueamount."',fee_status = 'Paid',stage_status='10',invite_code='".$invite_code."' where mobile_no='".$mobile."' and deli_code='".$delicode."'");
        if($result)
        $returnArr = array("ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Success");
            
    }

    echo json_encode($returnArr);

}

?>