<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/12/1
 * Time: 11:37
 */

namespace App\Http\Controllers\Channel\Web;

use App\Http\Controllers\Controller;
use App\Modules\Finance\Services\FinancialStatService;
use App\Modules\Trade\Services\OrderStatService;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use App\Modules\Finance\Services\CommissionRateService;
use App\Modules\Channel\Services\ChannelService;
use Illuminate\Http\Request;

class WelcomeController extends Controller
{
   

    public function index(Request $request)
    {
        $domain = _domain();
        $options = [];
        $current_channel_id = $request->has('current_channel_id') ? (int)$request->input('current_channel_id') : 0;
        $channel_user_id = session('ydyauth');
        if($current_channel_id && $channel_user_id)//切换当前渠道session
        {
            $channel = ChannelService::getUserChannel($current_channel_id,$channel_user_id);
            if($channel) $request->session()->put('ydychannel', serialize($channel));
        }

        if(session('ydychannel'))
        {
            $distribution_channel = unserialize(session('ydychannel'));
            $distribution_channel_id = $distribution_channel->id;
            //订单相关统计
            $order_stat = OrderStatService::getChannelStat($distribution_channel_id);
            //结算、提现相关统计
            $finance_stat = FinancialStatService::getFinancialStatSingle($distribution_channel_id);
            //获取授权公众号列表
            $official_accounts = OfficialAccountService::officialAuthAccountBydistributionChannelId(compact('distribution_channel_id'));

            //获取当前结算比例
            $rate = CommissionRateService::getRateCommissionRate($distribution_channel_id);
            $channel_company = ChannelService::getChannelCompanyInfo($distribution_channel_id);
            $company_gender = 2;
            if($channel_company && isset($channel_company->fans_gender)) {
                $company_gender = $channel_company->fans_gender;
                if(!in_array($company_gender,[1,2,'1','2'])) {
                    $company_gender = 2;
                }
            }
            //\Log::info('company_gender:'.$company_gender);
            //统计信息
            $options =[
                'total_recharge_amount'=>$order_stat ? (float)$order_stat->total_recharge_amount : 0,
                'paid_number'=>$order_stat ? (float)$order_stat->paid_num : 0,
                'unpaid_number'=>$order_stat ? (float)$order_stat->unpaid_num : 0,
                'accumulative_withdrawal_amount'=>$finance_stat ? number_format($finance_stat->accumulative_withdrawal_amount,2) : 0,
                
                'enable_withdrawal_amount'=>$finance_stat ? (float)$finance_stat->enable_withdrawal_amount : 0,
                'latest_withdrawal_amount'=>$finance_stat ? (float)$finance_stat->latest_withdrawal_amount : 0,
                'latest_withdraw_time'=>$finance_stat && $finance_stat->latest_withdraw_time ? $finance_stat->latest_withdraw_time : '',
                'commission_rate'=>(string)($rate*100).'%',
                'withdraw_pending_amount'=>$finance_stat ? (float)$finance_stat->withdraw_pending_amount : 0,

                //昨日统计数据
                'yesterday_recharge_amount'=>$order_stat ? (float)$order_stat->yesterday_recharge_amount : 0,
                'yesterday_paid_num'=>$order_stat ? (float)$order_stat->yesterday_paid_num : 0,
                'yesterday_unpaid_num'=>$order_stat ? (float)$order_stat->yesterday_unpaid_num : 0,

                'yesterday_ticket_recharge_paid_num'=>$order_stat ? (float)$order_stat->yesterday_ticket_recharge_paid_num : 0,
                'yesterday_ticket_recharge_unpaid_num'=>$order_stat ? (float)$order_stat->yesterday_ticket_recharge_unpaid_num : 0,
                'yesterday_ticket_recharge_amount'=>$order_stat ? (float)$order_stat->yesterday_ticket_recharge_amount : 0,

                'yesterday_year_recharge_paid_num'=>$order_stat ? (float)$order_stat->yesterday_year_recharge_paid_num : 0,
                'yesterday_year_recharge_unpaid_num'=>$order_stat ? (float)$order_stat->yesterday_year_recharge_unpaid_num : 0,
                'yesterday_year_recharge_amount'=>$order_stat ? (float)$order_stat->yesterday_year_recharge_amount : 0,

                //当月统计数据
                'current_month_recharge_amount'=>$order_stat ? (float)$order_stat->current_month_recharge_amount : 0,
                'current_month_paid_num'=>$order_stat ? (float)$order_stat->current_month_paid_num : 0,
                'current_month_unpaid_num'=>$order_stat ? (float)$order_stat->current_month_unpaid_num : 0,

                'current_month_ticket_recharge_paid_num'=>$order_stat ? (float)$order_stat->current_month_ticket_recharge_paid_num : 0,
                'current_month_ticket_recharge_unpaid_num'=>$order_stat ? (float)$order_stat->current_month_ticket_recharge_unpaid_num : 0,
                'current_month_ticket_recharge_amount'=>$order_stat ? (float)$order_stat->current_month_ticket_recharge_amount : 0,

                'current_month_year_recharge_paid_num'=>$order_stat ? (float)$order_stat->current_month_year_recharge_paid_num : 0,
                'current_month_year_recharge_unpaid_num'=>$order_stat ? (float)$order_stat->current_month_year_recharge_unpaid_num : 0,
                'current_month_year_recharge_amount'=>$order_stat ? (float)$order_stat->current_month_year_recharge_amount : 0,

                //上月统计数据
                'last_month_recharge_amount'=>$order_stat ? (float)$order_stat->last_month_recharge_amount : 0,
                'last_month_paid_num'=>$order_stat ? (float)$order_stat->last_month_paid_num : 0,
                'last_month_unpaid_num'=>$order_stat ? (float)$order_stat->last_month_unpaid_num : 0,

                'last_month_ticket_recharge_paid_num'=>$order_stat ? (float)$order_stat->last_month_ticket_recharge_paid_num : 0,
                'last_month_ticket_recharge_unpaid_num'=>$order_stat ? (float)$order_stat->last_month_ticket_recharge_unpaid_num : 0,
                'last_month_ticket_recharge_amount'=>$order_stat ? (float)$order_stat->last_month_ticket_recharge_amount : 0,

                'last_month_year_recharge_paid_num'=>$order_stat ? (float)$order_stat->last_month_year_recharge_paid_num : 0,
                'last_month_year_recharge_unpaid_num'=>$order_stat ? (float)$order_stat->last_month_year_recharge_unpaid_num : 0,
                'last_month_year_recharge_amount'=>$order_stat ? (float)$order_stat->last_month_year_recharge_amount : 0,

                'domain'=>get_channel_domain($distribution_channel_id),
                'distribution_channel_id'=>$distribution_channel_id,
                'is_bind_gzh'=>count($official_accounts) ? 1 : 0,
                'lowest_withdraw_money'=>(float)env('LOWEST_WITHDRAW_MONEY'),
                'phone'=>$distribution_channel->phone,
                'company_gender'=>$company_gender,
                'show_new_book_page'=>0
            ];
            $show_new_book_page = env('SHOW_NEW_BOOK_PAGE');
            if($show_new_book_page){
                if($show_new_book_page == 'all'){
                    $options['show_new_book_page'] = 1;
                }elseif(in_array($channel_user_id,explode(',',$show_new_book_page))){
                    $options['show_new_book_page'] = 1;
                }
            }
            if($options['is_bind_gzh']){
                $inner_setting = env('CHANNEL_INNER_ROLE',5);
                $inner = explode(',',$inner_setting);
                if(in_array($distribution_channel_id,$inner)){
                    $options['role'] = 'inner-bind';
                }else{
                    $options['role'] = 'outer-bind';
                }

            }else{
                $options['role'] = 'outer';
            }
        }

        $options = json_encode($options);
        return view('channel.web.welcome.index',compact('options'));
    }

    public function logout(Request $request)
    {
        $request->session()->forget('ydyauth');
        $request->session()->forget('ydychannel');
        return redirect()->secure('/passport/login');
    }

    public function setCurrentChannel($appid)
    {

    }

}