<?php

namespace App\Http\Controllers\HR;

use App\Fun\Fun;
use App\Model\setup\BranchModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Hr\HrDepartmentModel;
use Illuminate\Support\Facades\Gate;

class HrBranchController extends Controller
{

    function __construct()
    {
        $this->middleware('permission:Branch List', ['only' => ['index']]);
        $this->middleware('permission:Branch Create', ['only' => ['create', 'store']]);
        $this->middleware('permission:Branch Edit', ['only' => ['edit', 'update']]);
        $this->middleware('permission:Branch set_main', ['only' => ['branch_changeStatus']]);
    }
    public function index(Request $request)
    {

        if ($request->ajax()) {
            Fun::lang();
            $search = $request->input('search.value');
            $data = BranchModel::where(function ($q) use ($search) {
                if ($search) {
                    $q->where('name', 'like', $search . '%');
                }
            })
                ->latest();
                return Datatables::of($data)
                    ->addIndexColumn()
                    ->addColumn('action', function ($data) {
                        $btn = '';
                        if (Gate::check('Branch Show')) {
                            $btn .= '<button title="' . __('administrator.show') . '" class="button_warning show_productsinfo" data-url = "' . route("get-product", $data->id) . '"  >' . __('administrator.show') . '</button>';
                        }
                        if (Gate::check('Branch Edit')) {
                            $btn .= '<button class="button_warning btn-edit" data-href="' . route('branch.edit', $data->id) . '">' . __('administrator.edit') . '</button>';
                        }
                        return $btn;
                    })
                    ->editColumn('logo', function ($data) {
                        if ($data->logo) {
                            return '<img src="' . $data->logo != null && file_exists(public_path($data->logo)) ? asset($data->logo) : asset('images/default.jpg') . '" width="60px">';
                        }else{
                            return '<img scr="'.asset('images/default.jpg').'" alt="logo"> width="60px">';
                        }

                    })
                    ->editColumn('main', function ($data) {
                        if ($data->main) {
                            return '<span class="text-center label label-success" >' . __('administrator.default') . '</span>';
                        }
                    })
                    ->rawColumns(['action', 'logo', 'main'])
                    ->make(true);
        }
        return view('hr.branch.index');
    }

    public function create()
    {
        return view('hr.branch.create');
    }
    public function store(Request $request)
    {
        $this->validate($request, [
            'name'          => 'required',
            'phone'         => 'required',
            'address'       => 'required',
        ]);

        $branch                         = new BranchModel();
        $branch->name                   = $request->name;
        $branch->english_name           = $request->name;
        $branch->phone                  = $request->phone;
        $branch->address                = $request->address;
        $branch->email                  = $request->email;
        $branch->note                   = $request->note;
        // $branch->invoice_footer         = $request->invoice_footer;
        // $branch->invoice_phrase         = $request->invoice_phrase;
        $branch->save();

        return response()->json('success');
    }

    public function edit($id)
    {
        $branch = BranchModel::find($id);
        return response()->json($branch);
    }

    public function update(Request $request)
    {
        $this->validate($request, [
            'name'          => 'required',
            'phone'         => 'required',
            'address'       => 'required',
            'id'            => 'required',
        ]);

        $branch                         = BranchModel::find($request->id);
        $branch->name                   = $request->name;
        $branch->english_name           = $request->name;
        $branch->phone                  = $request->phone;
        $branch->address                = $request->address;
        $branch->email                  = $request->email;
        $branch->note                   = $request->note;
        // $branch->invoice_footer         = $request->invoice_footer;
        // $branch->invoice_phrase         = $request->invoice_phrase;
        $branch->update();

        return response()->json('success');
    }

    public function branch_changeStatus(Request $request)

    {

        BranchModel::where('main', 1)->update(['main' => 0]);

        BranchModel::find($request->id)->update(['main' => 1]);;

        return response()->json(['success' => 'Status change successfully.']);
    }
}
