<?php
namespace App\Http\Controllers;

use App\Http\Resources\PermissionResource;
use App\Http\Requests\UsersRequest;
use App\Http\Resources\UserResource;
use App\Laravue\JsonResponse;
use App\Laravue\Models\Installer;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Validator;

class InstallerController extends Controller
{
    //装维人员列表
    public function installerlist (Request $request){
        $pagenNum= (int)$request->input('page')-1;//页数
        $limit = (int)$request->input('limit');
        $instllerdata = Installer::offset($pagenNum)->limit($limit)->get();
        $count = Installer::count();
       return $this->jsonSuccessData(['instllerdata'=>$instllerdata,'count'=>$count]);
    }

    //用户添加
    public function addinstaller(Request $request){
        $keys = $request->all();
        $validator = Validator::make($request->all(), $this->getValidationRulesinstaller(false));
        if ($validator->fails()) {
            return response()->json(['errors' => $validator->errors()], 403);
        }
        $type = new Installer();
        foreach ($keys as $key => $val) {
            if ($key == 'ispassword') {
                unset($keys[$key]);
            }
        }
        $this->timeline('新增了装维人员');
        return $this->jsonSuccessData($type->addinstall($keys));
    }
    // 用户搜索
    public function seekInstaller(Request $request){
        $keys = $request->all();
        $handle = DB::table('installer');
        foreach ($keys as $key => $val) {
            if ($key == 'page') {
                unset($keys[$key]);
            } elseif ($key == 'limit'){
                unset($keys[$key]);
            }
        }
        foreach ($keys as $k => $v) {
            if ($v == '正常'){
                $v = 1;
            }else if ($v == '禁用'){
                $v = 2;
            }
            $keys[$key] && $handle->where($k, 'like', '%' . $v . '%');
        }
        return $this->jsonSuccessData(['instllerdata'=>$handle->get(),'count'=>$handle->count()]);
    }
    //更新用户操作
    public function updateinstaller(Request $request){
        $uid =  $request->input('id');//用户id
        $data = $request->all();
        if (is_null($uid)){
            return $this->jsonErrorData(105,'用户id不能为空');
        }
        if ($request->isMethod('post')){
            foreach ($data as $k => $v) {
                if ($k == 'id') {
                    unset($data[$k]);
                }else if ($k == 'ispassword'){
                    unset($data[$k]);
                }else if ($k == 'password') {
                    $data['password'] =  md5(md5($v) . $data['salt']);
                }
            }
            $upin = Installer::where('id','=',(int)$uid)->update($data);
            if($upin !== false){
                return $this->jsonSuccessData($upin);
            }
            return $this->jsonErrorData(105,'更新失败');

        }else{
                return $this->jsonSuccessData(Installer::where('id','=',$uid)->first());
        }
    }

    /*
     *删除用户
     * type 1== 物理 2==逻辑删除
     * */

    public function  deleteinstaller(Request $request){
        $type = $request->input('type');
        $uid = $request->input('uid');
        if(is_null($type)){
            return $this->jsonErrorData(105,'type不能为空');
        }
        if (is_null($uid)){
            return $this->jsonErrorData(105,'uid不能为空');
        }
        if ($type == 1){//物理删除
            $this->timeline('装维人员放置废纸篓');
            return $this->jsonSuccessData(Installer::where('id','=',$uid)->delete());
        }elseif($type == 2){//逻辑删除
            $this->timeline('删除了废纸篓');
            return $this->jsonSuccessData(Installer::where('id','=',$uid)->update(['delete'=>2,'status'=>2]));
        }

    }
    //用户回复正常
    public function normalinstaller($id){
        return $this->jsonSuccessData(Installer::where('id','=',$id)->update(['delete'=>1,'status'=>1]));
    }
    //验证用户提交表单

    private function getValidationRulesinstaller($isNew = true){
        return [
            'username'=>'required|unique:installer,username|max:3',
            'name'=> 'required|max:10',
            'phone'=> 'required|regex:/^1[3465789]\d{9}$/|unique:installer,phone|max:11',
            'email' => 'required|email',
            'password' =>'required|max:10',
        ];
    }
}