<?php

namespace App\Http\Controllers\Channel;

use App\Exceptions\ApiException;
use App\Libs\ApiResponse;
use App\Http\Controllers\Controller;
use App\Libs\Utils;
use App\Services\Channel\ChannelHomeService;
use App\Transformer\Channel\ChannelTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ChannelHomeController extends Controller
{
    use ApiResponse;
    private $channelHomeService;

    public function __construct(
        ChannelHomeService $channelHomeService
    ) {
        $this->channelHomeService = $channelHomeService;
    }

    public function index(Request $request)
    {
        return view('index');
    }

    /**
     * 获取当前站点信息
     * @return mixed
     */
    public function channelInfo() {
        $result = $this->channelHomeService->getChannelInfo();

        return $this->success($result);
    }

    /**
     * 设置全站点按本或按章及价格
     * @param Request $request
     * @return mixed
     */
    public function setChannelBookChargeType(Request $request) {
        $data = $request->all();
        $validator = Validator::make($data, [
            'book_charge_type'                      => 'required|in:MERGE,HYBRID',
            'book_calculate_price_type'             => 'required_if:book_charge_type,MERGE,HYBRID|in:bywords,const,all',
            'book_coin'                             => 'required_if:book_charge_type,MERGE,HYBRID|numeric',
            'chapter_calculate_price_type'          => 'required_if:book_charge_type,HYBRID|in:bywords,const,all',
            'chapter_coin'                          => 'required_if:book_charge_type,HYBRID|numeric',
        ], [
            'book_charge_type.required'                 => '请选择站点类型',
            'book_charge_type.in'                       => '站点类型选择范围不正确',
            'book_calculate_price_type.required_if'     => '请选择短篇书籍收费方式',
            'book_calculate_price_type.in'              => '短篇书籍收费方式选择范围不正确',
            'book_coin.required_if'                     => '请填写短篇书籍价格',
            'book_coin.numeric'                         => '短篇书籍价格格式不正确',
            'chapter_calculate_price_type.required_if'  => '请选择长篇书籍收费方式',
            'chapter_calculate_price_type.in'           => '长篇书籍收费方式选择范围不正确',
            'chapter_coin.required_if'                  => '请填写长篇书籍价格',
            'chapter_coin.numeric'                      => '长篇书籍价格格式不正确',
        ]);

        if ($validator->fails()) {
            $errors = $validator->errors();
            Utils::throwError('1003:'.$errors->all()[0]);
        }
        $result = $this->channelHomeService->setChannelBookChargeType($data);

        return $this->success(['success'=>$result ? 1 : 0]);
    }

    /**
     * 获取子账号信息
     * @return mixed
     */
    public function getSubUser() {
        $result = $this->channelHomeService->getSubUser();

        return $this->success($result);
    }

    /**
     * 站点总数据
     * @param Request $request
     * @return mixed
     */
    public function statisticsByTotal(Request $request) {
        $data   = $request->all();
        $result = $this->channelHomeService->statisticsByTotal($data);

        return $this->success($result);
    }

    /**
     * 站点数据按日明细
     * @param Request $request
     * @return mixed
     */
    public function statisticsByDay(Request $request) {
        $data   = $request->all();
        $result = $this->channelHomeService->statisticsByDay($data);

        return $this->success($result, [new ChannelTransformer(), 'newBuildStatisticsByDay']);
    }

    /**
     * 导出站点统计日明细
     *
     * @param Request $request
     * @return mixed
     */
    public function exportStatisticsByDay(Request $request)
    {
        $data   = $request->all();
        $result = $this->channelHomeService->exportStatisticsByDay($data);

        return $this->success(['success' => $result ? 1 : 0]);
    }

    /**
     * 站点数据按日明细
     * @param Request $request
     * @return mixed
     */
    public function statisticsByDayForMaster(Request $request) {
        $data   = $request->all();
        $result = $this->channelHomeService->statisticsByDayForMaster($data);

        return $this->success($result, [new ChannelTransformer(), 'newBuildStatisticsByDayForMaster']);
    }

    /**
     * 导出站点统计日明细
     *
     * @param Request $request
     * @return mixed
     */
    public function exportStatisticsByDayForMaster(Request $request)
    {
        $data   = $request->all();
        $result = $this->channelHomeService->exportStatisticsByDayForMaster($data);

        return $this->success(['success' => $result ? 1 : 0]);
    }

    /**
     * 站点数据按月明细
     * @param Request $request
     * @return mixed
     */
    public function statisticsByMonth(Request $request) {
        $data   = $request->all();
        $result = $this->channelHomeService->statisticsByMonth($data);

        return $this->success($result, [new ChannelTransformer(), 'newBuildStatisticsByMonth']);
    }

    /**
     * 导出站点统计月明细
     *
     * @param Request $request
     * @return mixed
     */
    public function exportStatisticsByMonth(Request $request)
    {
        $data   = $request->all();
        $result = $this->channelHomeService->exportStatisticsByMonth($data);

        return $this->success(['success' => $result ? 1 : 0]);
    }

    /**
     * 站点数据按月明细
     * @param Request $request
     * @return mixed
     */
    public function statisticsByMonthForMaster(Request $request) {
        $data   = $request->all();
        $result = $this->channelHomeService->statisticsByMonthForMaster($data);

        return $this->success($result, [new ChannelTransformer(), 'newBuildStatisticsByMonthForMaster']);
    }

    /**
     * 导出站点统计月明细
     *
     * @param Request $request
     * @return mixed
     */
    public function exportStatisticsByMonthForMaster(Request $request)
    {
        $data   = $request->all();
        $result = $this->channelHomeService->exportStatisticsByMonthForMaster($data);

        return $this->success(['success' => $result ? 1 : 0]);
    }
}
