<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\SoftDeletes;

class Recruit extends Model
{
    use SoftDeletes;
    protected $table = 'recruit';
    protected $guarded = [];

    //当前步骤
    public static $currentText = ['已结束','报名','审核','笔试','上机','面试','复试','体检','政审','公示'];

    //步骤和流程的处理
    public static function parse_index(Recruit $value)
    {
        //步骤处理
        $current = $value->current;
        $value->current_text = self::$currentText[$current];
        switch ($current){
            case '0':
                $value->current_time = "时间:" . date('m.d',strtotime($value->updated_at));
                break;
            case '1':
                if(empty($value->apply_start)){
                    $value->current_time = '待定';
                }else if(empty($value->apply_end)){
                    $value->current_time = date('m.d',strtotime($value->apply_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->apply_start)) . '-' . date('m.d',strtotime($value->apply_end));
                }
                break;
            case '2':
                if(empty($value->audit_start)){
                    $value->current_time = '待定';
                }else if(empty($value->audit_end)){
                    $value->current_time = date('m.d',strtotime($value->audit_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->audit_start)) . '-' . date('m.d',strtotime($value->audit_end));
                }
                break;
            case '3':
                if(empty($value->preliminary_start)){
                    $value->current_time = '待定';
                }else if(empty($value->preliminary_end)){
                    $value->current_time = date('m.d',strtotime($value->preliminary_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->preliminary_start)) . '-' . date('m.d',strtotime($value->preliminary_end));
                }
                break;
            case '4':
                if(empty($value->computer_start)){
                    $value->current_time = '待定';
                }else if(empty($value->computer_end)){
                    $value->current_time = date('m.d',strtotime($value->computer_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->computer_start)) . '-' . date('m.d',strtotime($value->computer_end));
                }
                break;
            case '5':
                if(empty($value->interview_start)){
                    $value->current_time = '待定';
                }else if(empty($value->interview_end)){
                    $value->current_time = date('m.d',strtotime($value->interview_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->interview_start)) . '-' . date('m.d',strtotime($value->interview_end));
                }
                break;
            case '6':
                if(empty($value->reexamine_start)){
                    $value->current_time = '待定';
                }else if(empty($value->reexamine_end)){
                    $value->current_time = date('m.d',strtotime($value->reexamine_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->reexamine_start)) . '-' . date('m.d',strtotime($value->reexamine_end));
                }
                break;
            case '7':
                if(empty($value->health_start)){
                    $value->current_time = '待定';
                }else if(empty($value->health_end)){
                    $value->current_time = date('m.d',strtotime($value->health_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->health_start)) . '-' . date('m.d',strtotime($value->health_end));
                }
                break;
            case '8':
                if(empty($value->political_start)){
                    $value->current_time = '待定';
                }else if(empty($value->political_end)){
                    $value->current_time = date('m.d',strtotime($value->political_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->political_start)) . '-' . date('m.d',strtotime($value->political_end));
                }
                break;
            case '9':
                if(empty($value->publicity_start)){
                    $value->current_time = '待定';
                }else if(empty($value->publicity_end)){
                    $value->current_time = date('m.d',strtotime($value->publicity_start)) . '-' . '待定';
                }else{
                    $value->current_time = date('m.d',strtotime($value->publicity_start)) . '-' . date('m.d',strtotime($value->publicity_end));
                }
                break;
        }

        //流程处理
        $step_arr = explode(',',$value->step);
        $step_text = '';
        foreach ($step_arr as $k => $v) {
            if ((count($step_arr) - 1 ) == $k) {
                $step_text .= self::$currentText[$v];
            } else {
                $step_text .= self::$currentText[$v] . '->';
            }
        }
        $value->step_text = $step_text;

        return $value;
    }

