<?php

namespace backend\modules\card\models;

use backend\modules\achievements\models\Achievement;
use Common\Behaviors\LogBehavior;
use common\models\AchievementUserCard;
use Yii;
use backend\modules\settings\models\Skill;
use common\classes\Debug;
use common\models\CardSkill;
use common\models\User;
use common\models\FieldsValue;
use common\models\FieldsValueNew;
use yii\helpers\ArrayHelper;

class UserCard extends \common\models\UserCard
{
    public $fields;
    public $skill;
    public $achievements;

    public function init()
    {
        parent::init();

        $fieldValue = FieldsValueNew::find()->where(
            [
                'item_id' => \Yii::$app->request->get('id'),
                'item_type' => FieldsValueNew::TYPE_PROFILE,
            ]
        )
            ->all();
        $array = [];
        if (!empty($fieldValue)) {
            foreach ($fieldValue as $item) {
                array_push(
                    $array,
                    [
                        'field_id' => $item->field_id,
                        'value' => $item->value,
                        'order' => $item->order,
                        'type_file' => $item->type_file,
                        'field_name' => $item->field->name
                    ]
                );
            }
            $this->fields = $array;
        } else {
            $this->fields = [
                [
                    'field_id'   => null,
                    'value'  => null,
                    'order' => null,
                    'field_name' => null,
                    'type_file' => null,
                ],
            ];
        }

        $skill = ArrayHelper::getColumn(
            CardSkill::find()->where(['card_id' => \Yii::$app->request->get('id')])->all(),
            'skill_id'
        );

        if (!empty($skill)) {
            $this->skill = $skill;
        }

        $achievements = ArrayHelper::getColumn(AchievementUserCard::find()
            ->where(['user_card_id' => \Yii::$app->request->get('id')])
            ->innerJoinWith(['achievement' => function($query) {
                $query->andWhere(['status' => \common\models\Achievement::STATUS_ACTIVE]);
            }])
            ->all(),
            'achievement_id'
        );

        if (!empty($achievements)) {
            $this->achievements = $achievements;
        }
    }

    public function behaviors()
    {
        return [
            'log' => [
                'class' => \common\behaviors\LogBehavior::class,
            ]
        ];
    }

    public function beforeSave($insert)
    {
        if(isset(\Yii::$app->request->post('UserCard')['salary']))
        {
            $this->salary = str_replace(' ', '', \Yii::$app->request->post('UserCard')['salary']);
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    public function afterSave($insert, $changedAttributes)
    {
        $post = \Yii::$app->request->post('UserCard');
        if($post) {
            if (isset($post['fields'])) {
                FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
                foreach ($post['fields'] as $item) {
                    $item['value'] = urldecode($item['value']);

                    $fieldsValue = new FieldsValueNew();
                    $fieldsValue->field_id = $item['field_id'];
                    $fieldsValue->value = $item['value'];
                    $fieldsValue->order = $item['order'];
                    $fieldsValue->item_id = $this->id;
                    $fieldsValue->item_type = FieldsValueNew::TYPE_PROFILE;
                    if (is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])) {
                        $fieldsValue->type_file = 'file';
                    } else {
                        $fieldsValue->type_file = 'text';
                    }

                    $fieldsValue->save();
                }
            }
            if (is_array($post['fields'])) {
                CardSkill::deleteAll(['card_id' => $this->id]);
                if (is_array($post['skill']))
                    foreach ($post['skill'] as $item) {
                        $skill = new CardSkill();
                        $skill->skill_id = $item;
                        $skill->card_id = $this->id;

                        $skill->save();
                    }
            }

            if(is_array($post['achievements'])){
                AchievementUserCard::deleteAll(['user_card_id' => $this->id]);

                foreach ($post['achievements'] as $item) {
                    $achCard = new AchievementUserCard();
                    $achCard->user_card_id = $this->id;
                    $achCard->achievement_id = $item;

                    $achCard->save();
                }
            }

            parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        }
    }

    public static function getParameter($params, $key)
    {
        try {
            return $params[$key];
        } catch (\Exception $e) {
            return '';
        }
    }
}
