<?php
    session_start();
    header('Content-Type: text/html; charset=utf-8');
    //echo chr(239).chr(187).chr(191);
    $user_id = $_SESSION['user_id'];
    $role = $_SESSION['role'];    
        //include "db.php";
        //include "../settings.php";
		//include ('mysql.php');
        
		$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')
{
  $i=0;
  $query = "SELECT (SELECT COUNT(id) FROM ms_job) AS total, ms_job.id, ms_job.date, ms_job.job, table_user_created.first_name AS first_name_created, table_user_created.last_name AS last_name_created, table_user_created.middle_name AS middle_name_created, table_user_performer.first_name AS first_name_performer, table_user_performer.last_name AS last_name_performer, table_user_performer.middle_name AS middle_name_performer, ms_job.state FROM ms_job LEFT JOIN (SELECT id, first_name, last_name, middle_name FROM ms_user) AS table_user_created ON ms_job.id_user_created=table_user_created.id LEFT JOIN (SELECT id, first_name, last_name, middle_name FROM ms_user) AS table_user_performer ON ms_job.id_user_performer=table_user_performer.id";
  if ($stmt = $db->prepare($query)){
    $stmt->execute();
    $stmt->bind_result($total, $id, $date, $job, $first_name_created, $last_name_created, $middle_name_created, $first_name_performer, $last_name_performer, $middle_name_performer, $state);
    while ($stmt->fetch()) {
      $StoreJob[$i]= 
      array(
        'id'=>$id,
        'date'=>$date,
        'job'=>$job,
        'name_created'=>$first_name_created.' '.$last_name_created.' '.$middle_name_created,
        'name_performer'=>$first_name_performer.' '.$last_name_performer.' '.$middle_name_performer,
        'state'=>$state
      );
      $i++;
    } 
    $stmt->close();
    $db->close();
  }
  echo '{"total":"'.$total.'","rows":'.json_encode($StoreJob).'}';
}
	
//-------------add--------------------------------------------------------------
           $i=0;
      /*  if ($_POST['func']=='users_adm_add')
        {
          $login=$_POST['login'];
		      $password=$_POST['password'];
          $role=$_POST['role'];
		       $firstName=$_POST['firstName'];
		       $lastName=$_POST['lastName'];
		       $middleName=$_POST['middleName'];
		       $position=$_POST['position'];
		       $bossID=$_POST['bossID'];
		       $email=$_POST['email'];
           $tel=$_POST['tel'];
		       $address=$_POST['address'];
           $comments=$_POST['comments'];
		       $client_id=$_POST['client_id'];
		   

           $query ="select max(id_user) from users";
           $stmt = $db->prepare($query);
           $stmt->execute();
           $stmt->bind_result($id);
            while ($stmt->fetch()) {
                $n_id=$id;
            }
            $n_id++;
            $query = "INSERT INTO users (id_user,bossId,login,password,firstName,lastName,middleName,position,email,tel,address,comments,role,client) VALUES (".$n_id.",'".$bossID."','".$login."','".md5($password)."','".$firstName."','".$lastName."','".$middleName."','".$position."','".$email."','".$tel."','".$address."','".$comments."',".$role.",".$client_id.")";
            $stmt = $db->prepare($query);
            $stmt->execute();       
            
			$i=0;
            $query = "SELECT users.id_user, users.login, users.firstName, users.lastName, roleNames.name_role FROM users INNER JOIN roleNames ON users.role = roleNames.id_role";
            if ($stmt = $db->prepare($query)){
            //$stmt->bind_param('i', $client_id);
            $stmt->execute();
            $stmt->bind_result($id,$login,$firstName,$lastName,$roleIndex);
            while ($stmt->fetch()) {
                  $storeJob [$i]= 
            array(
            'id_user'=>$id,
            'login'=>$login,
			      'firstName'=>$firstName,
			      'lastName'=>$lastName,
			      'name_role'=>$roleIndex
            );
            $i++;
            } 
            $stmt->close();
            $db->close();
        }

   echo'{rows:'.json_encode($storeJob).'}';
        }*/

