<?php

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_config.php';
require_once __DIR__ . '/image_upload.php';


if (isset($_POST['JSON'])) {
    
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());
    $img=new ImageUpload();

    $json=json_decode($_POST['JSON'],true);
    $assign_id=$json['assign'];
    $lat=$json['lat'];
    $lon=$json['lon'];
    $location=addslashes($json['location']);
    $cat=$json['cat'];
    $subcat=$json['subcat'];
    $facility=$json['facility'];
    $datetime=$json['datetime'];
    $list=$json['list'];
    $emailid=$json['email'];
    
    
    $query="SELECT s.scheduler_id,s.companyname,s.adminemailid,customertype,s.customername,c.questionmode,s.customertype,c.ID,c.typename,c.type,brandid,productid,modelid,serialid FROM scheduleassigndate s INNER JOIN combination c ON  s.combinationid=c.ID WHERE s.assignschedule_id='$assign_id'";
    
    $result = mysqli_query($con,$query);
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        $companyname=$row["companyname"];
        $adminemailid=$row["adminemailid"];
        $schedule_id=$row["scheduler_id"];
        $customername=$row["customername"];
        $questionmode=$row["questionmode"];
        $questiontype=$row["customertype"];
        $combinationname=$row["ID"];
        $typename=$row["typename"];
        $type=$row["type"];
        $brandid=$row["brandid"];
        $productid=$row["productid"];
        $modelid=$row["modelid"];
        $serialid=$row["serialid"];
        $unique_id="";
        }
    }
    
    $answerArray=array();
    
    
    foreach($list as $obj){
        $question=$obj['question'];
        $answer1=$obj['answer'];
        $issue=$obj['issue'];
        $number=$obj['number'];
        $qtype=$obj['type'];
        $subcat=$obj['subcat'];
        
        
        
        if($qtype=="signature" || $qtype=="image"){
            $image=$img->uploadImage2("$assign_id$cat$subcat$number",$answer1);
            $insertvalues="('$companyname','$adminemailid','$schedule_id','$assign_id','$customername','$facility','$emailid','$number','$cat','$subcat','$qtype','$question','','$image','$datetime','$lat','$lon','$location','$issue','$questionmode','$questiontype','$combinationname','$typename','$type','$unique_id','$brandid','$productid','$modelid','$serialid')";
            array_push($answerArray,$insertvalues);
        }else{
            $answer = addslashes($answer1);
            $insertvalues="('$companyname','$adminemailid','$schedule_id','$assign_id','$customername','$facility','$emailid','$number','$cat','$subcat','$qtype','$question','$answer','','$datetime','$lat','$lon','$location','$issue','$questionmode','$questiontype','$combinationname','$typename','$type','$unique_id','$brandid','$productid','$modelid','$serialid')";
            array_push($answerArray,$insertvalues);
        }
        
        
        
    }
   
   if(empty($answerArray)){
        $response["s"] = 1;
   }else{
    
    $insert_query="INSERT IGNORE INTO answerstable(companyname,adminemailid,schedule_id,assignschedule_id,customername,atmid,emailid,questionnumber,category,subcategory,typeofquestion,question,answer,imagepath,dateandtime,latitude,longitude,location,issue,questionmode,questiontype,combinationname,typename,type,unique_id,brandid,productid,modelid,serialid) VALUES".implode(",",$answerArray);
   $result = mysqli_query($con,$insert_query);

        if ($result) {
            $response["s"] = 1;
        } else {
            $response["s"] = 2;
        }
   }
    
    mysqli_close($con);
 
}
     // echoing JSON response
    echo json_encode($response);

?>