<?php

namespace app\controllers;

use Yii;
use app\models\Configurations;
use app\models\Store;
use app\models\Orders;
use app\models\ConfigurationsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ConfigurationController implements the CRUD actions for Configurations model.
 */
class ConfigurationController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Configurations models.
     * @return mixed
     */
    public function actionIndex() {

        //$this->layout = 'shopify';

        $session = Yii::$app->session;
        $new_settings = array(
            'enable_paypal' => "",
            'enable_test' => '',
            'test_it_username' => '', 'test_it_password' => '', 'test_it_signature' => '', 'live_it_username' => '', 'live_it_password' => '', 'live_it_signature' => '',);
        $shop = $session->get('shop');
        $meta_key = $shop . '_paypal_detail';

        $new_settings2 = array(
            'enable_stripe' => "",
            'enable_test' => '',
            'pk_test' => '', 'sk_test' => '', 'pk_live' => '', 'sk_live' => '',);
        $shop = $session->get('shop');
        $meta_key = $shop . '_stripe_detail';

        $new_settings3 = array(
            'enable_express' => "",
            'enable_test' => '',
            'client_id_test' => '', 'client_id_live' => '', 'currency_live' => '', 'currency_test' => '',);
        $shop = $session->get('shop');
        $meta_key = $shop . '_express_detail';

        $new_Setting = Configurations::find()->where(['meta_key' => $shop . '_paypal_detail'])->one();
        $new_Setting2 = Configurations::find()->where(['meta_key' => $shop . '_stripe_detail'])->one();
        $new_Setting3 = Configurations::find()->where(['meta_key' => $shop . '_express_detail'])->one();

        if (isset($new_Setting))
            $new_settings = unserialize($new_Setting->meta_value);
        if (isset($new_Setting2))
            $new_settings2 = unserialize($new_Setting2->meta_value);
        if (isset($new_Setting3))
            $new_settings3 = unserialize($new_Setting3->meta_value);

        $Store = Store::find()->where(['name' => $shop])->one();
        $store_id = $Store->id;
        $order = Orders::find()->where(['store_id' => $store_id])->all();
        $new_settings['shop'] = $shop;
        $new_settings2['shop'] = $shop;
        $new_settings3['shop'] = $shop;

        return $this->render('index', ['new_settings' => $new_settings, 'new_Setting2' => $new_Setting2, 'new_settings3' => $new_settings3]);
    }

    public function actionSavesetting() {
        $model = new Configurations();
        $session = Yii::$app->session;
        $shop = $session->get('shop');
        $data = array(
            "enable_paypal" => isset($_POST['enable_paypal']) ? $_POST['enable_paypal'] : '',
            "enable_test" => isset($_POST['enable_test']) ? $_POST['enable_test'] : '',
            "test_it_username" => $_POST['test_it_username'],
            "test_it_password" => $_POST['test_it_password'],
            "test_it_signature" => $_POST['test_it_signature'],
            "live_it_username" => $_POST['live_it_username'],
            "live_it_password" => $_POST['live_it_password'],
            "live_it_signature" => $_POST['live_it_signature'],
        );
        $data = serialize($data);
        $Store = Store::find()->where(['name' => $shop])->one();
        $store_id = $Store->id;
        $meta_key = $shop . '_paypal_detail';
        $model->meta_key = $meta_key;
        $model->meta_value = $data;
        $model->store_id = $store_id;
        $Setting = Configurations::find()->where(['meta_key' => $shop . '_paypal_detail'])->one();
        if ($Setting) {
            $model->updateAll(['meta_value' => $data], ['like', 'meta_key', $shop . '_paypal_detail']);
        } else {
            $model->save(false);
        }
        $new_Setting = Configurations::find()->where(['meta_key' => $shop . '_paypal_detail'])->one();
        $new_settings = unserialize($new_Setting->meta_value);
        return $this->redirect('index');
        //return $this->render('index',['new_settings'=>$new_settings,'new_settings2'=>$new_settings2,]); 
    }

    public function actionSavestripesetting() {
        $model = new Configurations();
        $session = Yii::$app->session;
        $shop = $session->get('shop');
        $data = array(
            "enable_stripe" => isset($_POST['enable_stripe']) ? $_POST['enable_stripe'] : '',
            "enable_test" => isset($_POST['enable_test']) ? $_POST['enable_test'] : '',
            "pk_test" => $_POST['pk_test'],
            "sk_test" => $_POST['sk_test'],
            "pk_live" => $_POST['pk_live'],
            "sk_live" => $_POST['sk_live'],
        );
        $data = serialize($data);
        $Store = Store::find()->where(['name' => $shop])->one();
        $store_id = $Store->id;
        $meta_key = $shop . '_stripe_detail';
        $model->meta_key = $meta_key;
        $model->meta_value = $data;
        $model->store_id = $store_id;
        $Setting = Configurations::find()->where(['meta_key' => $shop . '_stripe_detail'])->one();
        if ($Setting) {
            $model->updateAll(['meta_value' => $data], ['like', 'meta_key', $shop . '_stripe_detail']);
        } else {
            $model->save(false);
        }
        $new_Setting2 = Configurations::find()->where(['meta_key' => $shop . '_stripe_detail'])->one();
        $new_settings2 = unserialize($new_Setting2->meta_value);
        return $this->redirect('index');
        //return $this->render('index',['new_settings2'=>$new_settings2,'new_settings'=>$new_settings]); 
    }

    public function actionSaveexpresssetting() {
        $model = new Configurations();
        $session = Yii::$app->session;
        $shop = $session->get('shop');
        $data = array(
            "enable_express" => isset($_POST['enable_express']) ? $_POST['enable_express'] : '',
            "enable_test" => isset($_POST['enable_test']) ? $_POST['enable_test'] : '',
            "client_id_test" => $_POST['client_id_test'],
            "currency_test" => $_POST['currency_test'],
            "client_id_live" => $_POST['client_id_live'],
            "currency_live" => $_POST['currency_live'],
        );
        $data = serialize($data);
        $Store = Store::find()->where(['name' => $shop])->one();
        $store_id = $Store->id;
        $meta_key = $shop . '_express_detail';
        $model->meta_key = $meta_key;
        $model->meta_value = $data;
        $model->store_id = $store_id;
        $Setting = Configurations::find()->where(['meta_key' => $shop . '_express_detail'])->one();
        if ($Setting) {
            $model->updateAll(['meta_value' => $data], ['like', 'meta_key', $shop . '_express_detail']);
        } else {
            $model->save(false);
        }
        $new_Setting3 = Configurations::find()->where(['meta_key' => $shop . '_express_detail'])->one();
        $new_settings3 = unserialize($new_Setting3->meta_value);
        return $this->redirect('index');
        //return $this->render('index',['new_settings2'=>$new_settings2,'new_settings'=>$new_settings]); 
    }

    /**
     * Displays a single Configurations model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Configurations model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Configurations();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing Configurations model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
                    'model' => $model,
        ]);
    }

    /**
     * Deletes an existing Configurations model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Configurations model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Configurations the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Configurations::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

}
