<?php

namespace App\Jobs;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SyncOrderServiceStatus implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $user = $this->user;
        $user_fields = ['name', 'sex', 'birthday','stature'];
        $user_info_fields = ["weight", 'stomach', 'heart', 'sleep', 'man', 'other', 'dietary', 'living', 'mental', 'family', 'family_member', 'person', 'desc'];
        $complete = 1;
        foreach ($user_fields as $field)
        {
            if (empty($user->$field)) {
                $complete = 0;
                Order::where("user_id", $user->id)->where("pay_status", "PAID")->update(['service_status'=>'NOINFO']);
                break;
            }
        }

        foreach ($user_info_fields as $field)
        {
            if (empty($user->userInfo->$field)) {
                $complete = 0;
                Order::where("user_id", $user->id)->where("pay_status", "PAID")->update(['service_status'=>'NOINFO']);
                break;
            }
        }
        if ($complete) {
            Order::where("user_id", $user->id)->where("pay_status", "PAID")->update(['service_status'=>'COMPLETEINFO']);
        }
    }
}
