<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Models\Admin\Product;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;

class ProductController extends Controller
{
    public function index()
    {
        return view('admin.products.index');
    }

    public function create()
    {
        return view('admin.products.create');
    }

    public function edit($id)
    {
        $product = Product::find($id);

        return view('admin.products.edit', ['product' => $product]);
    }

    public function import()
    {
        require_once public_path('SimpleXLSX.php');

        // /* Dikkan Category Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_category_desc.xlsx'));
        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_category = $item[1];
        //         $name = $item[3];
        //         $language = $item[4];
        //         $parent_id = $item[5];

        //         $existingCategories[$dikkan_category][$language] = $name;
        //         $parentCategories[$dikkan_category] = $parent_id;
        //     }

        //     foreach ($existingCategories as $dikkan_category => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']),
        //             'en' => Str::slug($names['en']),
        //             'de' => Str::slug($names['de']),
        //         ], JSON_UNESCAPED_UNICODE);

        //         $parent_id = $parentCategories[$dikkan_category] ?: null;

        //         if($parent_id == null || $parent_id == '') continue;

        //         if($parent_id == 1){ $parent_id = 14; // Gemi vanaları = 1 => 14
        //         }else if($parent_id == 2){ $parent_id = 16; // Askeri Gemi Vanaları = 2 => 16
        //         }else if($parent_id == 3){ $parent_id = 17; // Kontrol = 3 => 17
        //         }else if($parent_id == 4){ $parent_id = 15; //Su sistemleri = 4 => 15
        //         }else{ $parent_id = null;}

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => $parent_id,
        //             'old_id' => $dikkan_category,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_category,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Dikkan Category Desc */

        // /* Dikkan Type Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_type_desc.xlsx'));
        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_type = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingTypes[$dikkan_type][$language] = $name;
        //     }

        //     foreach ($existingTypes as $dikkan_type => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 5,
        //             'old_id' => $dikkan_type,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_type,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Dikkan Type Desc */

        // /* Dikkan Option Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_option_desc.xlsx'));
        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_option = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingOptions[$dikkan_option][$language] = $name;
        //     }
        //     foreach ($existingOptions as $dikkan_option => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 6,
        //             'old_id' => $dikkan_option,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_option,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Dikkan Option Desc */

        // /* Dikkan Body Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_body_desc.xlsx'));
        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_body = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingBodies[$dikkan_body][$language] = $name;
        //     }

        //     foreach ($existingBodies as $dikkan_body => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 7,
        //             'old_id' => $dikkan_body,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_body,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Dikkan Body Desc */

        // /* Dikkan Material Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_material_desc.xlsx'));
        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_material = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingMaterials[$dikkan_material][$language] = $name;
        //     }

        //     foreach ($existingMaterials as $dikkan_material => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 8,
        //             'old_id' => $dikkan_material,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_material,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Dikkan Material Desc */

        // /* Trim Material Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_trim_material_desc.xlsx'));
        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_trim_material = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingTrimMaterials[$dikkan_trim_material][$language] = $name;
        //     }

        //     foreach ($existingTrimMaterials as $dikkan_trim_material => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 9,
        //             'old_id' => $dikkan_trim_material,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_trim_material,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Trim Material Desc */

        // /* Trim Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_trim_desc.xlsx'));

        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_trim = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingTrims[$dikkan_trim][$language] = $name;
        //     }

        //     foreach ($existingTrims as $dikkan_trim => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 10,
        //             'old_id' => $dikkan_trim,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_trim,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Trim Desc */

        // /* Stem Material Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_stem_material_desc.xlsx'));

        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_stem_material = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingStemMaterials[$dikkan_stem_material][$language] = $name;
        //     }

        //     foreach ($existingStemMaterials as $dikkan_stem_material => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 11,
        //             'old_id' => $dikkan_stem_material,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_stem_material,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Stem Material Desc */

        // /* End Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_end_desc.xlsx'));

        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_end = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingEnds[$dikkan_end][$language] = $name;
        //     }

        //     foreach ($existingEnds as $dikkan_end => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 12,
        //             'old_id' => $dikkan_end,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_end,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* End Desc */

        // /* Face Desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_face_to_face_desc.xlsx'));

        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $dikkan_face = $item[1];
        //         $name = $item[2];
        //         $language = $item[3];

        //         $existingFaces[$dikkan_face][$language] = $name;
        //     }

        //     foreach ($existingFaces as $dikkan_face => $names){
        //         $nameJson = json_encode($names, JSON_UNESCAPED_UNICODE);

        //         $slugJson = json_encode(['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ], JSON_UNESCAPED_UNICODE);

        //         DB::table('taxonomy')->insert([
        //             'parent_id' => 13,
        //             'old_id' => $dikkan_face,
        //             'name' => $nameJson,
        //             'slug' => $slugJson,
        //             'handle' => Str::slug($names['en']),
        //             'isSingleSelect' => 1,
        //             'isDeletable' => 1,
        //             'status' => 1,
        //             'ordernum' => $dikkan_face,
        //             'created_at' => now(),
        //             'updated_at' => now()
        //         ]);
        //     }
        // /* Face Desc */

        // /* Products */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_product.xlsx'));

        //     foreach ($xlsx->rows(0) as $key => $item){
        //         if ($key < 1) continue;

        //         $old_id = $item[0];
        //         $category = $item[2];
        //         $dikkan_figure = $item[3];
        //         $datasheet = $item[4];
        //         $type = $item[5];
        //         $option = $item[6];
        //         $body = $item[7];
        //         $material = $item[8];
        //         $trim_material = $item[9];
        //         $trim = $item[10];
        //         $stem_material = $item[11];
        //         $end = $item[12];
        //         $impa_code = $item[13];
        //         $face_to_face = $item[14];
        //         $active = $item[15];
        //         $img = $item[16];
        //         $main_category = $item[17];
        //         $td_img = $item[18];

        //         echo $old_id . ' - ' . $category . ' - ' . $dikkan_figure . ' - ' . $datasheet . ' - ' . $type . ' - ' . $option . ' - ' . $body . ' - ' . $material . ' - ' . $trim_material . ' - ' . $trim . ' - ' . $stem_material . ' - ' . $end . ' - ' . $impa_code . ' - ' . $face_to_face . ' - ' . $active . ' - ' . $img . ' - ' . $main_category . ' - ' . $td_img . '<hr>';

        //         $product = new Product();

        //         $product->title = '';
        //         $product->slug = '';
        //         $product->old_id = $old_id;
        //         $product->dikkan_figure = $dikkan_figure;
        //         $product->impa_code = $impa_code;
        //         $product->datasheet_path = $datasheet;
        //         $product->image_path = $img;
        //         $product->td_image_path = $td_img;

        //         $product->save();

        //         $category = DB::table('taxonomy')->where('old_id', $category)->first()->id;
        //         $product->taxonomies()->attach($category);

        //         $type = DB::table('taxonomy')->where('parent_id', 5)->where('old_id', $type)->first()->id;
        //         $product->taxonomies()->attach($type);

        //         $option = DB::table('taxonomy')->where('parent_id', 6)->where('old_id', $option)->first()->id;
        //         $product->taxonomies()->attach($option);

        //         if($body == 18)
        //             $body = 19;
        //         $body = DB::table('taxonomy')->where('parent_id', 7)->where('old_id', $body)->first()->id;
        //         $product->taxonomies()->attach($body);

        //         if($material == 14)
        //             $material = 15;
        //         $material = DB::table('taxonomy')->where('parent_id', 8)->where('old_id', $material)->first()->id;
        //         $product->taxonomies()->attach($material);

        //         $trim_material = DB::table('taxonomy')->where('parent_id', 9)->where('old_id', $trim_material)->first()->id;
        //         $product->taxonomies()->attach($trim_material);

        //         $trim = DB::table('taxonomy')->where('parent_id', 10)->where('old_id', $trim)->first()->id;
        //         $product->taxonomies()->attach($trim);

        //         $stem_material = DB::table('taxonomy')->where('parent_id', 11)->where('old_id', $stem_material)->first()->id;
        //         $product->taxonomies()->attach($stem_material);

        //         $end = DB::table('taxonomy')->where('parent_id', 12)->where('old_id', $end)->first()->id;
        //         $product->taxonomies()->attach($end);

        //         $face_to_face = DB::table('taxonomy')->where('parent_id', 13)->where('old_id', $face_to_face)->first()->id;
        //         $product->taxonomies()->attach($face_to_face);
        //     }
        // /* Products */

        // /* Product desc */
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_product_desc2.xlsx'));

        //     foreach ($xlsx->rows(0) as $key => $item){
        //                 if ($key < 1) continue;

        //                 $id = $item[0];
        //                 $dikkan_product = $item[1];
        //                 $name = $item[2];
        //                 $language = $item[3];
        //                 $technical_features = $item[4];
        //                 $pressure_temperature = $item[5];
        //                 $material_specifications = $item[6];
        //                 $figure_options = $item[7];
        //                 $dimensions = $item[8];

        //                 echo $key. ' - '. $id . ' - ' . $dikkan_product . ' - ' . $name . ' - ' . $language . '<br>';

        //                 $existingNames[$dikkan_product][$language] = $name;
        //                 $existingTechnicalFeatures[$dikkan_product][$language] = $technical_features;

        //                 $existingPressureTemperature[$dikkan_product][$language] = $pressure_temperature;
        //                 $existingMaterialSpecifications[$dikkan_product][$language] = $material_specifications;
        //                 $existingFigureOptions[$dikkan_product][$language] = $figure_options;
        //                 $existingDimensions[$dikkan_product][$language] = $dimensions;
        //     }

        //     foreach ($existingNames as $dikkan_product => $names){
        //         $nameJson = $names;
        //         $technicalFeaturesJson = $existingTechnicalFeatures[$dikkan_product];
        //         $pressureTemperatureJson = $existingPressureTemperature[$dikkan_product];
        //         $materialSpecificationsJson = $existingMaterialSpecifications[$dikkan_product];
        //         $figureOptionsJson = $existingFigureOptions[$dikkan_product];
        //         $dimensionsJson = $existingDimensions[$dikkan_product];

        //         $slugJson = ['tr' => Str::slug($names['tr']), 'en' => Str::slug($names['en']), 'de' => Str::slug($names['de']), ];

        //         $product = Product::where('old_id', $dikkan_product)->first();

        //         $product->title = $nameJson;
        //         $product->slug = $slugJson;

        //         $product->technical_features = $technicalFeaturesJson;
        //         $product->pressure_temperature_ranges = $pressureTemperatureJson;
        //         $product->material_specs = $materialSpecificationsJson;
        //         $product->figure_options = $figureOptionsJson;
        //         $product->dimensions = $dimensionsJson;

        //         $product->save();
        //     }
        // /* Product desc */

        // /* Product Variants */
        // //TODO! Product Variants
        //     $xlsx = \SimpleXLSX::parse(public_path('dikkan_product_variants.xlsx'));

        //     if ($xlsx) {
        //         foreach ($xlsx->rows(0) as $key => $item) {
        //             // Skip header row
        //             break;
        //             if ($key < 1) continue;

        //             $dikkan_product = $item[1]; // Assuming dikkan_product corresponds to product_id
        //             $product_code = $item[2];
        //             $wp_od_fl = $item[3];
        //             $flange = $item[4];
        //             $dn = $item[5];

        //             echo $key . ' - ' . $dikkan_product . ' - ' . $product_code . ' - ' . $wp_od_fl . ' - ' . $flange . ' - ' . $dn . '<br>';

        //             // Find product by dikkan_product or another identifier in the products table
        //             $product = DB::table('products')->where('old_id', $dikkan_product)->first();

        //             if ($product) {
        //                 // Insert the product_code with taxonomy_id = 18
        //                 DB::table('taxonomy_product')->insert([
        //                     'product_id' => $product->id,
        //                     'taxonomy_id' => 18, // For product_code
        //                     'value' => $product_code,
        //                     'created_at' => now(),
        //                     'updated_at' => now(),
        //                 ]);

        //                 // Insert the flange with taxonomy_id = 19
        //                 DB::table('taxonomy_product')->insert([
        //                     'product_id' => $product->id,
        //                     'taxonomy_id' => 19, // For flange
        //                     'value' => $flange,
        //                     'created_at' => now(),
        //                     'updated_at' => now(),
        //                 ]);

        //                 // Insert wp_od_fl with taxonomy_id = 20
        //                 DB::table('taxonomy_product')->insert([
        //                     'product_id' => $product->id,
        //                     'taxonomy_id' => 20, // For wp_od_fl
        //                     'value' => $wp_od_fl,
        //                     'created_at' => now(),
        //                     'updated_at' => now(),
        //                 ]);

        //                 // Insert dn with taxonomy_id = 21
        //                 DB::table('taxonomy_product')->insert([
        //                     'product_id' => $product->id,
        //                     'taxonomy_id' => 21, // For dn
        //                     'value' => $dn,
        //                     'created_at' => now(),
        //                     'updated_at' => now(),
        //                 ]);
        //             } else {
        //                 echo "Product not found for dikkan_product: $dikkan_product<br>";
        //             }
        //         }
        //     } else {
        //         echo 'Failed to parse the Excel file.';
        //     }
        // /* Product Variants */

    }
}
