<?php
    require_once "config/validation.php";

    if (isset($_POST["btnSubmit"]))			// save product
    {
        $errors  = array();
		$rules   = array();
        $rules[] = "required,product_stock_brand,Veuillez selectionner la marque du disque.";
        $rules[] = "required,product_stock_model,Veuillez selectionner le modèle du disque.";
        $rules[] = "required,product_stock_interface,Veuillez selectionner l'interface du disque.";
        $rules[] = "required,product_stock_format,Veuillez selectionner le format du disque.";
        $rules[] = "required,product_stock_capacity,Veuillez entrer la capacité du disque en Go.";
        $rules[] = "required,product_stock_sn,Veuillez entrer le SN du disque.";
        $rules[] = "required,product_stock_parameters,Veuillez entrer les paramètres du disque.";
        $rules[] = "required,product_stock_parts,Veuillez selectionner les pièces du disque.";
        $rules[] = "required,product_stock_qty,Veuillez entrer la quantité de disques.";
        
        $errors  = validateFields($_POST, $rules);

        if (empty($errors))			// pas d'erreurs
        {       
			$product_stock_id 			= $_POST["product_stock_id"];
			$product_stock_brand 		= $_POST["product_stock_brand"];
			$product_stock_model 		= $_POST["product_stock_model"];
			$product_stock_interface 	= $_POST["product_stock_interface"];
			$product_stock_format 		= $_POST["product_stock_format"];
			$product_stock_capacity 	= addslashes(trim($_POST["product_stock_capacity"]));
			$product_stock_rpm 			= $_POST["product_stock_rpm"];
			$product_stock_sn 			= addslashes($_POST["product_stock_sn"]);
			$product_stock_parameters 	= addslashes(trim($_POST["product_stock_parameters"]));
			$product_stock_parts 		= $_POST["product_stock_parts"];
			$product_stock_qty 			= addslashes(trim($_POST["product_stock_qty"]));
			$product_stock_pprice 		= addslashes(trim($_POST["product_stock_pprice"]));
			$product_stock_remark 		= addslashes(trim($_POST["product_stock_remark"]));
	
			if (empty($product_stock_id))			// ajout d'un nouveau produit
			{
				if (isduplicate("product_stock", array("product_stock_sn" => $product_stock_sn)))
				{
					$errors[] = "Un produit avec le même SN existe déjà!";
				}
				else
				{	
					$data_array = array(
						"product_stock_brand" => $product_stock_brand,
						"product_stock_model" => $product_stock_model,
						"product_stock_interface" => $product_stock_interface,
						"product_stock_format" => $product_stock_format,
						"product_stock_capacity" => $product_stock_capacity,
						"product_stock_rpm" => $product_stock_rpm,
						"product_stock_sn" => $product_stock_sn,
						"product_stock_parameters" => $product_stock_parameters,
						"product_stock_parts" => $product_stock_parts,
						"product_stock_qty" => $product_stock_qty,    
						"product_stock_pprice" => $product_stock_pprice,   
						"product_stock_remark" => $product_stock_remark 
					);
		
					$sql = db_makequery("product_stock", $data_array);
					if (!mysql_query($sql))
					{
						queryerror($sql);
						exit;
					}
		
					$strResult = "Produit ajouté avec succès.";
					$strResult = base64_encode($strResult);
					redirect("./?p=stock&strResult=$strResult");
					exit;
				}
			}
			else			// Modification du produit
			{
				if (isduplicate("product_stock", array("product_stock_sn" => $product_stock_sn), array("product_stock_id" => $product_stock_id)) )
				{
					$errors[] = "Un produit avec le même SN existe déjà!";
				}
				else
				{
					$data_array = array(
						"product_stock_brand" => $product_stock_brand,
						"product_stock_model" => $product_stock_model,
						"product_stock_interface" => $product_stock_interface,
						"product_stock_format" => $product_stock_format,
						"product_stock_capacity" => $product_stock_capacity,
						"product_stock_rpm" => $product_stock_rpm,
						"product_stock_sn" => $product_stock_sn,
						"product_stock_parameters" => $product_stock_parameters,
						"product_stock_parts" => $product_stock_parts,
						"product_stock_qty" => $product_stock_qty,    
						"product_stock_pprice" => $product_stock_pprice,   
						"product_stock_remark" => $product_stock_remark 
					);
		
					$param = " product_stock_id = ".$product_stock_id;
					
					$sql = db_makequery("product_stock", $data_array, "update", $param);
					if (!mysql_query($sql))
					{
						queryerror($sql);
						exit;
					}
		
					$strResult = "Produit modifié avec succès.";
					$strResult = base64_encode($strResult);
					redirect("./?p=stock&strResult=$strResult");
					exit;
				}
			}			    
		}
    }
	
    if (!empty($_REQUEST["product_stock_id"]))		// produit existant
    {
        $form_caption = "Modifier un produit";

        $sql = "SELECT * FROM product_stock WHERE product_stock_id = ".$_REQUEST["product_stock_id"];

        $rs = mysql_query($sql);
        $row = mysql_fetch_array($rs);

        $product_stock_id 			= $row["product_stock_id"];
        $product_stock_brand 		= stripslashes($row["product_stock_brand"]);
        $product_stock_format 		= stripslashes($row["product_stock_format"]);
        $product_stock_rpm 			= stripslashes($row["product_stock_rpm"]);
        $product_stock_interface 	= stripslashes($row["product_stock_interface"]);
        $product_stock_capacity 	= stripslashes($row["product_stock_capacity"]);
        $product_stock_model 		= stripslashes($row["product_stock_model"]);
        $product_stock_sn 			= stripslashes($row["product_stock_sn"]);
        $product_stock_parameters 	= stripslashes($row["product_stock_parameters"]);
        $product_stock_parts 		= stripslashes($row["product_stock_parts"]);
        $product_stock_qty 			= stripslashes($row["product_stock_qty"]);
        $product_stock_pprice 		= stripslashes($row["product_stock_pprice"]);
        $product_stock_remark 		= stripslashes($row["product_stock_remark"]);

        mysql_free_result($rs);
    }
	else
	{
	    $form_caption = "Ajouter un nouveau produit";
		
		$product_stock_id 			= 0;
        $product_stock_brand 		= "WESTERN DIGITAL";
        $product_stock_format 		= "2.5\"";
        $product_stock_rpm 			= "5400";
        $product_stock_interface 	= "SATA";
        $product_stock_capacity 	= "";
        $product_stock_model 		= "";
        $product_stock_sn 			= "";
        $product_stock_parameters 	= "";
        $product_stock_parts 		= "HDD";
        $product_stock_qty 			= 1;
        $product_stock_pprice 		= "";
        $product_stock_remark 		= "";
	
	}
