<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Jobs;
use App\Models\MemberInfo;
use App\Models\Resume;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class FindJobsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        $id = request()->id;
        $uid = request()->uid;
        $resume = Resume::where(['id'=>$id])->first();
        $member = MemberInfo::where(['uid'=>$uid])->first();
        return $content
            ->header('职位匹配')
            ->description('列表')
            ->body(view('admin.jobs.find_jobs')->with(['grid'=>$this->grid(),'resume'=>$resume,'member'=>$member]));
    }

    /**
     * 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 Jobs);

        $grid->id('ID');
        $grid->jobs_name('职位名称');
        $grid->company_name('公司名称');
        $grid->audit('审核状态')->display(function ($audit) {
            switch ($audit) {
                case 1:
                    $audit = '<span style="color: #00a65a;">审核通过</span>';
                    break;
                case 2:
                    $audit = '<span>审核中</span>';
                    break;
                case 3:
                    $audit = '<span style="color: red;">审核未通过</span>';
                    break;
                default:
                    $audit = '<span>审核中</span>';
                    break;
            }
            return $audit;
        });
        $grid->column('categoryScale.demand', '公司规模');
        $grid->column('categoryTrade.demand', '所属行业');
        $grid->column('categoryEducation.demand', '职位要求');
        $grid->column('categoryWage.demand', '薪资待遇');
        $grid->created_at('添加时间')->sortable();
        $grid->updated_at('更新时间')->sortable();
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableEdit();
        });

        $grid->tools(function ($tools) {
            $but = <<<EOT
<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-google btn-sm" id="Apply" title="投递">
        <i class="fa fa-audio-description"></i>
        <input type="radio" class="user-gender">投递
    </label>
</div>
EOT;
            $tools->append($but);
        });
        return $grid;
    }

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

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

    protected function createForm()
    {
        $form = new Form(new Jobs);
        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($id)->update($id);
    }
    protected function form()
    {
        $form = new Form(new Jobs);

        $form->display('ID');
        $form->display('添加时间');
        $form->display('更新时间');

        return $form;
    }
}