<?php

namespace App\Community\Controllers;

use App\Models\CommunityNotice;
use App\Models\Notice;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class NoticeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '通告列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $content = new Content();
        $content->title('通知公告')
        ->description('公告列表');
        $user = Admin::user();
        $org_id = $user->org_id;
        $docter_id = $user->docter_id;
        $notice = CommunityNotice::get();

        if($org_id){
            $notice = CommunityNotice::where('organization_id',$org_id)->get();
        }
        $content->view('cdms.notice',['notice'=>$notice]);
        return $content;
    }

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

        $show->field('id', __('Id'));
        $show->field('org_id', __('Org id'));
        $show->field('title', __('Title'));
        $show->field('content', __('Content'));
        $show->field('remark', __('Remark'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

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

        $form->number('org_id', __('Org id'));
        $form->text('title', __('Title'));
        $form->text('content', __('Content'));
        $form->text('remark', __('Remark'));

        return $form;
    }

    public function send(Content $content)
    {

        $content->title('fasong');
        $content->header('群发邮件');
        $form = new \Encore\Admin\Widgets\Form();
        $form->action('send');
        $form->method('post');
        $user = User::distinct()->pluck('nickname','id');
        $form->multipleSelect('user_ids','用户')->options($user);
        $form->text('title','标题')->rules('required',['requried'=>'请输入标题'])->placeholder('请输入标题，最多50个字');
        $form->textarea('content','内容')->rules('required')->placeholder('请输入标题，最多200个字');
        $form->textarea('remark','备注')->placeholder('请输入标题，最多200个字');;

        $content->row($form);
        $dibu = '<div>这是底部</div>';
        $content->row($dibu);
        return $content;

    }
}
