<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $json = file_get_contents('php://input');
    $data = json_decode($json, true);

    $username = $data['username'];
    $password = $data['password'];
    //$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
    $mac_address = $data['mac_address'];
    /*$conn = new mysqli('localhost', 'marsalog_mahran', 'gX-;hjZN-B*u', 'marsalog_shipping_clearance_db');

    if ($conn->connect_error) {
        die('Connection failed: ' . $conn->connect_error);
    }*/
    include("dbConfig.php");
   
    $sql = "SELECT * FROM user_tbl WHERE stop='0' AND deleted='0' AND (username='$username' or user_mobile='$username' or id_num='$username') and password='$password' LIMIT 1";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
       $em_id = $row["user_id"];
       $apple_delete = $row["apple_delete"];
       $password_q = $row["password_q"];
        $sql_ico="SELECT * FROM hr_employ_attachments_tbl where employ_id='".$row["user_id"]."' AND att_name='employ_photo' AND  stop='0' AND deleted='0' ";
        $result_ico = $conn->query($sql_ico);
         if ($result_ico->num_rows > 0) {
          while($row_ico = $result_ico->fetch_assoc()) {
            $attach_link=$row_ico['attach_link'];
          
          }}
           $sql_job="SELECT * FROM job_tb where job_id ='".$row["job_id"]."' ";
        $result_job = $conn->query($sql_job);
         if ($result_job->num_rows > 0) {
          $row_job = $result_job->fetch_assoc();
            $job_name_ar=$row_job['job_name'];
            $jop_name_en=$row_job['jop_name_en'];
          
          }
         $sql = "SELECT hr.*, c.company_name_ar, c.company_name_en, d.dept_name, d.dept_name_en, j.job_name, j.jop_name_en
        FROM hr_employ_tbl hr
        JOIN main_company_tbl c ON hr.company_id = c.company_id
        JOIN dept_tb d ON hr.dept_id = d.dept_id
        JOIN job_tb j ON hr.job_id = j.job_id
        WHERE hr.employ_id = '".$row['user_id']."'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $row_hr = $result->fetch_assoc();
 // ادخال الماك ادرس
        if($row_hr['mac_address']==""){
            $sql_up = "UPDATE hr_employ_tbl SET mac_address='$mac_address' WHERE employ_id='".$row['user_id']."'";
            if ($conn->query($sql_up) === TRUE) {
            
            } else {
            
            }
        }
        $sql_em = "SELECT * FROM hr_employ_tbl WHERE employ_id='".$row['user_id']."'";
    $result_em = $conn->query($sql_em);
    if ($result_em->num_rows > 0) {
        $row_em = $result_em->fetch_assoc();
        $mac_add = $row_em["mac_address"];
    }
        //
    $employ_name_ar = $row_hr['employ_name_ar'];
    $employ_name_en = $row_hr['employ_name_en'];
    $employ_email = $row_hr['employ_email'];
    $mobile_number = $row_hr['mobile_number'];
    $office_employ = $row_hr['office_employ'];
    $company_name_ar = $row_hr['company_name_ar'];
    $company_name_en = $row_hr['company_name_en'];

    $dept_name = $row_hr['dept_name'];
    $dept_name_en = $row_hr['dept_name_en'];

    $job_name = $row_hr['job_name'];
    $job_name_en = $row_hr['jop_name_en'];
    $company_lat = $row_hr['company_lat'];
    $company_lng = $row_hr['company_lng'];
    $mac_address = $mac_add;
    $company_id = $row_hr['company_id'];
    $employ_sex = $row_hr['employ_sex'];
    $apple_delete = $apple_delete;
}
        // Login successful
        $success = true;
        $message = 'Login successful.';
        $id = $row['user_id'];
        $name = $employ_name_ar;
        $nameen = $employ_name_en;
        $email = $employ_email;
        $mobilenumber = $mobile_number;
        $office_employ = $office_employ;
        if($attach_link!=""){
             $photo = 'https://marsalogistics.com/new/Shipping_Clearance_System/jquery/pages/system_maintenance/user_img/'.$attach_link;
        }else{
             $photo = '';
        }
       
        $companynamear = $company_name_ar;
        $companynameen = $company_name_en;
        $deptname = $dept_name;
        $deptnameen = $dept_name_en;
        $jobname = $job_name;
        $jobnameen = $job_name_en;
        $company_lat = $company_lat;
        $company_lng = $company_lng;
        $mac_address = $mac_address;
        $company_id = $company_id;
        $employ_sex = $employ_sex;
        $apple_delete = $apple_delete;
      //==================العملايات==================================

$browserAgent = $_SERVER['HTTP_USER_AGENT'];
 $user_id =$row["user_id"];
 $add_user_id = $row["user_id"];
 $system_type='hr_marsa_system';
  $proccess_type='Login from Hr App';
  $proccess_details_en='Login from Hr App';
  $proccess_details='تسجيل الدخول عن طريق التطبيق';
 $table_name='hr_employ_tbl';   
  date_default_timezone_set("Asia/Riyadh");
  $createdDate = date("Y-m-d h:i:sa");
   $sql_req = "INSERT INTO process_systems_tbl(system_type,user_id,user_ip,proccess_type,proccess_details,proccess_details_en,table_name,proccess_id,proccess_date,process_time,process_datetime,browser)values('$system_type','$add_user_id','$user_ip','$proccess_type','$proccess_details','$proccess_details_en','$table_name','$user_id',NOW(),NOW(),'$createdDate','$browserAgent')";
  $run_nesba = $conn->query($sql_req) or die("Error in saving ".$conn->error); 
 //==================العملايات==================================

    } else {
        // User not found
        $success = false;
        $message = 'Invalid email or password.';
    }
    $conn->close();

    // Prepare the response data
    $response = [
        'success' => $success,
        'message' => $message,
        'id' => $id,
        'name' => $name,
        'nameen' => $nameen,
        'email' => $email,
        'mobilenumber' => $mobilenumber,
        'office_employ' => $office_employ,
        'photo' => $photo,
        'companynamear' => $companynamear,
        'companynameen' => $companynameen,
        'deptname' => $deptname,
        'deptnameen' => $deptnameen,
        'jobname' => $jobname,
        'jobnameen' => $jobnameen,
        'company_lat' => $company_lat,
        'company_lng' => $company_lng,
        'mac_address' => $mac_address,
        'company_id' => $company_id,
        'employ_sex' => $employ_sex,
        'job_name_ar' => $job_name_ar,
        'jop_name_en' => $jop_name_en,
        'apple_delete' => $apple_delete,
        'password_q' => $password_q,
    ];

    // Send the response as JSON
    header("Access-Control-Allow-Origin: *");
   header('Content-Type: application/json');
    echo json_encode($response);
} else {
    // Invalid request method
    http_response_code(405);
    echo 'Method Not Allowed';
    
}