<?php

namespace App\Admin\Controllers\Person;

use App\Http\Controllers\Controller;
use App\Models\MembersHandsel;
use App\Models\MembersPoint;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class PersonalPointsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('个人积分')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('个人积分')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('个人积分')
            ->description('编辑')
            ->body($this->editForm($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('个人积分')
            ->description('创建')
            ->body($this->createForm());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new MembersHandsel);

        $grid->model()->when(get_subsite_id()>0, function ($query) {
            $query->whereHas('members',function ($query){
                $query->where('subsite_id', get_subsite_id());
            });
        })->where('utype', 2)->orderBy('id', 'desc');


        $grid->id('ID');
        $grid->uid('会员ID');
        $grid->utype('会员类型')->display(function ($utype) {
            if ($utype==1) {
                return '企业';
            } else {
                return '个人';
            }
        })->width(100);
        $grid->points('操作积分')->display(function ($points) {
            if ($this->operate==1) {
                return "<span style='color:green;'>+".$points."</span>";
            } else {
                return "<span style='color:red;'>-".$points."</span>";
            }
        })->width(100);
        $grid->htype_cn('操作说明')->width(150);
        $grid->created_at('添加时间')->sortable();
        $grid->updated_at('更新时间')->sortable();
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->actions(function ($actions) use ($grid) {
            if (Admin::user()->can('person_point_delete')) {
                $actions->disableDelete(false);
            }
            if (Admin::user()->can('person_point_edit')) {
                $actions->disableEdit(false);
            }
        });
        if (Admin::user()->can('person_point_delete')) {
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete(false);
                });
            });
        }

        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->equal('uid', '会员ID');

            $filter->like('htype_cn', '操作说明');
        });

        return $grid;
    }

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

        $show->id('ID');

        $show->uid('会员ID');
        $show->utype('会员类型')->as(function ($utype) {
            if ($utype==1) {
                return '企业';
            } else {
                return '个人';
            }
        });
        $show->points('操作积分')->as(function ($points) {
            if ($this->operate == 1) {
                return '+'.$points;
            } else {
                return '-'.$points;
            }
        });
        $show->created_at('添加时间');
        $show->updated_at('更新时间');
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new MembersHandsel);
        $form->display('id');
        $form->display('created_at');
        $form->display('updated_at');
        return $form;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function editForm($id)
    {
        $form = new Form(new MembersHandsel);
        $form->setAction(route('admin.personal.addPoints'));
        $MembersHandsel = MembersHandsel::findOrFail($id);
        $utype = $MembersHandsel->utype;
        $points = MembersHandsel::find($id)->membersPoints()->where('utype', $utype)->first();
        $form->display('points_num', '会员积分')->setWidth(2)->with(function () use ($points) {
            return $points->points;
        });
        $form->radio('points_type', '操作积分')->options([1 => '增加', 2=> '减少'])->default(1);
        $form->switch('ismany', '是否收费?');
        $form->number('many_point', '增减积分')->default(0)->min(0);
        $form->number('amount', '收费金额')->default(0)->min(0);

        $form->text('explain', '操作说明')->setWidth(8);
        $form->hidden('points_id')->default($points->id);
        return $form;
    }

    protected function createForm()
    {
        $form = new Form(new MembersHandsel);
        $form->display('id');
        $form->display('created_at');
        $form->display('updated_at');
        return $form;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return mixed
     */
    public function store()
    {
        return $this->createForm()->store();
    }
    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
     */
    public function update($id)
    {
        return $this->editForm()->update($id);
    }


    public function addPoints()
    {
        $id = request()->points_id;
        if (!$id) {
            admin_toastr('积分ID不存在!', 'error');
        }
        $type = request()->points_type;
        $points = request()->many_point;
        if ($type==1) {
            $res = MembersPoint::where('id', $id)->increment('points', $points);
        } else {
            $res = MembersPoint::where('id', $id)->decrement('points', $points);
        }
        if ($res) {
            admin_toastr('操作成功!', 'success');
        } else {
            admin_toastr('操作失败!', 'error');
        }
        return back();
    }
}