    public static function parse_info(Recruit $value)
    {
        $step = explode(',',$value->step);
        $steps = [];
        foreach ($step as $v){
            switch ($v){
                case '0':
                    $item['step_id'] = 10;
                    $item['name'] = '已结束';
                    $item['time'] = "时间:" . date('m.d',strtotime($value->updated_at));
                    break;
                case '1':
                    $item['step_id'] = 1;
                    $item['name'] = '报名';
                    if(empty($value->apply_start)){
                        $time = '待定';
                    }else if(empty($value->apply_end)){
                        $time = date('m.d',strtotime($value->apply_start)) . '-' . '待定';
                    }else{
                        if ($value->apply_start == $value->apply_end) {
                            $time = date('m.d',strtotime($value->apply_start));
                        } else {
                            $time = date('m.d',strtotime($value->apply_start)) . '-' . date('m.d',strtotime($value->apply_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '2':
                    $item['step_id'] = 2;
                    $item['name'] = '审核';
                    if(empty($value->audit_start)){
                        $time = '待定';
                    }else if(empty($value->audit_end)){
                        $time = date('m.d',strtotime($value->audit_start)) . '-' . '待定';
                    }else{
                        if ($value->audit_start == $value->audit_end) {
                            $time = date('m.d',strtotime($value->audit_start));
                        } else {
                            $time = date('m.d',strtotime($value->audit_start)) . '-' . date('m.d',strtotime($value->audit_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '3':
                    $item['step_id'] = 3;
                    $item['name'] = '笔试';
                    if(empty($value->preliminary_start)){
                        $time = '待定';
                    }else if(empty($value->preliminary_end)){
                        $time = date('m.d',strtotime($value->preliminary_start)) . '-' . '待定';
                    }else{
                        $value->preliminary_start = date('m.d',strtotime($value->preliminary_start));
                        $value->preliminary_end = date('m.d',strtotime($value->preliminary_end));
                        if ($value->preliminary_start == $value->preliminary_end) {
                            $time = $value->preliminary_start;
                        } else {
                            $time = $value->preliminary_start . '-' . $value->preliminary_end;
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '4':
                    $item['step_id'] = 4;
                    $item['name'] = '上机';
                    if(empty($value->computer_start)){
                        $time = '待定';
                    }else if(empty($value->computer_end)){
                        $time = date('m.d',strtotime($value->computer_start)) . '-' . '待定';
                    }else{
                        if ($value->computer_start == $value->computer_end) {
                            $time = date('m.d',strtotime($value->computer_start));
                        } else {
                            $time = date('m.d',strtotime($value->computer_start)) . '-' . date('m.d',strtotime($value->computer_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '5':
                    $item['step_id'] = 5;
                    $item['name'] = '面试';
                    if(empty($value->interview_start)){
                        $time = '待定';
                    }else if(empty($value->interview_end)){
                        $time = date('m.d',strtotime($value->interview_start)) . '-' . '待定';
                    }else{
                        if ($value->interview_start == $value->interview_end) {
                            $time = date('m.d',strtotime($value->interview_start));
                        } else {
                            $time = date('m.d',strtotime($value->interview_start)) . '-' . date('m.d',strtotime($value->interview_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '6':
                    $item['step_id'] = 6;
                    $item['name'] = '复试';
                    if(empty($value->reexamine_start)){
                        $time = '待定';
                    }else if(empty($value->reexamine_end)){
                        $time = date('m.d',strtotime($value->reexamine_start)) . '-' . '待定';
                    }else{
                        if ($value->reexamine_start == $value->reexamine_end) {
                            $time = date('m.d',strtotime($value->reexamine_start));
                        } else {
                            $time = date('m.d',strtotime($value->reexamine_start)) . '-' . date('m.d',strtotime($value->reexamine_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '7':
                    $item['step_id'] = 7;
                    $item['name'] = '体检';
                    if(empty($value->health_start)){
                        $time = '待定';
                    }else if(empty($value->health_end)){
                        $time = date('m.d',strtotime($value->health_start)) . '-' . '待定';
                    }else{
                        if ($value->health_start == $value->health_end) {
                            $time = date('m.d',strtotime($value->health_start));
                        } else {
                            $time = date('m.d',strtotime($value->health_start)) . '-' . date('m.d',strtotime($value->health_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '8':
                    $item['step_id'] = 8;
                    $item['name'] = '政审';
                    if(empty($value->political_start)){
                        $time = '待定';
                    }else if(empty($value->political_end)){
                        $time = date('m.d',strtotime($value->political_start)) . '-' . '待定';
                    }else{
                        if ($value->political_start == $value->political_end) {
                            $time = date('m.d',strtotime($value->political_start));
                        } else {
                            $time = date('m.d',strtotime($value->political_start)) . '-' . date('m.d',strtotime($value->political_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
                case '9':
                    $item['step_id'] = 9;
                    $item['name'] = '公示';
                    if(empty($value->publicity_start)){
                        $time = '待定';
                    }else if(empty($value->publicity_end)){
                        $time = date('m.d',strtotime($value->publicity_start)) . '-' . '待定';
                    }else{
                        if ($value->publicity_start == $value->publicity_end) {
                            $time = date('m.d',strtotime($value->publicity_start));
                        } else {
                            $time = date('m.d',strtotime($value->publicity_start)) . '-' . date('m.d',strtotime($value->publicity_end));
                        }
                    }
                    $item['time'] = $time;
                    break;
            }
            array_push($steps,$item);
        }
        $value->steps = $steps;

        if($value->current == '0'){
            $value->current = '10';
        }
        $value->introduction = htmlspecialchars_decode($value->introduction, ENT_QUOTES);
        $time = time();
        // 报名中
        if (strtotime($value->audit_start) < $time && strtotime($value->audit_end) > $time) {
            $value->isApply = 1;
        } else {
            $value->isApply = 0;
        }


        return $value;

    }


}