<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type:application/json");
header("Access-Control-Allow-Origin-Methods:POST");
header("Access-Control-Allow-Headers:Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Origin-Methods,Authorization,X-Requested-With");

require_once("../conn.php");


$data = json_decode(file_get_contents("php://input"));


$data1=array();
$seasonid=$data->seasonid;
$id=$data->id;
$userid=$data->userid;
$number_of_visits=0;
$daily_reports=0;

$sql1 = "Select * from salary_dates_and_months where id=$id limit 1";
$result1 = $conn->query($sql1);
 
 if ($result1->num_rows > 0) {
   // output data of each row
   while($row1 = $result1->fetch_assoc()) {

        $start=$row1['start_date'];
        $end=$row1['end_date'];
  
        $sql = "Select distinct monthly_salary_claims.id,month,start_date,end_date,salary,hectares,daily_reports,grower_visits,system_based_tasks,bike_maintenance,ctl_related,training_and_demo,username,claimyid  from monthly_salary_claims join users on users.id=monthly_salary_claims.claimyid  where start_date='$start'  and  end_date='$end' and sync=1 order by id desc ";
      $result = $conn->query($sql);
       
       if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
          // echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";


                $monthly_salary_claims=0;
                $claimyid=$row['claimyid'];
                $salary=0;
                $hectares=0;
                $daily_reports=0;
                $grower_visits=0;
                $system_based_tasks=0;
                $bike_maintenance=0;
                $ctl_related=0;
                $training_and_demo=0;
                $number_of_visits=0;
                $daily_reports=0;




               $sql45 = "select distinct visits.growerid,visits.created_at  from visits join growers on growers.id=visits.growerid where  visits.seasonid=$seasonid and visits.userid=$claimyid and visits.created_at between '$start' and '$end'  order by visits.created_at desc ";
              $result45 = $conn->query($sql45);
              $number_of_visits=$result45->num_rows;

              $sql45 = "select distinct visits.created_at  from visits join growers on growers.id=visits.growerid where  visits.seasonid=$seasonid and visits.userid=$claimyid and visits.created_at between '$start' and '$end'  order by visits.created_at desc ";
              $result45 = $conn->query($sql45);
              $daily_reports=$result45->num_rows;
                 
              
              $sql12 = "Select distinct month,start_date,end_date,days_worked,hectares,daily_reports,grower_visits,system_based_tasks,bike_maintenance,ctl_related,training_and_demo,username from monthly_salary_claims_performance join users on users.id=monthly_salary_claims_performance.claimyid  where start_date='$start'  and  end_date='$end' and claimyid=$claimyid order by monthly_salary_claims_performance.id desc ";
              $result12 = $conn->query($sql12);
               if ($result12->num_rows > 0) {
                 // output data of each row
                 while($row12 = $result12->fetch_assoc()) {

                    $monthly_salary_claims=$row12["month"];
                    $salary=$row12["days_worked"];
                    $hectares=$row12["hectares"];
                    $daily_reports=$row12["daily_reports"];
                    $grower_visits=$row12["grower_visits"];
                    $system_based_tasks=$row12["system_based_tasks"];
                    $bike_maintenance=$row12["bike_maintenance"];
                    $ctl_related=$row12["ctl_related"];
                    $training_and_demo=$row12["training_and_demo"];

                 }
               }


             $temp=array("month"=>$row["month"],"start_date"=>$row["start_date"],"end_date"=>$row["end_date"],"salary"=>$row["salary"],"hectares"=>$row["hectares"],"daily_reports"=>$row["daily_reports"],"grower_visits"=>$row["grower_visits"],"system_based_tasks"=>$row["system_based_tasks"],"bike_maintenance"=>$row["bike_maintenance"],"ctl_related"=>$row["ctl_related"]
        ,"training_and_demo"=>$row["training_and_demo"],"id"=>$row["id"],"username"=>$row["username"],
          "days_worked"=>$salary,"hectares_performance"=>$hectares,"daily_reports_performance"=>$daily_reports,"grower_visits_performance"=>$grower_visits,"system_based_tasks_performance"=>$system_based_tasks,"bike_maintenance_performance"=>$bike_maintenance,"ctl_related_performance"=>$ctl_related
        ,"training_and_demo_performance"=>$training_and_demo,"number_of_visits"=>$number_of_visits,"daily_reports_server"=>$daily_reports

          );

            array_push($data1,$temp);
         
          
             }


          }



 }

 }





 echo json_encode($data1); 

?>