<?php

namespace Modules\Bitrix\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

use Modules\Main\Models\Deal\Deal;
use Modules\Contracts\Models\Contract;
use Modules\Contracts\Models\ContractStatus;
use App\Notifications\ContractUpdated;
use App\Notifications\Deal\ContractUpdated as ContractUpdatedForDeal;
class ContractUpdateController
{
    public function __invoke(Deal $deal, Request $request)
    {
        $contract =
            Contract::updateOrCreate(
                ['deal_id' => $deal->id],
                [
                    'status' => constant(ContractStatus::class . "::" . $request->status),
                    'comment' => $request->comment,
                    'price' => $request->price,
                    'reward' => $request->reward,
                    'square' => $request->square,
                    'floor' => $request->floor,
                    'room' => $request->room,//Номер квартиры
                    'date' => $request->date,//дата ДДУ
                    'reg_date' => $request->reg_date,//Дата регистрации ДДУ
                    'payment_type' => $request->payment_type,//Вид оплаты
                    'plan7_id' => $request->plan7_id,
                    'base64_image' => $request->plan_image
                ]
            );
        $addInfo = $contract->add_info;
        if ($request->has('manager_name')) {
            $addInfo['manager']['name'] = $request->manager_name;
        }
        if ($request->has('manager_phone')) {
            $addInfo['manager']['phone'] = $request->manager_phone;
        }
        if ($request->has('manager_email')) {
            $addInfo['manager']['email'] = $request->manager_email;
        }
        if ($request->has('status_description')) {
            $addInfo['status_description'] = $request->status_description;
        }
        $contract->add_info = $addInfo;
        $contract->save();
        $agent = $deal->agent;
        $deal->notify(new ContractUpdatedForDeal($deal->contract));
        //$agent->user->notify(new ContractUpdated($deal->contract));
        return true;
    }
}