<?php
    require_once "config/validation.php";
	
	if (isset($_POST["btnSubmit"]))			// save product			
    {
        $errors  = array();
        $rules   = array();
        $rules[] = "required,case_type,Veuillez selectionner un type.";
        $rules[] = "required,case_hdd_brand,Veuillez selectionner la marque du disque.";
        $rules[] = "required,case_hdd_model,Veuillez selectionner le modèle du disque.";
        $rules[] = "required,case_hdd_interface,Veuillez selectionner l'interface du disque.";
        $rules[] = "required,case_hdd_format,Veuillez selectionner le format du disque.";
        $rules[] = "required,case_hdd_capacity,Veuillez entrer la capacité du disque en Go.";
        $rules[] = "required,case_hdd_sn,Veuillez entrer le SN du disque.";
        $rules[] = "required,case_hdd_parameters,Veuillez entrer les paramètres du disque.";
 
        $errors = validateFields($_POST, $rules);
        if (empty($errors))		// s'il n'y a pas d'erreurs
        {            
			$case_id 					= $_POST["case_id"];
			$case_customer_id 			= $_POST["customer_id"];
	   
			$case_type 					= $_POST["case_type"];
			$case_hdd_brand 			= $_POST["case_hdd_brand"];
			$case_hdd_model 			= addslashes(trim($_POST["case_hdd_model"]));
			$case_hdd_interface 		= $_POST["case_hdd_interface"];
			$case_hdd_format 			= $_POST["case_hdd_format"];
			$case_hdd_capacity 			= addslashes(trim($_POST["case_hdd_capacity"]));
			$case_hdd_rpm 				= $_POST["case_hdd_rpm"];
			$case_hdd_sn 				= addslashes($_POST["case_hdd_sn"]);
			$case_hdd_parameters 		= addslashes(trim($_POST["case_hdd_parameters"]));
			$case_status 				= $_POST["case_status"];
			$case_case_description 		= addslashes(trim($_POST["case_case_description"]));
			$case_solution_description 	= addslashes(trim($_POST["case_solution_description"]));
			$case_earned 				= addslashes(trim($_POST["case_earned"]));
			$case_spent 				= addslashes(trim($_POST["case_spent"]));
			$case_remark 				= addslashes(trim($_POST["case_remark"]));

			if (empty($case_id))			// un ajout
			{
				$data_array = array(
					"case_customer_id" => $case_customer_id,                
					"case_type" => $case_type,
					"case_hdd_brand" => $case_hdd_brand,
					"case_hdd_model" => $case_hdd_model,
					"case_hdd_interface" => $case_hdd_interface,
					"case_hdd_format" => $case_hdd_format,
					"case_hdd_capacity" => $case_hdd_capacity,
					"case_hdd_rpm" => $case_hdd_rpm,
					"case_hdd_sn" => $case_hdd_sn,
					"case_hdd_parameters" => $case_hdd_parameters,
					"case_date_open" => time(),
					"case_status" => "Open",
					"case_case_description" => $case_case_description,    
					"case_solution_description" => $case_solution_description,   
					"case_earned" => $case_earned, 
					"case_spent" => $case_spent, 
					"case_remark" => $case_remark 
				);
	
				$sql = db_makequery("`case`", $data_array);
				if (!mysql_query($sql))
				{
					queryerror($sql);
					exit;
				}
				
				// MAJ REF
				$case_id 		= getrecentlyinsertedid("`case`", "case_id");				
				$case_reference = GetCaseReference($case_type, $case_id, $case_customer_id);
					
				$data_array 	= array("case_reference" => $case_reference);				
				$param 			= " case_id =".$case_id;
				
				$sql = db_makequery("`case`", $data_array, "update", $param);		
				if (!mysql_query($sql))
				{
					queryerror($sql);
					exit;
				}

				$strResult = "Case ajoutée avec succès.";
				$strResult = base64_encode($strResult);				
			}
			else		// Modification
			{
				$data_array = array(
					"case_customer_id" => $case_customer_id,
					"case_type" => $case_type,
					"case_hdd_brand" => $case_hdd_brand,
					"case_hdd_model" => $case_hdd_model,
					"case_hdd_interface" => $case_hdd_interface,
					"case_hdd_format" => $case_hdd_format,
					"case_hdd_capacity" => $case_hdd_capacity,
					"case_hdd_rpm" => $case_hdd_rpm,
					"case_hdd_sn" => $case_hdd_sn,
					"case_hdd_parameters" => $case_hdd_parameters,
					"case_date_last_modif" => time(),
					"case_status" => $case_status,
					"case_case_description" => $case_case_description,    
					"case_solution_description" => $case_solution_description,   
					"case_earned" => $case_earned, 
					"case_spent" => $case_spent, 
					"case_remark" => $case_remark 
				);
				
				// MAJ Date Close
				if($case_status=="Resolved" || $case_status=="Not resolved")	$data_array["case_date_close"] = time();
				elseif($case_status=="Open")									$data_array["case_date_close"] = NULL; 
				
				$param = " case_id =".$case_id;
				
				$sql = db_makequery("`case`", $data_array, "update", $param);
								
				if (!mysql_query($sql))
				{
					queryerror($sql);
					exit;
				}
				
				// MAJ REF
				$case_reference = GetCaseReference($case_type, $case_id, $case_customer_id);
					
				$data_array 	= array("case_reference" => $case_reference);				
				$param 			= " case_id =".$case_id;
				
				$sql = db_makequery("`case`", $data_array, "update", $param);						
				if (!mysql_query($sql))
				{
					queryerror($sql);
					exit;
				}
				
				$strResult = "Case modifiée avec succès.";
				$strResult = base64_encode($strResult);
			}
			
			///////////////////	
			$tmp_sql_ 		= "SELECT * FROM case_file WHERE case_id=". $case_id;	//rechercher le nombre d'img pour incrementer le ID des images 
			$tmp_res_ 		= mysql_query($tmp_sql_);
			$case_file_no 	= mysql_num_rows($tmp_res_)+1;

			$case_files = trim($_POST["case_files"]);
			if(!empty($case_files))
			{
				$arr_case_files = explode("|", $case_files);
				if(is_array($arr_case_files))
				{
					foreach($arr_case_files as $case_file)
					{
						if(!empty($case_file))
						{
                            $oldname 	= $case_file;
                            $ext 		= pathinfo($oldname, PATHINFO_EXTENSION);
                            $newname 	= $case_reference."_".$case_file_no.".".$ext;
                             
                            rename(CASE_FILES_UPLOAD_DIR."/".$oldname, CASE_FILES_UPLOAD_DIR."/".$newname);
                            
							$data_array = array(
								"case_id" => $case_id,                
								"case_file_path" => $newname
							);
                            
							$sql = db_makequery("case_file", $data_array);
							if (!mysql_query($sql))
							{
								queryerror($sql);
								exit;
							}
                            
                            $case_file_no = $case_file_no + 1;
						}
					}
				}
			}	// end add files
			
			redirect("./?p=case&customer_id=$case_customer_id&strResult=$strResult");
			exit;	
		}
    }
		
    $customer_id = $_GET["customer_id"];

    if (!empty($_REQUEST["case_id"]))		// case existante
    {
        $form_caption = "Modifier une case";

        $sql = "SELECT * FROM `case` WHERE case_id =".$_REQUEST["case_id"];

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

        $case_id 					= $row["case_id"];
        $case_reference 			= stripslashes($row["case_reference"]);
        $case_type 					= stripslashes($row["case_type"]);
        $case_hdd_brand 			= stripslashes($row["case_hdd_brand"]);
        $case_hdd_format 			= stripslashes($row["case_hdd_format"]);
        $case_hdd_rpm 				= stripslashes($row["case_hdd_rpm"]);
        $case_hdd_interface 		= stripslashes($row["case_hdd_interface"]);
        $case_hdd_capacity 			= stripslashes($row["case_hdd_capacity"]);
        $case_hdd_model 			= stripslashes($row["case_hdd_model"]);
        $case_hdd_sn 				= stripslashes($row["case_hdd_sn"]);
        $case_hdd_parameters 		= stripslashes($row["case_hdd_parameters"]);
        $case_case_description 		= stripslashes($row["case_case_description"]);
        $case_solution_description 	= stripslashes($row["case_solution_description"]);
        $case_earned 				= stripslashes($row["case_earned"]);
        $case_spent 				= stripslashes($row["case_spent"]);
        $case_date_open 			= date("d/m/Y H:i:s", $row["case_date_open"]);
        $case_date_last_modif 		= $row["case_date_last_modif"];
		$case_date_close 			= $row["case_date_close"];
        $case_status 				= stripslashes($row["case_status"]);
        $case_remark 				= stripslashes($row["case_remark"]);

		if ($case_date_last_modif == NULL)		$case_date_last_modif 	= "/";
        else									$case_date_last_modif 	= date("d/m/Y H:i:s", $case_date_last_modif);
        
        if ($case_date_close == NULL)			$case_date_close = "/";
        else									$case_date_close = date("d/m/Y H:i:s", $case_date_close);
        
        mysql_free_result($rs);
    }
	else		// nouvelle case
	{
		$form_caption 				= "Ajouter une nouvelle case";	
		
		$case_id 					= 0;
        $case_reference 			= "";
        $case_type 					= "HDD";
        $case_hdd_brand 			= "WESTERN DIGITAL";
        $case_hdd_format 			= "2.5\"";
        $case_hdd_rpm 				= 5400;
        $case_hdd_interface 		= "SATA";
        $case_hdd_capacity 			= "";
        $case_hdd_model 			= "";
        $case_hdd_sn 				= "";
        $case_hdd_parameters 		= "";
        $case_case_description 		= "";
        $case_solution_description 	= "";
        $case_earned 				= 0;
        $case_spent 				= 0;
        $case_date_open 			= "/";
        $case_date_last_modif 		= "/";
        $case_date_close 			= "/";
        $case_status 				= "Open";
        $case_remark 				= "";
	}
