<?php

namespace App\Http\Controllers\Wap\Help;

use App\Modules\Help\Services\HelpService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\OfficialAccount\Services\CustomMsgService;
use DB;

class HelpController extends Controller
{
    public function index(Request $request)
    {
        $lists = HelpService::getEnableHelpList();
        $distribution_channel_id = $request->get('distribution_channel_id', 0);
        $qrcode = 'https://cdn-novel.iycdm.com/static/img/kefu20190319.jpg';
        if ($distribution_channel_id) {

            if(!is_numeric($distribution_channel_id)){
                $distribution_channel_id = decodeDistributionChannelId($distribution_channel_id);
            }

            $setting = CustomMsgService::customerImgUrlByChannelId($distribution_channel_id);
            if ($setting && $setting->customer_img_url) {
                $qrcode = $setting->customer_img_url;
            }

        }
        $title_array = [];
        if ($lists) {
            foreach ($lists as $list) {
                $title_array[] = $list->title;
            }
        }

        return view('help.help', ['title_array' => $title_array, 'lists' => $lists, 'qrcode' => $qrcode]);
    }

    public function helpDetail($id)
    {
        $content = HelpService::getById($id);
        return view('help.helpDetail', ['data' => $content]);
    }

    public function getLinkByAppid(Request $request){
        $appid = $request->get('appid','');
        $data = [
            'distribution_channel_id'=>0,
            'en_distribution_channel_id'=>0,
            'link'=>''
        ];
        $channel = DB::table('official_accounts')->where('appid',$appid)->select('distribution_channel_id')->first();
        if($channel){
            $distribution_channel_id = $channel->distribution_channel_id;
            $data['distribution_channel_id'] = $distribution_channel_id;
            $data['en_distribution_channel_id'] = encodeDistributionChannelId($distribution_channel_id);
            $url_format = '%s://site%s.%s.com/';
            $help_pay_page = sprintf(
                $url_format,
                env('PROTOCOL'),
                $data['en_distribution_channel_id'],
                env('CUSTOM_HOST')
            );
            $data['link'] = $help_pay_page;
        }
        return response()->success($data);
    }
}
