<?php

require_once('api/Simpla.php');

class ServicesAdmin extends Simpla
{
    public function fetch()
    {
        if ($this->request->method('post')) {

            $ids = $this->request->post('check');
            if (is_array($ids)) {
                // Действия с выбранными
                switch ($this->request->post('action')) {
                    case 'disable':
                        foreach ($ids as $id)
                            $this->services->set_visible($id, 0);
                        break;
                    case 'enable':
                        foreach ($ids as $id)
                            $this->services->set_visible($id, 1);
                        break;
                    case 'delete':
                        $this->services->delete($ids);
                        break;
                }
            }
        }

        $filters = array();

        // фильтры
        $filter = $this->request->get('filter', 'string');
        if (!empty($filter) && in_array($filter, array('disabled', 'enabled'))) {
            $filters['visible'] = intval($filter === 'enabled');
            $this->design->assign('filter', $filter);
        } elseif('branded' === $filter) {
            $filters[$filter] = $filter;
            $this->design->assign('filter', $filter);
        } elseif('home' === $filter) {
            $filters['show_home'] = 1;
            $this->design->assign('filter', $filter);
        } elseif('main' === $filter) {
            $filters['show_service'] = 1;
            $this->design->assign('filter', $filter);
        }
        if ($brand_id = $this->request->get('brand_id', 'integer')) {
            $filters['brand_id'] = $brand_id;
            $this->design->assign('brand_id', $brand_id);
        }
        if ($parent = $this->request->get('parent', 'integer')) {
            $filters['parent'] = $parent;
            $this->design->assign('parent', $parent);
        }

        // Поиск
        $keyword = $this->request->get('keyword');
        if (!empty($keyword) && is_string($keyword)) {
            $filters['keyword'] = $keyword;
            $this->design->assign('keyword', $keyword);
        }

        // сортировка
        $sort_order = $this->request->get('sort', 'string');
        $filters['order'] = empty($sort_order) ? 'header' : $sort_order;

        // постраничная навигация
        $filters['page'] = max(1, $this->request->get('page', 'integer'));
        $filters['limit'] = ($this->request->get('page') === 'all') ? 1000 : 20;

        $services_count = $this->services->count($filters);
        $services = $this->services->all($filters, true);

        $this->design->assign('pages_count', ceil($services_count / $filters['limit']));
        $this->design->assign('current_page', $filters['page']);
        $this->design->assign('sort', $filters['order']);
        $this->design->assign('services', $services);
        $this->design->assign('services_count', $services_count);
        $this->design->assign('brands', $this->services->get_all_brands());
        $this->design->assign('tree', $this->services->get_tree());
        $this->design->assign('root_url', $this->services->get_root_url());

        return $this->design->fetch('services.tpl');
    }

}