<?php

namespace App\Http\Controllers\V1;
use App\Models\Product;
use App\Models\UserCollect;
use App\Models\UserFolder;
use App\Models\UserFolderImage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 用户文件夹
 */
class UserFolderController extends Controller
{

    public function __construct()
    {
        $this->user = auth('api')->user();
        $this->userId = $this->user ? $this->user->id : 0;

        //如果用户被删除，会自动退出登录
        if (!empty($this->user->deleted_at)) {
            $this->user->online = 0;
            $this->user->save();
            auth('api')->logout();
        }
    }

    /**
     * @return void
     * 新建文件夹
     */
    public function addFolder(Request $request){
        $params = $request->all();
        if(empty($params['name'])){
            return $this->error("名称不能为空！");
        }
        $params['user_id'] = $this->userId; // 用户ID
        $res = UserFolder::query()->create($params);
        if(!$res){
            return $this->error("创建失败！");
        }
        return $this->success();
    }


    /**
     * @return void
     * 文件夹列表
     */
    public function folderList(){
        $list = UserFolder::query()->withCount('images')->where('user_id','=',$this->userId)->get();
        return $this->success($list);
    }

    /**
     * @return void
     * 文件夹详情
     */
    public function folderDetail(Request $request){
        $id = $request->get('id');
        if(!$id){
            return $this->error("缺少参数ID!");
        }
        $res = UserFolder::query()->where('id',$id)->first();
        return $this->success($res);
    }

    /**
     * @return void
     * 编辑文件夹
     */
    public function editFolder(Request $request){
        $params = $request->all();
        if(empty($params['id'])){
            return $this->error("缺少参数ID");
        }
        if(empty($params['name'])){
            return $this->error("标题不能为空！");
        }
        DB::beginTransaction();
        try {
            $folder = UserFolder::query()->where('id',$params['id'])->first();
            if(!$folder){
                throw new \Exception("数据不存在！");
            }
            if(!empty($params['is_del'])){
                // 删除图片库
                UserFolderImage::query()->where('folder_id',$params['id'])->where('user_id',$this->userId)->delete();
                // 删除文件夹
                $folder->delete();
            }else{
                $folder->update($params);
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return $this->error($e->getMessage());
        }
       return $this->success();
    }

    /**
     * @return void
     * 移动文件
     */
    public function moveFile(Request $request){

        $params = $request->all();
        if(empty($params['folder_id'])){
            return $this->error("缺少文件夹ID");
        }
//        $params['product_ids'] = [1];
        if(empty($params['product_ids'])){
            return $this->error("文件ID");
        }
        DB::beginTransaction();
        try {
            $product = Product::query()->whereIn('id',$params['product_ids'])->select("id","image")->get();
            if($product){
                foreach ($product as $v){
                    $data = [
                        'user_id'=>$this->userId,
                        'folder_id'=>$params['folder_id'],
                        'image'=>$v['image']
                    ];
                    UserFolderImage::query()->create($data); // 移动文件
                    UserCollect::query()->where('product_id',$v['id'])->update(['is_arrange'=>1]); // 标识已整理的文件
                }
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return $this->error($e->getMessage());
        }
        return $this->success();
    }

    /**
     * @return void
     * 图片列表
     */
    public function imageList(Request $request){

        $id = $request->get('folder_id');
        $limit = $request->get('limit',10);
        if(!$id){
            return $this->error("缺少参数ID!");
        }
        $list = UserFolderImage::query()->where('folder_id','=',$id)->where('user_id',$this->userId)->paginate($limit);
        return $this->success($this->page($list));
    }

}
