<?php

namespace App\Admin\Controllers\Health;

use App\Admin\Extensions\Form\ValidateForm;
use App\Http\Controllers\Controller;
use App\Models\PostAppoint;
use App\Models\Presentation;
use App\Models\PresentationAppoint;
use Encore\Admin\Auth\Permission;
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)
    {
        Permission::check('presentation');
        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', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setMustMark();
        $form->text('time', '宣讲时间段')->rules('required', ['required' => '时间段不能为空。'])->setMustMark();
        $form->text('address', '宣讲会地址')->rules('required', ['required' => '宣讲会地址不能为空。'])->setMustMark();
        $form->text('contact_info', '联系方式')->rules('required', ['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', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setMustMark();
        $form->text('time', '宣讲时间段')->rules('required', ['required' => '时间段不能为空。'])->setMustMark();
        $form->text('address', '宣讲会地址')->rules('required', ['required' => '宣讲会地址不能为空。'])->setMustMark();
        $form->text('contact_info', '联系方式')->rules('required', ['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('health_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 == 1) {
                $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('qjwjInfoList');

        $search_data = $request->all();

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

        foreach ($list as $k => $v) {
            $v->native_place_cn = get_district_cn($v->native_place);
            $v->education       = get_category($v->education);
            $v->pro_type        = get_category_major($v->pro_type);
            if ($v->sex == 1) {
                $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 qjwjStatus(Request $request)
    {

        $info = QjwjAppoint::where(['id' => $request->id])->first();
        if ($info) {
            $info->status = 1;
            $info->save();
            return response()->json(['status' => 1, 'msg' => '跳转下载中!', 'data' => 'ok', 'url' => $info->attachment]);
        } else {
            return response()->json(['status' => 0, 'msg' => '找不到记录!', 'data' => 'fail']);
        }
        //return "/storage/recruit/word/" . $recruit->name_en . '/' .$appoint->audit .$word_url;
    }

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

        $search_data = $request->all();

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

        foreach ($list as $k => $v) {

            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 jyyxStatus(Request $request)
    {

        $info = JyyxAppoint::where(['id' => $request->id])->first();
        if ($info) {
            $info->status = 1;
            $info->save();
            return response()->json(['status' => 1, 'msg' => '跳转下载中!', 'data' => 'ok', 'url' => $info->attachment]);
        } else {
            return response()->json(['status' => 0, 'msg' => '找不到记录!', 'data' => 'fail']);
        }
        //return "/storage/recruit/word/" . $recruit->name_en . '/' .$appoint->audit .$word_url;
    }

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

        $search_data = $request->all();

        $where = [];
        foreach ($search_data as $k => $v) {
            if ($k == 'realname') {
                $where[] = [$k, 'like', "%$v%"];
            } elseif (in_array($k, ['sex', 'education'])) {
                $where[] = ['post_appoint.' . $k, '=', $v];
            }
        }
        $perpage = 20;
        $list    = PostAppoint::where($where)
            ->leftJoin("jobs", "jobs.id", '=', 'post_appoint.job_id')
            ->select('post_appoint.*', 'jobs.jobs_name', 'jobs.company_name')
            ->orderBy('updated_at', 'desc')
            ->paginate($perpage);

        foreach ($list as $k => $v) {
            $v->native_place_cn = get_district_cn($v->native_place);
            $v->education       = get_category($v->education);
            if ($v->sex == 1) {
                $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,
            ]));
    }

    public function postStatus(Request $request)
    {
        Permission::check('postInfoList');
        $info = PostAppoint::where(['id' => $request->id])->first();
        if ($info) {
            $info->status = 1;
            $info->save();
            return response()->json(['status' => 1, 'msg' => '跳转下载中!', 'data' => 'ok', 'url' => $info->attachment]);
        } else {
            return response()->json(['status' => 0, 'msg' => '找不到记录!', 'data' => 'fail']);
        }
        //return "/storage/recruit/word/" . $recruit->name_en . '/' .$appoint->audit .$word_url;
    }

    public function presentationAppointList(Content $content,Request $request)
    {
        $search_data = $request->all();

        $where = [
            ['pid', '=', $search_data['id']],
        ];
        foreach ($search_data as $k => $v) {
            if ($k == 'realname') {
                $where[] = [$k, 'like', "%$v%"];
            } elseif (in_array($k, ['sex', 'education'])) {
                $where[] = [$k, '=', $v];
            }
        }
        $perpage = 20;
        $list    = PresentationAppoint::where($where)
            ->orderBy('updated_at', 'desc')
            ->paginate($perpage);

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

        return $content
            ->header('报名管理')
            ->description('招聘会报名的人员信息管理')
            ->body(view('admin.health.presentation_appoint_list')->with([
                'list'        => $list,
                'search_data' => $search_data,
            ]));
    }

    public function presentationStatus(Request $request)
    {
        $info = PresentationAppoint::where(['id' => $request->id])->first();
        if ($info && $info->attachment) {
            $info->status = 1;
            $info->save();
            return response()->json(['status' => 1, 'msg' => '跳转下载中!', 'data' => 'ok', 'url' => $info->attachment]);
        } else {
            return response()->json(['status' => 0, 'msg' => '用户未上传简历!', 'data' => 'fail']);
        }
        //return "/storage/recruit/word/" . $recruit->name_en . '/' .$appoint->audit .$word_url;
    }
}