<?php

namespace PortalPeople\shippingcosts\controllers;

use Yii;
use PortalPeople\shippingcosts\models\SysShippingSetting;
use PortalPeople\shippingcosts\models\SysShippingSettingSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ShipSettingController implements the CRUD actions for SysShippingSetting model.
 */
class ShipSettingController extends Controller
{
    /**
     * Is this used in this Project, Display in Permissions Force 403 if false
     *
        * @var bool
     */
    private $used = true;

    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\filters\AccessControl::class,
                'rules' => \common\models\Permissions::getPermissions(self::class),
            ],
            'verbs' => [
                'class' => \yii\filters\VerbFilter::class,
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => yii\web\ErrorAction::class,
            ],
        ];
    }

    /**
     * Lists all SysShippingSetting models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SysShippingSettingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

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

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

        if ($model->load(Yii::$app->request->post())) {
            $post = Yii::$app->request->post();
            $model->countries = $model->countries
                ? implode(",", $post['SysShippingSetting']['countries'])
                : "";
            $model->exclude_fornt_price = $model->exclude_fornt_price
                ? implode(",", $post['SysShippingSetting']['exclude_fornt_price'])
                : "";
            if ($model->save()) {
                $model = SysShippingSetting::find()
                    ->where(['=', 'created_at', $model->created_at])
                    ->andWhere(['=', 'created_by', $model->created_by])
                    ->one();
                return $this->redirect([
                    'view',
                    'id' => $model->id,
                    'language' => $model->language,
                ]);
            }
        }

        $model->countries = $model->countries ? explode(",", $model->countries) : '';

        $model->exclude_fornt_price = $model->exclude_fornt_price ? explode(",", $model->exclude_fornt_price) : '' ;

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

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

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

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

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

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

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

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