<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadimgController extends Controller
{
    // 上传图片接口
    public function uploadingimg(Request $request)
    {
        if ($request->isMethod('POST')) { //判断文件是否是 POST的方式上传
            $tmp = $request->file('file');
            if (is_null($tmp)){
                return $this->jsonErrorData(105,'上传图片不能为空');
            }
            $path = '/article';
            if ($tmp->isValid()) { //判断文件上传是否有效
                $FileType = $tmp->getClientOriginalExtension(); //获取文件后缀

                $FilePath = $tmp->getRealPath(); //获取文件临时存放位置

                $FileName = date('Y-m-d') . uniqid() . '.' . $FileType; //定义文件名

                Storage::disk('article')->put($FileName, file_get_contents($FilePath)); //存储文件

                $data = [
                    'code' => 200,
                    'imginfo' =>$tmp,
                    'path' => $path . '/' . $FileName //文件路径
                ];
                return $this->adduserimg($data);
            }
        } else {
            return $this->jsonErrorData('105', '上传请求方式错误');
        }
    }

    private function adduserimg($data)
    {
        if ($data['code'] == 200){
            if (!is_null($this->isadmin())){
                $data['isadmin'] = 1;
            }else{
                $data['isadmin'] = 2;
            }
            $data['addtime'] = time();
            $data['uid'] = Auth::id();
            foreach ($data as $k => $v) {
                if ($k == 'code') {
                    unset($data[$k]);
                }
            }
            $this->timeline('新增了新的图片');
            $add = DB::table('userimage')
                ->insert($data);
            return $this->jsonSuccessData($add);
        }else{
            return $this->jsonErrorData(105,'上传图片错误');
        }
    }

    //返回用户添加的图片
    public function userlist(){
        return DB::table('userimage')->where([['uid', '=', Auth::id()], ['isadmin', '=', is_null($this->isadmin()) ? '2' : '1', ],['delete', '=', '1']])->get();
    }
    //删除用户图片
    public function userimgdetele(Request $request){
        $userimage = DB::table('userimage');
        $type =  $request->input('type');//删除图片状态
        $imgid =  $request->input('imgid');//图片id
        if(is_null($type) || is_null($imgid)){
            return $this->jsonErrorData(105,'参数不能为空');
        }
        if($type == 1){//逻辑删除
            $this->timeline('将图片放置废纸篓');
            return $this->jsonSuccessData($userimage->where('id','=',$imgid)->update(['delete'=>'2']));
        }else if ($type == 2){//物理删除
            $this->timeline('删除了图片');
            return $this->jsonSuccessData($userimage->where('id','=',$imgid)->delete());
        }
    }

//    返回逻辑删除图片 和 改变图片状态
    public function updateimg(Request $request){
        $userimage = DB::table('userimage');
        if($_POST){//改变图片状态
            $imgid = $request->input('imgid');
            if(is_null($imgid)){
                return $this->jsonErrorData(105,'图片ID不能为空');
            }
            return $this->jsonSuccessData($userimage->where('id','=',$imgid)->update(['delete'=>1]));
        }else{
            return $this->jsonSuccessData($userimage->where('delete','=','2')->get());
        }
    }
}