//---------------- select to update------------------------------------------------------------------
if ($_POST['func']=='editJob')
{
  $id_job=$_POST['id_job'];

  $query = "SELECT id, id_user_created, date, job, id_user_performer, state FROM ms_job WHERE id=".$id_job;

  if ($stmt = $db->prepare($query)){
    $stmt->execute();
    $stmt->bind_result($id, $id_user_created, $date, $job, $id_user_performer, $state);

    while ($stmt->fetch()) {
      $data_edit_row .= $id.'/t'.$id_user_created.'/t'.$date.'/t'.$job.'/t'.$id_user_performer.'/t'.$state.'/n';
    } 
    $data_edit_row = substr($data_edit_row,0,strlen($data_edit_row)-2);
    $stmt->close();
    $db->close();
  }
  echo $data_edit_row;
}

 //---------------- get Boss Name------------------------------------------------------------------
  if ($_POST['func']=='getBossName')
  {//DebugBreak();
  $id_boss = $_POST['id_boss'];
    
  $query = "SELECT CONCAT_WS(' ',first_name, last_name, middle_name) AS bossName FROM ms_user WHERE id=".$id_boss;
  
  if ($stmt = $db->prepare($query)){
            $stmt->execute();
            $stmt->bind_result($bossName);
            
      while ($stmt->fetch()) {
            $data_edit_row .= $bossName.'/t/n';
            } 
      $data_edit_row = substr($data_edit_row,0,strlen($data_edit_row)-2);
            $stmt->close();
            $db->close();
        }
   echo $data_edit_row;
   }  
   
   
   //----------------create and update------------------------------------------------------------------
   $i=0;
   if ($_POST['func']=='CreateUpdateJob')
   {
     $id=$_POST['id'];
     $id_user_created=$_POST['id_user_created'];
     $date=$_POST['date'];
     $job=$_POST['job'];
     $id_user_performer=$_POST['id_user_performer'];
     $state=$_POST['state'];

     //DebugBreak();
     if ($id == NaN)                             // add new rows
     {
       $query = "SELECT MAX(id) FROM ms_job";
       $result = $db->query($query);
       //$stmt->execute();
       $row = $result->fetch_array(MYSQLI_NUM);
       $id = $row[0];
       $id++;
       $query = "INSERT INTO ms_job (id, id_user_created, date, job, id_user_performer, state) VALUES (".$id.",".$id_user_created.",'".$date."','".$job."',".$id_user_performer.", ".$state.")";
     }
     else                                        // update rows
     {
       $query = "UPDATE ms_job SET id_user_created=".$id_user_created.",date='".$date."',job='".$job."',id_user_performer=".$id_user_performer.", state=".$state." WHERE id=".$id;
     }

     $stmt = $db->prepare($query);
     $stmt->execute();
     $i++;       

     $i=0;
     $query = "SELECT (SELECT COUNT(id) FROM ms_job) AS total, ms_job.id, ms_job.date, ms_job.job, table_user_created.first_name AS first_name_created, table_user_created.last_name AS last_name_created, table_user_created.middle_name AS middle_name_created, table_user_performer.first_name AS first_name_performer, table_user_performer.last_name AS last_name_performer, table_user_performer.middle_name AS middle_name_performer, ms_job.state FROM ms_job LEFT JOIN (SELECT id, first_name, last_name, middle_name FROM ms_user) AS table_user_created ON ms_job.id_user_created=table_user_created.id LEFT JOIN (SELECT id, first_name, last_name, middle_name FROM ms_user) AS table_user_performer ON ms_job.id_user_performer=table_user_performer.id";
  if ($stmt = $db->prepare($query)){
    $stmt->execute();
    $stmt->bind_result($total, $id, $date, $job, $first_name_created, $last_name_created, $middle_name_created, $first_name_performer, $last_name_performer, $middle_name_performer, $state);
    while ($stmt->fetch()) {
      $StoreJob[$i]= 
      array(
        'id'=>$id,
        'date'=>$date,
        'job'=>$job,
        'name_created'=>$first_name_created.' '.$last_name_created.' '.$middle_name_created,
        'name_performer'=>$first_name_performer.' '.$last_name_performer.' '.$middle_name_performer,
        'state'=>$state
      );
      $i++;
    } 
    $stmt->close();
    $db->close();
  }
  echo '{"total":"'.$total.'","rows":'.json_encode($StoreJob).'}';
   }

