<?php

namespace App\Imports\User;

use App\Models\Patient;
use App\Models\User;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use phpDocumentor\Reflection\DocBlock;

class PatientsSheet implements ToCollection
{
    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        $patients_info = [];
        foreach ($collection as $row) {
            if ($row[0]=="患者姓名")
            {
                continue;
            }

            //姓名
            if ($row[2] == null)
            {
                $user_name = '默认用户';
                $nickname = '默认用户';
            }else{
                $user_name = $row[2] ;
                $nickname = $row[2] ;
            }
            if ($row[1])
            {
                $has_user = User::where('phone',$row[1])->count();
                if ($has_user == 0)
                {
                    $new_user = User::create([
                        'nickname' =>$nickname,
                        'user_name' => $user_name,
                        'phone' => $row[1],
                        'created_at' => date('Y-m-d H:i:s',time()),
                        'balance' => 0
                    ]);
                    \Log::info('新增用户'.$new_user['id']);
                }else{
                    $new_user['id'] = User::where('phone',$row[1])->value('id');
                }
            }
            if ($new_user['id'] != null)
            {
                $patients_info['user_id'] = $new_user['id'];
            }else{
                $patients_info['user_id'] = 0;
            }

            if ($row[4] == null)
            {
                $patients_info['sex'] = 0;
            }else{
                if ($row[5] == "男")
                {
                    $patients_info['sex'] = 1;
                }else{
                    $patients_info['sex'] = 2;
                }
            }
            if ($row[0]==null){
                $patients_info['name'] = '默认用户';
            }else{
                $patients_info['name'] = $row[0];
            }
            if ($row[1] == null){
                $patients_info['phone'] = 0;
            }else{
                $patients_info['phone'] = $row[1];
            }
            if ($row[3])
            {
                $patients_info['birthday'] = self::excelTime($row[3]);
            }else{
                $patients_info['birthday'] = self::excelTime(0);
            }
            Patient::create($patients_info);
        }
    }
    static function excelTime($date, $time = false) {
        if(function_exists('GregorianToJD')){
            if (is_numeric( $date )) {
                $jd = GregorianToJD( 1, 1, 1970 );
                $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
                $date = explode( '/', $gregorian );
                $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
                    ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
                    ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
                    . ($time ? " 00:00:00" : '');
                return $date_str;
            }
        }else{
            $date=$date>25568?$date+1:25569;
            /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
            $ofs=(70 * 365 + 17+2) * 86400;
            $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
        }
//        dd($date);
        return $date;
    }
}
