<?php    
  session_start();
  header('Content-Type: text/html; charset=utf-8');
  //echo chr(239).chr(187).chr(191);;
  $role = $_SESSION['role'];
  $id_user = $_SESSION['id'];
  
  $dbset = json_decode($_SESSION['settingsApp'],true);
  $db = new mysqli ($dbset['DB']['DB_HOST'],$dbset['DB']['DB_USER'],$dbset['DB']['DB_PASS'],$dbset['DB']['DB_NAME']);

  //$db = new mysqli ($DB_HOST,$DB_USER,$DB_PASS,$DB_NAME);
  $db->query("SET CHARSET SET utf8");
  $db->query("SET NAMES 'utf8'");
  if (mysqli_connect_errno()) {
    echo "Error connecting to DB";
    exit;
  }
  //-------------------------------------show--------------------------------------------------		

  if ($_GET['func']=='show')
  {
    $dateVisit = $_GET['date'];
    $id_name = $_GET['id_name'];
    
    $i=0;
    $query = "SELECT ms_visit.id, date, description, ms_trade_point.name, ms_trade_point.address, id_user, date_start, id_client, close FROM ms_visit LEFT JOIN ms_trade_point ON ms_visit.id_trade_point=ms_trade_point.id WHERE id_user=".$id_name." AND date BETWEEN '".$dateVisit." 00:00:00' AND '".$dateVisit." 23:59:59' ORDER BY date ASC";
    //echo $query;
    if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($id, $date, $description, $trade_point, $address, $id_user, $date_start, $id_client, $close);
      while ($stmt->fetch()) {
        
		if($close == 1){
			$close = '<img src="app/images/Circle_Green.png" width="16" height="16" />';
		}else{
			$close = '<img src="app/images/Circle_Red.png" width="16" height="16" />';
		}
		
		$Model[$i] = 
        array('id'=>$id,
              'date'=>$date,
              'description'=>$description,
              'trade_point'=>$trade_point,
              'address'=>$address,
              'id_user'=>$id_user,
              'date_start'=>$date_start,
              'id_client'=>$id_client,
			  'close'=>$close
        );
        $i++;
      } 
      $stmt->close();
      $db->close();
    }
    echo'{rows:'.json_encode($Model).'}';   
  }
  
  
  //---------------- show to update-------------------------------
  if ($_POST['func']=='edit')
  {
    $idVisit = $_POST['id'];

    $query = "SELECT id, date, description, id_trade_point, id_user FROM ms_visit WHERE id=".$idVisit;
    if ($stmt = $db->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($id,$date,$description,$id_trade_point,$id_user);
      while ($stmt->fetch()){
        $data_edit = $id.'/t'.$date.'/t'.$description.'/t'.$id_trade_point.'/t'.$id_user.'/t';
      } 
      $stmt->close();
      $db->close();
    }
    echo $data_edit;
  }

//----------------------------- create and update--------------------------------------------------

  if ($_POST['func']=='update')
  {
    $id = $_POST['id'];
    $date = $_POST['date'];
    $description = $_POST['description'];
    $id_trade_point = $_POST['id_trade_point'];
    $id_user = $_POST['id_user'];
    
    //if ($id == NaN)                             // add new rows
	if(!$id || $id=="")
    {
      
      $query = "INSERT INTO ms_visit (date, description, id_trade_point, id_user) VALUES ('".$date."', '".$description."', ".$id_trade_point.", ".$id_user.")";
	  echo $query;
	  $stmt = $db->prepare($query);
      $stmt->execute();    
    }
    else                                        // update rows
    {
      $query = "UPDATE ms_visit SET date='".$date."', description='".$description."', id_trade_point=".$id_trade_point.", id_user=".$id_user." WHERE id=".$id;
      echo $query;
	  $stmt = $db->prepare($query);
      $stmt->execute();
    }
    $stmt->close();
    $db->close();       
  }  
  
  
  //---------------------------- delete ----------------------------------------

  $i=0;
  if ($_POST['func']=='delete')
  {
    $id = $_POST['id'];

    $query = "DELETE FROM ms_visit WHERE id=".$id;
    $stmt = $db->prepare($query);
    $stmt->execute();
    
    /*$query = "DELETE FROM ms_task WHERE id_visit=".$id;
    $stmt = $db->prepare($query);
    $stmt->execute();
	*/
    $stmt->close();
    $db->close();
  }


  

if ($_POST['func']=='addVisitCycle')
{
	$dateTasksFrom = $_POST['dateTasksFrom'];
	$dateTasksTo = $_POST['dateTasksTo'];
	$timeTasks = $_POST['timeTasks'];
	$clientTasks = $_POST['clientTasks'];
	$infoTasks = $_POST['infoTasks'];
	$id_user = $_POST['id_user'];
	$periodDaysTasks = $_POST['periodDaysTasks'];
	
		$i=0;
		while ($i<count($clientTasks))
		{
			
			$dateTasksFrom2 = $dateTasksFrom;
			while (strtotime($dateTasksFrom2) <= strtotime($dateTasksTo))
			{
			
			$query = "INSERT INTO ms_visit (date, description, id_trade_point, id_user) VALUES ('".$dateTasksFrom2."', '".$infoTasks."', ".$clientTasks[$i].", ".$id_user.")";
			//echo $query;
			$stmt = $db->prepare($query);
			$stmt->execute();
			$stmt->close();
			
			
			
			//$dateTasksFrom2 = date("Y-m-d", strtotime($dateTasksFrom2) + 86400);
			$dateTasksFrom2 = date("Y-m-d", strtotime($dateTasksFrom2) + (86400*$periodDaysTasks));
			}
		
		$i++;
		}
		
	echo '{"success":true,"message":""}';
	$db->close();
	exit;
}
?>