<?php

namespace App\Admin\Controllers\Person;

/*use App\Models\JobSubscribe;*/

use App\Http\Controllers\Controller;
use App\Models\MemberInfo;
use App\Models\PersonJobsSubscribe;
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 Encore\Admin\Grid;*/

class JobSubscribeController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('职位订阅')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('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.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PersonJobsSubscribe);
        $grid->model()->orderBy('id','desc');
        $grid->email('接收邮箱')->width(150);
        $grid->nature_cn('职位性质')->width(100);
        $grid->intention_jobs('意向职位')->width(150);
        $grid->trade_cn('意向行业')->width(150);
        $grid->wage_cn('意向薪资')->width(100);
        $grid->district_cn('意向地区')->width(150);
        $grid->created_at('添加时间')->sortable();
        $grid->disableExport();
        $grid->disableCreateButton();

        if (Admin::user()->can('person_subscribe_delete')) {
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete(false);
                });
            });
        }

        $grid->actions(function ($actions) {
            if (Admin::user()->can('person_subscribe_delete')) {
                $actions->disableDelete(false);
            }
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            /*$subsites = array(''=>'不限','0'=>'总站', '1' => '定海', '2' => '普陀');
            $filter->equal('subsite_id', '所属分站')->select($subsites);*/
            $filter->like('intention_jobs', '意向职位');
            $filter->like('trade_cn', '意向行业');
            $filter->like('district_cn', '意向地区');
            $filter->like('email', '接收邮件');
            $date3   = date('Y-m-d', strtotime("-3 day"));
            $date7   = date('Y-m-d', strtotime("-7 day"));
            $date30  = date("Y-m-d", strtotime("-1 month"));
            $date180 = date("Y-m-d", strtotime("-6 month"));
            $date360 = date("Y-m-d", strtotime("-1 year"));
            $date_option = array(
                ''    => '不限',
                $date3   => '三天内',
                $date7   => '一周内',
                $date30  => '一月内',
                $date180 => '半年内',
                $date360 => '一年内',
            );
            $filter->where(function ($query) {
                $query->where('created_at', '>=', "{$this->input}");
            }, '添加时间', 'created_at')->radio($date_option);
        });

        return $grid;
    }

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

        $show->id('ID');
        $show->uid('订阅用户')->as(function ($uid) {
            $memberInfo = MemberInfo::where(['uid'=>$uid])->first();
            if ($memberInfo) {
                return $memberInfo->realname;
            }
            return '';
        });
        $show->title('订阅名称');
        $show->trade_cn('行业分类');
        $show->district_cn('工作地区');
        $show->intention_jobs('职能分类');
        $show->sendTime_cn('发布日期');
        $show->experience_cn('工作经验');
        $show->wage_cn('薪资范围');
        $show->education_cn('学历要求');
        $show->rate_cn('接收频率');
        $show->send_jobs_cn('发布职位数');
        $show->nature_cn('职位性质');
        $show->email('接收邮箱');
        $show->created_at('添加时间');
        $show->updated_at('更新时间');

        $show->panel()->tools(function ($tools) {
            $tools->disableEdit();
            $tools->disableDelete();
        });

        return $show;
    }

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

        $form->display('ID');
        $form->display('Created at');
        $form->display('Updated at');

        return $form;
    }
}