<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\Commande */
/* @var $form ActiveForm */
?>
<div class="commande-form">

   <?php $form = ActiveForm::begin([
   			'enableClientScript' => false
    	]);
   ?>
    	
    	<?= $form->field($model, 'id_production')->label('Pour quelle date ?')->hiddenInput() ;  ?>
    	
    	<?php if(isset($model->id)): ?>
				<h2><?php echo date('d/m/Y',strtotime($production->date)); ?></h2>
		<?php endif;?>
				
    	<div id="datepicker-production" <?php if(isset($model->id)): ?>style="display:none"<?php endif; ?>>
	    	<div id="infos-importantes" class="alert alert-warning">
	    		<strong>Jours de fournées :</strong>
	    		<ul>
	    			<li>Mercredi</li>
	    			<li>Jeudi</li>
	    			<li>Vendredi</li>
                                <li>Samedi</li>
	    			<li>Dimanche</li>
	    		</ul>
	    	</div>
	    </div>
	    <br />
	    <div id="dates" style="display:none;">
    	<?php 
    		foreach($jours_production as $id_production => $j) {
    			if($j != '--') {
    				echo '<div><span class="date">'.$j.'</span><span class="id_production">'.$id_production.'</span></div>' ;
    			}
    		}
    	?>
    	</div>
    	
    	<div class="clr"></div>
    	<div id="commandes-en-cours" style="display:none;">
    		<?php foreach($commandes_en_cours as $c): ?>
    			<?php echo '<div class="commande" data-idproduction="'.$c->id_production.'" data-id="'.$c->id.'" data-href="'.Yii::$app->urlManager->createUrl(['commande/update','id'=>$c->id]).'"></div>' ;?>
    		<?php endforeach; ?>
    	</div>
    	<div id="has-commande-en-cours" style="display:none;" class="alert alert-danger">Vous avez déjà une commande en cours pour cette date. <a href="#">Cliquez ici</a> pour la modifier.</div>
    	
    	<div class="clr"></div>
    	
    	<?= $form->field($model, 'id_point_vente')
        		->label('Où souhaitez-vous récupérer votre commande ?')
        		->hiddenInput() ;
        		//->dropDownList($points_vente) ;  ?>
    	    
    	  <div id="info-horaire-retrait-commande" class="alert alert-warning">Les commandes sont disponibles à partir de 11h au plus tard dans les points de vente. Merci de me laisser un commentaire si vous souhaitez récupérer votre commande en début de matinée.</div>
    	  
    	 <input type="hidden" id="livraison" value="<?php if(!is_null($production) && $production->livraison): ?>1<?php else: ?>0<?php endif; ?>" />
    	   
    	 <ul id="points-vente">
    	 <?php 
    	 //$points_vente = PointVente::find()->all() ;
	    	 foreach($points_vente as $pv) {
	    	 	//print_r($pv) ;
	    	 	echo '<li class="point-vente point-vente-'.$pv->id.'" data-vrac="'.(int)$pv->vrac.'" data-pain="'.(int)$pv->pain.'"><div class="contenu">'.
	 	    	 	'<span style="display:none;" class="id">'.$pv->id.'</span>'.
	    	 		'<div class="nom">'.Html::encode($pv->nom).'</div>'.
	    	 		'<div class="adresse">à '.Html::encode($pv->localite).'</div>'.
	    	 		'<div class="horaires">'.
	    	 			'<div class="jour jour-1">'.(strlen($pv->horaires_lundi) ? nl2br(Html::encode($pv->horaires_lundi)) : 'Fermé').'</div>'.
	    	 			'<div class="jour jour-2">'.(strlen($pv->horaires_mardi) ? nl2br(Html::encode($pv->horaires_mardi)) : 'Fermé').'</div>'.
	    	 			'<div class="jour jour-3">'.(strlen($pv->horaires_mercredi) ? nl2br(Html::encode($pv->horaires_mercredi)) : 'Fermé').'</div>'.
	    	 			'<div class="jour jour-4">'.(strlen($pv->horaires_jeudi) ? nl2br(Html::encode($pv->horaires_jeudi)) : 'Fermé').'</div>'.
	    	 			'<div class="jour jour-5">'.(strlen($pv->horaires_vendredi) ? nl2br(Html::encode($pv->horaires_vendredi)) : 'Fermé').'</div>'.
	    	 			'<div class="jour jour-6">'.(strlen($pv->horaires_samedi) ? nl2br(Html::encode($pv->horaires_samedi)) : 'Fermé').'</div>'.
	    	 			'<div class="jour jour-0">'.(strlen($pv->horaires_dimanche) ? nl2br(Html::encode($pv->horaires_dimanche)) : 'Fermé').'</div>'.
	    	 		'</div>'.
	    	 	'</div></li>' ;	
	    	 }
    	 ?>
    	 </ul>
    	 <div class="clr"></div>
    	
		<div id="produits">
		<strong id="label-produits">Quels produits désirez-vous ?</strong>
		
		<?php // confiance ?>
		<input type="hidden" id="confiance" value="<?php echo (int) Yii::$app->user->identity->confiance ;?>"  />
		<?php if(!Yii::$app->user->identity->confiance): ?>
			<div id="mess-limit-quantity" class="alert alert-warning"><strong>Attention,</strong> vous ne pouvez commander que 3 produits.</div>
		<?php endif;?>
		
		<?php // erreur ?>
		<?php if(Yii::$app->session->getFlash('error')): ?>
			<div class="alert alert-danger"><div class="icon"></div><?= Yii::$app->session->getFlash('error'); ?></div>
		<?php endif; ?>
		
		<div id="pain">
		<h2>Pain</h2>
		<div class="alert alert-warning indisponible">Pain indisponible pour ce point de vente</div>
    	<table class="table table-bordered" id="table-produits">
    		<thead>
    			<tr>
    				<th class="produit">Produit</th>
    				<th class="prix-unit">Prix unitaire</th>
    				<th class="colonne-quantite">Quantité</th>
    				<th class="total">Total</th>
    			</tr>
    		</thead>
    		<tbody>
    			<?php foreach($produits as $p): ?>
    			<tr class="produit-<?php echo $p->id; ?>" <?php if(count($produits_dispos) && !$produits_dispos[$p->id]['actif']): ?>style="display:none;"<?php endif;?>>
    				<td class="produit">
    					<?php if(strlen($p->photo)): ?><br /><a href="<?= Yii::$app->urlManager->getBaseUrl().'/uploads/'.$p->photo ?>" data-lightbox="produit-<?php echo $p->id; ?>"><img class="photo img-rounded img-thumbnail" src="<?= Yii::$app->urlManager->getBaseUrl().'/uploads/'.$p->photo ?>" alt="Photo <?= Html::encode($p->nom); ?>" /></a><?php endif; ?>
    					<?php if(strlen($p->illustration)): ?><img class="illu" src="<?= Yii::$app->urlManager->getBaseUrl().'/uploads/'.$p->illustration ?>" alt="illustration <?= Html::encode($p->nom); ?>" /><?php endif; ?>
    					<span class="nom"><?= Html::encode($p->nom); ?></span> - <span class="description"><?= Html::encode($p->getDescription()); ?></span><br />
    					<span class="recette"><?= Html::encode($p->recette); ?></span>
    				</td>
    				<td class="prix-unit"><span class="prix"><?= number_format($p->prix, 2) ; ?></span> €</td>
    				<td class="colonne-quantite">
    					<div class="input-group">
    						<span class="input-group-btn">
				            	<button type="button" class="btn btn-default move-quantity moins">-</button>
				          	</span>
				          	<?php $quantite = 0 ;
								if(isset($produits_selec[$p->id]))
									$quantite = $produits_selec[$p->id] ;
				          	?>
				          	<input type="text" value="<?php if(isset($produits_selec[$p->id])): echo $produits_selec[$p->id] ; else:?>0<?php endif;?>" readonly name="Produit[produit_<?php echo $p->id ; ?>]" class="quantity form-control">
				          	<span class="input-group-btn">
				            	<button type="button" class="btn btn-default move-quantity plus">+</button>
				          	</span>
				        </div>
				        
				        <div class="quantite-restante">Reste <span class="nb"><?php if(isset($produits_dispos[$p->id])): echo $produits_dispos[$p->id]['quantite_restante'] + $quantite ; endif;?></span> <?php echo Html::encode(strtolower($p->nom)); ?>(s)</div>
				        <div class="epuise">Épuisé</div>
    				</td>
    				<td class="total"><strong></strong></td>
    			</tr>
    			<?php endforeach; ?>
    		</tbody>
    		<tfoot>
    			<tr>
    				<td></td>
    				<td></td>
    				<td></td>
    				<!-- <td><strong><span id="total-commande">0</span> €</strong></td> -->
    				<td id="total-commande"><strong></strong></td>
    			</tr>
    		</tfoot>
    	</table>
    	</div>
    	
    	<div id="vrac">
    	<h2>Vrac</h2>
    	<div class="alert alert-warning indisponible">Vrac indisponible pour ce point de vente</div>
    	<table class="table table-bordered" id="table-produits-vrac">
    		<thead>
    			<tr>
    				<th>Produit</th>
    				<th class="prix-unit">Prix au kg</th>
    				<th class="colonne-quantite">Quantité (en g)</th>
    				<th class="total">Total</th>
    			</tr>
    		</thead>
    		<tbody>
    			<?php foreach($produits_vrac as $p): ?>
    			<tr class="vrac produit-<?php echo $p->id; ?>" <?php if(count($produits_dispos) && !$produits_dispos[$p->id]['actif']): ?>style="display:none;"<?php endif;?>>
    				<td class="produit">
    					<?php if(strlen($p->photo)): ?><br /><a href="<?= Yii::$app->urlManager->getBaseUrl().'/uploads/'.$p->photo ?>" data-lightbox="produit-<?php echo $p->id; ?>"><img class="photo img-rounded img-thumbnail" src="<?= Yii::$app->urlManager->getBaseUrl().'/uploads/'.$p->photo ?>" alt="Photo <?= Html::encode($p->nom); ?>" /></a><?php endif; ?>
    					<span class="nom"><?= Html::encode($p->nom); ?></span><br />
    					<span class="recette"><?= Html::encode($p->description); ?></span>
    				</td>
    				<td class="prix-unit"><span class="prix"><?= number_format($p->prix, 2) ; ?></span> €</td>
    				<td class="colonne-quantite">
    					<div class="input-group">
    						<span class="input-group-btn">
				            	<button type="button" class="btn btn-default move-quantity moins">-</button>
				          	</span>
				          	<?php $quantite = 0 ;
								if(isset($produits_selec[$p->id]))
									$quantite = $produits_selec[$p->id] ;
				          	?>
				          	<input type="text" value="<?php if(isset($produits_selec[$p->id])): echo $produits_selec[$p->id] ; else:?>0<?php endif;?>" readonly name="Produit[produit_<?php echo $p->id ; ?>]" class="quantity form-control">
				          	<span class="input-group-btn">
				            	<button type="button" class="btn btn-default move-quantity plus">+</button>
				          	</span>
				        </div>
				        
				        <div class="epuise">Épuisé</div>
    				</td>
    				<td class="total"><strong></strong></td>
    			</tr>
    			<?php endforeach; ?>
    		</tbody>
    		<tfoot>
    			<tr>
    				<td></td>
    				<td></td>
    				<td></td>
    				<!-- <td><strong><span id="total-commande">0</span> €</strong></td> -->
    				<td id="total-commande-vrac"><strong></strong></td>
    			</tr>
    		</tfoot>
    	</table>
    	</div>
    	
    	</div>
    	
    
        <div id="bar-fixed">
            <div class="container">
        	<a href="<?php echo Yii::$app->urlManager->createUrl('commande/index') ?>" class="btn btn-default btn-retour">Retour</a>
        	<?php if(isset($model->id)): ?>
        		<a href="<?php echo Yii::$app->urlManager->createUrl(['commande/annuler','id'=> $model->id]) ; ?>" class="btn btn-danger annuler-commande">Annuler ma commande</a>
        	<?php endif; ?>
        	
                        
                <?= Html::submitButton('Valider ma commande', ['class' => 'btn btn-primary valider-commande']) ?>
                <?= Html::submitButton('<span class="glyphicon glyphicon-comment"></span>', ['class' => 'btn btn-default btn-commentaire', 'data-placement'=>'top', 'data-toggle'=>'tooltip', 'data-original-title'=>'Ajouter un commentaire']) ?>
                        
                <span id="total-commande-bottom"><span></span> €</span>
                <div class="clr"></div>
                
                <?= $form->field($model, 'commentaire')->textarea(['rows'=>3,'placeholder'=>'Un commentaire ?'])->label('') ;  ?>
            </div>
        </div>
        
    <?php ActiveForm::end(); ?>
 
</div><!-- commande-form -->