<?php

namespace Modules\Plan7\Http\Livewire;

use Livewire\Component;
use Livewire\Attributes\Reactive;
use Livewire\Attributes\On;
use Modules\Main\Models\Complex;

class Plan7SelectorLivewire extends Component
{
    public $mode = 'new';
    #[Reactive] 
    public $complexId;
    public $complexName;
    public $containerId = 'plan7__selector';
    public $filter = [];

    public $allObjects;
    public $types;
    public $access;
    public $summary;
    public $room = false;
    public $objects = [];
    private $apiSummary;
    private $apiAll;
    public function mount()
    {
        $this->containerId = $this->containerId . '__' . $this->complexId;
        $this->load();
    }
    #[On('client_form_complex_updated')] 
    public function load() {
        $this->unsetRoom();
        $this->filter = [];
        $complex = Complex::find($this->complexId);
        $this->complexName = $complex->name;
        if ($plan7ApiData = $complex->getPlan7ApiData()) {
            $this->apiSummary = 'https://plan7.ru/catalog/exp/json/summary/?token=' . $plan7ApiData->token . '&zk=' . $plan7ApiData->zk;
            $this->apiAll = 'https://plan7.ru/catalog/exp/json/?token=' . $plan7ApiData->token . '&zk=' . $plan7ApiData->zk;
            $this->getSummary();
            $this->getData();
        }
    }
    private function getSummary() {
        $URL = $this->apiSummary;
        $opt = [];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $URL);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $fields = http_build_query($opt);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
        $out = curl_exec($curl);
        $this->summary = json_decode($out, true);
        curl_close($curl);
    }
    private function getData()
    {
        $URL = $this->apiAll;
        $opt = [];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $URL);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $fields = http_build_query($opt);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
        $out = curl_exec($curl);
        $this->allObjects = json_decode($out, true);
        $this->types = $this->allObjects['values']['type'];
        $this->access = $this->allObjects['values']['access'];
        curl_close($curl);
    }

    public function setHouse($id)
    {
        $this->filter['bs'] = $id;
    }

    public function setRoom($id)
    {
        $this->room = $this->allObjects['data'][$id];
        $data = [
            'complexId' => $this->complexId,
            'room' => $this->room
        ];
        $this->dispatch('plan7_selector_set_room', data: $data); 
    }
    public function unsetRoom() {
        $this->room = false;
        $this->dispatch('plan7_selector_set_room', data: false); 
    }
    public function resetFilter() {
        $this->filter = [];
    }
    public function start() {
        $this->mode = 'select';
        $this->filter = [];
    }
    public function done() {
        $this->mode = 'selected';
        $this->filter = [];
    }
    public function render()
    {
        $objs = [];
        if (count($this->filter)) {
            foreach ($this->allObjects['data'] as $key => $room) {
                if ($room['access'] != 0) {
                    if (array_key_exists('id', $this->filter) && $this->filter['id'] != null && $room['id'] != $this->filter['id']) {
                        continue;
                    }
                    if (array_key_exists('bs', $this->filter) && $this->filter['bs'] != null && $room['bs'] != $this->filter['bs']) {
                        continue;
                    }
                    if (array_key_exists('type', $this->filter) && $this->filter['type'] != null && $room['type'] != $this->filter['type']) {
                        continue;
                    }
                    if (array_key_exists('min_area', $this->filter) && $this->filter['min_area'] != null && $room['area'] < $this->filter['min_area']) {
                        continue;
                    }
                    if (array_key_exists('max_area', $this->filter) && $this->filter['max_area'] != null && $room['area'] > $this->filter['max_area']) {
                        continue;
                    }
                    if (array_key_exists('min_price', $this->filter) && $this->filter['min_price'] != null && $room['summ'] < $this->filter['min_price']) {
                        continue;
                    }
                    if (array_key_exists('max_price', $this->filter) && $this->filter['max_price'] != null && $room['summ'] > $this->filter['max_price']) {
                        continue;
                    }

                    $room['mode'] = 'room';
                    $objs[] = $room;
                }
            }
        } else {
            foreach ($this->allObjects['values']['bs'] as $key => $house) {
                $objs[] = [
                    'mode' => 'bs',
                    'id' => $house['id'],
                    'name' => $house['name'],
                ];
            }
        }

        return view('plan7::livewire.selector', [
            'objs' => $objs
        ]);
    }


}