<?php
use model\books;
use model\users;
use model\stock;
use model\records;
use model\template;
use model\categories;

class admin_controller {
    
    public function books() {
        $page = $_GET['page'] ?? 1;
        foreach ($books = books::list_books($page) as $k => $v)
            $books[$k]['stock'] = stock::count_in_stock_by_bid($v['bid']).'/'.stock::count_stock_by_bid($v['bid']);
        $this->template('书籍管理', 'books', ['books' => $books, 'page' => $page, 'count' => books::count_books(), 'stock' => stock::count_stock()]);
    }
    
    public function users() {
        $page = $_GET['page'] ?? 1;
        $users = users::list_users($page);
        $this->template('用户管理', 'users', ['users' => $users, 'page' => $page, 'count' => users::count_users(), 'stock' => stock::count_out_stock()]);
    }
    
    public function stock() {
        $page = $_GET['page'] ?? 1;
        if (isset($_GET['bid']) && is_numeric($_GET['bid'])) {
            $books = stock::list_stock_by_bid($_GET['bid'], $page);
            $count = stock::count_stock_by_bid($_GET['bid']);
            $stock = stock::count_in_stock_by_bid($_GET['bid']);
        } else {
            $books = stock::list_stock($page);
            $count = stock::count_stock();
            $stock = stock::count_in_stock();
        } $this->template('库存管理', 'stock', ['books' => $books, 'page' => $page, 'count' => $count, 'stock' => $stock]);
    }
    
    public function categories() {
        $page = $_GET['page'] ?? 1;
        $categories = categories::list_categories($page);
        $this->template('分类管理', 'categories', ['categories' => $categories, 'page' => $page, 'count' => categories::count_categories(), 'stock' => stock::count_stock()]);
    }
    
    public function add_user() {
        $this->template('添加用户', 'add_user');
    }
    
    public function update_user() {
        if (isset($_GET['uid']) && is_numeric($_GET['uid'])) {
            $this->template('更新用户', 'update_user', ['user' => users::get_user_by_id($_GET['uid'])]);
        } else header('Location: /admin/users');
    }
    
    public function delete_user() {
        if (isset($_GET['uid']) && is_numeric($_GET['uid'])) {
            $this->template('删除用户', 'delete_user', ['user' => users::get_user_by_id($_GET['uid'])]);
        } else header('Location: /admin/users');
    }
    
    public function add_book() {
        $categories = categories::get_categories();
        $this->template('添加书籍', 'add_book', ['categories' => $categories]);
    }
    
    public function update_book() {
        if (isset($_GET['bid']) && is_numeric($_GET['bid'])) {
            $this->template('更新书籍', 'update_book', ['book' => books::get_book_by_id($_GET['bid']), 'categories' => categories::get_categories()]);
        } else header('Location: /admin/books');
    }
    
    public function delete_book() {
        if (isset($_GET['bid']) && is_numeric($_GET['bid'])) {
            $this->template('删除书籍', 'delete_book', ['book' => books::get_book_by_id($_GET['bid']), 'categories' => categories::get_categories()]);
        } else header('Location: /admin/books');
    }
    
    public function add_stock() {
        if (isset($_GET['bid']) && is_numeric($_GET['bid'])) {
            $this->template('添加库存', 'add_stock', ['book' => books::get_book_by_id($_GET['bid']), 'categories' => categories::get_categories()]);
        } else header('Location: /admin/stock');
    }
    
    public function update_stock() {
        if (isset($_GET['sid']) && is_numeric($_GET['sid'])) {
            $this->template('更新库存', 'update_stock', ['book' => stock::get_stock_by_id($_GET['sid']), 'categories' => categories::get_categories()]);
        } else header('Location: /admin/stock');
    }
    
    public function delete_stock() {
        if (isset($_GET['sid']) && is_numeric($_GET['sid'])) {
            $this->template('删除库存', 'delete_stock', ['book' => stock::get_stock_by_id($_GET['sid']), 'categories' => categories::get_categories()]);
        } else header('Location: /admin/stock');
    }
    
    public function add_category() {
        $this->template('添加分类', 'add_category');
    }
    
    public function update_category() {
        if (isset($_GET['cid']) && is_numeric($_GET['cid'])) {
            $this->template('更新分类', 'update_category', ['category' => categories::get_category_by_id($_GET['cid'])]);
        } else header('Location: /admin/categories');
    }
    
    public function delete_category() {
        if (isset($_GET['cid']) && is_numeric($_GET['cid'])) {
            $this->template('删除分类', 'delete_category', ['category' => categories::get_category_by_id($_GET['cid']), 'categories' => categories::get_categories()]);
        } else header('Location: /admin/categories');
    }
    
    public function do_add_user() {
        $username = $_POST['username'];
        if (!(strlen($username) <= 30 && preg_match('/^[a-zA-Z_][a-zA-Z0-9_]+$/u', $username)))
            exit(header('Location: /admin/add-user?page='.$_GET['page'].'&error=用户名不合规'));
        $password = $_POST['password'];
        if ($password != $_POST['confirm'])
            exit(header('Location: /admin/add-user?page='.$_GET['page'].'&error=两次输入密码不一致'));
        
        header('Location: ' . (users::add_user($username, $password, $_POST['sex'], $_POST['birthday'], $_POST['id_card'], $_POST['borrow_num'], $_POST['type']) ?
            '/admin/users?page='.$_GET['page'] : '/admin/add-user?page='.$_GET['page'].'&error=数据库错误'));
    }
    