?>
<style type="text/css">
		.fancybox-custom .fancybox-skin {
			box-shadow: 0 0 50px #222;
		}
	</style>
<link rel="stylesheet" type="text/css" href="assets/plugins/fancybox/source/jquery.fancybox.css?v=2.1.2" media="screen" />
<!-- 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 cases</h3>					
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <!-- END PAGE HEADER-->		
            <!-- BEGIN PAGE CONTENT-->
            <div class="row">
                <div class="col-md-12">
                    <form id="case-form" class="form-horizontal form-row-seperated" method="post" action="">
                        <input type="hidden" name="case_id" id="case_id" value="<? echo $case_id; ?>" />
                        <input type="hidden" name="customer_id" id="customer_id" value="<? echo $customer_id; ?>" />

                        <div class="portlet box green">                                                    
                            <div class="portlet-title">
                                <div class="caption"><?php echo $form_caption; ?> de "<b><?php echo GetCaseOf($_GET["customer_id"])?></b>"</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=case&customer_id=<?php echo $_REQUEST["customer_id"]; ?>" 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_hdd" data-toggle="tab">HDD</a></li>
                                        <li><a href="#tab_case" data-toggle="tab">CASE</a></li>
                                        <li><a href="#tab_images" data-toggle="tab">Photos</a></li>
                                        <li><a href="#tab_earning" data-toggle="tab">Earning</a></li>
                                    </ul>
                                    <div class="tab-content no-space">
                                        <div class="tab-pane active" id="tab_hdd">
                                            <div class="form-body">
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Type:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            createdropdown_array("case_type", $ARRAY_CASE_TYPE, $case_type, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>
                                                <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("case_hdd_brand", $ARRAY_HDD_BRAND, $case_hdd_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="case_hdd_model" id="case_hdd_model" value="<? echo $case_hdd_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("case_hdd_interface", $ARRAY_HDD_INTERFACE, $case_hdd_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("case_hdd_format", $ARRAY_HDD_FORMAT, $case_hdd_format, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>												
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Capacité&nbsp;(GB):<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <input type="number" class="form-control" name="case_hdd_capacity" id="case_hdd_capacity" value="<? echo $case_hdd_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("case_hdd_rpm", $ARRAY_HDD_RPM, $case_hdd_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="case_hdd_sn" id="case_hdd_sn" value="<? echo $case_hdd_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="case_hdd_parameters" id="case_hdd_parameters"><? echo $case_hdd_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_case">                                		
                                            <div class="form-body">
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Statut:<span class="required">*</span></label>
                                                    <div class="col-md-8">
                                                        <?php
                                                            if (empty($case_id))	echo "<label class=\"control-label\">Open</label>";
                                                            else					createdropdown_array("case_status", $ARRAY_CASE_STATUS, $case_status, false, "Select...", "table-group-action-input form-control");
                                                        ?>
                                                    </div>
                                                </div>
                                                 <?												 
                                                if(isset($case_date_open) and $case_date_open!="/")
												{
												?> 
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Date d'ouverture:</label>
                                                    <div class="col-md-8">
                                                        <label class="control-label"><?php echo $case_date_open; ?></label>
                                                    </div>
                                                </div>
                                                <?
												}
												
                                                if(isset($case_date_last_modif) and $case_date_last_modif!="/")
												{
												?>                                                
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Date de la dernière modif.:</label>
                                                    <div class="col-md-8">
                                                        <label class="control-label"><?php echo $case_date_last_modif; ?></label>
                                                    </div>
                                                </div>
                                                <?
												}
																						
                                                if(isset($case_date_close) and $case_date_close!="/")
												{
												?>                                                
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Date de fermeture:</label>
                                                    <div class="col-md-8">
                                                        <label class="control-label"><?php echo $case_date_close; ?></label>
                                                    </div>
                                                </div>
                                                <?
												}
												?>
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Description de la panne:</label>
                                                    <div class="col-md-8">
                                                        <textarea class="form-control" rows="5" name="case_case_description" id="case_case_description"><? echo set_value('case_case_description', empty($case_case_description) ? "" : $case_case_description); ?></textarea>														
                                                    </div>
                                                </div>	
                                                <div class="form-group">
                                                    <label class="col-md-4 control-label">Description de la solution:</label>
                                                    <div class="col-md-8">
                                                        <textarea class="form-control" rows="8" name="case_solution_description" id="case_solution_description"><? echo set_value('case_solution_description', empty($case_solution_description) ? "" : $case_solution_description); ?></textarea>														
                                                    </div>
                                                </div>											
                                            </div>
                                        </div>  
                                        <div class="tab-pane" id="tab_images">
                                            <input type="hidden" id="case_files" name="case_files" value="" />
                                            <div id="tab_images_uploader_container" class="text-align-reverse margin-bottom-10">
                                                <a id="tab_images_uploader_pickfiles" href="javascript:;" class="btn blue"><i class="fa fa-plus"></i> Sélectionner des fichier</a>
                                                <a id="tab_images_uploader_uploadfiles" href="javascript:;" class="btn green"><i class="fa fa-check"></i> Télécharger les fichiers</a>
                                            </div>                                            
                                            <div id="tab_images_uploader_filelist" class="col-md-12"></div>  
											<?php
                                            $sql = "SELECT case_file_path FROM case_file WHERE case_id =".$case_id;
                                            $tmprs = mysql_query($sql);
                                                        
                                            if (!empty($case_id) && mysql_num_rows($tmprs)>0)
                                            {
                                                while ($tmprow = mysql_fetch_array($tmprs))
                                                {
													$case_file_path = CASE_FILES_UPLOAD_DIR."/".$tmprow["case_file_path"];
                                                    ?>
                                                    <div class="case-div" style="position:relative;float:left;padding: 30px 10px 30px 0;">
                                                    	<a class="fancybox" href="<?php echo $case_file_path; ?>" data-fancybox-group="gallery" data-rel="fancybox-button"><img src="<?php echo $case_file_path; ?>" alt="" width="150" height="150" /></a>
                                                    	<div style="position:absolute; bottom:0" align="center">
															<a href="javascript:;" class="btn default btn-sm remove-case-file" rel="<?php echo $tmprow["case_file_path"]; ?>"><i class="fa fa-times"></i> Remove</a>
                                                    	</div>
                                                    </div>                                             
													<?php
                                                }											
                                            }
                                        	?>                                         
                                        </div>	
                                        <div class="tab-pane" id="tab_earning">                                		
                                            <div class="form-body">
                                                <div class="form-group">
                                                    <label class="col-md-2 control-label">Gains:</label>
                                                    <div class="col-md-10">
                                                        <input type="text" class="form-control" name="case_earned" id="case_earned" value="<? echo $case_earned; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-2 control-label">Dépenses:</label>
                                                    <div class="col-md-10">
                                                        <input type="text" class="form-control" name="case_spent" id="case_spent" value="<? echo $case_spent; ?>" placeholder="" />
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-md-2 control-label">Remarques:</label>
                                                    <div class="col-md-10">
                                                        <textarea class="form-control" rows="5" name="case_remark" id="case_remark"><? echo $case_remark; ?></textarea>														
                                                    </div>
                                                </div>										
                                            </div>
                                        </div>																				
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <!-- END PAGE CONTENT-->
        </div>
    </div>
    <!-- END CONTENT -->
</div>
<!-- END CONTAINER -->
