<?php

namespace auctionforge\inc\model;

use Exception;
use auctionforge\inc\abs\Model;
use auctionforge\inc\InstanceDB;

class ImportHistory extends Model
{
    /**
     * Get all rows in table
     * @return array|object|null
     */
    public function getAllRows()
    {
        $db = InstanceDB::getInstanceDB();
        $sql = "SELECT * FROM " . Lot::getTableName($db) . " `syncTimes` > 0";
        return $db->get_results($sql);
    }

    /**
     * Get rows from the table by the passed parameters
     * @param $params array
     * @param $page int
     * @param $perPage int
     * @return array|object|null
     */
    public function getRows($params, $page, $perPage)
    {
        $db = InstanceDB::getInstanceDB();
        $start = ($page - 1) * $perPage;
        $sql = "SELECT * FROM " . Lot::getTableName($db);

        $where = " WHERE `id` IS NOT NULL ";
        $order = "ORDER BY id DESC";
        if (!empty($params)) {
            $where .= $this->whereFormatSql($params);
        }
        if (!empty($params['orderby'])) {
            $order = " ORDER BY " . $params['orderby'] . " " . $params['order'];
        }

        $limit = " LIMIT " . $start . "," . $perPage;

        $sql = $sql . $where . $order . $limit;
        return $db->get_results($sql);
    }

    /**
     * Get the number of rows by the passed parameters
     * @param array $params
     * @return string|null
     */
    public function getCountAllRows($params = array())
    {
        $db = InstanceDB::getInstanceDB();
        $where = "";
        if (!empty($params)) {
            $where .= $this->whereFormatSql($params);
        }
        $sql = "SELECT COUNT(`id`) FROM " . Lot::getTableName($db) . " WHERE `id` IS NOT NULL > 0";
        $sql = $sql . $where;
        return $db->get_var($sql);
    }

    /**
     * Formation of a request condition
     * @param $params
     * @return string
     */
    private function whereFormatSql($params) {
        $where = "";

        if (!empty($params['status'])) {
            if ($params['status'] == '1') {
                $where .= " AND `postId` IS NOT NULL ";
            } elseif ($params['status'] == '2') {
                $where .= " AND `postId` IS NULL ";
            }
        }
        
        if (!empty($params['date'])) {
            $where .= $this->formatSqlForFilterDate($params['date']);
        }
        return $where;
    }

    /**
     * Forming a query string for a filter by date
     * @param $date string
     * @return string
     */
    private function formatSqlForFilterDate($date) {
        $date = explode(' to ', $date);
        $where = "";
        if (!empty($date[0]) && !empty($date[1])) {
            try {
                $dateTime = new \DateTime($date[1]);
                $dateTime->modify('+1 day');
                $date[1] = $dateTime->format('Y-m-d');
            } catch (Exception $e) {
                return "";
            }
            $where .= " AND `syncDatetime` BETWEEN '" . $date[0] . "' AND '" . $date[1] . "' ";
        } elseif (!empty($date[0])) {
            $where .= " AND `syncDatetime` > '" . $date[0] . "' ";
        }
        return $where;
    }
}