<?php
class RecordController {
    const RECORDS_PER_PAGE = 10;
    private $record;
    private $business;
    private $user;
    private $settings;

    public function __construct() {
        $this->record = new Record();
        $this->business = new Business();
        $this->user = new User();
        $this->settings = new Settings();
    }

    public function createRecord() {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $user_id = $_SESSION['id'];
            $business_id = $_POST['business_id'];
            $category = $_POST['category'];
            $amount = $_POST['amount'];
            $description = $_POST['description'];
            $date = $_POST['date'];

            $json_options = $this->settings->getJsonOptions($business_id);
            $isApprovedCandidate = $this->user->isApprovedCandidate($user_id, $business_id);

            if ($json_options !== false && $isApprovedCandidate) {
                $result = $this->record->create($user_id, $business_id, $category, $amount, $description, $date);
                if ($result === TRUE) {
                    msg("成功记录经营账目 <a href='index.php?controller=Record&action=showRecords'>点击查看记录</a>.");
                } else {
                    msg("经营记录出错, 请联系系统管理员.");
                }
            } else {
                msg("经营记录出错, 请联系系统管理员.");
            }

        } else {
            $business_id = filter_input(INPUT_GET, 'business_id', FILTER_SANITIZE_NUMBER_INT);
            load_view('record/create_record', ['business_id' => $business_id]);
        }
    }

    public function showRecords() {
        $business_id = $_GET['business_id'];
        $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $offset = ($page > 1) ? ($page - 1) * self::RECORDS_PER_PAGE : 0;

        $totalRecords = $this->record->getTotalRecordsByBiz($business_id);
        $totalPages = ceil($totalRecords / self::RECORDS_PER_PAGE);

        $records = $this->record->getRecordsByBiz($business_id, self::RECORDS_PER_PAGE, $offset);

        if (!empty($records)) {
            load_view('record/show_records', ['records' => $records, 'totalPages' => $totalPages, 'currentPage' => $page, 'business_id' => $business_id]);
        } else {
            msg("没有找到经营记录.");
        }
    }

    public function getRecordsByDate() {        
        if (!isset($_GET['business_id']) || !isset($_POST['date'])) {
            $this->showRecords();
            return;
        }
        
        $business_id = $_GET['business_id'];
        $date = $_POST['date']; 

        $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
        $offset = ($page > 1) ? ($page - 1) * self::RECORDS_PER_PAGE : 0;

        $totalRecords = $this->record->getTotalRecordsByBiz($business_id);
        $totalPages = ceil($totalRecords / self::RECORDS_PER_PAGE);

        $records = $this->record->getRecordsByBizAndDate($business_id, $date, self::RECORDS_PER_PAGE, $offset);

        if (!empty($records)) {
            load_view('record/show_records', ['records' => $records, 'totalPages' => $totalPages, 'currentPage' => $page, 'business_id' => $business_id]);
        } else {
            msg("没有找到经营记录.");
        }
    }
}