<?php

namespace app\controller;

use support\Request;
use app\model\Book;
use app\model\Category;
use app\model\Reading;
use app\model\ReadingBook;
use app\model\BookLog;

class BookController
{
    //书籍列表
    public function index(Request $request)
    {
        $name = $request->get('name', '');
        $categoryID = $request->get('category_id', 0);
        $categoryList = Category::getList();
        return view('book/index', ['name' => $name, 'categoryID' => $categoryID, 'categoryList' => $categoryList]);
    }

    public function add(Request $request)
    {
        $method = $request->method();
        if($method == 'POST')
        {
            $name = $request->post('name');
            $categoryID = $request->post('category_id');
            $author = $request->post('author');
            $remark = $request->post('remark');

            $old = Book::getByName($name);
            if(!$old->isEmpty()){
                return json(['code' => 1, 'msg' => '已存在此书']);
            }

            $bookModel = new Book;
            $bookModel->save(['name' => $name, 'category_id' => $categoryID, 'author' => $author, 'remark' => $remark]);

            return json(['code' => 0, 'msg' => 'ok']);
        }

        $categoryList = Category::getList();
        return view('book/add', ['categoryList' => $categoryList]);
    }

    public function edit(Request $request)
    {
        $method = $request->method();
        if($method == 'POST')
        {
            $name = $request->post('name');
            $categoryID = $request->post('category_id');
            $author = $request->post('author');
            $remark = $request->post('remark');
            $id = $request->post('id');

            Book::updateByID($id, ['name' => $name, 'category_id' => $categoryID, 'author' => $author, 'remark' => $remark]);

            return json(['code' => 0, 'msg' => 'ok']);
        }

        $id = $request->get('id');
        $book = Book::getByID($id);
        $categoryList = Category::getList();

        return view('book/edit', ['book' => $book, 'categoryList' => $categoryList]);
    }

    //读书清单
    public function reading(Request $request)
    {
        $name = $request->get('name', '');
        return view('book/reading', ['name' => $name]);
    }

    public function addReading(Request $request)
    {
        $method = $request->method();
        if($method == 'POST')
        {
            $name = $request->post('name');

            $readingModel = new Reading;
            $readingModel->save(['name' => $name]);

            return json(['code' => 0, 'msg' => 'ok']);
        }

        return view('book/addReading');
    }

    public function editReading(Request $request)
    {
        $method = $request->method();
        if($method == 'POST')
        {
            $name = $request->post('name');
            $id = $request->post('id');

            Reading::updateByID($id, ['name' => $name]);

            return json(['code' => 0, 'msg' => 'ok']);
        }

        $id = $request->get('id');
        $reading = Reading::getByID($id);

        return view('book/editReading', ['reading' => $reading]);
    }

    public function readingBook(Request $request)
    {
        $id = $request->get('id', 0);
        $reading = Reading::getByID($id);
        return view('book/readingBook', ['reading' => $reading]);
    }

    public function addReadingBook(Request $request)
    {
        $method = $request->method();
        if($method == 'POST')
        {
            $readingID = $request->post('reading_id');
            $bookID = $request->post('book_id');
            $status = $request->post('status');
            $start = $request->post('start');
            $end = $request->post('end');
            $remark = $request->post('remark');

            ReadingBook::create([
                'reading_id' => $readingID,
                'book_id' => $bookID,
                'status' => $status,
                'start' => $start,
                'end' => $end,
                'remark' => $remark
            ]);
            return json(['code' => 0, 'msg' => 'ok']);
        }

        $readingID = $request->get('reading_id', 0);
        $reading = Reading::getByID($readingID);
        $categoryList = Category::getList();
        $bookList = Book::getPageList(0, '', 50);

        return view('book/addReadingBook', ['readingID' => $readingID, 'reading' => $reading, 'categoryList' => $categoryList, 'bookList' => $bookList]);
    }

    public function editReadingBook(Request $request)
    {
        $method = $request->method();
        if($method == 'POST')
        {
            $id     = $request->post('id');
            $bookID = $request->post('book_id');
            $status = $request->post('status');
            $start  = $request->post('start', '');
            $end    = $request->post('end', '');
            $remark = $request->post('remark');

            ReadingBook::updateByID($id, [
                'book_id' => $bookID,
                'status'  => $status,
                'start'   => $start,
                'end'     => $end,
                'remark'  => $remark
            ]);

            return json(['code' => 0, 'msg' => 'ok']);
        }

        $readingBookID = $request->get('id', 0);
        $readingBook   = ReadingBook::getByID($readingBookID);
        $reading       = Reading::getByID($readingBook->reading_id);
        $categoryList  = Category::getList();
        $bookList      = Book::getPageList(0, '', 50);
        $book          = Book::getByID($readingBook->book_id);

        return view('book/editReadingBook', ['reading' => $reading, 'categoryList' => $categoryList, 'bookList' => $bookList, 'readingBook' => $readingBook, 'book' => $book]);
    }

