<?php

    namespace app\controller;


    use Illuminate\Support\Arr;
    use Respect\Validation\Validator;
    use support\Db;
    use support\Request;
    use hg\apidoc\annotation as Apidoc;

    #[Apidoc\Title("配置信息+新闻新闻")]
    #[Apidoc\Group("Config")]
    #[Apidoc\Sort(2)]
    class ConfigController
    {
        #[Apidoc\Title("配置")]
        #[Apidoc\Url("api/config/data.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Returned(name: "association", type: "string", require: true, desc: '社群', default: '')]
        #[Apidoc\Returned(name: "service", type: "string", require: true, desc: '客服', default: '')]
        #[Apidoc\Returned(name: "affiche", type: "array", require: true, desc: '公告', default: '')]
        #[Apidoc\Returned(name: "mp4", type: "string", require: true, desc: '视频', default: '')]
        #[Apidoc\Returned(name: "company_introduction", type: "string", require: true, desc: '公司介绍', default: '')]
        #[Apidoc\Returned(name: "corporate_governance", type: "string", require: true, desc: '审查报告', default: '')]
        #[Apidoc\Returned(name: "public_policy", type: "string", require: true, desc: '公共政策', default: '')]
        #[Apidoc\Returned(name: "compliance_and_integrity", type: "string", require: true, desc: '合规与诚信', default: '')]
        #[Apidoc\Returned(name: "intelligent_world", type: "string", require: true, desc: '智能世界', default: '')]
        #[Apidoc\Returned(name: "micro_chat", type: "string", require: true, desc: '助力微聊', default: '')]
        #[Apidoc\Returned(name: "booster_potato", type: "string", require: true, desc: '助力土豆', default: '')]
        #[Apidoc\Returned(name: "service_txt", type: "string", require: true, desc: '客服富文本', default: '')]
        public function data(Request $request)
        {
            $data    = Db::table('wa_system')->first();
            $affiche = [];
            if ($data->affiche) {
                $affiche[] = imgtxt($data->affiche);
            }
            if ($data->affiche_one) {
                $affiche[] = imgtxt($data->affiche_one);
            }
            if ($data->affiche_two) {
                $affiche[] = imgtxt($data->affiche_two);
            }
            $articledata = Db::table('wa_article')
                ->where('article_type_id', 2)
                ->select(['id', 'name', 'describe', 'img', 'created_at'])
                ->orderBy('id')
                ->get()->toArray();
            return success([
                'association'              => imgtxt($data->association),
                'service'                  => $data->service,
                'affiche'                  => $affiche,
                'mp4'                      => getenv('MP4'),
                'company_introduction'     => imgtxt($data->company_introduction),
                'corporate_governance'     => imgtxt($data->corporate_governance),
                'public_policy'            => imgtxt($data->public_policy),
                'compliance_and_integrity' => imgtxt($data->compliance_and_integrity),
                'intelligent_world'        => imgtxt($data->intelligent_world),
                'micro_chat'               => $data->micro_chat,
                'booster_potato'           => $data->booster_potato,
                'service_txt'              => imgtxt($data->service_txt),
                'article_data'             => $articledata,

            ]);
        }

        #[Apidoc\Title("新闻列表")]
        #[Apidoc\Url("api/config/article.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Param("page", type: "int", require: true, desc: "页面", mock: 1)]
        #[Apidoc\Param("limit", type: "int", require: true, desc: "输出条数", mock: 10)]
        #[Apidoc\Returned(name: "id", type: "int", require: true, desc: '文章ID', default: '')]
        #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '文章标题', default: '')]
        #[Apidoc\Returned(name: "describe", type: "string", require: true, desc: '文章描述', default: '')]
        #[Apidoc\Returned(name: "img", type: "string", require: true, desc: '文章封面图', default: '')]
        #[Apidoc\Returned(name: "created_at", type: "string", require: true, desc: '创建时间', default: '')]
        #[Apidoc\Returned(name: "article_type_id", type: "string", require: true, desc: '1=新闻中心，2=热门话题', default: '')]
        public function article(Request $request)
        {
            try {
                $param = $request->param_data;
                Validator::input($param, [
                    'page'  => Validator::notEmpty()->intType()->setName('页面'),
                    'limit' => Validator::notEmpty()->intType()->setName('输出条数'),
                ]);

                $data = Db::table('wa_article')
                    ->where('article_type_id', 1)
                    ->select(['id', 'name', 'describe', 'img', 'created_at'])
                    ->orderBy('id')
                    ->paginate(Arr::get($param, 'limit', 10))->toArray();
                foreach ($data['data'] as $k => $v) {
                    $data['data'][$k]->img = imageToBase64( $v->img);
                }
            } catch (\Throwable $exception) {
                return error($exception->getMessage());
            }
            return success($data);
        }

        #[Apidoc\Title("新闻详情")]
        #[Apidoc\Url("api/config/articledetails.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Param("id", type: "int", require: true, desc: "文章ID", mock: 20)]
        #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '文章标题', default: '')]
        #[Apidoc\Returned(name: "txt", type: "string", require: true, desc: '文章详情【富文本内容】', default: '')]
        public function articleDetails(Request $request)
        {
            try {
                $param = $request->param_data;
                Validator::input($param, [
                    'id' => Validator::notEmpty()->intType()->setName('文章ID'),
                ]);
                $data      = Db::table('wa_article')->where('id', $param['id'])->select(['txt', 'name'])->first();
                $data->txt = imgtxt($data->txt);
            } catch (\Throwable $exception) {
                return error($exception->getMessage());
            }
            return success($data);
        }

        #[Apidoc\Title("支付通道")]
        #[Apidoc\Url("api/config/pay_aisle.html")]
        #[Apidoc\Method("POST")]
        #[Apidoc\Returned(name: "name", type: "string", require: true, desc: '名称', default: '支付宝')]
        #[Apidoc\Returned(name: "characteristic", type: "int", require: true, desc: '支付标识', default: '1')]
        #[Apidoc\Returned(name: "type", type: "int", require: true, desc: '渠道类型', default: '1')]
        #[Apidoc\Returned(name: "img", type: "string", require: true, desc: '图标', default: '')]
        public function payAisle()
        {
            $data = Db::table('wa_pay_aisle')->where('state', 1)->orderBy('sort')->select(['name', 'characteristic', 'type', 'img'])->get();
            foreach ($data as $k => $v) {
                $data[$k]->img = imageToBase64($v->img);
            }
            return success($data);
        }


    }
