<?php

namespace app\admin\controller;

use app\admin\service\SystemService;
use support\spark\model\user\UserAgreement;
use support\Request;
use support\View;
use yzh52521\mailer\mail\Mailer;

class SystemController extends Curd
{
    public function smsTest(Request $request)
    {
        if (request()->isAjax()) {
            $param = $request->all();
            \Hhink\WebmanSms\Sms::app()->send($param['phoneNumber'], [
                'content'  => '您的登录验证码: 032654',
                'template' => 'SMS_245210204',
                'data' => [
                    'code' => 032654
                ],
            ]);
        }
        return $this->json(0, 'ok');
    }

    public function sms(Request $request)
    {
        if (request()->isAjax()) {

            $param = $request->post();

            $systemService = new SystemService();
            $systemService->saveSystem($param);
            return $this->json(0, '保存成功');
        }

        $config = [
            'sql' => getConfByType('sms'),
            'file' => config('plugin.hhink.webman-sms.app')
        ];
        View::assign([
            'info' => json_encode($config)
        ]);

        return view('system/smsNew');
    }

    public function emailTest(Request $request)
    {
        if (request()->isAjax()) {
            $param = $request->all();
            $mailer = new \yzh52521\mailer\mail\Mailer();
            $mailer->setTo($param['to']);
            $mailer->setSubject($param['subject']);
            $mailer->setTextBody($param['content']);
            $mailer->send();
        }
        return $this->json(0, 'ok');
    }

    public function email(Request $request)
    {
        if (request()->isAjax()) {
            $param = $request->post();
            $systemService = new SystemService();
            $systemService->saveSystem($param);
            return $this->json(0, '保存成功');
        }

        $config = [
            'sql' => getConfByType('email'),
            'file' => config('plugin.yzh52521.mailer.app')
        ];
        View::assign([
            'info' => json_encode($config)
        ]);

        return view('system/email');
    }

    public function index()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $systemService = new SystemService();
            return sparkSuccess($systemService->saveSystem($param));
        }
        View::assign([
            'base' => json_encode(getConfByType('base')),
            'shop_base' => json_encode(getConfByType('shop_base')),
            'shop_user_level' => json_encode(getConfByType('shop_user_level')),
            'shop_refund' => json_encode(getConfByType('shop_refund')),
            'dingTalkForm' => json_encode(getConfByType('ding_talk'))
        ]);
        return view('system/index');
    }

    public function pay()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            if (isset($param['file'])) {
                unset($param['file']);
            }
            $systemService = new SystemService();
            return sparkSuccess($systemService->saveSystem($param));
        }
        View::assign([
            'alipay' => json_encode(getConfByType('alipay')),
            'wechat' => json_encode(getConfByType('wechat_pay')),
            'balance' => json_encode(getConfByType('balance_pay')),
        ]);
        return view('system/pay');
    }

    public function miniapp()
    {

        if (request()->isAjaxPost()) {
            $param = request()->post();
            $systemService = new SystemService();
            return sparkSuccess($systemService->saveSystem($param));
        }
        View::assign([
            'info' => json_encode(getConfByType('miniapp')),
        ]);
        return view('system/miniapp');
    }

    /**
     * 物流配置
     */
    public function express()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $systemService = new SystemService();
            $systemService->saveSystem($param);
            return sparkSuccess($systemService->saveSystem($param));
        }

        View::assign([
            'info' => json_encode(getConfByType('express'))
        ]);
        return view('system/express');
    }

    /**
     * 短信配置
     */
    public function sms666666666666()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $systemService = new SystemService();
            return sparkSuccess($systemService->saveSystem($param));
        }

        View::assign([
            'info' => json_encode(getConfByType('sms'))
        ]);

        return view('system/sms');
    }


    /**
     * 三方存储配置
     */
    public function store()
    {
        if (request()->isAjaxPost()) {
            $param = request()->post();
            $systemService = new SystemService();
            return sparkSuccess($systemService->saveSystem($param));
        }

        View::assign([
            'store' => json_encode(getConfByType('store')),
            'aliyun' => json_encode(getConfByType('store_oss')),
            'qiniu' => json_encode(getConfByType('store_qiniu')),
            'qcloud' => json_encode(getConfByType('store_tencent')),
        ]);

        return view('system/store');
    }

    /**
     * 协议配置
     */
    public function agreement()
    {
        if (request()->isAjaxPost()) {

            $param = request()->post();

            $userAgreementModel = new UserAgreement();
            $has = $userAgreementModel->getInfoByWhere([
                'type' => $param['type']
            ], ['id']);

            if (!empty($has)) {
                $userAgreementModel->updateById([
                    'content' => $param['content'],
                    'update_time' => now()
                ], $has['id']);
            } else {
                $userAgreementModel->insertOne([
                    'type' => $param['type'],
                    'content' => $param['content'],
                    'create_time' => now()
                ]);
            }

            return sparkSuccess(dataReturn(0, '保存成功'));
        }
        $userAgreementModel = new UserAgreement();
        $agreementList = $userAgreementModel->getAllList([]);
        $agreementMap = [
            1 => '',
            2 => ''
        ];
        if (!empty($agreementList)) {
            foreach ($agreementList as $vo) {
                $agreementMap[$vo['type']] = $vo['content'];
            }
        }
        View::assign([
            'agreementMap' => $agreementMap
        ]);

        return view('system/agreement');
    }
}