    public function apiGetList(Request $request)
    {
        $name = $request->get('name', '');
        $categoryID = $request->get('category_id', 0);
        $pageSize = $request->get('size', 20);
        $bookList = Book::getPageList($categoryID, $name, $pageSize);

        return json(['code' => 0, 'msg' => 'ok', 'bookList' => $bookList]);
    }

    public function apiGetReadingList(Request $request)
    {
        $name = $request->get('name', '');

        $readingList = Reading::getPageList($name);

        return json(['code' => 0, 'msg' => 'ok', 'readingList' => $readingList]);
    }

    public function apiGetReadingBookList(Request $request)
    {
        $readingID = $request->get('id', 0);

        $bookList = ReadingBook::getList($readingID);
        return json(['code' => 0, 'msg' => 'ok', 'bookList' => $bookList]);
    }

    //排序
    public function apiSortReadingBook(Request $request)
    {
        $id   = $request->post('id');
        $num  = $request->post('num');
        $sort = 'down';

        if($num < 0) $sort = 'up';
        $num = abs($num);

        $item = ReadingBook::getByID($id);

        $whereMap = [];
        $whereMap[] = ['reading_id', '=', $item['reading_id']];

        if ($sort == 'up'){
            $whereMap[] = ['sort', '<', $item['sort']];
            $order      = "sort desc";
        }else{
            $whereMap[] = ['sort', '>', $item['sort']];
            $order      = "sort asc";
        }

        $forSortItems = ReadingBook::getListByWhereAndOrder($whereMap, $order, $num);

        if (!empty($forSortItems)){
            $updateData   = [];
            $forSortCount = count($forSortItems);
            for ($i = 0; $i < $forSortCount; $i++){
                if ($i == 0){
                    $updateData[] = [
                        'id'   => $forSortItems[$i]['id'],
                        'sort' => $item['sort']
                    ];
                }else{
                    $updateData[] = [
                        'id'   => $forSortItems[$i]['id'],
                        'sort' => $forSortItems[$i - 1]['sort']
                    ];
                }
            }

            $updateData[] = [
                'id'   => $item['id'],
                'sort' => $forSortItems[$i - 1]['sort']
            ];
            if (!empty($updateData)){
                $model = new ReadingBook();
                $model->saveAll($updateData);

                return json(['code' => 0, 'msg' => 'ok']);
            }
        }
        return json(['code' => 1, 'msg' => '无须调整排序!']);
    }

    public function apiDelReadingBook(Request $request)
    {
        $id = $request->get('id');
        $readingBook = ReadingBook::getByID($id);
        if(empty($readingBook)) return json(['code' => 1, 'msg' => '清单中无此书!']);

        ReadingBook::destroy($id);

        return json(['code' => 0, 'msg' => 'ok']);
    }

    public function apiStartReadingBook(Request $request)
    {
        $id          = $request->post('id');
        $readingBook = ReadingBook::getByID($id);

        if(empty($readingBook)) return json(['code' => 1, 'msg' => '清单中无此书!']);

        if(!empty($readingBook->start)) return json(['code' => 2, 'msg' => '已经开始,无需重新设置']);

        ReadingBook::updateByID($id, [
            'status' => 'reading',
            'start'  => date('Y-m-d')
        ]);

        return json(['code' => 0, 'msg' => 'ok']);
    }

    public function apiEndReadingBook(Request $request)
    {
        $id = $request->post('id');
        $readingBook = ReadingBook::getByID($id);
        if(empty($readingBook)) return json(['code' => 1, 'msg' => '清单中无此书!']);

        if($readingBook->status == 'finished') return json(['code' => 2, 'msg' => '已经结束,无需重新设置']);

        $end = date('Y-m-d');
        ReadingBook::updateByID($id, ['end' => $end, 'status' => 'finished']);

        BookLog::create([
            'book_id' => $readingBook->book_id,
            'start' => $readingBook->start,
            'end' => $end
        ]);

        return json(['code' => 0, 'msg' => 'ok']);
    }
}