<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\Request;
use App\Model\Project;
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 ProjectController extends CommonController
{

    public function index()
    {

        $data = Project::orderby('id', 'asc')->paginate(10);

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

    }

    public function edit($project_id)
    {

        $data = Project::find($project_id);

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

    }

    public function create()
    {

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

    }

    public function update($project_id)
    {
        $input = Input::except('_token', '_method');

        if (!Project::where('id', $project_id)->update($input)) return back()->with('errors', '更新失败，请检查输入后重试!');

        return redirect('/project');

    }

    public function destroy($project_id)
    {

        if (!Project::find($project_id)->delete()) return ['status' => 0, 'msg' => '隐藏失败，请刷新重试！'];

        return ['status' => 1, 'msg' => '隐藏成功！'];

    }

    public function show($project_id)
    {

        if (!Project::find($project_id)->update(['display' => 1])) return ['status' => 0, 'msg' => '显示失败，请刷新重试！'];

        return ['status' => 1, 'msg' => '显示成功！'];

    }

    public function display($project_id)
    {

        if (!Project::find($project_id)->update(['display' => 0])) return ['status' => 0, 'msg' => '隐藏失败，请刷新重试！'];

        return ['status' => 1, 'msg' => '隐藏成功！'];

    }

    public function store()
    {

        $input = Input::except('_token');

        $input['display'] = 1;

        if (!Project::create($input)) return back()->with('新建失败，请检查输入后重试！');

        return redirect('/project');

    }

}