<?php

namespace App\Http\Controllers\Administrator\people;

use App\Fun\Fun;
use App\Model\people\TailorModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DataTables;

class TailorController extends Controller
{

    function __construct()
    {
       $this->middleware('permission:Tailor List', ['only' => ['index']]);
       $this->middleware('permission:Tailor Create', ['only' => [ 'create', 'store']]);
       $this->middleware('permission:Tailor Edit', ['only' => ['update','get_edit']]);

    }
    public function index(Request $request)

    {
        if ($request->ajax()) {
            Fun::lang();
            $customer = TailorModel::select('*')->latest();
            $count_filter = $customer->count();
            return Datatables::of($customer->take(20))
                ->with([
                    "recordsTotal" => $count_filter,
                    "recordsFiltered" => $count_filter,
                ])
                ->addIndexColumn()
                ->addColumn('action', function ($data) {
                    $btn = '<a class="button_warning btn-edit" data-href="'.route('tailor.edit',$data->id).'">'.__('administrator.edit').'</a>';
                    return $btn;
                })
                ->editColumn('type', function ($data) {
                    if($data->type){
                        return getTailorType($data->type);
                    }
                })
                ->editColumn('account_receivable_riel', function ($data) {
                    $ar = '';
                    if($data->account_receivable_riel){
                        $ar .= '៛'.$data->account_receivable_riel;
                    }
                    if($data->account_receivable_usd&&$data->account_receivable_riel){
                        $ar .='<br>';
                    }
                    if($data->account_receivable_usd){
                        $ar .= '$'.$data->account_receivable_usd;
                    }
                    return $ar;
                })
                ->rawColumns(['action' , 'account_receivable_riel'])
                ->make(true);
        }
        return view('administrator.people.tailor.index');

    }

    public function create()

    {

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

    }

    public function store(Request $request)

    {
        $this->update($request,null);
        return response()->json('ok');

    }

    public function edit($id)

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

    public function update(Request $request, $id)

    {
        $this->validate($request, [
            'type' => 'required',
            'name' => 'required|max:15|unique:nso007_tailor,name,'.$id,
            'phone' => 'required|max:15|unique:nso007_tailor,phone,'.$id,
        ]);
        $arr = $request->all();
        $arr['user_id'] = Auth::user()->id;
        TailorModel::updateOrCreate([
            'id'=>$id
        ],$arr);
        return response()->json('ok');

    }
}
