<?php

namespace App\Admin\Controllers\UserManagement\BmUser;

use App\Models\UserCoupon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

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

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserCoupon());
        $grid->disableCreateButton();
        $uesr_id = \request('user_id');
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->equal('user_id','用户id');
        });
        $grid->disableActions();
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('coupon_id', __('优惠券ID'));
        $grid->column('name', __('券名称'));
        $grid->column('icon', __('券图标'))->lightbox(['width' =>'', 'height' => 30]);
        $grid->column('title', __('券标题'));
        $grid->column('type', __('类型'))->using([1=>'满减券',2=>'折扣券']);
        $grid->column('usable_type', __('可用类型'))->using([1=>'全部产品通用',2=>'部分产品可用']);
        $grid->column('CouponType', __('可用产品'))->pluck('product_type','id')->display(function ($i){
            $name = [];
            foreach ($i as $value)
            {
                switch ($value){
                    case 1:
                        array_push($name,'电话咨询');
                        break;
                    case 2:
                        array_push($name,'图文咨询');
                        break;
                    case 3:
                        array_push($name,'门诊咨询');
                        break;
                    case 4:
                        array_push($name,'疫苗接种');
                        break;
                    case 5:
                        array_push($name,'儿保预约');
                        break;
                    case 6:
                        array_push($name,'服务包');
                        break;
                    case 7:
                        array_push($name,'充值');
                        break;
                }
            }
            return $name;
        })->label('info');
        $grid->column('money', __('满减券的优惠钱数'))->display(function ($money){
            return $money/100;
        });
        $grid->column('min_consume_amount', __('最低消费金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('discount', __('折扣'));
        $grid->column('max_reduce_amount', __('折扣券的最大抵扣金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('expire_type', __('过期类型'))->using([1=>'领取N天过期',2=>'设置固定过期时间']);
        $grid->column('effective_days', __('有效天数'));
        $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
            if($time == 0)
            {
                return '';
            }else
                return date("Y-m-d H:i",$time);
        });
        $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
            if($time == 0)
            {
                return '';
            }else
                return date("Y-m-d H:i",$time);
        });
        $grid->column('expire_time', __('过期时间'))->display(function ($time){
            if($time == 0)
            {
                return '';
            }else
                return date("Y-m-d H:i",$time);
        });
        $grid->column('use_time', __('使用时间'))->display(function ($time){
            if($time == 0)
            {
                return '';
            }else
                return date("Y-m-d H:i",$time);
        });
        $grid->column('status', __('优惠券状态'))->using([1=>'可用',2=>'已用',3=>'已过期']);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

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

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