    <?php

use App\Models\Items;
use App\Models\Sitemaps;
use Goutte\Client;
use Illuminate\Support\Facades\Route;
use Nesk\Puphpeteer\Puppeteer;
use Symfony\Component\BrowserKit\HttpBrowser;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpClient\HttpClient;


/*
 * |--------------------------------------------------------------------------
 * | Web Routes
 * |--------------------------------------------------------------------------
 * |
 * | Here is where you can register web routes for your application. These
 * | routes are loaded by the RouteServiceProvider within a group which
 * | contains the "web" middleware group. Now create something great!
 * |
 */

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::get('puntofarma/cargar/links', function () {
   //  $client = new Client();
    $client = new HttpBrowser(HttpClient::create());
    // $client = Client::createChromeClient();    // create a chrome client

    // $sites=['']
    $crawler = $client->request('GET', 'https://www.puntofarma.com.py/sitemap.xml');
    // dd($client->request('GET', 'https://www.google.com.py/search?q=seo')->getHeaders());

    $links = $crawler->filter('loc')->each(function ($node) {
        return $node->text();
    });

    $i = 1;
    $d = [];
    // $puppeteer = new Puppeteer();

    foreach ($links as $link) {
        $sitemap = new Sitemaps();
        $sitemap->business_id = 2;
        $sitemap->link = $link;
        $sitemap->save();

        /*
         * if($i>5 && $i < 16){
         *            $browser = $puppeteer->launch();
         *   $page = $browser->newPage();
         *
         *
         *       $page->setDefaultNavigationTimeout(90000);
         *                 $page->goto($link);
         *
         * $html = $page->content();
         *  $crawler = new Crawler($html);
         *  $price = $crawler->filter('span.price')->first()->text();
         *
         *  $codigo_barras= $crawler->filter('img[alt="Codigo de barras"]')->ancestors()->first()->text();
         *  $titulo = $crawler->filter('h1')->first()->text();
         *
         * $d[$i]['titulo']=$titulo;
         * $d[$i]['cb']=$codigo_barras;
         * $d[$i]['precio']=$price;
         *  $d[$i]['link']=$link;
         *
         *  //$data = $client->request('GET', $link);
         *  //$data = $data->h(tml();
         * //$data = $data->text();
         *
         *
         *
         *
         *         }
         */

        $i++;
        echo $link . 'n';
    }
});

Route::get('f/c/l', function () {
   $client = new Client();
     //$client = new HttpBrowser(HttpClient::create());
     //$client = Client::createChromeClient();    // create a chrome client

    // $sites=['']
    $crawler = $client->request('GET', 'https://www.farmacenter.com.py/sitemap.xml');
    // dd($client->request('GET', 'https://www.google.com.py/search?q=seo')->getHeaders());
dd($client);
    $links = $crawler->filter('loc')->each(function ($node) {
        return $node->text();
    });
    dd($links);
    $i = 1;
    $d = [];
    // $puppeteer = new Puppeteer();

    foreach ($links as $link) {
        $sitemap = new Sitemaps();
        $sitemap->business_id = 7;
        $sitemap->link = $link;
        $sitemap->save();

        /*
         * if($i>5 && $i < 16){
         *            $browser = $puppeteer->launch();
         *   $page = $browser->newPage();
         *
         *
         *       $page->setDefaultNavigationTimeout(90000);
         *                 $page->goto($link);
         *
         * $html = $page->content();
         *  $crawler = new Crawler($html);
         *  $price = $crawler->filter('span.price')->first()->text();
         *
         *  $codigo_barras= $crawler->filter('img[alt="Codigo de barras"]')->ancestors()->first()->text();
         *  $titulo = $crawler->filter('h1')->first()->text();
         *
         * $d[$i]['titulo']=$titulo;
         * $d[$i]['cb']=$codigo_barras;
         * $d[$i]['precio']=$price;
         *  $d[$i]['link']=$link;
         *
         *  //$data = $client->request('GET', $link);
         *  //$data = $data->h(tml();
         * //$data = $data->text();
         *
         *
         *
         *
         *         }
         */

        $i++;
        echo $link . 'n';
    }
});

