<?php

namespace App\Http\Controllers\Configuration;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Controllers\Utils\PaginatorController;
use stdClass;

class ConsigneeController extends Controller
{

    protected  $route = 'configurations.consignee';
    public function index() {

        $exampledata = [
            [
                'id' => 1,
                'CSN_CODE' => 'Netbay',
                'CSN_NAME' => 'บริษัทเน็ตเบย์ จำกัด มหาชน',
                'CSN_EMAIL' => 'jatuporn@netbay.co.th',
                'CSN_TAXID' => '0107557000101',
                'CSN_BRANCH' => '0000',
            ]
            ,  
            [
                'id' => 2,
                'CSN_CODE' => 'Netbay2',
                'CSN_NAME' => 'บริษัทเน็ตเบย์ จำกัด มหาชน',
                'CSN_EMAIL' => 'jatuporn@netbay.co.th',
                'CSN_TAXID' => '0107557000101',
                'CSN_BRANCH' => '0000',
            ],
            [
                'id' => 3,
                'CSN_CODE' => 'Netbay3',
                'CSN_NAME' => 'บริษัทเน็ตเบย์ จำกัด มหาชน',
                'CSN_EMAIL' => 'jatuporn@netbay.co.th',
                'CSN_TAXID' => '0107557000101',
                'CSN_BRANCH' => '0000',
            ],
            [
                'id' => 4,
                'CSN_CODE' => 'Netbay4',
                'CSN_NAME' => 'บริษัทเน็ตเบย์ จำกัด มหาชน',
                'CSN_EMAIL' => 'jatuporn@netbay.co.th',
                'CSN_TAXID' => '0107557000101',
                'CSN_BRANCH' => '0000',
            ],
            [
                'id' => 5,
                'CSN_CODE' => 'Netbay5',
                'CSN_NAME' => 'บริษัทเน็ตเบย์ จำกัด มหาชน',
                'CSN_EMAIL' => 'jatuporn@netbay.co.th',
                'CSN_TAXID' => '0107557000101',
                'CSN_BRANCH' => '0000',
            ],
            ];
        
            $consignee = PaginatorController::paginate($exampledata, 3);
            $url = '/configurations/consignee';
            $route = $this->route;
            $consignee->withPath(  $url );

            $searchBy = [ 
                "name" => "Name",
                "blacklist" => "BlackList",
            ];
            return view('configurations.consignees.index', compact('consignee', 'url', 'route', 'searchBy'));
    }


    public function create()
    {
        $route = $this->route;
        return view('configurations.consignees.create' , compact('route'));
    }

    public function store(Request $request)
    {
        $rules = array(
            'name'       => 'required',
            'password'       => 'required',
            'email'      => 'required|email',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        } else {
          
            $user = new User;
            $user->password = Hash::make($request->input('password'));
            $user->name = $request->input('name');
            $user->email = $request->input('email');
            $user->save();

        return redirect()->route('configurations.consignee')
            ->withSuccess(__('Company created successfully.'));
        }
    }

    public function show(User $user)
    {
        return view('configurations.consignees.show', [
            'user' => $user
        ]);
    }

    public function edit(User $user)
    {
        $consignee =  [
            'id' => 1,
            'CSN_CODE' => 'Netbay',
            'CSN_NAME' => 'บริษัทเน็ตเบย์ จำกัด มหาชน',
            'CSN_TAXID' => '0107557000101',
            'CSN_BRANCH' => '0000',
            'CSN_ADDR' => '1/54ม.10ซ.นวลจันทร์16 ถ.นวลจันทร์ แขวงคลองกุ่ม',
            'CSN_DISTRICT' => 'คลองกุ่ม',
            'CSN_SUBPROVINCE' => 'บึงกุ่ม',
            'CSN_PROVINCE' => 'คลองกุ่ม',
            'CSN_POSTCODE' => '10240',
            'CSN_COUNTRY' => 'AD',
            'CSN_EMAIL' => 'jatuporn@netbay.co.th',
            'CSN_PHONE' => '0933333333333',

        ];
        $countryCode = PaginatorController::paginate( config('mockdata.getCountryCode'), 4 );
        $countryCode->withPath('/configurations/consignee/edit');
        $url = '/configurations/consignee/edit';
        return view('configurations.consignees.edit', [
            'consignee' => $consignee,
            'route' => $this->route,
            'countryCode'=> $countryCode,
            'url'=> $url,
        ]);
    }

    public function update(Request $request, User $user)
    {
        $rules = array(
            'name'       => 'required',
            'email'      => 'required|email',
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::back()
                ->withErrors($validator)
                ->withInput();
        } else {
            $user->name = $request->input('name');
            $user->email = $request->input('email');
            $user->save();

        return redirect()->route('configurations.consignee')
            ->withSuccess(__('Company updated successfully.'));
        }
    }
    public function destroy(User $user)
    {
        $user->delete();

        return redirect()->route('configurations.consignee')
            ->withSuccess(__('Company deleted successfully.'));
    }
}
