<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Throwable;
use Exception;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;
use App\Models\WxAdmin;
use App\Models\WxUserImg;
use App\Models\User;
use App\Models\WxAldGift;
use App\Models\WxBindRole;


class AdminController extends Controller
{
    private $time;

    public function __construct()
    {
        $this->time = Carbon::now()->format('Y-m-d H:i:s');
    }

    /**
     * 后台登陆页
     * @date 2021/06/02
     * @author live
     */
    public function getLogin()
    {
        if(Auth::check()) return redirect(route('checkImg'));
        $data['ERROR'] = Session::get('error','');
        return $this->view('admin.login',$data);
    }

    /**
     * 用户登录页提交
     * @date 2021/06/02
     * @author live
     */
    public function getPostLogin(Request $request)
    {
        $postData = $request->all();
        if( Auth::check()) return redirect(route('checkImg'));
        if($request->isMethod('post'))
        {
            if (Auth::attempt(['name' => $postData['username'], 'password' =>  $postData['password']]))
            {
                return redirect(route('checkImg'))->with( 'success', '登录成功,明信片审核后台欢迎你!' );
            }else{
                $error = '用户名或密码不正确！';
            }
        }else{
            $error = '非法登录！';
        }
        return redirect(route('adminLogin'))->with( 'error', $error );
    }

    /**
     * 用户上传图片列表
     * @date 2021/06/02
     * @author live
     */
    public function getImgList(Request $request)
    {
        $postData = $request->all();
        $pageNum = 20;
        $startTime = $postData['startTime'] ?? '';
        $endTime = $postData['endTime'] ?? '';
        $status = $postData['status'] ?? 1;
        $userName = $postData['user'] ?? '';

        $paginateAppends['status'] = $status;
        $lists = WxUserImg::when($userName != '', function ($query) use ($userName) {
            $user = User::where('account',$userName)->select('uid')->first();
            if($user){
                return $query->where('uid',$user->uid);
            }else{
                return $query->where('uid','-1');
            }
        })->when($status > 0, function ($query) use ($status) {
            $imgStatus = $status - 1;
            return $query->where('status',$imgStatus);
        })->when($startTime != '', function ($query) use ($startTime) {
            return $query->where('updated_at','>',$startTime);
        })->when($endTime != '', function ($query) use ($endTime) {
            return $query->where('updated_at','<',$endTime);
        })->when($status == 0, function ($query) use ($status) {
            return $query->orderBy('status','ASC');
        })->orderBy('updated_at','DESC')->select('id', 'uid', 'img_url', 'status', 'updated_at')->paginate($pageNum);
        if($startTime != '') $paginateAppends['startTime'] = $startTime;
        if($endTime != '') $paginateAppends['endTime'] = $endTime;
        if($userName != '') $paginateAppends['user'] = $userName;
        if (!empty($paginateAppends)) $lists->appends($paginateAppends);

        $userInfo = [];
        $uids = [];
        foreach($lists as $list) $uids[] = $list->uid;
        if(!empty($uids)){
            $users =  User::whereIn('uid',$uids)->select('uid','account')->get();
            foreach($users as $user) $userInfo[$user->uid] = $user->account;
        }

        $data['LIST'] = $lists;
        $data['SEARCH'] = $postData;
        $data['USER_INFO'] = $userInfo;
        return $this->view('admin.img', $data);
    }

    /**
     * 审核图片
     * @date 2021/06/02
     * @author live
     */
    public function getSaveStatus(Request $request)
    {
        $postData = $request->all();
        $id = $postData['id'] ?? 0;
        $status = $postData['status'];
        if($id < 1 || !in_array($status,[1,2])) return $this->getErrorMsg('10060', '参数错误');
        $row = WxUserImg::find($id);
        if(!$row) return $this->getErrorMsg('10061', '数据异常');
        $row->status = $status;
        if($row->save()) return $this->getSuccessMsg([]);
        return $this->getErrorMsg('10063', '审核失败');
    }

    /**
     * 批量审核图片
     * @date 2021/06/07
     * @author live
     */
    public function getSaveStatusAll(Request $request)
    {
        $postData = $request->all();
        $ids = explode('#',$postData['ids']);
        $status = $postData['status'];
        if(!in_array($status,[1,2]) || empty($ids)) return $this->getErrorMsg('10060', '参数错误');
        $result = WxUserImg::whereIn('id',$ids)->update(['status' => $status]);
        if($result) return $this->getSuccessMsg([]);
        return $this->getErrorMsg('10063', '审核失败');
    }

}