<?php

namespace common\models;

use Yii;
use yii\base\Model;
use common\models\CommandeAuto;
use common\models\CommandeAutoProduit;

/**
 * Login form
 */
class CommandeAutoForm extends Model {

    public $id;
    public $id_user;
    public $username;
    public $id_etablissement;
    public $id_point_vente;
    public $date_debut;
    public $date_fin;
    public $lundi;
    public $mardi;
    public $mercredi;
    public $jeudi;
    public $vendredi;
    public $samedi;
    public $dimanche;
    public $periodicite_semaine;
    public $produits;
    public $paiement_automatique;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['id_etablissement', 'periodicite_semaine', 'id_point_vente'], 'integer'],
            [['date_debut', 'date_fin'], 'date', 'format' => 'php:d/m/Y'],
            [['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'paiement_automatique'], 'boolean'],
            [['id_point_vente', 'id_etablissement', 'date_debut'], 'required', 'message' => 'Champs obligatoire'],
            [['produits', 'id_user', 'username'], 'safe'],
            ['id_user', function ($attribute, $params) {
                    if (!$this->id_user && !strlen($this->username)) {
                        $this->addError($attribute, 'Vous devez sélectionner ou saisir un utilisateur.');
                    }
                }, 'skipOnEmpty' => false],
        ];
    }

    public function attributeLabels() {
        return [
            'id' => 'ID',
            'id_user' => 'Utilisateur',
            'id_etablissement' => 'ID Etablissement',
            'id_point_vente' => 'Point de vente',
            'date_debut' => 'Date de début',
            'date_fin' => 'Date de fin',
            'lundi' => 'Lundi',
            'mardi' => 'Mardi',
            'mercredi' => 'Mercredi',
            'jeudi' => 'Jeudi',
            'vendredi' => 'Vendredi',
            'samedi' => 'Samedi',
            'dimanche' => 'Dimanche',
            'periodicite_semaine' => 'Périodicité (semaines)',
            'username' => 'Nom d\'utilisateur',
            'paiement_automatique' => 'Paiement automatique'
        ];
    }

    public function save() {
        if ($this->id) {
            $commandeauto = CommandeAuto::findOne($this->id);
        } else {
            $commandeauto = new CommandeAuto;
        }

        if ($commandeauto) {
            $commandeauto->id_user = $this->id_user;
            $commandeauto->username = $this->username;
            $commandeauto->id_etablissement = $this->id_etablissement;
            $commandeauto->id_point_vente = $this->id_point_vente;
            $commandeauto->date_debut = date('Y-m-d', strtotime(str_replace('/', '-', $this->date_debut)));
            if (strlen($this->date_fin)) {
                $commandeauto->date_fin = date('Y-m-d', strtotime(str_replace('/', '-', $this->date_fin)));
            }
            $commandeauto->lundi = $this->lundi;
            $commandeauto->mardi = $this->mardi;
            $commandeauto->mercredi = $this->mercredi;
            $commandeauto->jeudi = $this->jeudi;
            $commandeauto->vendredi = $this->vendredi;
            $commandeauto->samedi = $this->samedi;
            $commandeauto->dimanche = $this->dimanche;
            $commandeauto->periodicite_semaine = $this->periodicite_semaine;
            $commandeauto->paiement_automatique = $this->paiement_automatique;

            $commandeauto->save();

            // produits
            if ($this->id) {
                CommandeAutoProduit::deleteAll(['id_commande_auto' => $this->id]);
            }

            foreach ($this->produits as $name_input => $quantite) {
                if ($quantite) {
                    $id_produit = str_replace('produit_', '', $name_input);
                    $commandeauto_produit = new CommandeAutoProduit;
                    $commandeauto_produit->id_commande_auto = $commandeauto->id;
                    $commandeauto_produit->id_produit = $id_produit;
                    $commandeauto_produit->quantite = $quantite;
                    $commandeauto_produit->save();
                }
            }
        }

        return true;
    }

}