<?php 
namespace PortalPeople\qvc\models;

use yii;
use \backend\models\Accounts;
class Cart extends \backend\models\Cartsbe
{
    public function rules() : array
    {
        $rules = parent::rules();
        $rules[] = [['account_code'], 'default', 'value' => 'QVC001'];
        $rules[] = [['paymentmethod'], 'default', 'value' => 'QVC'];
        $rules[] = [['data'], 'safe'];
        return $rules;
    }

    public function CreateDeliveryLines(){
        
            $shippingcosts = \backend\models\Shippingcosts::costs($this->id);
            $shippingline = $this->shippingline;
            foreach($shippingcosts as $haulier => $data){
                \Yii::Warning($data, 'CreateDeliveryLines Shipment');
                $cartline = \backend\models\Cartlines::find()
                    ->where([
                        'basketid' => $this->id, 
                        'haulierint' => $haulier, 
                        'productid' => '331D4D9B-7F11-9473-1279-CAFDA629360A'
                    ])
                    ->one();
                if (!$cartline){
                    $cartline = new \backend\models\Cartlines(['id' => \backend\models\Cartlines::Tempid(), 'basketid' => $this->id]);
                    $cartline->productid = '331D4D9B-7F11-9473-1279-CAFDA629360A';
                    $cartline->requested_date = $shippingline?->requested_date;
                    $cartline->qty = 1;
                    $cartline->erpqty = 1;
                    $cartline->price = 0;
                    $cartline->linetotal = 0.00;
                    $cartline->deliveryline = 0;
                    $cartline->paid = true;
                } 
                $cartline->haulierint = $haulier;
                $cartline->cost = $data['cost'];
                $cartline->costtotal = $data['cost'];
                $cartline->product_description = $shippingline?->product_description;
                
                $cartline->hidden_info = $data['haulier'].' Delivery ' . Inflector::pluralize($data['pallets'] . ' Pallet');
                $cartline->save();
            } 
    }
    public function afterSave($insert, $changedAttributes)
    {
        $this->CreateDeliveryLines();
        if($this->status == 'Paid' && 1 == 2){
            Yii::$app->trigger('cart-order-after', ($event = new \common\events\UserDataEvent(['customData' => $changedAttributes, 'model' => $this])));
            }
        return parent::afterSave($insert, $changedAttributes);
    }

    public function beforeSave($insert)
    {
        $account = Accounts::find()->where(['account_code' => 'QVC001'])->one() ?? throw new \RuntimeException("QVC Account not found");
        $qvcaddress = $account->addressl ?? null;
        $billing = json_decode($this->fulladdresses, true);
        $billing['billing'] = $qvcaddress ?? [];
        $this->contact = $billing['shipping']['firstname'].' '.$billing['shipping']['lastname'];
        $this->fulladdresses = $billing;
        $this->data = json_encode($this->data);
        
        parent::beforeSave($insert);
        $shipment = new \backend\models\Shipment();
        $shipment->shipment_type = 0;
        $shipment->cart_id = $this->id;
        $shipment->delivery = 'Eco' ?? null;
        $shipment->shipment_date = date('Y-m-d', strtotime('+3 days'));
        $shipment->save();

        $payment = new \backend\models\Paymentresponsesbe();
        $payment->basketid  = $this->id;
        $payment->source  = 'QVC';
        $payment->camount = $this->total;
        $payment->amount = $this->total;
        $payment->status = 'Paid';
        $payment->save();

        return true;
    }
}