<?php

namespace App\Http\Controllers\Admin;

use App\Addresses;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Customers;
use Illuminate\Support\Facades\Validator;
use App\Helpers\PasswordEncryptHelper;
use App\Transactions;
class CustomerController extends AdminController
{
    public function __construct() {
        $this->middleware('auth');

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/customers";
        $this->breadcrumbs[1]['name'] = "Customers";
    }
    
    public function create() {
        $data["pageHeader"] = false;
        $data["model"] = new Customers();
        $data["title"] = "Customers";
        $data["subTitle"] = "Create";

        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/customers/create', $data);
    }
    public function save(Request $request) {

        $rules = [
            'firstName' => 'required|string',
            'lastName' => 'required|string',
            'email' => 'required|string|email|unique:users',
            'password' => 'required|string'
        ];
        $input = $request->all();

        $message = [];
        $validator = Validator::make($input, $rules, $message);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            $password=bcrypt($request->password);
            d($password,1);

            $input["password"] = PasswordEncryptHelper::encrypt_decrypt('encrypt', $request->password);
            $input["created_at"] = date("Y-m-d H:i:s");
            if (!isset($input["status"])) {
                $input["status"] = "0";
            }
            unset($input["_token"]);
            $userId = Customers::insertGetId($input);
           
         

            \Session::flash('success', 'Customers added successfully!');
            return redirect('admin/customers');
        }
    }
    public function index() {

        $model = Customers::get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["title"] = "Customers";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/customers/index', $data);
    }
    public function show($id) {

        $q = new Customers();
        $data['model'] = $q->find($id);
        
        $data['transaction']= Transactions::where('customer_id',$id)->get();
        
        $data["title"] = "Customers";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = $data['model']->firstName . " " . $data['model']->lastName;

        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/customers/show', $data);
    }
    public function delete($id) {

        Customers::where('id', $id)->delete();

        \Session::flash('error', 'Customers deleted successfully!');
        return redirect('admin/customers');
    }
    public function edit($id) {

        $q = new Customers();
        $banners = $q->with('address')->find($id);
     
        $data['model'] = $banners;
     
        $data["title"] = "Customers";
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;

        return view('admin/customers/edit', $data)->with('id', $id);
    }
    public function update($id, Request $request) {

        $rules = [
            'firstName' => 'required|string',
            'lastName' => 'required|string',
            'email' => 'required|string|email',
                //'Password' => 'required|string'
        ];
        $input = $request->all();

        $message = [];
        $validator = Validator::make($input, $rules, $message);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            
            unset($input["_token"]);

            if (!empty($input["password"])) {
             
                $input["password"] = PasswordEncryptHelper::encrypt_decrypt('encrypt', $request->password);
            }


            $customer["updated_at"] = date("Y-m-d H:i:s");
            $customer["firstName"] =  $input['firstName'];
            $customer["lastName"] =  $input['lastName'];
            $customer["email"] =  $input['email'];
            $customer["phone"] =  $input['phone'];
            $customer["type"] =  $input['type'];
            $customer["status"] =  $input['status'];
            $address["address1"] =  $input['address'];
            $address["address2"] =  $input['address2'];
            $address["city"] =  $input['city'];
            $address["postcode"] =  $input['postcode'];
            

            Customers::where('id', '=', $id)->update($customer);
            Addresses::where('customer_id', '=', $id)->update($address);
           
            \Session::flash('message', 'Customers updated successfully!');
            return redirect('admin/customers');
        }
    }
 


  


    
}
