<?php

namespace App\Admin\Controllers\Resume;

use App\Models\Category;
use App\Models\Resume;
use App\Models\ResumeGroup;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class IndexController extends AdminController
{

    protected $title = '简历管理';

    public function grid(): Grid
    {
        $grid = new Grid(new Resume());
        $grid->model()->orderBy('id', 'desc');

        $grid->filter(function ($filter) {
            $filter->column(1 / 2, function ($filter) {
                $filter->like('name', '姓名');
                $filter->equal('category.id', '所属分类')
                    ->select(Category::where('status', 1)->where('type', Category::TYPE_PERSON)->pluck('title', 'id'));
            });

            $filter->disableIdFilter();
        });

        $grid->column('id', '#ID#');
        $grid->column('name', '姓名');
        $grid->column('cover', '封面图片')->image('', 100);
        $grid->column('category.title', '所属分类');
        $grid->column('status', '状态')->switch();
        $grid->column('sort', '序号')->editable();
        $grid->column('created_at', '创建时间');
        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new Resume());

        $form->text('name', '姓名')->rules('min:2')->required();
        $form->textarea('description', '简介');
        $form->image('cover', '封面')
            ->move('person/'.date('Y/m/d'))
            ->removable()
            ->uniqueName();

        $form->select('category_id', '所属分类')
            ->options(function () {
                return Category::where('status', 1)->where('type', Category::TYPE_PERSON)->pluck('title', 'id');
            })
            ->required();

        $form->select('group_id', '所属科室')
            ->options(function () {
                return ResumeGroup::where('status', 1)->pluck('name', 'id');
            })
            ->required();

        $form->embeds('information', '基本信息', function ($form) {
            $form->text('duty', '职务');
//            $form->text('tenure', '任期');
            $form->text('rank', '职称');
            $form->text('education', '学历');
//            $form->text('mobile', '电话');
            $form->text('email', '电子邮件');
//            $form->text('address', '通讯地址');
            $form->text('direction', '研究方向');
        });

        $form->ueditor('project', '主要科研项目');
        $form->ueditor('outcome', '成果获奖情况');
        $form->ueditor('thesis', '发表专著及论文选列');
        $form->ueditor('part_job', '学术兼职');
//        $form->ueditor('content', '简历')->rules('required', ['required' => '简历不能为空']);
        $form->datetime('created_at', '发布时间');
        $form->switch('status', '状态')->default(1);
        $form->number('sort', '序号')->default(0)->rules('required', ['required' => '序号必须填写'])->help('倒序优先');
        $form->url('url', '外链地址');

        return $form;
    }

}
