<?php

namespace App\Http\Controllers\Agents\User;

use App\Http\Controllers\Agents\User\Transformers\SendOrderTransformer;
use App\Modules\Agents\Models\AgentsManage;
use App\Modules\Agents\Services\AgentsManageService;
use App\Modules\Book\Services\BookConfigService;
use App\Modules\OfficialAccount\Services\ForceSubscribeService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\Trade\Services\OrderService;
use App\Modules\User\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class SendOrdersController extends Controller
{

    public function getSendorderList()
    {

    }

    function getSendOrderListByAgentName(Request $request)
    {
        $agentName = $request->session()->get('agent_auth');
        $agent = AgentsManage::where('id',$agentName)->first();
        $agentName = $agent->agent_name;

        /*$distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if (empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }*/
        //echo $agentName;
        if ($agentName) {
            $sendOrders = AgentsManageService::getSendOrdersByAgentName($agentName, false);
            //var_dump($sendOrders);die;
            foreach ($sendOrders as $item) {
                $sendOrderId = $item->id;
                $item->name=mb_convert_encoding($item->name, 'UTF-8', 'UTF-8');
                $item->book_name=mb_convert_encoding($item->book_name, 'UTF-8', 'UTF-8');
                $item->chapter_name=mb_convert_encoding($item->chapter_name, 'UTF-8', 'UTF-8');
                $browser_visit_info = SendOrderService::getBrowserUvAndPv($sendOrderId);
                $item->browserClickNum = $browser_visit_info['pv'];
                $item->browserClickNumUV = $browser_visit_info['uv'];
                $item->clickNum = SendOrderService::getPv($sendOrderId);
                $item->clickNumUV = SendOrderService::getUv($sendOrderId);//uv
                $item->payUserNum = OrderService::getPayUserNum($sendOrderId);
                $item->registerNum = UserService::getPromotionTotal($sendOrderId);
                $item->fansNum = ForceSubscribeService::getNewFansNum($sendOrderId);
                $item->continue_read_uv = SendOrderService::getContinueTotalReadUv($item->id);
                $item->promotion_url = 'https://site' . encodeDistributionChannelId($item->distribution_channel_id) . '.' . $this->getDomainByBid($item->book_id) . '/yun/' . $item->id;
                $item->totalChargeAmount = (float)OrderService::getAmount(['send_order_id' => $sendOrderId]);
                $item->qrcode = "";//"data:image/png;base64," . base64_encode(QrCode::format('png')->size(200)->generate($promotion_url));

                $item->consumeChargeFee = 0;//$bookOrderInfo['charge_balance'] + $chapterOrderInfo['charge_balance'];
                $item->consumeRewardFee = 0;//$bookOrderInfo['reward_balance'] + $chapterOrderInfo['reward_balance'];;
                $item->consumeTotalFee = 0;//$item->consumeChargeFee + $item->consumeRewardFee;
            }

            return response()->pagination(new SendOrderTransformer, $sendOrders);
        }
    }

    /**
     * 根据图书id获取域名
     * @param $bid
     */
    function getDomainByBid($bid = '')
    {
        $domain = 'leyuee.com';
        //如果图书id为空，则返回默认的域名
        if (!empty($bid)) {
            $bookConfig = BookConfigService::getBookById($bid);
            if ($bookConfig) {
                $domain = $bookConfig->promotion_domain;
            }
        }
        return $domain;
    }
}