?>
<!-- BEGIN CONTAINER -->
<div class="page-container">
    <!-- BEGIN CONTENT -->
    <div class="page-content-wrapper">
        <div class="page-content">	
        	<!-- BEGIN PAGE HEADER-->
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <h3 class="page-title">Gestion du stock</h3>					
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <!-- END PAGE HEADER-->			
            <!-- BEGIN PAGE CONTENT-->
            <div class="row">
                <div class="col-md-12">
                    <?php
                        /*if (!empty($_GET["strResult"]))
                        {
                            ?>
                            <div class="alert alert-success alert-dismissable"><span><?php echo base64_decode($_GET["strResult"]); ?></span></div>
                            <?php
                        }*/
                    
                        if (!empty($errors))
                        {
                            ?>  
                            <div class="alert alert-danger"><span><?php PrintValidateErrors($errors); ?></span></div>
                            <?php
                        }
                    ?>

                    <form id="product-form" class="form-horizontal form-row-seperated" method="post" action="">
                        <input type="hidden" name="product_stock_id" id="product_stock_id" value="<? echo $product_stock_id; ?>" />

                        <div class="portlet box green">                        	
                            <div class="portlet-title">
                                <div class="caption"><?php echo $form_caption; ?></div>
                                <div class="actions btn-set">
                                    <button type="submit" name="btnSubmit" id="btnSubmit" class="btn green"><i class="fa fa-check"></i> Sauvegarder</button>
                                    <a href="./?p=stock" class="btn red filter-cancel"><i class="fa fa-times"></i> Annuler</a>                                    
                                </div>
                            </div>
                            <div class="portlet-body">
                                <div class="tabbable">
                                    <ul class="nav nav-tabs">
                                        <li class="active">
                                            <a href="#tab_general" data-toggle="tab">General</a>
                                        </li>
                                        <li>
                                            <a href="#tab_remark" data-toggle="tab">Notes</a>
                                        </li>
                                    </ul>
                                    <div class="tab-content no-space">
                                        <div class="tab-pane active" id="tab_general">
                                            <div class="form-body">
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Marque:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("product_stock_brand", $ARRAY_HDD_BRAND, $product_stock_brand, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Modèle:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="text" class="form-control" name="product_stock_model" id="product_stock_model" value="<? echo $product_stock_model; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Interface:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("product_stock_interface", $ARRAY_HDD_INTERFACE, $product_stock_interface, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Format:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("product_stock_format", $ARRAY_HDD_FORMAT, $product_stock_format, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>												
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Capacité (GB):<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="number" class="form-control" name="product_stock_capacity" id="product_stock_capacity" value="<? echo $product_stock_capacity; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">RPM:</label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("product_stock_rpm", $ARRAY_HDD_RPM, $product_stock_rpm, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">SN:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="text" class="form-control" name="product_stock_sn" id="product_stock_sn" value="<? echo $product_stock_sn; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Paramètres:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <textarea class="form-control" rows="4" name="product_stock_parameters" id="product_stock_parameters"><? echo $product_stock_parameters; ?></textarea>
                                                        <span class="help-block">
                                                            Pays: China, Thailand, Indonesia, Malaysia, Korea, Japan, USA, India, Singapore, Philippines
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="tab-pane" id="tab_remark">
                                            <div class="form-body">
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Pièces:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("product_stock_parts", $ARRAY_HDD_PARTS, $product_stock_parts, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Quantité:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="number" class="form-control" name="product_stock_qty" id="product_stock_qty" value="<? echo $product_stock_qty; ?>" placeholder="" />
                                                    </div>
                                                </div>                                                	
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Prix d'achat (DA):</label>
                                                    <div class="col-md-8">
                                                        <input type="number" class="form-control" name="product_stock_pprice" id="product_stock_pprice" value="<? echo $product_stock_pprice; ?>" placeholder="" />
                                                    </div>
                                                </div>	
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Remarque:</label>
                                                    <div class="col-md-8">
                                                        <textarea class="form-control" rows="3" name="product_stock_remark" id="product_stock_remark"><? echo $product_stock_remark; ?></textarea>														
                                                    </div>
                                                </div>										
                                            </div>
                                        </div>																				
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <!-- END PAGE CONTENT-->
        </div>
    </div>
    <!-- END CONTENT -->
</div>
<!-- END CONTAINER -->