<?php

namespace App\Http\Controllers\Wap\IndividualWechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\Activity\Services\UserNationalDay;
use DB;

/**
 * 
 */
class ActivityController extends Controller
{
    /**
     * 国庆活动
     */
    public function national(Request $request)
    {
        $uid = $this->getUserColumnValue('id');
        $channel_id = $this->getUserColumnValue('channel_id');
        $userNationalDay = new UserNationalDay($uid);
        $is_sign = $userNationalDay->is_sign;
        $userNationalDay->sign();
        $sign_configs = $userNationalDay->getSignConfigs();
        $bonus = $userNationalDay->bonus;
        $is_begin = $userNationalDay->is_begin;
        $is_end = $userNationalDay->is_end;
        $continue_link = sprintf(
            '%s://site%s.%s.com/recent',
            env('PROTOCOL'),
            encodeDistributionChannelId($this->getUserColumnValue('channel_id')),
            env('CUSTOM_HOST')
        );
        $pay_link = env('CREATE_PAY_URL') . '?' . http_build_query([
            'product_id' => $userNationalDay->product_id,
            'uid' => $uid, 'distribution_channel_id' => $channel_id,
            'pay_redirect_url' => $continue_link, 'activity_id' => 15871, 'crm' => 'crm_national'
        ]);
        $this->statsDetail($uid, 'crm_national');
        return view('crm.notionalDay')->with(compact('sign_configs', 'is_sign', 'bonus', 'is_begin', 'is_end', 'continue_link', 'pay_link'));
    }

    private function getUserColumnValue(string $column)
    {
        $user = app()->make('user');
        if ($user) {
            return $user->$column;
        }
    }

    private function statsDetail($uid, $page, $bid = 0)
    {
        try {
            DB::table('crm_visit_detail')->insert([
                'uid' => $uid, 'page' => $page,
                'bid' => $bid, 'day' => date('Y-m-d'),
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]);
        } catch (\Exception $e) { }
    }
}