//---------------------------------- delete ------------------------------------------------------------------
        $i=0;
		if ($_POST['func']=='delete')
        {
           $id=$_POST['id'];
           $query = "DELETE FROM ms_job WHERE id=".$id;
           $stmt = $db->prepare($query);
            $stmt->execute();       
               $i=0;
        $query = "SELECT (SELECT COUNT(id) FROM ms_job) AS total, ms_job.id, ms_job.date, ms_job.job, table_user_created.first_name AS first_name_created, table_user_created.last_name AS last_name_created, table_user_created.middle_name AS middle_name_created, table_user_performer.first_name AS first_name_performer, table_user_performer.last_name AS last_name_performer, table_user_performer.middle_name AS middle_name_performer, ms_job.state FROM ms_job LEFT JOIN (SELECT id, first_name, last_name, middle_name FROM ms_user) AS table_user_created ON ms_job.id_user_created=table_user_created.id LEFT JOIN (SELECT id, first_name, last_name, middle_name FROM ms_user) AS table_user_performer ON ms_job.id_user_performer=table_user_performer.id";
  if ($stmt = $db->prepare($query)){
    $stmt->execute();
    $stmt->bind_result($total, $id, $date, $job, $first_name_created, $last_name_created, $middle_name_created, $first_name_performer, $last_name_performer, $middle_name_performer, $state);
    while ($stmt->fetch()) {
      $StoreJob[$i]= 
      array(
        'id'=>$id,
        'date'=>$date,
        'job'=>$job,
        'name_created'=>$first_name_created.' '.$last_name_created.' '.$middle_name_created,
        'name_performer'=>$first_name_performer.' '.$last_name_performer.' '.$middle_name_performer,
        'state'=>$state
      );
      $i++;
    } 
    $stmt->close();
    $db->close();
  }
  echo '{"total":"'.$total.'","rows":'.json_encode($StoreJob).'}';
        }
		
//---------------------- select model boss ------------------------------------------------------------------------
		if ($_GET['func']=='boss')
        {
             $i=0;
        $query = "SELECT id, CONCAT_WS(' ',first_name, last_name, middle_name) AS FIO_B FROM ms_user";
		if ($stmt = $db->prepare($query)){
            $stmt->execute();
            $stmt->bind_result($id, $FIO_B);
            while ($stmt->fetch()) {
                  $boss_id [$i]= 
    array(
    
            'id'=>$id,
			      'FIO_B'=>$FIO_B
    );
    $i++;
            } 
            
            $stmt->close();
            $db->close();
        }

   echo'{rows:'.json_encode($boss_id).'}';
            
        }
        /*if ($_GET['func']=='client')
        {
             $i=0;
        $query = "SELECT id,name FROM mw_clients";
        if ($stmt = $db->prepare($query)){
            $stmt->execute();
            $stmt->bind_result($id, $name);
            while ($stmt->fetch()) {
                  $boss_id [$i]= 
    array(
    
            'id'=>$id,
            'name'=>$name
    );
    $i++;
            } 
       
            
            $stmt->close();
            $db->close();
        }

   echo'{rows:'.json_encode($boss_id).'}';
            
        }*/
?>