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

	if (isset($_POST["btnSubmit"]))					// Save customer 
    {
		$errors  = array();
        $rules   = array();
        $rules[] = "required,customer_type,Veuillez choisir un Type.";
        $rules[] = "required,customer_lastname,Veuillez entrer le Nom du client.";
        $rules[] = "required,customer_firstname,Veuillez entrer le Prénom du client.";
        $rules[] = "required,customer_tel,Veuillez entrer le N° Tel du client.";
        $rules[] = "required,customer_email,Veuillez entrer l'adresse email du client.";
        $rules[] = "valid_email,customer_email,Veuillez entrer une adresse email valide.";
		
        $errors  = validateFields($_POST, $rules);
		if(empty($errors))		// s'il n'y a pas d'erreurs
		{
			$customer_id 			= $_POST["customer_id"];
			$customer_type 			= $_POST["customer_type"];
			$customer_firstname 	= ucfirst(addslashes(trim($_POST["customer_firstname"])));
			$customer_lastname 		= strtoupper(addslashes(trim($_POST["customer_lastname"])));
			$customer_company 		= addslashes(trim($_POST["customer_company"]));
			$customer_tel 			= addslashes(trim($_POST["customer_tel"]));
			$customer_email 		= addslashes(trim($_POST["customer_email"]));
			$customer_find_us 		= $_POST["customer_find_us"];
			$customer_remark 		= addslashes(trim($_POST["customer_remark"]));
	
			if (empty($customer_id))			// un ajout
			{
				if (isduplicate("customer", array("customer_firstname" => $customer_firstname, "customer_lastname" => $customer_lastname,"customer_email" => $customer_email)))
				{
					$errors[] = "Ce client existe déjà!";
				}
				else
				{
					$data_array = array(
						"customer_type" => $customer_type,
						"customer_firstname" => $customer_firstname,
						"customer_lastname" => $customer_lastname,
						"customer_company" => $customer_company,
						"customer_tel" => $customer_tel,
						"customer_email" => $customer_email,
						"customer_find_us" => $customer_find_us,
						"customer_remark" => $customer_remark
					);
		
					$sql = db_makequery("customer", $data_array);
					if (!mysql_query($sql))
					{
						queryerror($sql);
						exit;
					}
		
					$strResult = "Client ajouté avec succès.";	
					$strResult = base64_encode($strResult);
					redirect("./?p=customer&strResult=$strResult");
					exit;				
				}
			}
			else				// une modification
			{
				if (isduplicate("customer", array("customer_firstname" => $customer_firstname, "customer_lastname" => $customer_lastname, "customer_email" => $customer_email), array("customer_id" => $customer_id)))
				{
					$errors[] = "Ce Client existe déjà!";
				}
				else
				{	
					$data_array = array(
						"customer_type" => $customer_type,
						"customer_firstname" => $customer_firstname,
						"customer_lastname" => $customer_lastname,
						"customer_company" => $customer_company,
						"customer_tel" => $customer_tel,
						"customer_email" => $customer_email,
						"customer_find_us" => $customer_find_us,
						"customer_remark" => $customer_remark
					);
		
					$param = " customer_id = ".$customer_id;
					
					$sql = db_makequery("customer", $data_array, "update", $param);
					if (!mysql_query($sql))
					{
						queryerror($sql);
						exit;
					}
		
					$strResult = "Client modifié avec succès.";
					$strResult = base64_encode($strResult);
					redirect("./?p=customer&strResult=$strResult");
					exit;
				}
			}						
		}	// end error validation
    }	// end Save customer     
	
    if (!empty($_REQUEST["customer_id"]))		// client existant
    {
        $form_caption = "Modifier un client";

        $sql = "SELECT * FROM customer WHERE customer_id = ".$_REQUEST["customer_id"];

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

        $customer_id 		= $row["customer_id"];
        $customer_type 		= $row["customer_type"];
        $customer_lastname 	= stripslashes($row["customer_lastname"]);
        $customer_firstname = stripslashes($row["customer_firstname"]);
        $customer_company 	= stripslashes($row["customer_company"]);
        $customer_tel 		= stripslashes($row["customer_tel"]);
        $customer_email 	= stripslashes($row["customer_email"]);
        $customer_find_us 	= $row["customer_find_us"];
        $customer_remark 	= stripslashes($row["customer_remark"]);

        mysql_free_result($rs);
    }
	else	// nouveau client
	{
		$form_caption 		= "Ajouter un nouveau client";
		
		$customer_id	 	= 0;
		$customer_type 		= "Particulier";
        $customer_lastname 	= "";
		$customer_firstname = "";        
        $customer_company 	= "";
        $customer_tel 		= "";
        $customer_email 	= "";
        $customer_find_us 	= "Google";
        $customer_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 des clients</h3>					
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <!-- END PAGE HEADER-->		
            <!-- BEGIN PAGE CONTENT-->
            <div class="row">
                <div class="col-md-12">
                    <?php
                        if (!empty($errors))
                        {
                            ?>
                            <div class="alert alert-danger"><span><?php PrintValidateErrors($errors); ?></span></div>
                            <?php
                        }
                    ?>

                    <form id="customer-form" class="form-horizontal form-row-seperated" method="post" action="">
                        <input type="hidden" name="customer_id" id="customer_id" value="<? echo $customer_id;?>" />
                        <div class="portlet box green">
                            <div class="portlet-title">
                            <i class="fa fa-remove"></i>
                                <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=customer" 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_remarque" data-toggle="tab">Remarques</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">Type:</label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("customer_type", $ARRAY_CUSTOMER_TYPE, $customer_type, false, "Select...","table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Nom:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="text" class="form-control" name="customer_lastname" id="customer_lastname" value="<? echo $customer_lastname; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Prénom:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="text" class="form-control" name="customer_firstname" id="customer_firstname" value="<? echo $customer_firstname; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Société:</label>
                                                    <div class="col-md-8">
                                                        <input type="text" class="form-control" name="customer_company" id="customer_company" value="<? echo $customer_company; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Tel.:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="tel" class="form-control" name="customer_tel" id="customer_tel" value="<? echo $customer_tel; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Email:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="email" class="form-control" name="customer_email" id="customer_email" value="<? echo $customer_email; ?>" placeholder="" />
                                                    </div>
                                                </div>                                                
                                            </div>
                                        </div>                                        																			
                                        
                                        <div class="tab-pane" id="tab_remarque">                                		
                                            <div class="form-body">
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label form-inline">Comment vous nous avez trouvé? :</label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("customer_find_us", $ARRAY_CUSTOMER_FIND_US, $customer_find_us, false, "Select...","table-group-action-input form-control");
                                                        ?>
                                                    </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="customer_remark" id="customer_remark"><? echo $customer_remark; ?></textarea>														
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <!-- END PAGE CONTENT-->
        </div>
    </div>
    <!-- END CONTENT -->
</div>
<!-- END CONTAINER -->