<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Employee;
use App\Models\EmployeeEducation;
use App\Models\EmployeeDocument;
use App\Models\EducationLevel;
use App\Models\DocumentType;

class DocumentTypeController extends Controller
{
    
    public function index()
    {
        $data['title'] = "Document Type";
        $data['all'] = DocumentType::orderBy('document_name','ASC')->get();
        return view('admin.document_type.index',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data['title'] = "Document Type Create";
        return view('admin.document_type.add',$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'document_name'            => 'required'
        ]);
        $user_id = \Auth::user()->id;
        $doc_type = new DocumentType;
        $doc_type->document_name = $request->document_name;
        $doc_type->added_by = $user_id;
        $doc_type->status = 1;
        $doc_type->save();
        return redirect()->back()->with('msg','Data Insert Successful!');

    }
    public function edit($id)
    {
        $data['title'] = "Document Type Edit";
        $data['doc_type'] = DocumentType::find($id);
        return view('admin.document_type.edit',$data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'document_name'            => 'required'
        ]);
        $user_id = \Auth::user()->id;
        $doc_type = DocumentType::find($id);
        $doc_type->document_name = $request->document_name;
        $doc_type->updated_by = $user_id;
        $doc_type->save();
        return redirect('admin/document_type')->with('msg','Data Update Successful!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        DocumentType::whereId($id)->delete();
        return redirect('admin/document_type')->with('msg','Successfully Deleted!');
    }
    public function statusUpdate($id)
    {

        $user_id = \Auth::user()->id;
        $doc_info = DocumentType::find($id);
        if($doc_info->status == 0){
            $doc_info->status = 1;
            $status = 'published';
        }else{
            $doc_info->status = 0;
            $status = 'unpublished';
        }
        $doc_info->approved_by = $user_id;
        $doc_info->update();

    }
}
