<?php 
namespace PortalPeople\netsuite\controllers;
use Yii;
use backend\models\Cartsbe;
class NetsuiteController extends \yii\web\Controller
{
     /**
     * Links Order to Netsuite, Pops a modal.
     *
     * @param [type] $id
     * @return void
     */
    public function actionLinktons($id)
    {
        $netsuiteConfig = Yii::$app->params['netsuite'] ?? [];
        $cartClass = $netsuiteConfig['cartClass'] ?? 'backend\models\Cartsbe';
        
        $model = $cartClass::findOne($id, 'en');
        $netsuite = $model->data->netsuite;
        Yii::$app->response->format = 'json';
        if (Yii::$app->request->isGet == true) {
            return $this->renderAjax('@PortalPeople/netsuite/views/cart/linktons', ['model' => $model, 'netsuite' => $netsuite]);
        } elseif (Yii::$app->request->isPost == true && $netsuite->load(Yii::$app->request->post()) && $netsuite->validate() && $model->save()) {
            return ['status' => 200, 'Message' => 'Linked'];
        } elseif (Yii::$app->request->isPost) {
            return ['status' => 100, 'Message' => 'Error'];
        }
    }
}
