<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use app\models\SystemConfigurations;
use app\models\SystemConfigurationsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * SystemConfigurationsController implements the CRUD actions for SystemConfigurations model.
 */
class SystemConfigurationsController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => false,
                        'roles' => ['?'],
                    ],
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
                'denyCallback' => function ($rule, $action) {
                    Yii::$app->session->setFlash('unauthenticated', 'You are not authenticated, please login to access this page.');
                    return $this->redirect(['admins/login']);
                }
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actionConfigureOptions() 
    {
        $options = new SystemConfigurations();
        
        if ($options->load(Yii::$app->request->post())) {
            $data = Yii::$app->request->post()['SystemConfigurations'];
            $uploadedLogo = UploadedFile::getInstance($options, 'logo');
            if($uploadedLogo != null){
                $dir_name = 'home-icon/';
                if(!is_dir($dir_name))
                {
                    mkdir($dir_name, 0777, true);
                }
                $uploadedLogo->saveAs($dir_name . $uploadedLogo->name);
                $logo = SystemConfigurations::find()->where(['config_option' => 'logo'])->one();
                $logo->value = $dir_name . $uploadedLogo->name;
                if(!$logo->save()){
                    return var_dump($logo->getErrors());
                }
            }

            $UploadedRestaurantImage = UploadedFile::getInstance($options, 'restaurant_image');
            if($UploadedRestaurantImage != null){
                $dir_name = 'home-icon/';
                if(!is_dir($dir_name))
                {
                    mkdir($dir_name, 0777, true);
                }
                $UploadedRestaurantImage->saveAs($dir_name . $UploadedRestaurantImage->name);
                $RestaurantImage = SystemConfigurations::find()->where(['config_option' => 'restaurant_image'])->one();
                $RestaurantImage->value = $dir_name . $UploadedRestaurantImage->name;
                if(!$RestaurantImage->save()){
                    return var_dump(false);
                }
            }

            foreach ($data as $key => $value) {
                if($key != 'logo' && $key != 'restaurant_image'){
                    $new_value = SystemConfigurations::find()->where(['config_option' => $key])->one();
                    if ($new_value){
                        $new_value->value = (is_array($value) ? implode(" ", $value) : $value);
                        if (! $new_value->save()){
                            return var_dump($new_value->getErrors());
                        }
                    }
                }
            }
            Yii::$app->session->setFlash('updated', Yii::t('app', 'Updated Successfuly'));
            return $this->redirect(['configure-options']);
        }
        
        $max_var_num = SystemConfigurations::find()->where(['config_option' => 'max_var_num'])->one();
        if($max_var_num){
            $options->max_var_num = $max_var_num->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'max_var_num';
            $temp_model->value = 3;
            $temp_model->save();
        }

        $max_item_images = SystemConfigurations::find()->where(['config_option' => 'max_item_images'])->one();
        if($max_var_num){
            $options->max_item_images = $max_item_images->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'max_item_images';
            $temp_model->value = 5;
            $temp_model->save();
        }

        $logo = SystemConfigurations::find()->where(['config_option' => 'logo'])->one();
        if($max_var_num){
            $options->logo = $logo->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'logo';
            $temp_model->value = null;
            $temp_model->save();
        }

        $facebook_link = SystemConfigurations::find()->where(['config_option' => 'facebook_link'])->one();
        if($max_var_num){
            $options->facebook_link = $facebook_link->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'facebook_link';
            $temp_model->value = null;
            $temp_model->save();
        }

        $whatsapp_link = SystemConfigurations::find()->where(['config_option' => 'whatsapp_link'])->one();
        if($max_var_num){
            $options->whatsapp_link = $whatsapp_link->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'whatsapp_link';
            $temp_model->value = null;
            $temp_model->save();
        }

        $instagram_link = SystemConfigurations::find()->where(['config_option' => 'instagram_link'])->one();
        if($max_var_num){
            $options->instagram_link = $instagram_link->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'instagram_link';
            $temp_model->value = null;
            $temp_model->save();
        }

        $messenger_link = SystemConfigurations::find()->where(['config_option' => 'messenger_link'])->one();
        if($max_var_num){
            $options->messenger_link = $messenger_link->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'messenger_link';
            $temp_model->value = null;
            $temp_model->save();
        }

        $twitter_link = SystemConfigurations::find()->where(['config_option' => 'twitter_link'])->one();
        if($max_var_num){
            $options->twitter_link = $twitter_link->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'twitter_link';
            $temp_model->value = null;
            $temp_model->save();
        }

        $restaurant_image = SystemConfigurations::find()->where(['config_option' => 'restaurant_image'])->one();
        if($max_var_num){
            $options->restaurant_image = $restaurant_image->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'restaurant_image';
            $temp_model->value = null;
            $temp_model->save();
        }

        $arabic_about = SystemConfigurations::find()->where(['config_option' => 'arabic_about'])->one();
        if($max_var_num){
            $options->arabic_about = $arabic_about->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'arabic_about';
            $temp_model->value = null;
            $temp_model->save();
        }

        $english_about = SystemConfigurations::find()->where(['config_option' => 'english_about'])->one();
        if($max_var_num){
            $options->english_about = $english_about->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'english_about';
            $temp_model->value = null;
            $temp_model->save();
        }

        $currency = SystemConfigurations::find()->where(['config_option' => 'currency'])->one();
        if($max_var_num){
            $options->currency = $currency->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'currency';
            $temp_model->value = null;
            $temp_model->save();
        }
        
        $currency_ar = SystemConfigurations::find()->where(['config_option' => 'currency_ar'])->one();
        if($max_var_num){
            $options->currency_ar = $currency_ar->value;
        }
        else{
            $temp_model = new SystemConfigurations();
            $temp_model->config_option = 'currency_ar';
            $temp_model->value = null;
            $temp_model->save();
        }
        
        // return "<pre>" . var_export($options, true) . "</pre>";
        return $this->render('configure-options', [
            'options' => $options,
        ]);
    }
}