<?php
include 'inc/connect.php';
include 'inc/fun/functions.php';
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$pdo = $con;
// استعراض الجداول في قاعدة البيانات
/* $tables = $pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);

// إنشاء ملف Excel
$spreadsheet = new Spreadsheet();

foreach ($tables as $table) {
    // استعلام لاسترداد البيانات من الجدول الحالي
    $query = "SELECT * FROM $table";
    $statement = $pdo->prepare($query);
    $statement->execute();
    $data = $statement->fetchAll(PDO::FETCH_ASSOC);

    // إنشاء ورقة عمل جديدة لكل جدول
    $sheet = $spreadsheet->createSheet();
    $sheet->setTitle($table);

    // كتابة البيانات في الورقة الحالية
$dataArray = array();
foreach ($data as $row_data) {
    $dataArray[] = array_values($row_data);
}

$sheet->fromArray($dataArray, null, 'A1');
}
// حفظ ملف Excel
$writer = new Xlsx($spreadsheet);
$backup_file = 'backup_' . date('Y-m-d_H-i-s') . '.xlsx';
$writer->save($backup_file);

// تنزيل ملف النسخة الاحتياطية
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($backup_file) . '"');
readfile($backup_file);

// حذف ملف النسخة الاحتياطية بعد تنزيله
unlink($backup_file); */

// $json_data = file_get_contents("https://sdk.preprod.invoicing.eta.gov.eg/files/CountryCodes.json");
// try {
$nationalities = '{
    "1": "Andorran",
    "2": "Emirati",
    "3": "Afghan",
    "4": "Antiguan",
    "5": "Anguillan",
    "6": "Albanian",
    "7": "Armenian",
    "8": "Angolan",
    "9": "South African",
    "10": "Argentinian",
    "11": "Samoan",
    "12": "Austrian",
    "13": "Australian",
    "14": "Aruban",
    "15": "Dutch",
    "16": "Azerbaijani",
    "17": "Bosnian",
    "18": "Barbadian",
    "19": "Bangladeshi",
    "20": "Belgian",
    "21": "Burkinabe",
    "22": "Bulgarian",
    "23": "Bahraini",
    "24": "Burundian",
    "25": "Beninese",
    "26": "Saint Barthélemy",
    "27": "Bermudian",
    "28": "Bruneian",
    "29": "Bolivian",
    "30": "Dutch Caribbean",
    "31": "Brazilian",
    "32": "Bahamian",
    "33": "Bhutanese",
    "34": "Bouvet Island",
    "35": "Motswana",
    "36": "Belarusian",
    "37": "Belizean",
    "38": "Canadian",
    "39": "Cocos Islander",
    "40": "Congolese",
    "41": "Central African",
    "42": "Congolese",
    "43": "Swiss",
    "44": "Ivorian",
    "45": "Cook Islander",
    "46": "Chilean",
    "47": "Cameroonian",
    "48": "Chinese",
    "49": "Colombian",
    "50": "Costa Rican",
    "51": "Cuban",
    "52": "Cape Verdean",
    "53": "Curaçaoan",
    "54": "Christmas Islander",
    "55": "Cypriot",
    "56": "Czech",
    "57": "German",
    "58": "Djiboutian",
    "59": "Danish",
    "60": "Dominican",
    "61": "Dominican",
    "62": "Algerian",
    "63": "Ecuadorian",
    "64": "Estonian",
    "65": "Egyptian",
    "66": "Sahrawi",
    "67": "Eritrean",
    "68": "Spanish",
    "69": "Ethiopian",
    "70": "Finnish",
    "71": "Fijian",
    "72": "Falkland Islander",
    "73": "Micronesian",
    "74": "Faroese",
    "75": "French",
    "76": "Gabonese",
    "77": "British",
    "78": "Grenadian",
    "79": "Georgian",
    "80": "Guyanese",
    "81": "Guernsey",
    "82": "Ghanaian",
    "83": "Gibraltarian",
    "84": "Greenlandic",
    "85": "Gambian",
    "86": "Guinean",
    "87": "Guadeloupean",
    "88": "Equatorial Guinean",
    "89": "Greek",
    "90": "South Georgia and the South Sandwich Islands",
    "91": "Guatemalan",
    "92": "Guamanian",
    "93": "Bissau-Guinean",
    "94": "Guyanese",
    "95": "Hong Konger",
    "96": "Heard Islander and McDonald Islander",
    "97": "Honduran",
    "98": "Croatian",
    "99": "Haitian",
    "100": "Hungarian",
    "101": "Indonesian",
    "102": "Irish",
    "103": "Israeli",
    "104": "Manx",
    "105": "Indian",
    "106": "British Indian Ocean Territory",
    "107": "Iraqi",
    "108": "Iranian",
    "109": "Icelander",
    "110": "Italian",
    "111": "Jersey",
    "112": "Jamaican",
    "113": "Jordanian",
    "114": "Japanese",
    "115": "Kenyan",
    "116": "Kyrgyzstani",
    "117": "Cambodian",
    "118": "Kiribati",
    "119": "Comorian",
    "120": "Kittitian and Nevisian",
    "121": "North Korean",
    "122": "South Korean",
    "123": "Kuwaiti",
    "124": "Caymanian",
    "125": "Kazakhstani",
    "126": "Laotian",
    "127": "Lebanese",
    "128": "Saint Lucian",
    "129": "Liechtensteiner",
    "130": "Sri Lankan",
    "131": "Liberian",
    "132": "Mosotho",
    "133": "Lithuanian",
    "134": "Luxembourger",
    "135": "Latvian",
    "136": "Libyan",
    "137": "Moroccan",
    "138": "Monégasque",
    "139": "Moldovan",
    "140": "Montenegrin",
    "141": "Saint Martiner",
    "142": "Malagasy",
    "143": "Marshallese",
    "144": "Macedonian",
    "145": "Malian",
    "146": "Burmese",
    "147": "Mongolian",
    "148": "Macanese",
    "149": "Northern Mariana Islander",
    "150": "Martinican",
    "151": "Mauritanian",
    "152": "Montserratian",
    "153": "Maltese",
    "154": "Mauritian",
    "155": "Maldivian",
    "156": "Malawian",
    "157": "Mexican",
    "158": "Malaysian",
    "159": "Mozambican",
    "160": "Namibian",
    "161": "New Caledonian",
    "162": "Nigerian",
    "163": "Norfolk Islander",
    "164": "Nigerian",
    "165": "Nicaraguan",
    "166": "Dutch",
    "167": "Norwegian",
    "168": "Nepali",
    "169": "Nauruan",
    "170": "Niuean",
    "171": "New Zealander",
    "172": "Omani",
    "173": "Panamanian",
    "174": "Peruvian",
    "175": "Polynesian",
    "176": "Papua New Guinean",
    "177": "Filipino",
    "178": "Pakistani",
    "179": "Polish",
    "180": "Saint-Pierrais and Miquelonnais",
    "181": "Pitcairn Islander",
    "182": "Puerto Rican",
    "183": "Palestinian",
    "184": "Portuguese",
    "185": "Palauan",
    "186": "Paraguayan",
    "187": "Qatari",
    "188": "Réunionese",
    "189": "Romanian",
    "190": "Serbian",
    "191": "Russian",
    "192": "Rwandan",
    "193": "Saudi",
    "194": "Solomon Islander",
    "195": "Seychellois",
    "196": "Sudanese",
    "197": "Swedish",
    "198": "Singaporean",
    "199": "Saint Helenian",
    "200": "Slovenian",
    "201": "Svalbard and Jan Mayen",
    "202": "Slovak",
    "203": "Sierra Leonean",
    "204": "San Marinese",
    "205": "Senegalese",
    "206": "Somali",
    "207": "Surinamer",
    "208": "South Sudanese",
    "209": "São Toméan",
    "210": "Salvadoran",
    "211": "Saint Martiner",
    "212": "Syrian",
    "213": "Swazi",
    "214": "Turks and Caicos Islander",
    "215": "Chadian",
    "216": "French Southern and Antarctic",
    "217": "Togolese",
    "218": "Thai",
    "219": "Tajikistani",
    "220": "Tokelauan",
    "221": "East Timorese",
    "222": "Turkmen",
    "223": "Tunisian",
    "224": "Tongan",
    "225": "Turkish",
    "226": "Trinidadian and Tobagonian",
    "227": "Tuvaluan",
    "228": "Taiwanese",
    "229": "Tanzanian",
    "230": "Ukrainian",
    "231": "Ugandan",
    "232": "United States Minor Outlying Islands",
    "233": "American",
    "234": "Uruguayan",
    "235": "Uzbekistani",
    "236": "Vatican",
    "237": "Saint Vincentian and Grenadine",
    "238": "Venezuelan",
    "239": "British Virgin Islander",
    "240": "U.S. Virgin Islander",
    "241": "Vietnamese",
    "242": "Ni-Vanuatu",
    "243": "Wallis and Futuna",
    "244": "Samoan",
    "245": "Yemeni",
    "246": "Mahoran",
    "247": "South African",
    "248": "Zambian",
    "249": "Zimbabwean"
}';

