<?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\Device;
use App\Laravue\Models\Users;
use App\Laravue\Models\Homepage;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\src\PHPMailer;
use App\Http\Controllers\src\Exception;
use Validator;

class UinappHomeController extends Controller
{
    public function devicedatainfo($id)
    {
        $data = DB::table('device as d')
            ->join('device_type as t', 'd.dtype', '=', 't.tid')
            ->join('gas as g', 'd.status', '=', 'g.id')
            ->join('danwei as  w', 'd.devicemonad', '=', 'w.id')
            ->join('status as s', 'd.devicepolice', '=', 's.id')
            ->join('users as u', 'd.uid', '=', 'u.id')
            ->where('d.id', '=', $id)
            ->select('d.*', 't.tname', 'g.gas', 'w.danwei', 's.status_name', 'u.username as nameh', 'u.isadmin')
            ->first();
        return $this->jsonSuccessData($data);
    }

    //获取模糊查询设备
    public function imitation(Request $request)
    {
        $imitationdata = $request->input('imitationdata');

        if ($imitationdata != '') {
            if ($this->isKanji($imitationdata)) {
                $data = DB::table('device as d')
                    ->join('device_type as t', 'd.dtype', '=', 't.tid')
                    ->join('gas as g', 'd.status', '=', 'g.id')
                    ->join('danwei as  w', 'd.devicemonad', '=', 'w.id')
                    ->join('status as s', 'd.devicepolice', '=', 's.id')
                    ->join('users as u', 'd.uid', '=', 'u.id')
                    ->where('d.username', 'like', '%' . $imitationdata . '%')
                    ->select('d.*', 't.tname', 'g.gas', 'w.danwei', 's.status_name', 'u.username as nameh', 'u.isadmin')
                    ->get();
            } else {
                $data = DB::table('device as d')
                    ->join('device_type as t', 'd.dtype', '=', 't.tid')
                    ->join('gas as g', 'd.status', '=', 'g.id')
                    ->join('danwei as  w', 'd.devicemonad', '=', 'w.id')
                    ->join('status as s', 'd.devicepolice', '=', 's.id')
                    ->join('users as u', 'd.uid', '=', 'u.id')
                    ->where('d.devicenum', 'like', '%' . $imitationdata . '%')
                    ->select('d.*', 't.tname', 'g.gas', 'w.danwei', 's.status_name', 'u.username as nameh', 'u.isadmin')
                    ->get();
            }
            return $this->jsonSuccessData($data);
        } else {
            return $this->jsonSuccessData('');
        }

    }

    //指定查询
    public function setseachdata(Request $request)
    {
        $setdata = $request->input('setseachdata');
        if ($this->isKanji($setdata)) {
            $data = DB::table('device as d')
                ->join('device_type as t', 'd.dtype', '=', 't.tid')
                ->join('gas as g', 'd.status', '=', 'g.id')
                ->join('danwei as  w', 'd.devicemonad', '=', 'w.id')
                ->join('status as s', 'd.devicepolice', '=', 's.id')
                ->join('users as u', 'd.uid', '=', 'u.id')
                ->where('d.username', '=', $setdata)
                ->select('d.*', 't.tname', 'g.gas', 'w.danwei', 's.status_name', 'u.username as nameh', 'u.isadmin')
                ->get();
        } else {
            $data = DB::table('device as d')
                ->join('device_type as t', 'd.dtype', '=', 't.tid')
                ->join('gas as g', 'd.status', '=', 'g.id')
                ->join('danwei as  w', 'd.devicemonad', '=', 'w.id')
                ->join('status as s', 'd.devicepolice', '=', 's.id')
                ->join('users as u', 'd.uid', '=', 'u.id')
                ->where('d.devicenum', '=', $setdata)
                ->select('d.*', 't.tname', 'g.gas', 'w.danwei', 's.status_name', 'u.username as nameh', 'u.isadmin')
                ->get();
        }
        return $this->jsonSuccessData($data);
    }

    //返回设备类型和单位和介质
    public function devicetype()
    {
        $gas = DB::table('gas')->get()->toArray();
        $danwei = DB::table('danwei')->get()->toArray();
        $devicetypecount = DB::table('device_type as t')
            ->leftjoin('device as d', 'd.dtype', '=', 't.tid')
            ->selectRaw('t.*,count(d.id) as counnum')
            ->groupBy('t.tid')
            ->get()->toArray();
        return $this->jsonSuccessData(['devicetype' => $devicetypecount, 'gas' => $gas, 'danwei' => $danwei]);
    }

    public function isemail(Request $request)
    {
        $isemail = $request->input('email');
        $data = DB::table('users')->where('email', '=', $isemail)->first();
        if ($data) {
            return $this->jsonSuccessData(1);
        } else {
            return $this->jsonSuccessData(2);
        }
    }

    public function ispassword(Request $request)
    {
        $password = $request->input('password');
        $isemail = $request->input('email');
        $data = DB::table('users')->where('email', '=', $isemail)->update(['password' => Hash::make($password)]);
        if ($data !== false) {
            return $this->jsonSuccessData(1);
        } else {
            return $this->jsonSuccessData(2);
        }
    }

//    发送邮箱验证
    public function mails(Request $request)
    {
//        print_r( base_path() . '/tests/textcert/4544322_iot.zhkjgf.com_public.crt');
//        die();
        $isemail = $request->input('email');
        $num = str_pad(mt_rand(0, 999999), 6, "0", STR_PAD_BOTH);
        if ($isemail != ''){
           $send =  $this->PHPMailer($isemail,$num);
           if($send == 200){
               return $this->jsonSuccessData($num);
           }
        }
    }

    //监测是否是汉子
    private function isKanji($str)
    {
        if (preg_match("/^[\x7f-\xff]+$/", $str)) {
            return true;
        } else {
            return false;
        }
    }
    /*
     * $name 发件人
     * $num 随机字符串 验证码
     * */
    private function PHPMailer($name,$num)
    {
        include_once app_path() . '/Http/Controllers/src/PHPMailer.php';
        include_once app_path() . '/Http/Controllers/src/SMTP.php';
        $mail = new PHPMailer(true);
        try {
            //服务器配置
            $mail->CharSet = "UTF-8";                     //设定邮件编码
            $mail->SMTPDebug = 0;                        // 调试模式输出
            $mail->isSMTP();                             // 使用SMTP
            $mail->Host = 'smtp.qq.com';                // SMTP服务器
            $mail->SMTPAuth = true;                      // 允许 SMTP 认证
            $mail->Username = '2758505256@qq.com';                // SMTP 用户名  即邮箱的用户名
            $mail->Password = 'qekjtybbeswsdhec';             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
            $mail->SMTPSecure = 'ssl';                    // 允许 TLS 或者ssl协议
            $mail->Port = 465;
            $mail->setFrom('2758505256@qq.com', '河北泽宏科技股份有限公司');  //发件人
            $mail->addAddress($name);  // 可添加多个收件人

            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
            $mail->Subject = '找回密码操作';
            $mail->Body = "<h3>您的验证码是:</h3><h1>$num</h1>" . date('Y-m-d H:i:s');
            $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
            $mail->send();
            return 200;
        } catch (Exception $e) {
            return 105 . $mail->ErrorInfo;
        }
    }

    //主页home的
}