<?php
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "http://api.mysyde-cms.de/main_admin_user",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => [
    "clientid: 6LdfO1kkAAAAAHiV352fB-CBoGhdm1HrQVk_Lthh",
    "dbname: myintranet_db",
    "dbpass: !Ww9d31i",
    "dbuser: intranetdb_user",
    "token: Bearer KLoMV6erYC8s7lzB4GtIdjFhO"
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);


$data = json_decode($response, true);

// foreach ($data as $key => $value) {
//     $mandant_id = get_mandant($value['master_mandant_id']);
//     $birthday_state = $value['birthday_state']-1;

//     $query = "INSERT INTO main_contact (id, name, email, shortcut, password, image, address, post_code, city, birthday, birthday_state, phone_no, master_mandant_id, current_mandant_id, active) VALUES (
//         NULL,
//         '" . $value['name'] . "',
//         '" . $value['email'] . "',
//         '" . $value['shortcut'] . "',
//         '" . $value['password'] . "',
//         '" . $value['image'] . "',
//         '" . $value['address'] . "',
//         '" . $value['post_code'] . "',
//         '" . $value['city'] . "',
//         '" . $value['birthday'] . "',
//         '" . $birthday_state . "',
//         '" . $value['phone_no'] . "',
//         '" . $mandant_id . "',
//         '" . $mandant_id . "',
//         '1'
//         )";
//         @mysqli_query($GLOBALS['mysql_con'], $query);
//         $main_contact_id = mysqli_insert_id($GLOBALS['mysql_con']);
//         foreach ($value['mandant'] as $key => $department) {
//             $mandant_id = get_mandant($department['main_mandant_id']);

//             $query = "INSERT INTO main_contact_mandant_link (id, main_contact, main_mandant_id) VALUES (
//                 NULL,
//                 '" . $main_contact_id . "',
//                 '" . $mandant_id . "'
//                 )";
//             @mysqli_query($GLOBALS['mysql_con'], $query);
//             $query2 = "INSERT INTO main_contact_department (id, main_contact_id, main_mandant_id, main_department_id, main_role_id, phone_no, email) VALUES (
//                 NULL,
//                 '" . $main_contact_id . "',
//                 '" . $mandant_id . "',
//                 '',
//                 '',
//                 '" . $department['phone_no'] . "',
//                 '" . $department['email'] . "'
//                 )";
//             @mysqli_query($GLOBALS['mysql_con'], $query2);
//         }
//         echo $value['name'];
// }

// function get_mandant($mandant_id){
//     switch ($mandant_id) {
//         case '143':
//             $new_id = 1;
//             break;
//         case '144':
//             $new_id = 2;
//             break;
//         case '145':
//             $new_id = 7;
//             break;
//         case '146':
//             $new_id = 4;
//             break;
//         case '147':
//             $new_id = 5;
//             break;
//         case '311':
//             $new_id = 6;
//             break;
//         default:
//             break;
//     }
//     return $new_id;
// }

foreach ($data as $key => $value) {
    $query = "INSERT INTO main_admin_user (id, name, email, login, password, right_create_user, edit_mode, is_super_user, main_site_id, main_language, password_recovery_code, right_website_admin) VALUES (
        NULL,
        '" . $value['name'] . "',
        '" . $value['email'] . "',
        '" . $value['login'] . "',
        '" . $value['password'] . "',
        '" . $value['right_create_user'] . "',
        '" . $value['edit_mode'] . "',
        '" . $value['is_super_user'] . "',
        '" . $value['main_site_id'] . "',
        '" . $value['main_language'] . "',
        '" . $value['password_recovery_code'] . "',
        '" . $value['right_website_admin'] . "'
        )";
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $main_contact_id = mysqli_insert_id($GLOBALS['mysql_con']);
}