Route::get('farmaciacatedral/cargar/links', function () {
    // $client = new Client();
    $client = new HttpBrowser(HttpClient::create());
    // $client = Client::createChromeClient();    // create a chrome client

    // $sites=['']
    $crawler = $client->request('GET', 'http://localhost/comparapp/public/sitemap.xml');
    // dd($client->request('GET', 'https://www.google.com.py/search?q=seo')->getHeaders());
    //dd($crawler);
    $links = $crawler->filter('loc')->each(function ($node) {
        return $node->text();
    });

    $i = 1;
    $d = [];
    // $puppeteer = new Puppeteer();

    foreach ($links as $link) {
        $sitemap = new Sitemaps();
        $sitemap->business_id = 4;
        $sitemap->link = $link;
        $sitemap->save();

        /*
         * if($i>5 && $i < 16){
         *            $browser = $puppeteer->launch();
         *   $page = $browser->newPage();
         *
         *
         *       $page->setDefaultNavigationTimeout(90000);
         *                 $page->goto($link);
         *
         * $html = $page->content();
         *  $crawler = new Crawler($html);
         *  $price = $crawler->filter('span.price')->first()->text();
         *
         *  $codigo_barras= $crawler->filter('img[alt="Codigo de barras"]')->ancestors()->first()->text();
         *  $titulo = $crawler->filter('h1')->first()->text();
         *
         * $d[$i]['titulo']=$titulo;
         * $d[$i]['cb']=$codigo_barras;
         * $d[$i]['precio']=$price;
         *  $d[$i]['link']=$link;
         *
         *  //$data = $client->request('GET', $link);
         *  //$data = $data->h(tml();
         * //$data = $data->text();
         *
         *
         *
         *
         *         }
         */

        $i++;
        echo $link . "\n";
    }
});

// csargaritems scrapeados
Route::get('/catedral/cargar/links', function () {
    //$client = new Client();
    $client = new HttpBrowser(HttpClient::create());
    // $client = Client::createChromeClient();    // create a chrome client

    // $sites=['']
    $crawler = $client->request('GET', 'http://localhost/catedral/sitemap.xml');
    // dd($client->request('GET', 'https://www.google.com.py/search?q=seo')->getHeaders());

    $links = $crawler->filter('loc')->each(function ($node) {
        return $node->text();
    });
    // dd($links);
    $i = 1;
    $d = [];
    // $puppeteer = new Puppeteer();

    foreach ($links as $link) {
        if ($link == 'https://farmaciacatedral.com.py') {
            continue;
        } else {
            $item = new Sitemaps();
            $item->business_id = 4;
            $item->link = $link;
            $item->save();
        }

        /*
         * if($i>5 && $i < 16){
         *            $browser = $puppeteer->launch();
         *   $page = $browser->newPage();
         *
         *
         *       $page->setDefaultNavigationTimeout(90000);
         *                 $page->goto($link);
         *
         * $html = $page->content();
         *  $crawler = new Crawler($html);
         *  $price = $crawler->filter('span.price')->first()->text();
         *
         *  $codigo_barras= $crawler->filter('img[alt="Codigo de barras"]')->ancestors()->first()->text();
         *  $titulo = $crawler->filter('h1')->first()->text();
         *
         * $d[$i]['titulo']=$titulo;
         * $d[$i]['cb']=$codigo_barras;
         * $d[$i]['precio']=$price;
         *  $d[$i]['link']=$link;
         *
         *  //$data = $client->request('GET', $link);
         *  //$data = $data->h(tml();
         * //$data = $data->text();
         *
         *
         *
         *
         *         }
         */

        $i++;
        echo $link . "\n";
    }
});

//farmacenter
Route::get('farmacenter/cargar/links', [App\Http\Controllers\ItemsController::class, 'farmacenter_guardar_links']);
Route::get('farmacenter/cargar/items', [App\Http\Controllers\ItemsController::class, 'farmacenter_guardar_items']);
Route::get('farmacenter/buscar/items', [App\Http\Controllers\ItemsController::class, 'farmacenter_buscar_items_ajax']);
Route::get('farmacenter/actualizar/precios', [App\Http\Controllers\ItemsController::class, 'farmacenter_actualizar_items']);
// Punto farma
Route::get('puntofarma/cargar/items', [App\Http\Controllers\ItemsController::class, 'puntofarma_guardar_items']);
Route::get('puntofarma/actualizar/precios', [App\Http\Controllers\ItemsController::class, 'puntofarma_actualizar_items']);
Route::get('puntofarma/buscar/items', [App\Http\Controllers\ItemsController::class, 'puntofarma_buscar_items']);
// Catedral
Route::get('catedral/cargar/items', [App\Http\Controllers\ItemsController::class, 'catedral_guardar_items']);
Route::get('catedral/guardar/items', [App\Http\Controllers\ProductsController::class, 'catedral_guardar_items']);
Route::get('catedral/actualizar/precios', [App\Http\Controllers\ItemsController::class, 'catedral_actualizar_items']);
Route::get('catedral/buscar/items',[App\Http\Controllers\ItemsController::class,'catedral_buscar_items_ajax']);

