<?php

namespace App\Http\Controllers\Order;


use App\Exceptions\ApiException;
use App\Libs\ApiResponse;
use App\Services\Book\BookService;
use App\Services\Order\OrderService;
use App\Services\User\UserService;
use App\Transformer\Order\OrderTransformer;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;

class OrderController extends BaseController
{
    use ApiResponse;

    protected $bookService;
    protected $userService;
    protected $orderService;

    public function __construct(
        BookService  $bookService,
        UserService  $userService,
        OrderService $orderService
    )
    {
        $this->bookService  = $bookService;
        $this->userService  = $userService;
        $this->orderService = $orderService;
    }

    /**
     * 订单明细
     *
     * @param Request $request
     * @return mixed
     */
    public function orderData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->getOrderData($data);

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

    /**
     * 用户信息
     *
     * @param Request $request
     * @return mixed
     */
    public function userData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->getUserData($data);

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

    /**
     * 回传日志
     *
     * @param Request $request
     * @return mixed
     */
    public function reportData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->getReportData($data);

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

    /**
     * 导出回传日志
     *
     * @param Request $request
     * @return mixed
     */
    public function exportReportData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->exportReportData($data);

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

    /**
     * 派单列表
     *
     * @param Request $request
     * @return mixed
     */
    public function sendOrderData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->getSendOrderData($data);

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

    /**
     * 导出派单数据
     *
     * @param Request $request
     * @return mixed
     */
    public function exportSendOrderData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->exportSendOrderData($data);

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

    /**
     * 获取派单链接适用的模板
     * @param Request $request
     * @return mixed
     */
    public function sendOrderTemplates(Request $request) {
        $data   = $request->all();
        $result = $this->orderService->getSendOrderTemplates($data);

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

    /**
     * 编辑派单链接
     *
     * @param Request $request
     * @return mixed
     */
    public function editSendOrderUrl(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->editSendOrderUrl($data);

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

    /**
     * 派单日数据
     *
     * @param Request $request
     * @return mixed
     */
    public function sendOrderDayData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->getSendOrderDayData($data);

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

    /**
     * 导出派单日数据
     *
     * @param Request $request
     * @return mixed
     */
    public function exportSendOrderDayData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->exportSendOrderDayData($data);

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

    /**
     * 设置派单日成本
     *
     * @param Request $request
     * @return mixed
     */
    public function setDayCost(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->setDayCost($data);

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

    /**
     * 充值模板
     * @param Request $request
     * @return mixed
     */
    public function templateList(Request $request) {
        $data   = $request->all();
        $result = $this->orderService->getTemplateList($data);

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

    /**
     * 设置充值模板启用状态
     * @param Request $request
     * @return mixed
     */
    public function setTemplateStatus(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->setTemplateStatus($data);

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

    /**
     * 新增模版
     * @param Request $request
     * @return mixed
     */
    public function addTemplate(Request $request) {
        $data   = $request->all();
        $result = $this->orderService->addTemplate($data);

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

    /**
     * 编辑模版
     * @param Request $request
     * @return mixed
     */
    public function editTemplate(Request $request) {
        $data   = $request->all();
        $result = $this->orderService->editTemplate($data);

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

    /**
     * 推广数据
     *
     * @param Request $request
     * @return mixed
     */
    public function promotionData(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->getPromotionData($data);

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

    /**
     * 补回传
     *
     * @param Request $request
     * @return mixed
     */
    public function reportPromotionOrder(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->reportPromotionOrder($data);

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

    /**
     * 订单列表
     *
     * @param Request $request
     * @return mixed
     * @throws ApiException
     */
    public function orderList(Request $request)
    {
        $data   = $request->all();
        $result = $this->orderService->orderList($data);
        return $this->success($result, [new OrderTransformer(), 'orderList']);
    }

    /**
     * 订单列表导出
     *
     * @param Request $request
     * @return mixed
     * @throws ApiException
     */
    public function orderExport(Request $request)
    {
        $data = $request->all();
        $result  = $this->orderService->exportOrderList($data);
        return response($result)->header('Content-type', 'application/vnd.ms-excel');
    }
}
