<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Users\SetUserInfo;
use App\Admin\Actions\Users\UserAction;
use App\Admin\Actions\Users\UserAuth;
use App\Admin\Actions\Users\UserChange;
use App\Admin\Actions\Users\UserVip;
use App\Models\User;
use App\Models\UserInfoModel;
use App\Services\TencentImAccountService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());
        $grid->model()
            ->leftJoin('users_info','users.id','=','users_info.user_id')
            ->leftJoin('users_vip_log','users.id','=','users_vip_log.user_id')
            ->select(['users.*','users_info.nickname','users_info.avatar','users_info.area','users_vip_log.end_day'])
            ->where('is_distory',0)
            ->orderBy('id','desc');

        $grid->column('id')->sortable();
        $grid->column('avatar','头像')->image("",50);
        $grid->column('mobile','手机号');
        $grid->column('nickname','昵称');

        $grid->column('pid','上级ID');
        $grid->column('sex','性别')->using([0=>"未知",1=>'男',2=>'女'])->label(['','primary','danger']);
        $grid->column('is_vip','VIP/到期时间')->display(function ($res){
            $str = "<div style='text-align: center'>";
            if($res==1){
                $str .= '<span class="label" style="background:#dda451">VIP</span>';
                $day = date('Y/m/d',strtotime($this->end_day));
                $str .= '<br><p style="margin-top: 5px">'.$day.'</p>';
            }else{
                $str .= '<span class="label" style="background:#b9c3cd">无</span>';
            }
            $str .="</div>";
            return $str;
        });
        $grid->column('is_auth','认证状态')->using([0=>'未认证',1=>"真人认证",2=>"女神认证"])->label(['gray','success','red']);;
        $grid->column('ycode','邀请码');
//        $grid->column('area','所在城市');
        $grid->column('online','在线状态')->using([0=>'离线',1=>'在线'])->label(['gray','success']);
//        $grid->column('notice_status');
        $grid->column('like_num','他喜欢');
        $grid->column('like_me_num','喜欢他');
        $grid->column('look_num','看过他');
        $grid->column('status','状态')->switch('success',true);
        $grid->column('is_distory','注销')->using([0=>'正常',1=>'已注销']);
        $grid->filter(function (Grid\Filter $filter) {
            $filter->equal('id','用户ID');
            $filter->equal('pid','上级ID');
            $filter->like('mobile','手机号');
            $filter->like('nickname','昵称');
            $filter->equal('sex','性别')->select(['1'=>'男','2'=>'女']);
            $filter->equal('is_vip','VIP')->select(['0'=>'无','1'=>'VIP']);
            $filter->equal('is_auth','认证状态')->select(['0'=>'未认证','1'=>'真人认证','2'=>'女神认证']);
            $filter->equal('online','在线状态')->select(['0'=>'离线','1'=>'在线']);
            $filter->between('end_day', '会员期限')->date();
        });

//        $grid->disableCreateButton();
        //操作管理
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new SetUserInfo(User::class));
            $actions->append(new UserAction(User::class));
            $actions->append(new UserChange(User::class));
//            if ($actions->row->is_auth == 0) {
            $actions->append(new UserAuth(User::class));
//            }
            $actions->append(new UserVip(User::class));
        });

        //批量操作
        $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
            $batch->disableDelete();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('mobile','手机号');
            $show->field('pid');
            $show->field('tencent_im_user_id');
            $show->field('sex');
            $show->field('is_vip');
            $show->field('is_auth');
            $show->field('ycode');
            $show->field('city');
            $show->field('online');
            $show->field('notice_status');
            $show->field('like_num');
            $show->field('like_me_num');
            $show->field('look_num');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        $form = new Form(new User());
        $form->display('id');
        $form->text('mobile','手机号')->required();
        $form->text('pid','上级ID')->default(0);

        $form->radio('sex','性别')->options([1=>'男',2=>'女'])->default(1);
        $form->radio('is_vip','是否是VIP')->options([0=>'否',1=>'是'])->default(0);
        $form->radio('is_auth','是否认证')->options([0=>'未认证',1=>'真人认证',2=>'女神认证'])->default(1);
        $form->text('city','所在城市')->default("成都市");
        $form->radio('online','在线状态')->options([0=>'离线',1=>'在线'])->default(1);
        $form->switch('status','状态')->default(1);
        $form->saving(function ($res)use ($form){
            if(User::query()->where('mobile',$res->mobile)->first()){
                return $form->response()->error('手机号已存在');
            }
        });
        $form->saved(function ($res){
            $user = User::query()->where('mobile',$res->mobile)->first();
            if (!$user->tencent_im_user_id) {
                $tencentImAccountService = new TencentImAccountService();
                $user->tencent_im_user_id = $tencentImAccountService->accountImport($user);
            }

            if(!UserInfoModel::query()->where('user_id',$user->id)->first()){
                UserInfoModel::query()->create([
                    'user_id'=>$user->id,
                    'avatar'=>"https://zhengda.oss-cn-chengdu.aliyuncs.com/chengluApp/default.jpg",
                    'nickname'=>"用户".$user->mobile,
                    'birthday'=>"1990-01-01"
                ]);
            }
        });
        return $form;
    }
}
