<?php

namespace App\Imports;

use App\Exceptions\ImportError;
use App\Models\Device;
use App\Models\DeviceName;
use App\Models\InnerDevice;
use App\Models\InnerDeviceNamesModel;
use App\Models\Option;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\ProjectZone;
use App\Models\Road;
use App\Models\Spec;
use App\Models\User;
use App\Models\WorkPoint;
use App\Models\Zone;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class ProjectImport implements ToCollection
{
    protected $model;

    protected $zone;

    protected $road;

    protected $project_zone;

    public function __construct()
    {
        $this->model = new Project();
    }

    public function collection(Collection $rows)
    {
        if(count($rows) <= 1) {
            /** @noinspection PhpUnhandledExceptionInspection */
            return false;
        };
        foreach ($rows as $key => $row) {
            if($key == 0) continue;
            if(empty($row[0])&&empty($row[1])&&empty($row[2]))
            {
                break;
            }

            //项目id
            $project_id = null;
            //用户id
            $user_id = null;
            $password = '123456';
            $password = bcrypt($password);
            /*
             * 项目名称
             * 逻辑
             *      1.如果不存在该项目，那么就创建一个项目，并且active为激活状态
             *      2.如果存在该项目，返回项目id
             * */
            if ($row[0])
            {
                $project_id = Project::firstOrCreate([
                    'name' => $row[0],
                ],[
                    'name' => $row[0],
                    'active' => 1
                ]);
            }

            //手机号
            if ($row[1]&&$row[2])
            {
                $user_id = User::firstOrCreate([
                    'phone' => $row[2]
                ],[
                    'name' => $row[1],
                    'phone'=> $row[2],
                    'password' => $password
                ]);
            }
            //如果相同电话号码，不同名字的话就替换原来的名字
            if ($user_id->name != $row[1])
            {
                User::where('id',$user_id->id)->update(['name'=>$row[1]]);
            }
            /*
             * 导入到项目成员表里
             *      当前这个用户是否在项目里
             *          如果在项目里，判断是否是项目经理
             *              如果是项目经理就不去做替换操作
             *              如果不是项目经理就替换掉原来的项目经理，当前这个用户就变成项目经理
             *          如果不在项目里，判断原来是否有项目经理
             *              如果有就替换掉
             *              如果没有就新建项目经理
             *
             * */
//            dd($user_id);
            //判断当前用户是否在项目里 计数不等于0就是在项目里
            $has_user = ProjectUser::where('project_id',$project_id->id)->where('user_id',$user_id->id)->get()->count();
            if ($has_user != 0)
            {
                //该用户在当前项目的职位
                $in_project_user = ProjectUser::where('project_id',$project_id['id'])->where('user_id',$user_id->id)->value('project_role_id');
                //如果职位不是项目经理就替换
                if ($in_project_user != 4){
                    //查找项目经理人数
                    $manager_num = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->count();
                    //如果有项目经理就替换
                    if ($manager_num != 0)
                    {
                        //获取到项目经理的数据
                        $manager_user_project_id = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->value('project_id');
                        $manager_user_user_id = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->value('user_id');
                        //替换成项目副经理
                        ProjectUser::where('project_id',$manager_user_project_id)->where('user_id',$manager_user_user_id)->update(['project_role_id'=>3]);
                    }
                    ProjectUser::where('project_id',$project_id['id'])->where('user_id',$user_id->id)->update(['project_role_id'=> 4]);
                }
            }else
            {
                //判断项目里是否有项目经理
                $manager_num = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->count();
                //如果有项目经理
                if ($manager_num != 0)
                {
                    //获取到项目经理的数据
                    $manager_user_project_id = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->value('project_id');
                    $manager_user_user_id = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->value('user_id');
                    //替换项目经理
                    ProjectUser::where('project_id',$manager_user_project_id)->where('user_id',$manager_user_user_id)->update(['project_role_id'=>3]);
                    $data =[
                        'project_id' => $project_id['id'],
                        'user_id' => $user_id['id'],
                        'project_role_id' => 4
                    ];
                    ProjectUser::create($data);
                }else
                {
                    $data =[
                        'project_id' => $project_id['id'],
                        'user_id' => $user_id['id'],
                        'project_role_id' => 4
                    ];
                    ProjectUser::create($data);
                }
            }
//            $manager_num = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->count();
//            if ($manager_num != 0)
//            {
//                $manager_user = ProjectUser::where('project_id',$project_id['id'])->where('project_role_id',4)->first();
//                ProjectUser::where('id',$manager_user['id'])->update(['project_role_id'=>3]);
//            }
//            $data =[
//                'project_id' => $project_id['id'],
//                'user_id' => $user_id['id'],
//                'project_role_id' => 4
//            ];
//            ProjectUser::create($data);
        }
        return true;
    }
}
