<?php


namespace App\Http\Controllers\Administrator\people;



use App\Fun\Fun;

use App\Model\pos\SaleModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\people\CustomerModel;
use Auth;
use Yajra\DataTables\DataTables;
use Illuminate\Support\Facades\Gate;


class CustomerController extends Controller

{
    public  $user;
    function __construct()
    {
//            $this->middleware('permission:Customer Create', ['only' => [ 'create', 'store']]);
//            $this->middleware('permission:Customer List', ['only' => ['index']]);
//            $this->middleware('permission:Customer Edit', ['only' => ['update','edit']]);

    }
    public function index(Request $request)

    {
        if ($request->ajax()) {
            Fun::lang();
            $customer = CustomerModel::select('*')->latest();
            $count_filter = $customer->count();
            return Datatables::of($customer->take(20))
                ->with([
                    "recordsTotal" => $count_filter,
                    "recordsFiltered" => $count_filter,
                ])
                ->addIndexColumn()
                ->addColumn('action', function ($customer) {
                    $btn = '<a class="button_warning getHtmlForm" data-name="'.__('administrator.edit').'" data-href="'.route('customer.edit',$customer->id).'">'.__('administrator.edit').'</a>
                                <a class="button_info btn_show_customer_info_public " data-href="'.route('customer.show',$customer->id).'">'.__('administrator.show').'</a>';
                    return $btn;
                })
                ->editColumn('user_id', function ($customer) {
                     return findUser($customer->user_id)->name??'';
                })
                ->rawColumns(['action' => 'action'])
                ->make(true);
        }
        return view('administrator.people.customers.index');

    }

    public function create()

    {

        return view('administrator.people.customers.create');

    }

    public function store(Request $request)

    {
        return $this->update($request,null);
    }

    public function edit($id)

    {
        $customer = CustomerModel::find($id);
        return view('administrator.people.customers.edit', compact('customer'));
    }

    public function update(Request $request, $id)

    {
        $this->validate($request, [
            'name' => 'required|max:50|unique:nso007_customers,name,'.$id,
        ]);
        $arr = $request->all();
        $arr['branch_id'] = Auth::user()->branch_id;
        $arr['user_id'] = Auth::user()->id;
        $customer = CustomerModel::updateOrCreate([
            'id'=>$id
        ],$arr);
        return response()->json(['status'=>'ok','reload'=>($request->pos?'':1),'customer_data'=>$customer]);

    }

    public function show(Request $request,$id)

    {
        $id = $id?$id:$request->customer_id;
        $customer = CustomerModel::find($id);
        $history = SaleModel::where('customer_id',$id)->paginate(20);
        return view('administrator.people.customers.show',compact('customer','history'));

    }

    public function search_customer(Request $request)
    {
        $key = $request->term['term'];
        $customer = CustomerModel::where(function ($q) use ($key){
            if($key){
                $q->where('name','like',$key.'%');
                $q->orwhere('phone','like',$key.'%');
            }
        })->limit(50)->orderBy('name','ASC')->pluck('name','id')->prepend('Please Select','');
        $arr = [];
        foreach ($customer as $key =>  $row) {
            $arr[] = ['id' => $key, 'text' => $row];
        }
        return \Response::json($arr);
    }
}

