<?php

namespace App\Web\Controllers;

use App\Ad\Facades\SysAdFacade;
use App\Form\Facades\FormRecordFacade;
use App\Http\Controllers\BaseController;
use App\User\Facades\AttachmentFacade;
use App\Web\Facades\MeetingFacade;
use App\Web\Facades\SysGlobalConfigFacade;
use App\Web\Services\BlogService;
use App\Web\Services\WebService;
use Illuminate\Http\Request;

class WebController extends BaseController
{

    protected $service = null;

    /**
     * 构造函数
     * BlogController constructor.
     * @param BlogService $service
     */
    public function __construct(WebService $service)
    {
        $this->service = $service;
    }


    /**
     * 获取静态页面列表
     * */
    public function getStaticPageList(Request $request){
        $params=$request->only(['page_size','page','status','keyword']);
        $ret=$this->service->getStaticPageList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取静态页面详情
     * */
    public function getStaticPageInfo(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => '文章id不能为空',
        ]);
        $id=$request->input('id',0);
        $ret=$this->service->getStaticPageInfo(['id'=>$id]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    /**
     * 获取网页菜单
     * */
    public function getPageMenuInfo(Request $request){
        $this->validate($request, [
            'route_path' => 'required',
        ], [
            'route_path.required' => '路由不能为空',
        ]);
        $route_path=$request->input('route_path','');
        $ret=$this->service->getPageMenuInfo(['route_path'=>$route_path]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    /**
     * 保存静态页面数据
     * */
    public function saveStaticPageData(Request $request){
        $this->validate($request, [
            'page_name' => 'required',
            'route_path' => 'required',
        ], [
            'page_name.required' => '页面名称不能为空',
            'route_path.required' => '路由地址不能为空',
        ]);
        $params=$request->only(['id','page_name','route_path','seo_id','seo_data','status','sort','type','page_content']);
        $ret=$this->service->saveStaticPageData($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更静态页面状态
     * */
    public function changeStaticPageStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveStaticPageData($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 清楚静态页面缓存
     * */
    public function clearPageCache(Request $request){
        $clearKey=$request->input('clear_key','');
        $ret=$this->service->clearPageCache($clearKey);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * @api post /api/user/get-phone-code 获取手机验证码
     * @group 用户模块-用户授权
     * @throws \Illuminate\Validation\ValidationException
     */
    public function getPhoneCode(Request $request)
    {
        $this->validate($request, [
            'phone' => 'required',
        ], [
            'phone.required' => '手机号码不能为空',
        ]);
        $phone = $request->input('phone', '');
        $countryCode = $request->input('country_code', '86');
        return $this->service->getPhoneCode($phone, $countryCode);
    }

    /**
     * 获取全局配置列表
     * */
    public function getGlobalConfig(Request $request){
        $params=[];
        $ret=SysGlobalConfigFacade::getGlobalConfig($params);
        $ret=array_values($ret);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 文章全局配置数据保存
     * */
    public function saveGlobalConfigData(Request $request){

        $params=$request->only(['id','global_key','label','value']);
        $ret=SysGlobalConfigFacade::saveGlobalConfigData($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 搜索获取已发布的文章
     * */
    public function queryAllWebsite(Request $request){
        $params=$request->only(['page_size','keyword','page']);
        $ret=$this->service->queryAllWebsite($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    public function mailTest(Request $request){
        $globalParams=[];
        $globalData= SysGlobalConfigFacade::getGlobalConfig($globalParams);

        $globalData=mapByKey($globalData,'global_key');
        $referer=empty($params['referer'])?'':$params['referer'];
        if(!empty($globalData['FORM_RECEIVE_MAIL']['value'])){ // 插入数据成功，并且有邮箱。发送邮件
            $mail_pwd=empty($globalData['FORM_RECEIVE_MAIL']['value']['mail_pwd'])?'':$globalData['FORM_RECEIVE_MAIL']['value']['mail_pwd'];
            $mail_user=empty($globalData['FORM_RECEIVE_MAIL']['value']['mail_user'])?'':$globalData['FORM_RECEIVE_MAIL']['value']['mail_user'];
            $smtp_host=empty($globalData['FORM_RECEIVE_MAIL']['value']['smtp_host'])?'':$globalData['FORM_RECEIVE_MAIL']['value']['smtp_host'];
            $smtp_port=empty($globalData['FORM_RECEIVE_MAIL']['value']['smtp_port'])?'':$globalData['FORM_RECEIVE_MAIL']['value']['smtp_port'];

            if(!empty($mail_pwd)&&!empty($mail_user)&&!empty($smtp_host)&&!empty($smtp_port))
                foreach ($globalData['FORM_RECEIVE_MAIL']['value']['to_mail'] as $value){
                    $mail_data['email'] = $value;
                    $mail_data['name'] = $value;
                    $mail_data['mail_user'] = $mail_user;
                    $mail_data['mail_pwd'] = $mail_pwd;
                    $mail_data['smtp_host'] = $smtp_host;
                    $mail_data['smtp_port'] = $smtp_port;
                    $mail_data['referer'] = $referer;
                    $mail_data['data'] = [
                        [
                            'name'=>'aaaa',
                            'content'=>'bbbbb',
                        ]
                    ];
                    FormRecordFacade::mailInvite($mail_data);
                }

        }
    }

    public function uploadCustomImage(Request $request)
    {
        set_time_limit(120);
        $ret= AttachmentFacade::uploadCustomImage($request);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    public function adClick(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $ad_id=$request->input('id',0);
        $ret= SysAdFacade::adClick($ad_id);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    public function updateMeetingPv(Request $request)
    {
        $this->$this->validate($request, [
            'id' => 'required',
        ],[
            'id.required'=>'id不能为空'
        ]);
        $meeting_id = $request->input('id',0);
        $ret= MeetingFacade::upMeetingPv($meeting_id,1);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    public function importSEOSetting(Request $request)
    {
        $this->validate($request, [
            'file' => 'required|file'
        ]);
        $isRecover = $request->input('is_recover', 1);
        $file = $request->file('file');
        $extension = $file->getClientOriginalExtension();
        if($extension == 'xls' || $extension == 'xlsx') {
            $ret = $this->service->uploadPageSEOExcel($file, $isRecover);
            return $this->jsonResponse(
                'ok',
                $ret
            );
        } else {
            return $this->jsonResponse(
                '导入文件格式不对',
                [],
                100000
            );
        }
    }

    public function confirmImportSEOSetting(Request $request)
    {
        $duplicateUrla = $request->input('duplicate_urla', []);
        $ret = $this->service->uploadPageSEOByData($duplicateUrla);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    public function exportSEOSetting(Request $request)
    {
       return $this->service->downloadPageSEOExcel();
    }

    public function updateTransStatus(Request $request) {
        $this->validate($request, [
            'id' => 'required',
            'trans_status' => 'required',
        ],[
            'id.required'=>'id不能为空',
            'trans_status.required'=>'trans_status不能为空'
        ]);
        $data = $this->service->updateTransStatus($request->input('id'), $request->input('trans_status'));
        return $this->jsonResponse(
            'ok',
            $data
        );
    }

    public function updateSeoData(Request $request) {
        $this->validate($request, [
            'id' => 'required'
        ],[
            'id.required'=>'id不能为空'
        ]);
        $params = $request->only(['id', 'seo_title', 'seo_keyword', 'seo_describe']);
        $data = $this->service->saveSeoData($params);
        return $this->jsonResponse(
            'ok',
            $data
        );
    }
}
