<?php

namespace App\Http\Controllers\Admin;

use App\Model\Adminuser;
use App\Model\Job;
use App\Model\Tablelist;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Admin\CommonController;
use Session;

class JobController extends CommonController
{

    public function index()
    {
        $data = Job::orderby('role', 'asc')->paginate(20);

        return view('admin.job.index', compact('data'));

    }

    public function edit($job_id)
    {

        $data = Job::find($job_id);

        return view('admin.job.edit', compact('data'));

    }

    public function create()
    {

        return view('admin.job.create');

    }

    public function update($job_id)
    {

        if (!Job::where('id', $job_id)->update(Input::except('_token', '_method'))) return back()->with('errors', '职位信息更新失败，请稍后重试!');

        return redirect('/job');

    }

    public function destroy($job_id)
    {

        if (Job::find($job_id)->delete()) return ['status' => 0, 'msg' => '删除失败，请刷新重试！'];

        return ['status' => 1, 'msg' => '删除成功！'];

    }

    public function store()
    {
        $input = Input::except('_token');

        $input['display'] = '1';

        if (!Job::create($input)) return back()->withErrors('新建失败，请稍后重试！');

        return redirect('job');

    }


    public function ting($job_id)
    {
        $temp['display'] = 0;
        $job = Job::where('id', $job_id)->update($temp);
        if ($job) {
            $data = [
                'status' => '1',
                'msg' => '隐藏成功！'
            ];
            return $data;
        } else {
            $data = [
                'status' => '0',
                'msg' => '隐藏失败！'
            ];
            return $data;
        }
    }

    public function start($job_id)
    {
        $temp['display'] = 1;
        $job = Job::where('id', $job_id)->update($temp);
        if ($job) {
            $data = [
                'status' => '1',
                'msg' => '显示成功！'
            ];
            return $data;
        } else {
            $data = [
                'status' => '0',
                'msg' => '显示失败！'
            ];
            return $data;
        }
    }

}