<?php

namespace App\Admin\Controllers\CouponManagement;

use App\Admin\Actions\backstage\Coupon\CouponDetails;
use App\Admin\Actions\backstage\Coupon\CouponDistribution;
use App\Admin\Actions\backstage\Coupon\CouponDistributions;
use App\Admin\Actions\backstage\Coupon\UpdateCouponType;
use App\Models\Coupon;
use App\Models\CouponType;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CouponManagementController 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->tools(function (Grid\Tools $tools) {
            $tools->append(new CouponDistributions());
        });
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->add(new CouponDetails());
            if ($actions->row->usable_type == 2)
            {
                $actions->add(new UpdateCouponType());
            }
        });
        $grid->filter(function($filter){
            $filter->timestampBetween('start_time', "有效期开始时间")->datetime();
            $filter->timestampBetween('end_time', "有效期结束时间")->datetime();
        });

        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('name', __('劵名称'));
        $grid->column('title', __('劵标题'));
        $grid->column('icon', __('券图标'))->lightbox(['width' =>'', 'height' => 30]);
        $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('discount', __('折扣'));
        $grid->column('position_type', __('发放平台'))->using([1=>'领券中心',2=>'后台发放']);
        $grid->column('num', __('数量'));
        $grid->column('effective_days', __('有效天数'));
        $grid->column('start_time', __('有效期开始时间'))->display(function ($time){
            if ($time ==0)
                return '';
            return date('Y-m-d H:i:s',$time);
        });
        $grid->column('end_time', __('有效期结束时间'))->display(function ($time){
            if ($time ==0)
                return '';
            return date('Y-m-d H:i:s',$time);
        });
        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->hidden('id', __('id'));
        $form->text('name', __('优惠券名称'));
        $form->text('title', __('券标题'));
        $form->text('desc', __('使用说明'));
        $form->text('rules', __('使用规则'));
//        dd($form->toArray());
        $form->image('icon', __('券图标'))->help('图标大小48px * 48px');
        $form->select('type', '类型')
            ->options([
                1 => '满减券',
                2 => '折扣券',
            ])->when(1, function (Form $form) {
                $form->text('money', '满减券的优惠钱数');
                $form->text('min_consume_amount', '最低消费金额');
            })->when(2, function (Form $form) {
                $form->text('discount', '折扣');
                $form->text('max_reduce_amount', '折扣券的最大抵扣金额');
            });
        $form->select('position_type',__('发放平台'))->options([1=>'领券中心',2=>'后台发放']);
        $form->number('num', __('数量'));
        if ($form->isCreating())
        {
            $form->select('usable_type',__('可用类型'))
                ->options([
                    1=>'全部产品通用',
                    2=>'部分产品可用'
                ])->when(2,function (Form $form){
                    $form->hasMany('CouponType','可用类型',function (Form\NestedForm $form){
                        $form->select('product_type','可用类型')->options([
                            1=>'电话咨询',
                            2=>'图文咨询',
                            3=>'门诊咨询',
                            4=>'疫苗接种',
                            5=>'儿保预约',
                            6=>'服务包',
                            7=>'充值',
                        ]);
                    });
                });
        }else
        {
            $form->select('usable_type',__('可用类型'))
                ->options([
                    1=>'全部产品通用',
                    2=>'部分产品可用'
                ]);
        }

        $form->select('expire_type', '过期类型')
            ->options([
                1 => '领取N天过期',
                2 => '设置固定过期时间',
            ])->when(1, function (Form $form) {
                $form->text('effective_days', __('有效天数'));
            })->when(2, function (Form $form) {
                $form->date('start_time', __('有效期开始时间'));
                $form->date('end_time', __('有效期结束时间'));
                $form->saving(function ($form){
                    $form->start_time = strtotime($form->start_time);
                    $form->end_time = strtotime($form->end_time);
                    $form->money = $form->money*100;
                    $form->max_reduce_amount = $form->max_reduce_amount*100;
                });
            });
        return $form;
    }
}
