<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models;
use Session;
use Illuminate\Support\Arr;


class SystemUsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $allusers = new Models\SystemUser;
        $table_columns = \DB::select(\DB::raw("show full columns from auth_users"));
        $exclude_arr = [
            'updated_at', 'id', 'password'
        ];
        $columns = [];
        $queries = [];
        foreach ($table_columns as $key) {
          $columns[$key->Field] = $key->Field;
        }
        foreach ($columns as $col) {
            if (request('filter') == $col) {
                $filter = request('filter');
                $keyword = request('keyword');
                $table_arr = ['staff_id'];
                if (in_array($col, $table_arr)) {
                    $key = $this->get_filter_ids($filter, $keyword);
                    if (!empty($key)) {
                        $keyword = $key;
                    }
                }
                $allusers = $allusers->where($col, 'like', '%'.$keyword.'%');
                $queries[$col] = request('keyword');
            }
        }

        $allusers = $allusers->orderBy('created_at', 'DESC')->paginate(40)->appends($queries);

        $data = [
            'page_title' => 'Users',
            'columns' => Arr::except($columns, $exclude_arr),
            'allusers' => $allusers
        ];

        return view('systemusers.index', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $designation = Models\Designation::pluck('name', 'id');
        $data = [
            'page_title' => 'Create Users',
            'designation' => $designation
        ];

        return view('systemusers.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'designation' => 'required',
            'email' => 'required',
            // 'phone' => 'required',
            'password' => 'required',
            'confirm_password' => 'same:password',
            
        ]);

        $make_account = [
            'name' => $request->name,
            'designation' => $request->designation,
            'email' => $request->email,
            // 'phone' => $request->phone,
            'password' => md5($request->password)
        ];

        $inserted = Models\SystemUser::create($make_account);
        Session::flash('success_message', 'Account successfully added');
        return redirect(url('systemusers'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $account_arr = Models\SystemUser::find($id);
        $data = [
            'page_title' => 'Show Service',
            'account_arr' => $account_arr
        ];

        return view('systemusers.show', $data);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $designation = Models\Designation::pluck('name', 'id');
        $account_arr = Models\SystemUser::find($id);
        $data = [
            'page_title' => 'Edit Users',
            'designation' => $designation,
            'account_arr' => $account_arr
        ];

        return view('systemusers.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)
    {
        $account_arr = Models\SystemUser::find($id);
        $account_arr->name = $request->name;
        $account_arr->designation = $request->designation;
        $account_arr->email = $request->email;
        // $account_arr->phone = $request->phone;
        $account_arr->password = md5($request->password);
        $account_arr->save();
        Session::flash('success_message', 'Account successfully Updated');
        return redirect(url('systemusers'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $result = Models\SystemUser::destroy($id);
        if (request()->ajax()) {
            $result_arr = ['code' => 1];
            return response()->json($result_arr);
        }
        Session::flash('success_message', 'Account successfully deleted!');
        return redirect(route('accounts.index'));
    }
    public function get_filter_ids($filter, $keyword)
    {
        switch ($filter) {
            case 'name':
              $id = Models\SystemUser::where('name', 'like', "%$keyword%")->get(['id']);
              if ($id->isEmpty()) {
                return '';
              }
              $step = json_decode($id);
              $the_id = $step[0]->id;
              return (count($step) > 0 ) ? $step[0]->id : "";
              break;
            case 'designation':
              $id = Models\SystemUser::where('designation', 'like', "%$keyword%")->get(['id']);
              if ($id->isEmpty()) {
                return '';
              }
              $step = json_decode($id);
              $the_id = $step[0]->id;
              return (count($step) > 0 ) ? $step[0]->id : "";
              break;
            case 'email':
              $id = Models\SystemUser::where('email', 'like', "%$keyword%")->get(['id']);
              if ($id->isEmpty()) {
                return '';
              }
              $step = json_decode($id);
              $the_id = $step[0]->id;
              return (count($step) > 0 ) ? $step[0]->id : "";
              break;
            case 'phone':
              $id = Models\SystemUser::where('phone', 'like', "%$keyword%")->get(['id']);
              if ($id->isEmpty()) {
                return '';
              }
              $step = json_decode($id);
              $the_id = $step[0]->id;
              return (count($step) > 0 ) ? $step[0]->id : "";
              break;
            default:
              return '';
              break;
        }
    }


}
