<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Method;

class MethodController extends Controller
{
    public function create()
    {
        return view('components.admin.method', ['data' => method::orderBy('name', 'asc')->paginate(100)]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'images' => 'required|file|mimes:jpg,png,webp',
            'code' => 'required',
            'keterangan' => 'required',
            'payment' => 'required',
            'tipe' => 'required',
        ]);

        $file = $request->file('images');
        $folder = 'assets/thumbnail';
        $file->move($folder, $file->getClientOriginalName());    
        $method = new method();
        $method->name = $request->name;
        $method->code = $request->code;
        $method->keterangan = $request->keterangan;
        $method->tipe = $request->tipe;
        $method->payment = $request->payment;
        $method->images = "/".$folder."/".$file->getClientOriginalName();
        $method->save();

        return back()->with('success', 'Berhasil menambahkan payment');
    }

    public function delete($id)
    {
        try{
            $data = method::where('id', $id)->select('images')->first();
            unlink(public_path($data->images));
            method::where('id', $id)->delete();
            return back()->with('success', 'Berhasil hapus!');
        }catch(\Exception $e){
            method::where('id', $id)->delete();
            return back()->with('success', 'Berhasil hapus!');
        }
    }

    public function detail($id)
    {
        $data = Method::where('id', $id)->first();
        if (!$data) {
            return back()->withErrors('Metode pembayaran tidak ditemukan');
        }
        $send = "
            <form action='" . route('method.detail.update', [$id]) . "' method='POST' enctype='multipart/form-data'>
                <input type='hidden' name='_token' value='" . csrf_token() . "'>
                <div class='mb-3 row'>
                    <label class='col-lg-2 col-form-label' for='example-fileinput'>Nama</label>
                    <div class='col-lg-10'>
                        <input type='text' class='form-control' value='" . $data->name . "' name='name' required>
                    </div>
                </div>
                <div class='mb-3 row'>
                    <label class='col-lg-2 col-form-label' for='example-fileinput'>Kode</label>
                    <div class='col-lg-10'>
                        <input type='text' class='form-control' value='" . $data->code . "' name='code' required>
                    </div>
                </div>
                <div class='mb-3 row'>
                    <label class='col-lg-2 col-form-label' for='example-fileinput'>Keterangan</label>
                    <div class='col-lg-10'>
                        <input type='text' class='form-control' value='" . $data->keterangan . "' name='keterangan' required>
                    </div>
                </div>
                <div class='mb-3 row'>
                    <label class='col-lg-2 col-form-label'>Payment</label>
                    <div class='col-lg-10'>
                        <select class='form-select' name='payment' required>
                            <option value='tokopay' " . ($data->payment == 'tokopay' ? 'selected' : '') . ">TOKOPAY.ID</option>
                            <option value='paydisini' " . ($data->payment == 'paydisini' ? 'selected' : '') . ">PAYDISINI.CO.ID</option>
                            <option value='tripay' " . ($data->payment == 'tripay' ? 'selected' : '') . ">Tripay</option>

                        </select>
                    </div>
                </div>
                <div class='mb-3 row'>
                    <label class='col-lg-2 col-form-label'>Tipe</label>
                    <div class='col-lg-10'>
                        <select class='form-select' name='tipe' required>
                            <option value='qris' " . ($data->tipe == 'qris' ? 'selected' : '') . ">QRIS</option>
                            <option value='e-walet' " . ($data->tipe == 'e-walet' ? 'selected' : '') . ">E-Wallet</option>
                            <option value='virtual-account' " . ($data->tipe == 'virtual-account' ? 'selected' : '') . ">Virtual Account</option>
                            <option value='convenience-store' " . ($data->tipe == 'convenience-store' ? 'selected' : '') . ">Convenience Store</option>
                            <option value='pulsa' " . ($data->tipe == 'pulsa' ? 'selected' : '') . ">PULSA</option>
                        </select>
                    </div>
                </div>
                <div class='mb-3 row'>
                    <label class='col-lg-2 col-form-label' for='example-fileinput'>Thumbnail</label>
                    <div class='col-lg-10'>
                        <input type='file' class='form-control' name='images'>
                        <img src='" . asset($data->images) . "' alt='thumbnail' class='mt-3' width='100'>
                    </div>
                </div>
                <div class='modal-footer'>
                    <button type='button' class='btn btn-danger' data-bs-dismiss='modal'>Close</button>
                    <button type='submit' class='btn btn-primary'>Simpan</button>
                </div>
            </form>
        ";
    
        return $send;
    }

    
    public function patch(Request $request, $id)
    {
        if($request->file('images')){
            $file = $request->file('images');
            $folder = 'assets/thumbnail';
            $file->move($folder, $file->getClientOriginalName());      
            method::where('id', $id)->update([
                'images' => "/".$folder."/".$file->getClientOriginalName()
            ]);
        }
        
        $method = method::where('id', $id)->update([
            'name' => $request->name,
            'code' => $request->code,
            'keterangan' => $request->keterangan,
            'tipe' => $request->tipe,
            'payment' => $request->payment,
        ]);
           
        return back()->with('success', 'Berhasil update payment');        
    }        
}
