<?php

namespace App\Http\Controllers\Admin;

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

class CateController extends Controller
{
    //添加分类
    public function addCate(Request $request)
    {
        $name=$request->input('name');
        $img=$request->input('img');
        $pid=$request->input('pid');
        $sort=$request->input('sort');
        $res=Cate::create([
            'pid'=>$pid,
            'img'=>$img,
            'name'=>$name,
            'sort'=>$sort,
        ]);
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

    //插入图片
    public function uploadImg(Request  $request){
        $file = $request->file('img')->store('img/cate');
        if(empty($file)){
            return $this->error();
        }else{
            return $this->success(Storage::url($file));
        }
    }

    //获取一级分类
    public function getOneCate(Request  $request)
    {
        $search_name=$request->input('search_name');
        $page_index=$request->input('page_index');
        $page_size=$request->input('page_size');
        $num=$page_size*($page_index-1);
        $total=Cate::where('pid',0)->count();
        $list=Cate::where('pid',0)->where('name','like','%'.$search_name.'%')
            ->orderBy('sort','desc')->orderBy('id','desc')
            ->skip($num)->take($page_size)->get();
        return $this->success_list($list,'',$total);
    }

    //获取二级分类
    public function getTwoCate(Request  $request)
    {
        $search_name=$request->input('search_name');
//        $page_index=$request->input('page_index');
//        $page_size=$request->input('page_size');
        $pid=$request->input('pid');
//        $num=$page_size*($page_index-1);
        $total=Cate::where('pid',$pid)->count();
        $list=Cate::where('pid',$pid)->where('name','like','%'.$search_name.'%')
            ->orderBy('sort','desc')->orderBy('id','desc')
            ->get();//->skip($num)->take($page_size)
        return $this->success_list($list,'',$total);
    }

    //获取分类
    public function getCate(Request $request)
    {
        $pid=$request->input('pid');
        $cates=Cate::where('pid',$pid)
            ->with('cate:id,pid,sort,name')
            ->select('id','name','pid','sort')
            ->orderBy('sort','desc')->orderBy('id','desc')
            ->get();
        return $this->success($cates);
    }

    //修改分类
    public function editCate(Request $request)
    {
        $name=$request->input('name');
        $img=$request->input('img');
        $pid=$request->input('pid');
        $sort=$request->input('sort');
        $id=$request->input('id');
        $res=Cate::where('id',$id)->update([
            'pid'=>$pid,
            'img'=>$img,
            'name'=>$name,
            'sort'=>$sort,
        ]);
        if($res){
            return $this->success([]);
        }
        return $this->error();
    }

    //删除分类
    public function deleteCate(Request $request){
        $id=$request->input('id');
        $count=Cate::where('pid',$id)->count();
        if($count>0){
            return $this->error('450001','该分类存在二级分类，不能删除');
        }
        $course_count=Course::where('c_id',$id)->count();
        if($course_count>0){
            return $this->error('450001','该分类存在课程，不能删除');
        }
        $res=Cate::where('id',$id)->delete();
        if($res){
            return $this->success([]);
        }else{
            return $this->error();
        }
    }


}
