<?php

namespace App\Http\Controllers\Admin;

use App\Model\Adminuser;
use App\Model\Department;
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 DepartmentController extends CommonController{

    public function index()
    {
        $data = Department::orderby('id','asc')->paginate(20);
        return view('admin.department.index',compact('data'));
    }

    public function edit($dep_id)
    {
        $data = Department::find($dep_id);
        return view('admin.department.edit',compact('data'));
    }

    public function create()
    {
        return view('admin.department.create');
    }

    public function update($dep_id)
    {
        $input = Input::except('_token','_method');
        $re = Department::where('id',$dep_id)->update($input);
        if($re){
            return redirect('/department');
        }else{
            return back()->with('errors','机构信息更新失败，请稍后重试!');
        }
    }

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

    public function store()
    {
        $input = Input::except('_token');
        $input['display'] = '1';
        $rules = [
            'department_description'=>'max:200',
            'department_name'=>'required|max:100',
        ];
        $msg = [
            'department_name.required'=>'名称不能为空！',
            'department_description.max'=>'描述不能超过100字！',
            'department_name.max'=>'名称请保持在50字内！',
        ];
        $validator = Validator::make($input,$rules,$msg);
        if($validator->passes()){
            $re = Department::create($input);
            if($re){
                return redirect('/department');
            }else{
                return back()->with('msg','新建失败，请稍后重试！');
            }
        }else{
            return back()->withErrors($validator);
        }
    }


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

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

}