<?php

namespace App\Admin\Controllers\CouponManagement;

use App\Admin\Actions\backstage\Coupon\AllCouponBatchGrant;
use App\Admin\Actions\backstage\Coupon\CouponBatchGrant;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CouponDistributionUserList extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '优惠券发放用户列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User());
        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->equal('phone','手机')->mobile()->placeholder("请输入手机");
            $filter->like('patient.name','患者姓名')->placeholder("请输入患者姓名");
            $filter->like('nickname','昵称');
            $filter->equal('status','用户状态')->radio([
                ''   => '不限',
                0    => '黑名单',
                1    => '正常',
            ]);
            $filter->equal('is_pack','用户状态')->radio([
                ''   => '全部用户',
                0    => '普通用户',
                1    => '服务包用户',
            ]);
        });
        $grid->batchActions(function ($batch) {
            $batch->add(new CouponBatchGrant());
            $batch->add(new AllCouponBatchGrant());
        });
        $grid->disableActions();
        $grid ->model()->where('status','>=','0')->where('is_docter',0);
        $grid->column('id', __('用户id'))->sortable();
        $grid->column('nickname', __('用户名'));
        $grid->column('sex', __('性别'))->using([0=>'未知',1=>'男',2=>'女']);
        $grid->column('age', __('年龄'));
        $grid->column('status', __('状态'))->using([0=>'黑名单',1=>'正常',]);
        $grid->column('is_pack', __('用户身份'))->using([0=>'普通用户',1=>'付费用户']);
        $grid->column('balance', __('当前余额'))->display(function ($price){
            return $price/100;
        });
        $grid->column('created_at', __('注册时间'));
        $grid->column('last_login_time', __('最后登录时间'))->display(function ($time){
            return date("Y-m-d H:i",$time);
        });
//            $grid->column('patient', __('患者姓名'))->pluck('name')->map('ucwords')->implode(',');
        $grid->column('patient', __('患者姓名'))->pluck('name')->label('default');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('nickname', __('Nickname'));
        $show->field('phone', __('Phone'));
        $show->field('sex', __('Sex'));
        $show->field('avatar', __('Avatar'));
        $show->field('openid', __('Openid'));
        $show->field('remark', __('Remark'));
        $show->field('birthday', __('Birthday'));
        $show->field('status', __('Status'));
        $show->field('balance', __('Balance'));
        $show->field('giving_balance', __('Giving balance'));
        $show->field('topup_balance', __('Topup balance'));
        $show->field('session_key', __('Session key'));
        $show->field('pay_password', __('Pay password'));
        $show->field('latitude', __('Latitude'));
        $show->field('longitude', __('Longitude'));
        $show->field('is_pack', __('Is pack'));
        $show->field('last_login_time', __('Last login time'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('nickname', __('Nickname'));
        $form->mobile('phone', __('Phone'));
        $form->switch('sex', __('Sex'));
        $form->image('avatar', __('Avatar'));
        $form->text('openid', __('Openid'));
        $form->text('remark', __('Remark'));
        $form->text('birthday', __('Birthday'));
        $form->switch('status', __('Status'))->default(1);
        $form->number('balance', __('Balance'));
        $form->number('giving_balance', __('Giving balance'));
        $form->number('topup_balance', __('Topup balance'));
        $form->text('session_key', __('Session key'));
        $form->text('pay_password', __('Pay password'));
        $form->decimal('latitude', __('Latitude'))->default(0.0000000);
        $form->decimal('longitude', __('Longitude'))->default(0.0000000);
        $form->switch('is_pack', __('Is pack'));
        $form->number('last_login_time', __('Last login time'));

        return $form;
    }
}
