<?php

namespace App\Http\Controllers\Master;

use App\Http\Controllers\Controller;
use App\Models\Master\ManagementMasters;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class ManagementMasterController extends Controller
{
    public function index()
    {
        $management_master = ManagementMasters::paginate(10);

        return view('pages.masters.management_masters.index', compact('management_master'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        // call view pages.masters.management_masters.create
        return view('pages.masters.management_masters.create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        // validation
        $rules = array(
            'master_code'  => 'required',
            'master_value'  => 'required',
            'master_column'  => 'required',
        );
        $validator = Validator::make($request->all(), $rules);

        // check validation
        if ($validator->fails()) {
            return Redirect::to('master/management-master/create')
                ->withErrors($validator)
                ->withInput($request->all());
        } else {
            // create new master data
            $management_master = ManagementMasters::create([
                'master_code' => $request->master_code,
                'master_value' => $request->master_value,
                'master_column' => $request->master_column,
            ]);

            // Create Session message
            Session::flash('management_masters-message', [
                'type' => 'success',
                'msg' => 'Anda berhasil menambahkan data!'
            ]);

            return Redirect::to('master/management-master');
        }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        // get data from management_master
        $management_master = ManagementMasters::findOrFail($id);

        // call view pages
        return view('pages.masters.management_masters.edit', compact('management_master'));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        // validation
        $rules = array(
            'master_code'  => 'required',
            'master_value'  => 'required',
            'master_column'  => 'required',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('master/management-master/edit/' . $id)
                ->withErrors($validator)
                ->withInput($request->all());
        } else {
            // create new account
            $management_master = ManagementMasters::findOrFail($id);
            if (!$management_master) {
                // Create Session message
                Session::flash('management_masters-message', [
                    'type' => 'warning',
                    'msg' => 'Data tidak ditemukan!'
                ]);

                return Redirect::to('master/management-master');
            }
            $management_master->update([
                'master_code' => $request->master_code,
                'master_value' => $request->master_value,
                'master_column' => $request->master_column,
            ]);

            // Create Session message
            Session::flash('management_masters-message', [
                'type' => 'success',
                'msg' => 'Anda berhasil mengubah data!'
            ]);

            return Redirect::to('master/management-master');
        }
    }
}
