<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ProductController extends Controller
{
    //保存产品
    public function saveProductInfo(Request $request)
    {
        if($request->has('id')){
            $data = Product::where('id',$request->id);
        }else{
            $data = new Product();
        }
        $data->name = $request->name;
        $data->contents = $request->contents;
        $data->desc = $request->desc;
        if($data->save()){
            return $this->success([]);
        }
        return $this->error();
    }

    //上传图片
    public function uploadImg(Request $request){
        $path_url = 'public/product';
        $path = $request->file('img')->store($path_url);
        if($path){
            $url = env('APP_URL').Storage::url($path);
            return $this->success($url);
        }
        return $this->error();
    }

    //上传视频
    public function uploadVideo(Request $request){
        $path_url = 'public/product';
        $path = $request->file('video')->store($path_url);
        if($path){
            $url = env('APP_URL').Storage::url($path);
            return $this->success($url);
        }
        return $this->error();
    }

    public function getProductInfo(Request $request){
        $product = Product::where('id',$request->id)->first();
        return $this->success($product);
    }
}
