<?php

namespace App\Http\Controllers\Admin;

use App\Exports\ProductExport;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Admin;
use App\Models\Product;
use App\Models\ProductCategory;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;

class ProductController extends Controller
{
    use ResponseJson;

    public function addProduct(Request $request)
    {
        try {
            $name = $request->input('name');
            if(empty($name)) return $this->failure('产品名不为空');

            $img = $request->input('img');
            if(empty($img)) return $this->failure('请上传图片');

            $category_id = $request->input('category_id');
            if(empty($category_id)) return $this->failure('请上传图片');

            $source = $request->input('source');
            $remark = $request->input('remark');

            $insert = [];
            $insert['name'] = $name;
            $insert['img'] = $img;
            $insert['category_id'] = $category_id;
            $insert['source'] = $source;
            $insert['remark'] = $remark;
            Product::create($insert);

            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }


    public function addProductCategory(Request $request){
        try {
            $name = $request->get('name');
            if(empty($name)){
                return $this->failure('类别名称不为空');
            }
            $model = ProductCategory::create(['name'=>$name]);
            return $this->success('ok',$model);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function getProductList(Request $request){
        try {
            $keyword = $request->get('keyword');
            $no_page = $request->get('no_page');
            $is_export = $request->get('is_export');
            if($is_export){
                $no_page = true;
            }
            $query = Product::query()->when($keyword,function ($query) use ($keyword){
                $query->where('name','like',"%{$keyword}%");
            })->orderByDesc('id');

            if($no_page){
                $list = $query->get();
            }else{
                $list = $query->paginate();
            }
            foreach ($list as $item){
                $item->category_name = ProductCategory::where('id',$item->category_id)->value('name');
            }
            if($is_export){
                return Excel::download(new ProductExport($list), '产品列表.xlsx');
            }
            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function getProductCategoryList(Request $request){
        try {
            $keyword = $request->get('keyword');
            $no_page = $request->get('no_page');
            $query = ProductCategory::query()->when($keyword,function ($query) use ($keyword){
                $query->where('name','like',"%{$keyword}%");
            })->orderByDesc('id');

            if($no_page){
                $list = $query->get();
            }else{
                $list = $query->paginate();
            }

            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function updateProduct(Request $request,$id){
        try {
            $product = Product::find($id);
            if($request->has('name')){
                $product->name = $request->get('name');
            }
            if($request->has('img')){
                $product->img = $request->get('img');
            }
            if($request->has('category_id')){
                $product->category_id = $request->get('category_id');
            }
            if($request->has('source')){
                $product->source = $request->get('source');
            }
            if($request->has('remark')){
                $product->remark = $request->get('remark');
            }
            $product->save();
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }

    public function deleteProduct($id){
        try {
            Product::where('id',$id)->delete();
            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure('file:'.$e->getFile().'|line:'.$e->getLine().'|message:'.$e->getMessage());
        }
    }
}
