<?php

namespace App\Admin\Controllers\CommunityNotice;

use App\Models\CommunityNotice;
use App\Models\Organization;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CommunityNotices extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '社区端通告';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CommunityNotice());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'))->sortable();
        $grid->column('organization.name', __('发放社区'));
        $grid->column('title', __('标题'));
        $grid->column('type', __('类型'))->using([1=>'社区通告']);
        $grid->column('content', __('内容'))->limit(100,'...');
        $grid->column('admin_id', __('发布人'))->display(function ($id){
            return Administrator::where('id',$id)->value('username');
        });
        $grid->column('updated_at', __('更新时间'));
        $grid->column('created_at', __('创建时间'));



        return $grid;
    }

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



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CommunityNotice());
        $form->text('title', __('标题'));
        $form->select('organization_id', __('发放的社区'))->options(Organization::all()->pluck('name','id'));
        $form->select('type', __('类型'))->options([1=>'社区端通告']);
        $form->editor('content', __('内容'));
        $form->submitted(function ($form){
            $form->model()->admin_id = Admin::user()->id;
        });

        return $form;
    }
}