/* $nationalitiesArray = json_decode($nationalities, true);
$con->beginTransaction(); */
/* foreach ($nationalitiesArray as $id => $nationality) {
    $stmt = $con->prepare("UPDATE nationality SET nationality_name_en = :nationality WHERE ID = :id");
    $stmt->bindParam(':nationality', $nationality);
    // تعديل الرقم بدءً من 1 بدلاً من 0
    $stmt->bindParam(':id', $id);
    $stmt->execute();
} */
// تطبيق التغييرات على قاعدة البيانات
/* $con->commit();
echo "تم تحديث الجنسيات بنجاح!"; */
/* } catch (PDOException $e) {
    // في حالة وجود أي أخطاء، يمكن التراجع عن التحديثات
    $con->rollBack();
    echo "حدث خطأ: " . $e->getMessage();
} */
// $json_data_decode = json_decode($json_data,true);
// foreach ($json_data_decode as $key => $values) {

        // $id_tax_types = getName('tax_types', 'code_tax', $values["TaxtypeReference"], 'ID');

        /* $stmt = $con->prepare("INSERT INTO nationality
                                        (name_ar, name_en, nationality_name, nationality_code	)
                                    VALUES(:name_ar, :name_en, :nationality_name, :nationality_code)");
        $stmt->execute(array(
            'name_ar' => $values["Desc_ar"],
            'name_en' => $values["Desc_en"],
            'nationality_name' => $values["Desc_ar"],
            'nationality_code' => $values["code"]
        )); */
    
    // echo $values["code"];
    // echo "<br>";
    // echo $values["Desc_en"];
    // echo "<br>";
    // echo $key . " - ";
    // echo $values["Desc_ar"];
    // echo "<br>";
    // echo "<br>";
    /* echo $id_tax_types;
    echo "<br>"; */
    // echo "<br>";
    // foreach ($values as $key => $value) {
    //     echo "<br>";
    //     print_r($value);
    //     echo "<br>";
    // }
    
// }

?>