<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Care;
use App\Models\CareItem;

class AdminCareController extends Controller
{
    public function index(Request $request)
    {
        $perPage = 12;
        
        $cares = Care::paginate($perPage)->withQueryString();
        
        if(isset($request->categoryId) && $request->categoryId >= 0){
            $cares = Care::where('category',$request->categoryId)->paginate($perPage)->withQueryString();

            if($request->page == ""){
                return redirect('/admin-care?page='.$cares->lastPage().'&categoryId='.$request->categoryId);
            }
        }
        
        if($request->page == ""){
            return redirect('/admin-care?page='.$cares->lastPage());
        }
        
        return view('admin.admin_care',['items'=>$cares,'title'=>'AR Care','categoryId'=>$request->categoryId]);

    }
    
    public function show(Request $request){
        $care = Care::where('id',$request->id)->first();
        $images = DB::table('care_item_images')->where('product_id','=',$request->id)->get();
        $care['imgs'] = $images;
        return $care;
    }
    
    public function store(Request $request)
    {
        if($request->id === '' || $request->id === null)
        {
            return $this->insert($request);
        }
        else
        {
            return $this->update($request);
        }
    }

    public function insert(Request $request)
    {
        if($request->hasFile('photo'))
        {
            $file = $request->file('photo');
            $fileName = $file->getClientOriginalName();

            $file->move('imgs/care',$fileName);

            $values = array(
                'name'=>$request->name,
                'category'=>$request->category,
                'img'=>$fileName
            );

            $insertedId = Care::insertGetId($values);
            $values['id'] = $insertedId;
            $values['img'] = $fileName;

            return response()->json([
                'success' => true,
                'message' => "Insert data berhasil!",
                'data' => $values,
            ]);
    
        }else{
            return response()->json([
                'success' => false,
                'message' => "Insert data gagal!",
            ]);
        }
    }
    
    public function update(Request $request)
    {
        $fileName = '';
        $values = array(
            'id'=>$request->id,
            'name'=>$request->name,
            'category'=>$request->category
        );

        if($request->hasFile('photo')){
            $file = $request->file('photo');
            $fileName = $file->getClientOriginalName();
            $file->move('imgs/care',$fileName);
            $values['img'] = $fileName;
        }else{
            $values['img'] = $request->image_file;
        }

        Care::where('id',$request->id)->update($values);

        return response()->json([
            'success' => true,
            'message' => 'Data Berhasil Diperbaharui',
            'data'    => $values
        ]);
    }
    
    
    public function destroy(Request $request)
    {
        $careItems = CareItem::where('product_id','=',$request->id)->get();

        if(count($careItems) > 0){
            return response()->json([
                'success' => false,
                'message' => 'Hapus Data GAGAL!, Silahkan hapus produk item terlebih dahulu.'
            ]);
        }else{
            Care::where('id',$request->id)->delete();
            return response()->json([
                'success' => true,
                'message' => 'Hapus Data Berhasil',
            ]);
        }


    }
    
    public function getFiles(){
        $dir = 'imgs/care';
        $files = scandir($dir);
        
        /*
        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                echo $file . "<br>";
            }
        }*/
        if(count($files) == 0){
            return response()->json([
                'error' => true,
                'message' => 'File tidak tersedia!',
            ]);
        }else{
            return response()->json([
                'error' => false,
                'data' => $files
            ]);
        }
    }
    
}

?>