<?php

namespace App\Console\Commands;

use App\Models\MstStationPortal;
use Illuminate\Console\Command;

//https://manager.suumo.jp/chukai/tn00Xx0413.do?ROSEN=0010&EKI=07240&SENIMOTO=01
///run console on chrome
/*var data = [];
var i = 1;
var rosen = document.getElementsByName('ROSEN');
var options = document.getElementsByName('ROSEN')[0].getElementsByTagName('option');
for (let item of options) {
    setTimeout(function(){
        rosen[0].value = item.value;
        let changeEvent = new Event('change');
rosen[0].dispatchEvent(changeEvent);
setTimeout(function(){
    let eki = document.getElementsByName('EKI')[0].getElementsByTagName('option');
let child = [];
for(let ekiitem of eki ){
        child.push({name: ekiitem.text, value: ekiitem.value});
}
data.push({name: item.text, value: item.value, child: child});
}, 500)
}, 1000*i);
    i++;
}
setTimeout(function(){
    console.log(data);
}, 1500*(i+1))
*/

class ImportStationToSuumo extends Command
{
    protected $signature = 'import:station-suumo';

    public function __construct()
    {
        parent::__construct();
    }
    public function handle()
    {
        $data = json_decode(file_get_contents(public_path() . "/data/suumo/station_202304.json"), true);
        //MstStationPortal::where('type', 2)->delete();
        foreach ($data as $line){
            MstStationPortal::where('line_name', $line['name'])->where('type', 2)->delete();
            foreach ($line['child'] as $station){
                MstStationPortal::insert([
                    'line_id' => $line['value'],
                    'line_name' => $line['name'],
                    'station_id' => $station['value'],
                    'station_name' => $station['name'],
                    'type' => 2
                ]);
            }
        }
    }
}
