<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Notice\NoticeSend;
use App\Models\Notice;
use App\Models\User;
use App\Models\UserSystemMessageModel;
use App\Services\JPushService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Faker\Factory;
use Illuminate\Support\Facades\DB;

class NoticeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Notice());
        $grid->model()->orderBy('id','desc');

        $grid->column('id')->sortable();
        $grid->column('title');
        $grid->column('content')->limit(60);
        $grid->column('users')->using(['全部用户','男性用户','女性用户'])->label(['success','primary','warning']);
        $grid->column('created_at');
        //$grid->column('updated_at')->sortable();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id');
            $filter->like('title');
        });

        //操作管理
//        $grid->actions(function (Grid\Displayers\Actions $actions) {
//            $actions->append(new NoticeSend(User::class));
//        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Notice(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('content')->unescape();
            $show->field('users')->using(['全部用户','男性用户','女性用户']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Notice(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->textarea('content');
            $form->radio('users')->options([0=>'全部用户'
//                ,1=>'男性用户',2=>"女性用户"
            ])->default(0);
            $form->display('created_at');
            $form->display('updated_at');
            $form->saved(function ($res){
                //dd($res->attributes);
                //推送消息
                $notice = Notice::query()->orderBy('id','desc')->first();

                $users = User::query()->where(['notice_status'=>1,'is_distory'=>0])->whereNotNull('registrationId')->get(['id','registrationId'])->toArray();

                $registrationIds = array_column($users,'registrationId');

                JPushService::pushNotify([
                    //标题
                    'title' => $res->title,
                    //内容
                    'content' => $res->content,
                    //设备标识，跟设备相关
                    'reg_id' => $registrationIds,
//                    'image' => "https://zhengda.oss-accelerate.aliyuncs.com/chengluApp/image_cropper_1628325545267_compressed4883651550324666535.jpg",
                    //扩展字段
                    'extras' => [
                        'type' => 'notice',
                    ],
                    //推送类型
                    'type' => JPushService::PUSH_TYPE_REG_ID,
                ]);

                //给用户添加消息记录
                $users = User::query()->get(['id']);
                $datas = array();
                foreach ($users as $k=>$v){
                    $arr = array();
                    $arr['user_id'] = $v['id'];
                    $arr['msg_id'] = $notice['id'];
                    $arr['status'] = 0;
                    $arr['is_delete'] = 0;
                    $arr['created_at'] = date("Y-m-d H:i:s");
                    $arr['updated_at'] = date("Y-m-d H:i:s");
                    $datas[]=$arr;
                }
                DB::table('users_system_message')->insert($datas);
            });
        });
    }
}
