<?php

namespace App\Admin\Controllers\CouponManagement;

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

class CouponDetailsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '优惠券详情';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Coupon());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('name', __('券名称'));
        $grid->column('title', __('券标题'));
        $grid->column('desc', __('使用说明'))->limit(20,'...');
        $grid->column('rules', __('使用规则'))->limit(20,'...');
        $grid->column('min_consume_amount', __('最低消费金额'))->display(function ($money){
            return $money/100;
        });
        $grid->column('max_reduce_amount', __('折扣券的最大抵扣金额'))->display(function ($money){
            return $money/100;
        });

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('title', __('Title'));
        $show->field('desc', __('Desc'));
        $show->field('rules', __('Rules'));
        $show->field('icon', __('Icon'));
        $show->field('type', __('Type'));
        $show->field('usable_type', __('Usable type'));
        $show->field('money', __('Money'));
        $show->field('discount', __('Discount'));
        $show->field('min_consume_amount', __('Min consume amount'));
        $show->field('max_reduce_amount', __('Max reduce amount'));
        $show->field('expire_type', __('Expire type'));
        $show->field('effective_days', __('Effective days'));
        $show->field('start_time', __('Start time'));
        $show->field('end_time', __('End 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 Coupon());

        $form->text('name', __('Name'));
        $form->text('title', __('Title'));
        $form->text('desc', __('Desc'));
        $form->text('rules', __('Rules'));
        $form->text('icon', __('Icon'));
        $form->switch('type', __('Type'));
        $form->switch('usable_type', __('Usable type'));
        $form->number('money', __('Money'));
        $form->decimal('discount', __('Discount'))->default(0.00);
        $form->number('min_consume_amount', __('Min consume amount'));
        $form->number('max_reduce_amount', __('Max reduce amount'));
        $form->switch('expire_type', __('Expire type'));
        $form->number('effective_days', __('Effective days'));
        $form->number('start_time', __('Start time'));
        $form->number('end_time', __('End time'));

        return $form;
    }
}
