<?php

namespace App\Admin\Controllers\Health;

use App\Admin\Extensions\Form\ValidateForm;
use App\Http\Controllers\Controller;
use App\Models\ArticleCategory;
use App\Models\PostAppoint;
use App\Models\Presentation;
use App\Models\PresentationAppoint;
use Encore\Admin\Auth\Permission;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\QjwjAppoint;
use App\Models\JyyxAppoint;

class IndexController extends Controller
{
    /**
     * 宣讲会管理
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('宣讲会管理')
            ->description('')
            ->body($this->grid());
    }

    protected function grid()
    {
        $grid = new Grid(new Presentation);
        $grid->model()->orderBy('created_at', 'DESC');
        $grid->model()->paginate(20);
        if (Admin::user()->can('recruit_manager_create')) {
            $grid->disableCreateButton(false);
        }
        $grid->perPages([10, 20, 30, 40, 50, 100]);
        $grid->id('ID');
        $grid->name('名称');
        $grid->time('时间');
        $grid->address('地址');
        $grid->contact_info('联系方式');
        $grid->column('status','状态')->display(function ($status) {
            return $status ? '显示' : '隐藏';
        });
        $grid->created_at('创建时间');
        $grid->actions(function ($actions) {
            $actions->append('<a href="/ST3IXxKlOa4eGEv0eTw0CfORI9444Mgj/health/presentation_appoint_list?id=' . $actions->row['id'] . '"  class="btn btn-primary btn-xs" >报名管理</a>');
            $actions->disableEdit(false);
            $actions->disableDelete(false);
            $actions->disableView();
        });

        return $grid;
    }

    public function create(Content $content)
    {
        return $content
            ->header('宣讲会创建')
            ->description('带*号必填项')
            ->body($this->form());
    }

    public function edit($id, Content $content)
    {
        return $content
            ->header('宣讲会编辑')
            ->description('')
            ->body($this->editForm($id)->edit($id));
    }

    protected function editForm($id){
        $info = Presentation::find($id);
        $form = new ValidateForm(new Presentation);
        $form->text('name', '宣讲会标题')->rules('required|max:100', array('required'=>'标题不能为空。','max'=>'标题长度不能大于100。'))->setMustMark();
        $form->text('time', '宣讲时间段')->rules('required', array('required'=>'时间段不能为空。'))->setMustMark();
        $form->text('address', '宣讲会地址')->rules('required', array('required'=>'宣讲会地址不能为空。'))->setMustMark();
        $form->text('contact_info', '联系方式')->rules('required', array('required'=>'联系方式不能为空。'))->setMustMark();
        $display_option = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('status', '是否显示')->states($display_option)->default('1')->setMustMark();
        return $form;

    }

    protected function form()
    {
        $form = new ValidateForm(new Presentation);
        $form->text('name', '宣讲会标题')->rules('required|max:100', array('required'=>'标题不能为空。','max'=>'标题长度不能大于100。'))->setMustMark();
        $form->text('time', '宣讲时间段')->rules('required', array('required'=>'时间段不能为空。'))->setMustMark();
        $form->text('address', '宣讲会地址')->rules('required', array('required'=>'宣讲会地址不能为空。'))->setMustMark();
        $form->text('contact_info', '联系方式')->rules('required', array('required'=>'联系方式不能为空。'))->setMustMark();
        $display_option = [
            'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];
        $form->switch('status', '是否显示')->states($display_option)->default('1')->setMustMark();

        return $form;
    }

    public function store(Request $request){
        $verify = $this->form()->getValidateInput();

        $data = [
            'name' => $verify['name'],
            'time'  => $verify['time'],
            'address' => $verify['address'],
            'contact_info' => $verify['contact_info'],
            'status' => $verify['status']
        ];


        try {
            Presentation::create($data);
            DB::commit();
        }catch (\Exception $e) {
            DB::rollback();
            return admin_toastr($e->getMessage(), 'error');
        }

    }

    public function update($id)
    {
        return $this->editForm($id)->update($id);
    }

    public function destroy($id)
    {
        if ($this->form()->destroy($id)) {
            $data = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } else {
            $data = [
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ];
        }
        return response()->json($data);
    }

    public function appointList(Content $content, Request $request)
    {
        Permission::check('presentation_appoint_list');
        $id = $request->id;
        $presentation = Presentation::find($id);
        if (empty($presentation)) {
            return back();
        }

        $search_data = $request->all();

        $where = [];
        $where[] = ['pid','=',$presentation->id];
        if (isset($search_data['id'])) {

            foreach ($search_data as $k => $v) {
                if ($k == 'realname') {
                    $where[] = [$k, 'like', "%$v%"];
                } elseif ($k != '_pjax' and $k != 'page' && $k != 'perpage') {
                    $where[] = [$k, '=', $v];
                }
            }
        }
        $perpage = 20;
        $list = PresentationAppoint::where($where)
            ->select('*')
            ->orderBy('updated_at','desc')
            ->paginate($perpage);

        foreach ($list as $k => $v){
            if($v->sex == 0){
                $list[$k]['sex'] = '女';
            }else{
                $list[$k]['sex'] = '男';
            }
            if($v->fresh == 0){
                $list[$k]['fresh'] = '否';
            }else{
                $list[$k]['fresh'] = '是';
            }
            if($v->status == 0){
                $list[$k]['status'] = '未下载';
            }else{
                $list[$k]['status'] = '已下载';
            }
        }

        return $content
            ->header('报名管理')
            ->description('人员信息列表')
            ->body(view('admin.health.appoint_list')->with([
                'list' => $list,
                'search_data' => $search_data,
                'recruit' => $presentation
            ]));

    }

    public function qjwjInfoList(Content $content, Request $request){
        Permission::check('presentation_appoint_list');

        $search_data = $request->all();

        $where = [];
        if (isset($search_data['id'])) {

            foreach ($search_data as $k => $v) {
                if ($k == 'realname') {
                    $where[] = [$k, 'like', "%$v%"];
                } elseif ($k != '_pjax' and $k != 'page' && $k != 'perpage') {
                    $where[] = [$k, '=', $v];
                }
            }
        }
        $perpage = 20;
        $list = QjwjAppoint::where($where)
            ->select('*')
            ->orderBy('updated_at','desc')
            ->paginate($perpage);

        foreach ($list as $k => $v){
            if($v->sex == 0){
                $list[$k]['sex'] = '女';
            }else{
                $list[$k]['sex'] = '男';
            }
            if($v->status == 0){
                $list[$k]['status'] = '未下载';
            }else{
                $list[$k]['status'] = '已下载';
            }
        }

        return $content
            ->header('信息管理')
            ->description('泉籍卫技人员信息列表')
            ->body(view('admin.health.qjwj_appoint_list')->with([
                'list' => $list,
                'search_data' => $search_data
            ]));
    }

    public function jyyxInfoList(Content $content, Request $request){
        Permission::check('presentation_appoint_list');

        $search_data = $request->all();

        $where = [];
        if (isset($search_data['id'])) {

            foreach ($search_data as $k => $v) {
                if ($k == 'realname') {
                    $where[] = [$k, 'like', "%$v%"];
                } elseif ($k != '_pjax' and $k != 'page' && $k != 'perpage') {
                    $where[] = [$k, '=', $v];
                }
            }
        }
        $perpage = 20;
        $list = JyyxAppoint::where($where)
            ->select('*')
            ->orderBy('updated_at','desc')
            ->paginate($perpage);

        foreach ($list as $k => $v){
            if($v->sex == 0){
                $list[$k]['sex'] = '女';
            }else{
                $list[$k]['sex'] = '男';
            }
            if($v->status == 0){
                $list[$k]['status'] = '未下载';
            }else{
                $list[$k]['status'] = '已下载';
            }
        }

        return $content
            ->header('信息管理')
            ->description('就业单身人员信息列表')
            ->body(view('admin.health.jyyx_appoint_list')->with([
                'list' => $list,
                'search_data' => $search_data
            ]));
    }

    public function postInfoList(Content $content, Request $request){
        Permission::check('presentation_appoint_list');

        $search_data = $request->all();

        $where = [];
        if (isset($search_data['id'])) {

            foreach ($search_data as $k => $v) {
                if ($k == 'realname') {
                    $where[] = [$k, 'like', "%$v%"];
                } elseif ($k != '_pjax' and $k != 'page' && $k != 'perpage') {
                    $where[] = [$k, '=', $v];
                }
            }
        }
        $perpage = 20;
        $list = PostAppoint::where($where)
            ->select('*')
            ->orderBy('updated_at','desc')
            ->paginate($perpage);

        foreach ($list as $k => $v){
            if($v->sex == 0){
                $list[$k]['sex'] = '女';
            }else{
                $list[$k]['sex'] = '男';
            }
            if($v->status == 0){
                $list[$k]['status'] = '未下载';
            }else{
                $list[$k]['status'] = '已下载';
            }
        }

        return $content
            ->header('信息管理')
            ->description('投递岗位人员信息列表')
            ->body(view('admin.health.post_appoint_list')->with([
                'list' => $list,
                'search_data' => $search_data
            ]));
    }
}