<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Resources\CashTransferRequestResource;
use App\Models\Branch;
use App\Models\CashTransferRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class CashTransferRequestController extends Controller
{
   
    function __construct()
    {
        $this->middleware('permission:Cash Transfer Request List', ['only' => ['index']]);
        $this->middleware('permission:Cash Transfer Request Create', ['only' => ['store']]);
        $this->middleware('permission:Cash Transfer Request Edit', ['only' => ['update']]);
        $this->middleware('permission:Cash Transfer Request Delete', ['only' => ['destroy']]);
    }

    public function index(Request $request)
    {
        $per_page = $request->per_page == null?25: $request->per_page;
        $data = CashTransferRequest::where(function($query){
            $query->whereIn('branch_id',Helper::byBranch());
            $query->where('currency_id',Helper::byCurrency());
        })
        ->search($request->search_text)
        ->orderBy('id','DESC')
        ->paginate($per_page);
        return CashTransferRequestResource::collection($data);
    }

    public function create(Request $request)
    {
        $cash_transfer = CashTransferRequest::find($request->id);

        if($cash_transfer){
            $res = [
                'status' => 1,
                'message'=> 'Create Cash Transfer Request Success',
                'data'   => new CashTransferRequestResource($cash_transfer),
            ];
        }else{
            $res = [
                'status'  => 0,
                'message' => 'Create Cash Transfer Request not found',
                'data'    => [],
            ];
        }
        return response()->json($res,200);
    }

   
    public function store(Request $request)
    {
        $this->validate($request, [
            'amount'      => 'required',
            'request_by'  => 'required',
            'date_request'=> 'required',
        ]);
        try{
            DB::beginTransaction();
            $branch = Branch::find(Auth::user()->branch_id);
            $cash_transfer = CashTransferRequest::updateOrCreate(
                ['id'=>$request->id],[
                    'branch_code'   => $branch->branch_code,
                    'branch_id'     => Auth::user()->branch_id,
                    'currency_id'   => Auth::user()->currency_id,
                    'created_by'    => Auth::user()->id,
                    'request_by'    => $request->request_by,
                    'amount'        => $request->amount,
                    'date_request'  => date('Y-m-d',strtotime($request->date_request)),
                    'purpose'       => $request->purpose,
                    'status'        => 1, // 1=Request,2=Waiting Approve,3=Approved, 4=Reject
                ]
            );

            DB::commit();
            $res = [
                'status' => 1,
                'message'=> $cash_transfer->wasRecentlyCreated?'Create Cash Transfer Request success':'Update Cash Transfer Request success',
                'data'   => new CashTransferRequestResource($cash_transfer),
            ];
        }catch(\Exception $e){
            DB::rollBack();
            $res = [
                'status'  => 0,
                'message' => 'Create Cash Transfer Request not success',
                'data'    => $e->getMessage(),
            ];
        }
        return response()->json($res,200);
    }

    public function show($id)
    {
        //
    }

    public function edit($id)
    {
        //
    }

    public function update(Request $request, $id)
    {
        $cash_transfer = CashTransferRequest::find($id);
        $cash_transfer->status = $request->status;
        $cash_transfer->update();
        $res = [
            'status' => 1,
            'message'=> 'Success',
        ];
        return response()->json($res,200);
    }

    public function destroy($id)
    {
        DB::beginTransaction();
        try {
            $cash_transfer = CashTransferRequest::find($id);
            $cash_transfer->delete();

            DB::commit();
            $res = [
                'status' => 1,
                'message'=> 'Deleted success',
                'data'   => $cash_transfer,
            ];
        } catch (\Exception $e) {
            DB::rollback();
            $res = [
                'status'  => 0,
                'message' => 'Deleted not success',
                'data'    => $e->getMessage(),
            ];
        }
        return response()->json($res,200);
    }
}