    public function do_update_user() {
        if (isset($_GET['uid']) && is_numeric($_GET['uid'])) {
            if (empty($password = $_POST['password']))
                $password = null;
            elseif ($password != $_POST['confirm'])
                exit(header('Location: /admin/update-user?page='.$_GET['page'].'&uid='.$_GET['uid'].'&error=两次输入密码不一致'));
            
            header('Location: ' . (users::update_user($_GET['uid'], $password, $_POST['sex'], $_POST['birthday'], $_POST['id_card'], $_POST['borrow_num'], $_POST['type']) ?
                '/admin/users?page='.$_GET['page'] : '/admin/update-user?page='.$_GET['page'].'&uid='.$_GET['uid'].'&error=数据库错误'));
        } else header('Location: /admin/users?page='.$_GET['page']);
    }
    
    public function do_delete_user() {
        if (isset($_GET['uid']) && is_numeric($_GET['uid'])) {
            header('Location: ' . (users::del_user_by_id($_GET['uid']) ?
                '/admin/users?page='.$_GET['page'] : '/admin/delete-user?page='.$_GET['page'].'&uid='.$_GET['uid'].'&error=数据库错误'));
        } else header('Location: /admin/users?page='.$_GET['page']);
    }
    
    public function do_add_book() {
        header('Location: ' . (books::add_book($_POST['name'], $_POST['author'], $_POST['publish'], $_POST['isbn'], $_POST['cover'], $_POST['type'], $_POST['lang']) ?
            '/admin/books?page='.$_GET['page'] : '/admin/add-book?page='.$_GET['page'].'&error=数据库错误'));
    }
    
    public function do_update_book() {
        if (isset($_GET['bid']) && is_numeric($_GET['bid'])) {
            header('Location: ' . (books::update_book($_GET['bid'], $_POST['name'], $_POST['author'], $_POST['publish'], $_POST['isbn'], $_POST['cover'], $_POST['type'], $_POST['lang']) ?
                '/admin/books?page='.$_GET['page'] : '/admin/update-book?page='.$_GET['page'].'&bid='.$_GET['bid'].'&error=数据库错误'));
        } else header('Location: /admin/books?page='.$_GET['page']);
    }
    
    public function do_delete_book() {
        if (isset($_GET['bid']) && is_numeric($_GET['bid'])) {
            header('Location: ' . (books::del_book_by_id($_GET['bid']) ?
                '/admin/books?page='.$_GET['page'] : '/admin/delete-book?page='.$_GET['page'].'&bid='.$_GET['bid'].'&error=数据库错误'));
        } else header('Location: /admin/books?page='.$_GET['page']);
    }
    
    public function do_add_stock() {
        if (isset($_GET['bid']) && is_numeric($_GET['bid'])) {
            header('Location: ' . (stock::add_stock($_GET['bid'], $_POST['place'], $_POST['health'], $_POST['state']) ?
                '/admin/stock?bid='.$_GET['bid'] : '/admin/add-stock?page='.$_GET['page'].'%bid='.$_GET['bid'].'&error=数据库错误'));
        } else header('Location: /admin/stock?bid='.$_GET['bid'].'&page='.$_GET['page']);
    }
    
    public function do_update_stock() {
        if (isset($_GET['sid']) && isset($_GET['bid']) && is_numeric($_GET['sid']) && is_numeric($_GET['bid'])) {
            header('Location: ' . (stock::update_stock($_GET['sid'], $_GET['bid'], $_POST['place'], $_POST['health'], $_POST['state']) ?
                '/admin/stock?page='.$_GET['page'] : '/admin/update-stock?page='.$_GET['page'].'&sid='.$_GET['sid'].'&bid='.$_GET['bid'].'&error=数据库错误'));
        } else header('Location: /admin/stock?page='.$_GET['page']);
    }
    
    public function do_delete_stock() {
        if (isset($_GET['sid']) && is_numeric($_GET['sid'])) {
            header('Location: ' . (stock::del_stock_by_id($_GET['sid']) ?
                '/admin/stock?page='.$_GET['page'] : '/admin/delete-stock?page='.$_GET['page'].'&sid='.$_GET['sid'].'&error=数据库错误'));
        } else header('Location: /admin/stock?page='.$_GET['page']);
    }
    
    public function do_add_category() {
        header('Location: ' . (categories::add_category($_POST['name']) ?
            '/admin/categories?page='.$_GET['page'] : '/admin/add-categories?page='.$_GET['page'].'&error=数据库错误'));
    }
    
    public function do_update_category() {
        if (isset($_GET['cid']) && is_numeric($_GET['cid'])) {
            header('Location: ' . (categories::update_category($_GET['cid'], $_POST['name']) ?
                '/admin/categories?page='.$_GET['page'] : '/admin/update-category?page='.$_GET['page'].'&cid='.$_GET['cid'].'&error=数据库错误'));
        } else header('Location: /admin/categories?page='.$_GET['page']);
    }
    
    public function do_delete_category() {
        if (isset($_GET['cid']) && is_numeric($_GET['cid'])) {
            header('Location: ' . (books::update_book_category($_GET['cid'], $_POST['category']) && categories::del_category_by_id($_GET['cid']) ?
                '/admin/categories?page='.$_GET['page'] : '/admin/delete-category?page='.$_GET['page'].'&cid='.$_GET['cid'].'&error=数据库错误'));
        } else header('Location: /admin/categories?page='.$_GET['page']);
    }
    
    private function template($title, $name, $parameters = []) {
        template::render('admins', $name, array_merge(['title' => $title], $parameters));
    }
    
}