// oliva
Route::get('oliva/cargar/links', [App\Http\Controllers\ItemsController::class, 'oliva_guardar_links']);
Route::get('oliva/cargar/items', [App\Http\Controllers\ItemsController::class, 'oliva_guardar_items']);
Route::get('oliva/actualizar/precios', [App\Http\Controllers\ItemsController::class, 'oliva_actualizar_items']);
Route::get('oliva/buscar/items', [App\Http\Controllers\ItemsController::class, 'oliva_buscar_items_ajax']);

// farmatotal
Route::get('farmatotal/cargar/links', [App\Http\Controllers\ItemsController::class, 'farmatotal_guardar_links']);
Route::get('farmatotal/cargar/items', [App\Http\Controllers\ItemsController::class, 'farmatotal_guardar_items']);
Route::get('farmatotal/buscar/items', [App\Http\Controllers\ItemsController::class, 'farmatotal_buscar_items_ajax']);

//biggie
Route::get('biggie/buscar/items', [App\Http\Controllers\ItemsController::class, 'biggie_buscar_items_ajax']);
Route::get('biggie/cargar/item', [App\Http\Controllers\ItemsController::class, 'biggie_guardar_item']);

//superseis
Route::get('superseis/buscar/items', [App\Http\Controllers\ItemsController::class, 'superseis_buscar_items']);


//stock
Route::get('stock/buscar/items', [App\Http\Controllers\ItemsController::class, 'stock_buscar_items']);

//casarica 
Route::get('casarica/buscar/items', [App\Http\Controllers\ItemsController::class, 'casarica_buscar_items_ajax']);

//fvs
Route::get('fvs/buscar/items', [App\Http\Controllers\ItemsController::class, 'fvs_buscar_items']);
Route::get('fvs', [App\Http\Controllers\ItemsController::class, 'fvs_ldjson_scrap']);

//arete suopercados
Route::get('arete/cargar/links', [App\Http\Controllers\ItemsController::class, 'arete_guardar_links']);
Route::get('arete/buscar/items', [App\Http\Controllers\ItemsController::class, 'arete_buscar_items_ajax']);

//salemma
Route::get('salemma/buscar/items', [App\Http\Controllers\ItemsController::class, 'salenma_buscar_items_ajax']);

//buscador global
Route::get('global/buscar/items', [App\Http\Controllers\ItemsController::class, 'web_buscador_global']);

//buscador global
Route::get('global/buscador', [App\Http\Controllers\ItemsController::class, 'web_global_buscador_array']);


//buscador global
Route::get('playstore/buscar', [App\Http\Controllers\ItemsController::class, 'playstore_buscador']);

//colombia drogueriaalamedasur - farmacia
Route::get('colombia/drogueriaalamedasur/cargar/links', [App\Http\Controllers\ItemsController::class, 'drogueriaalamedasur_guardar_links']);
Route::get('colombia/drogueriaalamedasur/cargar/items', [App\Http\Controllers\ItemsController::class, 'drogueriaalamedasur_guardar_items']);


// /listar items
Route::get('items/buscar', [App\Http\Controllers\ItemsController::class, 'web_items_buscar']);
Route::get('items/listar', [App\Http\Controllers\ItemsController::class, 'web_items_listar']);
Route::get('items/ver/{sku}', [App\Http\Controllers\ItemsController::class, 'web_items_details'])->name('frontend.items.details');
Route::get('backend/items/add-url', [App\Http\Controllers\ItemsController::class, 'web_items_add_url'])->name('backend.items.form.add');
Route::post('backend/items/save-url', [App\Http\Controllers\ItemsController::class, 'web_items_save_url'])->name('backend.items.form.save');
Route::get('backend/links/add-sitemap', [App\Http\Controllers\ItemsController::class, 'web_links_add_sitemap'])->name('backend.links.form.add');
Route::post('backend/links/save-sitemap', [App\Http\Controllers\ItemsController::class, 'web_links_save_sitemap'])->name('backend.links.form.save');

//catego
Route::get('backend/categorias/agregar', [App\Http\Controllers\CategoriesController::class, 'WebCategoriesFormAdd'])->name('backend.categories.form.add');
Route::get('backend/categorias/editar/{cat}', [App\Http\Controllers\CategoriesController::class, 'WebCategoriesFormEdit'])->name('backend.categories.form.edit');

Route::post('backend/categorias/guardar', [App\Http\Controllers\CategoriesController::class, 'WebCategoriesSave'])->name('backend.categories.save');
Route::get('backend/categorias/listar', [App\Http\Controllers\CategoriesController::class, 'WebCategoriesList'])->name('backend.categories.list');










// ajax
Route::get('ajax/items/verify', [App\Http\Controllers\ItemsController::class, 'ajax_items_verifify_url'])->name('ajax.items.verify.url');

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
