<?php

namespace App\Admin\Controllers\Company;

use App\Http\Controllers\Controller;
use App\Models\Task;
use Encore\Admin\Auth\Permission;
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;
use Illuminate\Support\Facades\Route;

class TaskController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @param $type_id
     * @return Content
     */
    public function index(Content $content)
    {
        $name = Route::currentRouteName();
        $arr = explode('.', $name);
        $header = '企业任务';
        if ($arr[0] == 'task') {  //路由参数变化导致。 
            $type_id =1;
            Permission::check('companytask_manager');
        } else {
            $type_id =2;
            $header = '个人任务';
            Permission::check('person_task');
        }
        return $content
            ->header($header)
            ->description('列表')
            ->body($this->grid($type_id));
    }

    /**
     * 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('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @param $type_id
     * @return Grid
     */
    protected function grid($type_id)
    {
        if ($type_id == 1) {
            Permission::check('task_manager_company');
        } else {
            Permission::check('person_task');
        }
        $grid = new Grid(new Task);
        $grid->model()->where('utype', $type_id);
        $grid->title('任务名称')->width(200);

        if ($type_id == 1) {
            if (Admin::user()->can('task_manager_company_edit')) {
                $grid->status('是否启用')->switch()->display(function ($status) {
                    if ($this->title == '每日签到') {
                        return '不允许关闭';
                    }
                    return $status;
                });
            } else {
                $grid->status('是否启用')->display(function ($status) {
                    if ($this->title == '每日签到') {
                        return '不允许关闭';
                    }
                    if ($status =0) {
                        return "关闭";
                    }
                    return "开启";
                });
            }
        } else {
            if (Admin::user()->can('person_task_edit')) {
                $grid->status('是否启用')->switch()->display(function ($status) {
                    if ($this->title == '每日签到') {
                        return '不允许关闭';
                    }
                    return $status;
                });
            } else {
                $grid->status('是否启用')->display(function ($status) {
                    if ($this->title == '每日签到') {
                        return '不允许关闭';
                    }
                    if ($status =0) {
                        return "关闭";
                    }
                    return "开启";
                });
            }
        }
        if ($type_id == 1) {
            if (Admin::user()->can('task_manager_company_edit')) {
                $grid->points('增加(积分)')->editable();
            } else {
                $grid->points('增加(积分)');
            }
        } else {
            if (Admin::user()->can('person_task_edit')) {
                $grid->points('增加(积分)')->editable();
            } else {
                $grid->points('增加(积分)');
            }
        }
        if ($type_id == 1) {
            if (Admin::user()->can('task_manager_company_edit')) {
                $grid->times('每天赠送积分上限')->editable()->display(function ($times) {
                    if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
                        return $times . "/次";
                    } else {
                        return "--";
                    }
                });
            } else {
                $grid->times('每天赠送积分上限')->display(function ($times) {
                    if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
                        return $times . "/次";
                    } else {
                        return "--";
                    }
                });
            }
        } else {
            if (Admin::user()->can('person_task_edit')) {
                $grid->times('每天赠送积分上限')->editable()->display(function ($times) {
                    if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
                        return $times . "/次";
                    } else {
                        return "--";
                    }
                });
            } else {
                $grid->times('每天赠送积分上限')->display(function ($times) {
                    if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
                        return $times . "/次";
                    } else {
                        return "--";
                    }
                });
            }
        }
        $grid->disableFilter();
        return $grid;
    }

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

        $show->id('ID');
        $show->title('任务名称');
        $show->status('是否启用')->as(function ($status) {
            if ($status == 1) {
                return "启用";
            } else {
                return "关闭";
            }
        });
        $show->points('增加(积分)');
        $show->once('是否一次性任务')->as(function ($once) {
            switch ($once) {
                case 1:
                    return "是";
                    break;
                case 0:
                    return '否';
                    break;
            }
        });
        $show->times('每天赠送积分上限')->as(function ($times) {
            if ($this->once == 0 && $this->times != -1 && $this->dayly == 0) {
                return $this->times;
            }
        });

        return $show;
    }

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

        $form->display('ID');
        $form->switch('status');
        $form->text('points', '积分');
        $form->text('times', '每天赠送积分上限');
        $form->display('Created at');
        $form->display('Updated at');

        return $form;
    }
}