<?php

namespace App\Http\Controllers\WapBrowser;

use Illuminate\Http\Request;
use App\Http\Controllers\WapBrowser\BaseController;
use App\Modules\Book\Services\BookCategoryService;
use App\Modules\Book\Services\BookConfigService;
use App\Http\Controllers\Wap\Book\Transformers\BookTransformer;


class BookController extends BaseController
{

    /* 书库 */
    public function stock(Request $request)
    {
        $categories = BookCategoryService::getCategory(true);
        return view('wap-browser.stock', [
            'title' => '书库',
            'categories' => $categories,
        ]);
    }

    /* 书库 */
    public function search(Request $request)
    {
        return view('wap-browser.search', [
            'title' => '搜索',
        ]);
    }

    public function library(Request $request)
    {
        $where = [];
        //$order = ['book_configs.id','asc'];
        $order = [];
        $where['is_on_shelf'] = [2];

        $category_id = $request->input('category_id');
        if($category_id){
            if($category_id == 1){
                $where['channel_name'] = '男频';
            }elseif($category_id == 2){
                $where['channel_name'] = '女频';
            }else{
                $where['category_id'] = $category_id;
            }
        }
        $key = $request->input('key');
        $where['key'] = $key;
        $order_field = $request->input('order_field');
        $order_seq = $request->input('order_seq');
        if($order_field != '' && in_array($order_field,['recommend_index','click_count','update','size','create'])){
            if($order_field == 'update'){
                $order = ['book_configs.updated_at','desc'];
            }elseif ($order_field == 'create') {
                $order = ['book_configs.created_at','desc'];
            }
            else{
                $order = [$order_field,'desc'];
            }

            if($order_seq == 'asc'){
                $order = [$order_field,'asc'];
            }
            if($order_seq == 'desc'){
                $order = [$order_field,'desc'];
            }
        }
        $status = $request->input('status');
        if($status != ''){
            $where['status'] = $status;
        }
        $page_size = $request->input('page_size',15);
        $books = BookConfigService::getBooks($where,$order,$page_size);
        return response()->pagination(new BookTransformer,$books);
    }
}
