<?php

namespace App\Admin\Controllers\Content;

use App\Models\Category;
use App\Models\CategoryDistrict;
use App\Models\Feature;
use App\Http\Controllers\Controller;
use App\Models\FeatureSort;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class FeatureController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content,Request $request)
    {
        return $content
            ->header('人才展示')
            ->description(' ')
            ->body(view('admin.content.photo')->with(['grid'=>$this->grid($request)]));
    }

    /**
     * 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));
    }
    public function update($id)
    {
        return $this->editForm($id)->update($id);
    }
    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('人才展示')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Feature);
        $grid->id('ID');
        $grid->column('photo', '特色人才信息')->display(function ($images) {

            $pbstr='';
            if($this->is_display==0){
                $pbstr= '<span style="color:#039990">&nbsp;&nbsp;&nbsp;&nbsp;' .$this->name.'</span><span style="color:#999999">&nbsp;&nbsp;&nbsp;&nbsp;[已屏蔽]</span>';
            }else{
                $pbstr= '<span style="color:#039990">&nbsp;&nbsp;&nbsp;&nbsp;' .$this->name.'</span>';
            }

            $params = '?rc_show='.$this->rc_show;
            $title_url = url(admin_base_path().'/content/feature'.$params);
            $title_url1 = url('content/features/show?id='.$this->id);

            if ($images) {
                return '<span class="vtip" title="<img src='.upload_asset($images).' width=120 height=120>">
                            <img class="avatar small" src="'.upload_asset($images).'" align="absmiddle" style="width: 36px;height: 36px;">
                        </span>&nbsp;&nbsp;&nbsp;'. '<a href='.$title_url.' style="color: #b67647;">[' .get_category($this->rc_show).']</a><a target="_blank"  href='.$title_url1.'><span>'.$this->title.'</span></a>'.$pbstr;
            } else {
                return '<a href='.$title_url.' style="color: #b67647;">['.get_category($this->rc_show).']</a><span class="vtip" ></span>&nbsp;&nbsp;&nbsp;'. '<a target="_blank"  href='.$title_url1.'><span >'.$this->title.'</span></a>'.$pbstr;
            }
        })->width(300);
        $grid->district('所属地区')->display(function ($district) {
            if($district!=0 && $district!=''){

                return get_district_cn($district);
            }else{
                return '未作选择';
            }
        })->width(150);
        $grid->occupation('职位')->display(function ($occupation) {
            if($occupation!=''){
                return $occupation;
            }else{
                return '未评职称';
            }
        })->width(150);

        $grid->type_id('行业')->display(function ($type_id) {
            if($type_id!=0 && $type_id!=''){
                $category = \App\Models\FeatureSort::where('id', $type_id)->select(['categoryname'])->first();
                return $category?$category['categoryname']:null;
            }else{
                return '未选行业';
            }
        })->width(150);

        $grid->personal('人才类别')->display(function ($personal) {
            if($personal!=0 && $personal!=''){
                $category =get_category($personal);
                return $category?$category:null;
            }else{
                return '未作分类';
            }
        })->width(150);
        $grid->read('阅读');
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete(false);
            });
        });
        $grid->disableCreateButton(false);
        $grid->actions(function ($actions) {
            $actions->disableEdit(false);
            $actions->disableDelete(false);
        });
        $grid->filter(function ($filter) {
            $filter->equal('id', 'ID');
            $filter->like('title', '标题');
            $filter->like('name', '名称');
            $filter->equal('parentid', '行业分类')->select(FeatureSort::where('parent_id','0')->pluck('categoryname', 'id'));
            $filter->equal('rc_show', '人才展示')->select(Category::where('alias', 'RC_show')->pluck('demand', 'id'));
            $filter->equal('personal', '人才分类')->select(Category::where('alias', 'RC_category')->pluck('demand', 'id'));
            $filter->equal('district', '所属地区')->select(CategoryDistrict::where('parent_id', 623)->pluck('name', 'id'));


        });


        return $grid;
    }

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

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

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

        $form->text('name', '姓名')->rules('required|max:100', array('required'=>'姓名不能为空。','max'=>'姓名长度不能大于100。'))->setWidth(6)->setMustMark();
        $form->text('occupation', '职称')->rules('required|max:200', array('required'=>'职称不能为空。','max'=>'职称长度不能大于100。'))->setWidth(6)->setMustMark();
        $form->text('title', '主题')->rules('required|max:300', array('required'=>'主题不能为空。','max'=>'主题长度不能大于300。'))->setWidth(6)->setMustMark();

        $district= CategoryDistrict::where('parent_id', 623)->pluck('name', 'id');
        $district['0'] = '请选择';
        $form->select('district', '所属乡镇')->options($district)->default('0')->setWidth(6);

        $trade= FeatureSort::select()->pluck('categoryname', 'id');
        $trade['0'] = '请选择';
        $form->select('type_id', '所属行业')->options($trade)->default('0')->setWidth(6);

        $personal= Category::where('alias', 'RC_category')->pluck('demand', 'id');
        $personal['0'] = '请选择';
        $form->select('personal', '所属五类人才')->options($personal)->default('0')->setWidth(6);

        $rc_show= Category::where('alias', 'RC_show')->pluck('demand', 'id');
        $rc_show['0'] = '请选择';
        $form->select('rc_show', '文章类型')->options($rc_show)->default('0')->setWidth(6);

        $form->image('photo', '人才照片')->help('(建议尺寸240*70)')->setWidth(6);

        $form->textarea('summary', '人物简介')->rules('max:80', array('max'=>'Description不能大于80个字符。'))->setWidth(6);
        $form->editor('content', '履历介绍')->rules('required', array('required'=>'业绩介绍不能为空。'))->setWidth(6)->setMustMark();
        $display_option = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('is_display', '是否显示')->default(1)->states($display_option)->setMustMark();
//        $form->date('created_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'));
        $form->number('article_order', '排序')->min(0)->default(255)->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)');
        $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
        $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
        $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
        $form->hidden('addtime');//添加时间
        $form->hidden('parentid');//行业大类

        $form->saving(function (Form $form){
            //设置添加时间
            $form->addtime=time();

            if($form->type_id!=0){
                // 设置行业大类
                $featureSort= FeatureSort::where(['id'=>$form->type_id])->first();
                if($featureSort->parent_id==0){
                    $form->parentid=$form->type_id;
                }else{
                    $form->parentid=$featureSort->parent_id;
                }
            }else{
                $form->parentid=0;
            }
        });
        return $form;
    }





    protected function editForm($id)
    {
        $form = new Form(new Feature);
        $form->text('name', '姓名')->rules('required|max:100', array('required'=>'姓名不能为空。','max'=>'姓名长度不能大于100。'))->setWidth(6)->setMustMark();
        $form->text('occupation', '职称')->rules('required|max:200', array('required'=>'职称不能为空。','max'=>'职称长度不能大于100。'))->setWidth(6)->setMustMark();
        $form->text('title', '主题')->rules('required|max:300', array('required'=>'主题不能为空。','max'=>'主题长度不能大于300。'))->setWidth(6)->setMustMark();

        $district= CategoryDistrict::where('parent_id', 623)->pluck('name', 'id');
        $district['0'] = '请选择';
        $form->select('district', '所属乡镇')->options($district)->default('0')->setWidth(6);

        $trade= FeatureSort::select()->pluck('categoryname', 'id');
        $trade['0'] = '请选择';
        $form->select('type_id', '所属行业')->options($trade)->default('0')->setWidth(6);

        $personal= Category::where('alias', 'RC_category')->pluck('demand', 'id');
        $personal['0'] = '请选择';
        $form->select('personal', '所属五类人才')->options($personal)->default('0')->setWidth(6);

        $rc_show= Category::where('alias', 'RC_show')->pluck('demand', 'id');
        $rc_show['0'] = '请选择';
        $form->select('rc_show', '文章类型')->options($rc_show)->default('0')->setWidth(6);

        $form->image('photo', '人才照片')->help('(建议尺寸240*70)')->setWidth(6);

        $form->textarea('summary', '人物简介')->rules('max:80', array('max'=>'Description不能大于80个字符。'))->setWidth(6);
        $form->editor('content', '履历介绍')->rules('required', array('required'=>'业绩介绍不能为空。'))->setWidth(6)->setMustMark();
        $display_option = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('is_display', '是否显示')->states($display_option)->setMustMark();
//        $form->date('created_at', '发布日期')->format('YYYY-MM-DD')->rules('required|date', array('required'=>'发布日期不能为空。', 'date'=>'发布日期格式不正确。'));
        $form->number('article_order', '排序')->min(0)->default(0)->rules('required', array('required'=>'排序不能为空。'))->help('(数字越大越靠前)');
        $form->url('is_url', '外部链接')->rules('max:250', array('max'=>'外部链接长度不能超过250。'))->help('(请输入包含http://或https://的完整链接)');
        $form->text('seo_keywords', 'Keywords')->placeholder('合理设置Keywords有利于搜索引擎排名')->rules('max:80', array('max:Keywords不能大于80个字符。'));
        $form->textarea('seo_description', 'Description')->placeholder('合理设置Description有利于搜索引擎排名')->rules('max:80', array('max'=>'Description不能大于80个字符。'));
        $form->hidden('addtime');//添加时间
        $form->hidden('parentid');//行业大类

        $form->saving(function (Form $form){


            if($form->type_id!=0){
                // 设置行业大类
                $featureSort= FeatureSort::where(['id'=>$form->type_id])->first();
                if($featureSort->parent_id==0){
                    $form->parentid=$form->type_id;
                }else{
                    $form->parentid=$featureSort->parent_id;
                }
            }else{
                $form->parentid=0;
            }
        });

        return $form;
    }


    public function destroy($id)
    {
        $ids = array();
        if ($id) {
            $ids = explode(',', $id);
        }
        if (!$ids) {
            return admin_toastr('请勾选需要删除的人才展示', 'error');
        }
        \DB::beginTransaction();
        try {
            Feature::whereIn('id', $ids)->delete();
            $data = [
                'status'  => true,
                'message' => '删除成功!',
            ];
            \DB::commit();
            return response()->json($data);
        } catch (\Exception $e) {
            \DB::rollback();
            $data = [
                'status'  => false,
                'message' => '删除失败!',
            ];
            return response()->json($data);
        }
    }
}