<?php

class admin_polygon extends module {

    public $templ      = "shop_admin_polygon.htm";
    public $templBlade = 'admin_polygon';
    public $_get_vars  = [ 'act', 'id' ];
    public $_post_vars = [ 'act', 'geom' ];
    public $_loc_vars  = ['obj_name'];

    public function _init() {
        $this->obj = new cobject($this->obj_name);
        return parent::_init();
    }

    public function _on_() {
        global $blade;


        $data = [];
        $db2 = $this->obj->getDB()->q($this->obj->getSelectEx('id, title, ASTEXT(`geom`) as `geom` '));
        while($db2->nr()) {

            $coords = $this->mysql2json($db2->f('geom'));
            $row = [];
            $row['coords'] = $coords;
            $row['id'] = $db2->f('id');
            $row['title'] = $db2->f('title');

            $data['rows'][] = $row;
        }

        return $blade->run($this->getBladeTempl('main'), $data);
    }

    public function _on_save_edit() {

        $this->obj->set_hidden('geom');
        $this->obj->lPost();
        $this->obj->save();
        $this->id = $this->obj->id;
        return $this->_ajax_getForm();

    }


    public function _ajax_getForm() {
        $this->obj->byId($this->id);
        $this->obj->set_hidden('geom');
        $form = new form($this->obj);
        $form->setTempl('attrs/poly_form.htm');

        $formdata  = $form->render();
        $polycolor = $this->obj->get('color') ? $this->obj->get('color') : '#00FF00';

        $returndata = ['html' => $formdata, 'color' => $polycolor, 'title' => $this->obj->get('title') ];
        echo json_encode($returndata);

        // echo $form->render();
        die();
    }

    public function _ajax_editgeom() {
        $id = $this->id;

        $this->obj->byId($id);

        if ($this->geom) {

            if (!$this->obj->id) {
                $this->obj->set('title', 'Новая зона');
                $this->obj->save();
                $id = $this->obj->id;
            }

            $data = [];
            foreach ($this->geom[0] as $p) {
                $data[] = $p[0] . ' '. $p[1];
            }
            $str = "POLYGON((".implode(",", $data)."))";
            $sql = "UPDATE `".$this->obj_name."` SET `geom` = GeomFromText('".$str."') WHERE `id` = '".$id."' ";
            $db2 = $this->obj->getDB()->q($sql);

            $ret          = [];
            $ret['id']    = $id;
            $ret['title'] = htmlspecialchars_decode($this->obj->get('title'));

            echo json_encode($ret);
            die();
        }

        $ret       = [];
        $ret['id'] = 0;
        echo json_encode($ret);
        die();
    }

    public function mysql2json($str) {
        // POLYGON((59.94778267548529 30.047386140625,60.12503918773836 30.468986482421876,59.92986664045532 30.963371248046883,59.84083181279909 30.456626863281247,59.960180395813275 30.38933560351563,59.94778267548529 30.047386140625))
        preg_match_all('/POLYGON\(\((.*?)\)\)/', $str, $arr);
        $points = explode(',', $arr[1][0]);

        foreach($points as $point) {
            $ret[0][] = explode(' ', $point);
        }

        return json_encode($ret);
    }

    public function _ajax_remove() {
        $this->obj->byId($this->id);
        $this->obj->del();
        echo '1';
        die();
    }

}

?>