<?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($input){
            $re = Project::where('id',$project_id)->update($input);
            if($re){
                return redirect('/project');
            }else{
                return back()->with('errors','更新失败，请检查输入后重试!');
            }
        }
    }

    public function destroy($project_id)
    {
        $re = Project::where('id',$project_id)->delete();
        if($re){
            $data = [
                'status'=>1,
                'msg' => '隐藏成功！'
            ];
        }else{
            $data =[
                'status'=>0,
                'msg' => '隐藏失败，请刷新重试！'
            ];
        }
        return $data;
    }

    public function show($project_id)
    {
        $re = Project::find($project_id);
        $project['display'] = 1;
        $project['id'] = $re->id;
        $project['project_name'] = $re->project_name;
        $test = Project::where('id',$project_id)->update($project);
        if($test){
            $data = [
                'status'=>1,
                'msg' => '显示成功！'
            ];
        }else{
            $data =[
                'status'=>0,
                'msg' => '显示失败，请刷新重试！'
            ];
        }
        return $data;
    }

    public function display($project_id)
    {
        $re = Project::find($project_id);
        $project['display'] = 0;
        $project['id'] = $re->id;
        $project['project_name'] = $re->project_name;
        $test = Project::where('id',$project_id)->update($project);
        if($test){
            $data = [
                'status'=>1,
                'msg' => '隐藏成功！'
            ];
        }else{
            $data =[
                'status'=>0,
                'msg' => '隐藏失败，请刷新重试！'
            ];
        }
        return $data;
    }

    public function store()
    {
        $input = Input::except('_token');
        $input['display'] = 1;
        if($input){
            $re = Project::create($input);
            if($re){
                return redirect('/project');
            }else{
                return back()->with('errors','新建失败，请检查输入后重试！');
            }
        }
    }

}