<?php

namespace App\Http\Controllers\Manage\Book;

use App\Http\Controllers\Controller;

use App\Modules\Book\Models\BookTags;
use App\Modules\Book\Services\BookTagsService;
use App\Modules\Book\Services\TagsService;
use Illuminate\Http\Request;

class BooksTagController extends Controller
{
    public function getTags(){
        $tags = TagsService::getAllTags();
        return response()->success($tags);
    }

    public function getSearchBooks(Request $request){
        if(!$request->has('tags')) {
            return response()->error('PARAM_EMPTY');
        }
        $tags = $request->input('tags');
        $page_size = $request->input('page_size',15);
        $books = BookTagsService::getSearchBooks($tags);
        $res = [];
        foreach ($books as $book){
               $res[] = ['book_id' => $book->bid,
                'book_name' => $book->book_name,
                'book_summary' => $book->intro,
                'book_author' => $book->author,
                'cover_url' => $book->cover,
                'book_word_count' => $book->size,
                'book_chapter_total' => $book->chapter_count,
                'book_category_id' => $book->category_id,
                'book_category' => $book->category_name,
                'book_end_status' => $book->status,
                'book_published_time' => isset($book->updated_at) ? '' : $book->updated_at,
                'copyright' => isset($book->copyright) ? '' : $book->copyright,
                'charge_type' => isset($book->charge_type) ? '' : $book->charge_type,
                'force_subscribe_chapter_id' => $book->force_subscribe_chapter_seq,
                'update_time' => $book->updated_at,
                'is_on_shelf' => $book->is_on_shelf,
                //'book_price' => isset($book->price) ? 0 : $book->price,
                'keyword' => $book->keyword,
                //'roles' => isset($book->roles) ? $book->roles : '',
                'recommend_index' => $book->recommend_index,
                'is_show_index_content' => $book->is_show_index_content,
                'click_count' => $book->click_count,
                'product_id' => $book->product_id,
                'sex_preference' => $book->channel_name,
                'last_cid' => $book->last_cid,
                'last_chapter' => $book->last_chapter,
                'first_cid' => $book->first_cid,
                'category_array' => [$book->channel_name == '男频' ? 1 : 2, $book->category_id],
                'copyright_limit_data' => isset($book->copyright_limit_data) ? '' : $book->copyright_limit_data,
                'promotion_domain' => $book->promotion_domain,
                'old_name' => $book->old_name,
                'recommend_text' => $book->recommend_text,
                'book_special_channels' => $book->book_special_channels,
                'is_high_quality' => $book->is_high_quality,
                'is_promotion' => isset($book->is_promotion) ? $book->is_promotion : 0,
                'tags'=>BookTagsService::getBookTags($book->bid),
            ];
        }
        return response()->success($books);
    }

    public function addBookTags(Request $request) {
        if(!$request->has('tags')) {
            return response()->error('PARAM_EMPTY');
        }
        if(!$request->has('bid')) {
            return response()->error('PARAM_EMPTY');
        }
        $bid = $request->input('bid');
        $tags = $request->input('tags');
        BookTags::addBookTag($bid,$tags);
        return response()->success();
    }

    public function deleteBookTags(Request $request){
        if(!$request->has('tags')) {
            return response()->error('PARAM_EMPTY');
        }
        if(!$request->has('bid')) {
            return response()->error('PARAM_EMPTY');
        }
        $bid = $request->input('bid');
        $tags = $request->input('tags');
        if(BookTags::deleteTagBook($bid,$tags)){
            return response()->success();
        }
        return response()->error('PARAM_ERROR');
    }
}
