<?php


// ini_set('display_errors', 1);

/**
 * Template of the module listing
 */
debug_backtrace() || die ("Direct access not permitted");
 
// Action to perform
$action = (isset($_GET['action'])) ? htmlentities($_GET['action'], ENT_QUOTES, "UTF-8") : "";

// Item ID
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? $_GET['id'] : 0;

// Items per page
if(isset($_GET['limit']) && is_numeric($_GET['limit'])) $limit = $_GET['limit'];
elseif(isset($_SESSION['limit']) && isset($_SESSION['module_referer']) && $_SESSION['module_referer'] == MODULE) $limit = $_SESSION['limit'];
else $limit = 100;

$_SESSION['limit'] = $limit;

// current page
if(isset($_GET['offset']) && is_numeric($_GET['offset'])) $offset = $_GET['offset'];
elseif(isset($_SESSION['offset']) && isset($_SESSION['module_referer']) && $_SESSION['module_referer'] == MODULE) $offset = $_SESSION['offset'];
else $offset = 0;

$_SESSION['offset'] = $offset;

// Inclusions
require_once(SYSBASE.ADMIN_FOLDER."/includes/fn_list.php");

if($db !== false){
    // Initializations
    $cols = getCols();
    $filters = getFilters($db);
    if(is_null($cols)) $cols = array();
    if(is_null($filters)) $filters = array();
    $total = 0;
    $total_page = 0;
    $q_search = "";
    $result_lang = false;
    $total_lang = 1;
    $result = false;
    $referer = DIR."index.php?view=list";

    // Sort order
    if(isset($_GET['order'])) $order = htmlentities($_GET['order'], ENT_QUOTES, "UTF-8");
    elseif(isset($_SESSION['order']) && $_SESSION['order'] != "" && isset($_SESSION['module_referer']) && $_SESSION['module_referer'] == MODULE) $order = $_SESSION['order'];
    else $order = getOrder();

    if(isset($_GET['sort'])) $sort = htmlentities($_GET['sort'], ENT_QUOTES, "UTF-8");
    elseif(isset($_SESSION['sort']) && $_SESSION['sort'] != "" && isset($_SESSION['module_referer']) && $_SESSION['module_referer'] == MODULE) $sort = $_SESSION['sort'];
    else $sort = "asc";

    if(strpos($order, " ") !== false){
        $sort = strtolower(substr($order, strpos($order, " ")+1));
        $order = substr($order, 0, strpos($order, " "));
    }

    $_SESSION['order'] = $order;
    $_SESSION['sort'] = $sort;

    $rsort = ($sort == "asc") ? "desc" : "asc";

	// echo "sort >>> ".$rsort;

    // Getting languages
    if(MULTILINGUAL){
        $result_lang = $db->query("SELECT id, title FROM pm_lang WHERE id != ".DEFAULT_LANG." AND checked = 1");
        if($result_lang !== false)
            $total_lang = $db->last_row_count();
    }

    // Getting filters values
    if(isset($_SESSION['module_referer']) && $_SESSION['module_referer'] !== MODULE){
        unset($_SESSION['filters']);
        unset($_SESSION['q_search']);
    }
    if(isset($_POST['search'])){
        foreach($filters as $filter){
            $fieldName = $filter->getName();
            $value = (isset($_POST[$fieldName])) ? htmlentities($_POST[$fieldName], ENT_QUOTES, "UTF-8") : "";
            $filter->setValue($value);
        }
        $q_search = htmlentities($_POST['q_search'], ENT_QUOTES, "UTF-8");
        $_SESSION['filters'] = serialize($filters);
        $_SESSION['q_search'] = $q_search;
    }else{
        if(isset($_SESSION['filters'])) $filters = unserialize($_SESSION['filters']);
        if(isset($_SESSION['q_search'])) $q_search = $_SESSION['q_search'];
    }

    // Getting items in the database
    $condition = "";

    if(MULTILINGUAL) $condition .= " lang = ".DEFAULT_LANG;
	
	$module = array('bl','bc','bf','be','br','facture_achat','facture_avoir','brv','facture_avoir_vente','transaction','facture');
	if (in_array(MODULE, $module))
	{
		// $get_mois  = $_GET['mois'] ;
		// $get_annee = $_GET['annee'] ;
		
		// $from_date = $_GET['from'] ;
		// $to_date = $_GET['to'] ;
		
		$from_date = $_GET['from'] ?? '';
		$to_date = $_GET['to'] ?? '';


		if($from_date == ""){
			// $from_date = date("d-m-Y");
			// $to_date   =  date("d-m-Y");
			switch (MODULE) {
				case "transaction":
					$from_date = date("d-m-Y");
					$to_date   =  date("d-m-Y");
					break;				
				default:
					$from_date = date("01-01-Y");
					$to_date   =  date("30-12-Y");
			}	
			
		}
		
		$to_date = strtotime(date("d-m-Y", strtotime($to_date)) . " +1 day");
		
		// if($get_mois == ""){
			// $get_mois  = date("m") ;
			// $get_annee = date("Y") ;
		// }
		
		
		
		
		// if(($get_mois <> "") && ($get_annee <> "") && (($get_mois <> "tout") || ($get_annee <> "tout")) ){
		if(($from_date <> "") && ($to_date <> "")){
			$limit = 5000;
			$_SESSION['limit'] = $limit;
			// $from_date = strtotime("01-".$get_mois."-".$get_annee);
			// $to_date = strtotime('last day of this month', $from_date);
			
			$from_date = strtotime($from_date);
			$to_date = $to_date;
			
			// $offset = 5000;
			$other_condition = "";
			
			switch (MODULE) {
				case "bc":
					$datoo = " date_livraison" ;
					$order = "valider";
					break;
				case "be":
					$datoo = " date_entre" ;
					break;
				case "br":
					$datoo = " date_entre" ;
					break;
				case "facture_achat":
					$datoo = " date_entre" ;
					break;
				case "facture_avoir":
					$datoo = " date_avoir" ;
					break;
				case "facture_avoir_vente":
					$datoo = " date_facture" ;
					break;	
				case "transaction":
					$datoo = " add_date" ;
					$order = "id";
					break;	
				case "facture":
					$datoo = " date_facture" ;
					break;	
					
				default:
					$datoo = " date_".MODULE ;
			}	
			
			
			if(MODULE <> "bc"){
				$condition .= " ".$datoo." >=".$from_date." AND ".$datoo." < ".$to_date;
			}
			
			

			
			
			$condition .= " ".$other_condition ;
			// echo $condition;
			// exit();
		?>
		<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
		<script>
			$(document).ready(function() {
			 $(".input-group").hide();
			});
		</script>
		<?php 
		}
	}
	
		if(MODULE == "bc"){
			if(isset($_GET['solde'])){
				

				if($_GET['solde'] <> "tout" && $_GET['solde'] == 1){
					$condition .= " valider = 1";
				}elseif($_GET['solde'] <> "tout" && $_GET['solde'] == 0){				
					$condition .= " valider <> 1 ";
				}else{
					$condition .= " valider <> 1 OR valider = 1 ";
				}	
			}else{
				$condition .= " valider <> 1";
			}
			// echo "==>>".$condition;
		}	
		
		
		if(MODULE == "transaction"){
			if(isset($_GET['journal'])){
				

				
				if($_GET['journal'] <> ""){
					if($condition <> ""){
						$condition .= " AND ";
					}
					$condition .= " journal = ".$_GET['journal'];
				}else{
					
				}	
			}
			
		}	
		
		//===========================================
		$param_get = "";
		// Récupération des variables GET
		$tabUrl = parse_url ( $_SERVER [ 'REQUEST_URI' ] ) ;
		$listparam = explode ( "&amp;" , $tabUrl [ 'query' ] ) ;
		$nb_param = count ( $listparam ) ;
	 
		// on associe les valeurs 
		for ( $i=0 ; $i<$nb_param ; $i++)  {
		  $param_get = str_replace("view=list&","",$listparam[$i]);
		  // echo "==>".$param ;
		}
		if($param_get <> ""){
			$param_get = "&".$param_get ;
		}
		//===========================================
		// $get_data = $condition;
		
		
			
	
    foreach($filters as $filter){
        $fieldName = $filter->getName();
        $fieldValue = $filter->getValue();
        if($fieldValue != ""){
            if($condition != "") $condition .= " AND";
            $condition .= " ".$fieldName." = '".$fieldValue."'";
        }
    }
    
    if(!in_array($_SESSION['user']['type'], array("administrator", "manager", "editor")) && db_column_exists($db, "pm_".MODULE, "users")){
        if($condition != "") $condition .= " AND";
        $condition .= " users REGEXP '(^|,)".$_SESSION['user']['id']."(,|$)'";
    }
	
			//================= Permission =============
				if($_SESSION['user']['type'] == "editor"){
					$module_2 = array('produitsachat','bc');
					if (in_array(MODULE, $module_2))
					{
						if($condition == ""){
							$condition .= " status = 1 ";
						}else{
							$condition .= " AND status = 1 ";
						}	
						
					}
				}
			//==========================================
	
	// echo $condition;
    
    $query_search = db_getRequestSelect($db, "pm_".MODULE, getSearchFieldsList($cols), $q_search, $condition, $order." ".$sort);

    $result_total = $db->query($query_search);
    if($result_total !== false)
        $total = $db->last_row_count();
        
    if($limit > 0) $query_search .= " LIMIT ".$limit." OFFSET ".$offset;

    $result = $db->query($query_search);
    if($result !== false)
        $total_page = $db->last_row_count();
        
    if(in_array("edit", $permissions) || in_array("all", $permissions)){
        
        // Setting main item
        if($action == "define_main" && $id > 0 && check_token($referer, "list", "get"))
            define_main($db, "pm_".MODULE, $id, 1);

        if($action == "remove_main" && $id > 0 && check_token($referer, "list", "get"))
            define_main($db, "pm_".MODULE, $id, 0);
            
        // Items displayed in homepage
        if($action == "display_home" && $id > 0 && check_token($referer, "list", "get"))
            display_home($db, "pm_".MODULE, $id, 1);

        if($action == "remove_home" && $id > 0 && check_token($referer, "list", "get"))
            display_home($db, "pm_".MODULE, $id, 0);
            
        if($action == "display_home_multi" && isset($_POST['multiple_item']) && check_token($referer, "list", "get"))
            display_home_multi($db, "pm_".MODULE, 1, $_POST['multiple_item']);
            
        if($action == "remove_home_multi" && isset($_POST['multiple_item']) && check_token($referer, "list", "get"))
            display_home_multi($db, "pm_".MODULE, 0, $_POST['multiple_item']);
            
        // Item activation/deactivation
        if($action == "check" && $id > 0 && check_token($referer, "list", "get"))
            check($db, "pm_".MODULE, $id, 1);

        if($action == "uncheck" && $id > 0 && check_token($referer, "list", "get"))
            check($db, "pm_".MODULE, $id, 2);
            
        if($action == "archive" && $id > 0 && check_token($referer, "list", "get"))
            check($db, "pm_".MODULE, $id, 3);
            
        if($action == "check_multi" && isset($_POST['multiple_item']) && check_token($referer, "list", "get"))
            check_multi($db, "pm_".MODULE, 1, $_POST['multiple_item']);
            
        if($action == "uncheck_multi" && isset($_POST['multiple_item']) && check_token($referer, "list", "get"))
            check_multi($db, "pm_".MODULE, 2, $_POST['multiple_item']);
        
        if($action == "archive_multi" && isset($_POST['multiple_item']) && check_token($referer, "list", "get"))
            check_multi($db, "pm_".MODULE, 3, $_POST['multiple_item']);
    }
    
    if(in_array("delete", $permissions) || in_array("all", $permissions)){

        // Item deletion
        if($action == "delete" && $id > 0 && check_token($referer, "list", "get"))
            delete_item($db, $id);

        if($action == "delete_multi" && isset($_POST['multiple_item']) && check_token($referer, "list", "get"))
            delete_multi($db, $_POST['multiple_item']);
    }
    
    if(in_array("all", $permissions)){
        
        // Languages completion
        if(MULTILINGUAL && isset($_POST['complete_lang']) && isset($_POST['languages']) && check_token($referer, "list", "post")){
            foreach($_POST['languages'] as $id_lang){
                complete_lang_module($db, "pm_".MODULE, $id_lang);
                if(NB_FILES > 0) complete_lang_module($db, "pm_".MODULE."_file", $id_lang, true);
            }
        }
    }
}

$_SESSION['module_referer'] = MODULE;
$csrf_token = get_token("list"); ?>
<!DOCTYPE html>
<head>
    <?php include(SYSBASE.ADMIN_FOLDER."/includes/inc_header_list.php"); ?>
</head>
<body>
    <div id="wrapper">
        <?php 
		
		include(SYSBASE.ADMIN_FOLDER."/includes/inc_top.php"); 
		
		// if($_SESSION['user']['type'] == "administrator"){
			// include(SYSBASE.ADMIN_FOLDER."/includes/inc_top.php"); 
		// }else if($_SESSION['user']['type'] == "editor"){
			// include(SYSBASE.ADMIN_FOLDER."/includes/inc_top_editor.php"); 
		// }else{
			// include(SYSBASE.ADMIN_FOLDER."/includes/inc_top.php"); 
		// }	
		
		?>
        <div id="page-wrapper" style="padding: 100px 10px 20px 10px;">
            <div class="page-header">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-6 clearfix">
                            <h1 class="pull-left"><i class="fa fa-<?php echo ICON; ?>"></i> <?php echo TITLE_ELEMENT; ?></h1>
                            <div class="pull-left text-right">
                                &nbsp;&nbsp;
                                <?php
                                if(in_array("add", $permissions) || in_array("all", $permissions)){ ?>
                                    <a href="index.php?view=form&id=0" class="btn btn-primary mt15">
                                        <i class="fa fa-plus-circle"></i> <?php echo $texts['NEW']; ?>
                                    </a>
                                    <?php
                                } ?>
                            </div>
                        </div>
						<div class="col-md-6 text-right" style="margin-top:15px;">
						<div class="form-inline">
						<?php
				
				if (in_array(MODULE, $module))
				{
					
			?>	<form action="index.php" method="GET" id="form1" style="float: left;">	
					<input type="hidden" name="view" value="list" />

				<?php if(MODULE <> "bc"){ ?>

				<b>Du : </b><input type="text" class="form-control datepicker" name="from" value="<?php if($_GET['from'] <> ""){ echo $_GET['from']; }else{ echo date("d-m-Y",$from_date); } ?>" >
				<b>Au : </b><input type="text" class="form-control datepicker" name="to" value="<?php if($_GET['to'] <> ""){ echo $_GET['to']; }else{ echo date("d-m-Y",$to_date); } ?>" >
				
				<?php } ?>
							
				<?php if(MODULE == "bc"){ ?>
				<b>Soldé ? </b>
				<select name="solde" class="chosen-select form-control">
				<option value="0" <?php if($_GET["solde"] == "0"){ echo "selected='selected'";}?>>Non solder</option>
					<option value="1" <?php if($_GET["solde"] == "1"){ echo "selected='selected'";}?>>Soldé</option>						
					
					<option  value="tout" <?php if($_GET["solde"] == "tout"){ echo "selected='selected'";}?>>Tout</option>
				</select>			
				
				<?php } ?>
				
				<?php if(MODULE == "transaction"){ ?>
				<b>Journal : </b>
				<select name="journal" class="chosen-select form-control">
						<option value="">Tous</option>						
						<?php
						if($db !== false){
							$queryo = "SELECT * FROM `pm_journal` ";
							$resulto = @$db->query($queryo);
                            if($resulto !== false){
								foreach($resulto as $rowo){
									if($_GET["journal"] == $rowo['id']){
										echo '<option value="'.$rowo['id'].'" selected="selected">'.$rowo['name'].'</option>';
									}else{	
										echo '<option value="'.$rowo['id'].'">'.$rowo['name'].'</option>';
									}
								}	
							}	
							
						} ?>
				</select>			
				
				<?php } ?>
				
				<button class="btn btn-default" type="submit" form="form1" ><i class="fa fa-search-plus"></i> Valider</button>
				
				
				</form>
				
				<?php } ?>
				<a href="#" target="_blank"><button class="btn btn-default" type="button" id="print_page"><i class="fa fa-print"></i> Imprimer</button></a>
					</div>	
						</div>
                    </div>
                </div>

            </div>
			

        </div>

        <div class="container-fluid">
		
            <div class="alert-container">				
				
                <div class="alert alert-success alert-dismissable"></div>
                <div class="alert alert-warning alert-dismissable"></div>
                <div class="alert alert-danger alert-dismissable"></div>
				<div style="text-align:center;margin-bottom:10px;">
			
		</div>
            </div>
            <?php
            if($db !== false){
                if(!in_array("no_access", $permissions)){ ?>
                    <form id="form" action="index.php?view=list" method="post">
                        <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>"/>
                        <div class="panel panel-default">
                            <div class="panel-heading form-inline clearfix">
                                <div class="row">
                                    <div class="col-md-6 text-left">
                                        <div class="form-inline">
                                            <input type="text" name="q_search" value="<?php echo $q_search; ?>" class="form-control input-sm" placeholder="<?php echo $texts['SEARCH']; ?>..."/>
                                            <?php displayFilters($filters); ?>
                                            <button class="btn btn-default btn-sm" type="submit" id="search" name="search"><i class="fa fa-search"></i> <?php echo $texts['SEARCH']; ?></button>
                                        </div>
                                    </div>
                                    <div class="col-md-6 text-right">
                                        <div class="input-group">
                                            <div class="input-group-addon"><i class="fa fa-th-list"></i> <?php echo $texts['DISPLAY']; ?></div>
                                            <select class="select-url form-control input-sm">
                                                <?php
                                                echo ($limit != 20) ? "<option value=\"index.php?view=list&limit=20\">20</option>" : "<option selected=\"selected\">20</option>";
                                                echo ($limit != 50) ? "<option value=\"index.php?view=list&limit=50\">50</option>" : "<option selected=\"selected\">50</option>";
                                                echo ($limit != 100) ? "<option value=\"index.php?view=list&limit=100\">100</option>" : "<option selected=\"selected\">100</option>"; ?>
                                            </select>
                                        </div>
                                        <?php
                                        if($limit > 0){
                                            $nb_pages = ceil($total/$limit);
                                            if($nb_pages > 1){ ?>
                                                <div class="input-group">
                                                    <div class="input-group-addon"><?php echo $texts['PAGE']; ?></div>
                                                    <select class="select-url form-control input-sm">
                                                        <?php

                                                        for($i = 1; $i <= $nb_pages; $i++){
                                                            $offset2 = ($i-1)*$limit;
                                                            
                                                            if($offset2 == $offset)
                                                                echo "<option value=\"\" selected=\"selected\">".$i."</option>";
                                                            else
                                                                echo "<option value=\"index.php?view=list&offset=".$offset2."\">".$i."</option>";
                                                        } ?>
                                                    </select>
                                                </div>
                                                <?php
                                            }
                                        } ?>
                                    </div>
                                </div>
                            </div>
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    <thead>
                                        <tr class="nodrop nodrag">
                                            <th width="70">
                                                <?php
                                                if(RANKING){ ?>
                                                    <a href="index.php?view=list&order=rank&sort=<?php echo ($order == "rank") ? $rsort : "asc"; ?>">
                                                        # <i class="fa fa-sort<?php if($order == "rank") echo "-".$sort; ?>"></i>
                                                    </a>
                                                    <?php
                                                } ?>
                                            </th>
                                            <th width="70">
                                                <a href="index.php?view=list&order=id&sort=<?php echo ($order == "id") ? $rsort : "asc"; ?>">
                                                    ID <i class="fa fa-sort<?php if($order == "id") echo "-".$sort; ?>"></i>
                                                </a>
                                            </th>
                                            <?php
                                            // if(NB_FILES > 0) echo "<th width=\"160\">".$texts['IMAGE']."</th>";
                                            foreach($cols as $col){ ?>
                                                <th>
                                                    <a href="index.php?view=list&order=<?php echo $col->getName(); ?>&sort=<?php echo ($order == $col->getName()) ? $rsort : "asc"; ?><?php echo $param_get;?>">
                                                        <?php echo $col->getLabel(); ?>
                                                        <i class="fa fa-sort<?php if($order == $col->getName()) echo "-".$sort; ?>"></i>
                                                    </a>
                                                </th>
                                                <?php
                                            }
                                            if(count($cols) == 0){
                                                $type_module = "file";
                                                if(NB_FILES > 0){ ?>
                                                    <th><?php echo $texts['FILE']; ?></th>
                                                    <th><?php echo $texts['LABEL']; ?></th>
                                                    <?php
                                                }
                                            }
                                            if(DATES){ ?>
                                                <th width="160">
                                                    <a href="index.php?view=list&order=add_date&sort=<?php echo ($order == "add_date") ? $rsort : "asc"; ?><?php echo $param_get;?>">
                                                        <?php echo $texts['ADDED_ON']; ?> <i class="fa fa-sort<?php if($order == "add_date") echo "-".$sort; ?>"></i>
                                                    </a>
                                                </th>
                                                <?php
                                            }
                                            if(MAIN){ ?>
                                                <th width="100">
                                                    <a href="index.php?view=list&order=main&sort=<?php echo ($order == "main") ? $rsort : "asc"; ?>">
                                                        <?php echo $texts['MAIN']; ?> <i class="fa fa-sort<?php if($order == "main") echo "-".$sort; ?>"></i>
                                                    </a>
                                                </th>
                                                <?php
                                            }
                                            if(HOME){ ?>
                                                <th width="100">
                                                    <a href="index.php?view=list&order=home&sort=<?php echo ($order == "home") ? $rsort : "asc"; ?>">
                                                        <?php echo $texts['HOME']; ?> <i class="fa fa-sort<?php if($order == "home") echo "-".$sort; ?>"></i>
                                                    </a>
                                                </th>
                                                <?php
                                            }
                                            if(VALIDATION){ ?>
                                                <th width="100">
                                                    <a href="index.php?view=list&order=checked&sort=<?php echo ($order == "checked") ? $rsort : "asc"; ?>">
                                                        <?php echo $texts['STATUS']; ?> <i class="fa fa-sort<?php if($order == "checked") echo "-".$sort; ?>"></i>
                                                    </a>
                                                </th>
                                                <?php
                                            } ?>
											<?php if(MODULE == "bc"){ ?>
                                                <th width="100">Status</th>
                                                <?php
                                            } ?>
                                            <th width="130"><?php echo $texts['ACTIONS']; ?></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        if($result !== false){
                                            foreach($result as $i => $row){
                                                
                                                $id = $row['id'];
                                                $cols = getColsValues($db, $row, $i, $cols);
                                                
                                                if(isset($preview_path)) unset($preview_path); ?>
												
												<?php
												$module = array('bc','be','br','bf','bl','brv');
												if (in_array(MODULE, $module))
												{
													switch (MODULE) {
														case "bf":
															$ref = 'B';
															break;
														case "brv":
															$ref = 'BR';
															break;	
														case "br":
															$ref = 'BS';
															break;	
															
														case "facture":
															$ref = 'FAC18';
															break;															
														default:
															$ref = MODULE;
													}
													$ref = $ref.getId($id);
													
													$color = 'style="color:blue;font-weight: bold;"';
												}else{
													$ref = $id;
													$color = "";
												}	
												?>
                                                
                                                <tr id="item_<?php echo $id ?>">
                                                
                                                    <td class="text-left" >
                                                        <input type="checkbox" class="checkitem" name="multiple_item[]" value="<?php echo $id; ?>"/>
                                                        <?php if(RANKING) echo $row['rank']; ?>
                                                    </td>
                                                    
                                                    <td class="text-left" <?php echo $color; ?>><?php echo strtoupper($ref); ?></td>
                                                   
                                                   <?php
                                                    if(isset($type_module) && $type_module == "file"){
                                                    
                                                        $query_file = "SELECT * FROM pm_".MODULE."_file WHERE id_item = ".$id;
                                                        if(MULTILINGUAL) $query_file .= " AND lang = ".DEFAULT_LANG;
                                                        $query_file .= " ORDER BY rank LIMIT 1";
                                                        $result_file = $db->query($query_file);
                                                        
                                                        if($result_file !== false && $db->last_row_count() > 0){
                                                            $row_file = $result_file->fetch();
                                                            
                                                            $label = $row_file['label'];
                                                            $filename = $row_file['file'];
                                                        }else{
                                                            $label = "";
                                                            $filename = "";
                                                        }
                                                        echo "<td>".$filename."</td>";
                                                        echo "<td>".$label."</td>";
                                                    }
                                                    foreach($cols as $col){
                                                        echo "<td";
                                                        $type = $col->getType();
                                                        if($type == "date" || $type == "date") echo " class=\"text-left\"";
                                                        if($type == "price") echo " class=\"text-right\"";
                                                        if($type == "qte") echo " class=\"text-right\"";
														
														// modif CODE 
														$name ="";
														if($type == "name_fr"){
															$query_fr = "SELECT name FROM pm_fournisseurs WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$name = $row_fr['name'];
															}
															echo " class=\"text-left\"";
															echo "><b style='color:red;'>".$name."</b></td>";
														}elseif($type == "name_prd"){
															$query_fr = "SELECT desingnation FROM pm_produits WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['desingnation'];
															}
															echo " class=\"text-left\"";
															echo "><b style='color:red;'>".$desingnation."</b></td>";
														}elseif($type == "code_fr"){
															echo " class=\"text-left\"";
															echo "><b style='color:red;'>FRS_00".$col->getValue($i)."</b></td>";
														}elseif($type == "code_cl"){
															echo " class=\"text-left\"";
															echo "><b style='color:red;'>CLT_00".$col->getValue($i)."</b></td>";
														}elseif($type == "ref_bc"){
															echo " class=\"text-left\"";
															if($col->getValue($i) <> ""){
																echo "><b style='color:green;'>BC_00".$col->getValue($i)."</b></td>";
															}else{
																echo "><b style='color:green;'>-</b></td>";
															}	
														}elseif($type == "ref_be"){
															$desingnation ='';
															$query_fr = "SELECT ref_bl FROM pm_be WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['ref_bl'];
															}
															echo " class=\"text-left\"";
															echo "><b >".$desingnation."</b></td>";
														}elseif($type == "ref_bl"){
															$ref_bl ='';
															$arr = explode(",", $col->getValue($i));
															foreach ($arr as &$value) {
																if($value <> "") $ref_bl = $ref_bl ." BL_".$value." / ";
																
															}
															echo " class=\"text-left\"";
															echo "><b >".$ref_bl."</b></td>";
														}elseif($type == "ref_bc"){
															echo " class=\"text-left\"";
															if($col->getValue($i) <> ""){
																echo "><b style='color:green;'>BC_00".$col->getValue($i)."</b></td>";
															}else{
																echo "><b style='color:green;'>-</b></td>";
															}	
														}elseif($type == "valider"){
															echo " class=\"text-center\"";
															if($col->getValue($i) == 1){
																echo "><b style='color:green;'>OUI</b></td>";
															}else{
																echo "><b style='color:red;'>NON</b></td>";
															}	
														}elseif($type == "status"){
															echo " class=\"text-center\"";
															if($col->getValue($i) == 1){
																echo "><b style='color:green;'>Payé</b></td>";
															}else{
																echo "><b style='color:red;'>Non Payer</b></td>";
															}	
														}elseif($type == "famille_cat"){
															$desingnation ='';
															$query_fr = "SELECT nom FROM pm_category WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['nom'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "unite"){
															$desingnation ='';
															$query_fr = "SELECT name FROM pm_unite WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['name'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "sous_famille_classe"){
															$desingnation ='';
															$query_fr = "SELECT nom FROM pm_famille WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['nom'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "famille_classe"){
															$desingnation ='';
															$query_fr = "SELECT nom FROM pm_famille_classe WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['nom'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "fourniseur_cat"){
															$desingnation ='';
															$query_fr = "SELECT nom FROM pm_fourniseur_cat WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['nom'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "pm_famille"){
															$desingnation ='';
															$query_fr = "SELECT nom FROM pm_famille WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['nom'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "ref_fac_vente"){
															$desingnation ='';
															$query_fr = "SELECT idd FROM pm_facture WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['idd'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "contact_cat"){
															$desingnation ='';
															$query_fr = "SELECT name FROM pm_contact_cat WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['name'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "ref_fac_achat"){
															$desingnation ='';
															$query_fr = "SELECT ref_bl FROM pm_facture_achat WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['ref_bl'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "nom_client"){
															$desingnation ='';
															$query_fr = "SELECT name FROM pm_client WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['name'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "idd"){
															echo " style='color:blue;font-weight: bold;'>".$col->getValue($i)."</td>";	
														}elseif($type == "journal"){
															$desingnation ='';
															$query_fr = "SELECT name FROM pm_journal WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$name = $row_fr['name'];
															}
																$obj ="<b>".$name."</b>";
															echo " class=\"text-left\"";
															echo "><b>".$obj."</b></td>";	
														}elseif($type == "compte"){
															$name ='';
															$query_fr = "SELECT name FROM pm_compte_financier WHERE id = ".$col->getValue($i);
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$name = $row_fr['name'];
															}
															echo " class=\"text-left\"";
															echo ">".$name."</td>";	
														}elseif($type == "type_transaction"){
															$desingnation ='';
															$query_fr = "SELECT name FROM pm_classe_compte WHERE id = ".$col->getValue($i);
															if($col->getValue($i) == "in"){
																$name ="<b style='color:green;'>".$col->getValue($i)."</b><i class='fa fa-arrow-up text-success'></i>";
															}else{
																$name ="<b style='color:red;'>".$col->getValue($i)."</b><i class='fa fa-arrow-down text-danger'></i>";
															}
															echo " class=\"text-left\"";
															echo "><b>".$name."</b></td>";	
														}elseif($type == "classe_name"){
															$desingnation ='';
															$query_fr = "SELECT name FROM pm_classe_compte WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$name = $row_fr['name'];
															}
															echo " class=\"text-left\"";
															echo "><b>".$name."</b></td>";	
														}elseif($type == "cat_client"){
															$desingnation ='';
															$query_fr = "SELECT name FROM pm_cat_client WHERE id = ".$col->getValue($i);
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['name'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}elseif($type == "all_name"){
															$desingnation ='';
															$query_fr = "SELECT desingnation FROM pm_produitsachat WHERE id = ".$id;
															// echo $query_fr;
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$desingnation = $row_fr['desingnation'];
															}
															echo " class=\"text-left\"";
															echo ">".$desingnation."</td>";	
														}else{
															echo " class=\"text-left\"";
															echo ">".$col->getValue($i)."</td>";
														} 
                                                    }
													
                                                    if(DATES){
                                                        $add_date = (is_null($row['add_date'])) ? "-" : strftime(DATE_FORMAT." ".TIME_FORMAT, $row['add_date']);
                                                        $edit_date = (is_null($row['edit_date'])) ? "-" : strftime(DATE_FORMAT." ".TIME_FORMAT, $row['edit_date']); ?>
                                                        <td class="text-left" width="200">
                                                            <?php echo $add_date; ?>
                                                        </td>                                                        
                                                        <?php
                                                    }
                                                    if(MAIN){
                                                        $main = $row['main']; ?>
                                                        <td class="text-left">
                                                            <?php
                                                            if($main == 0){
                                                                if((in_array("publish", $permissions) || in_array("all", $permissions))){ ?>
                                                                    <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=define_main" title="<?php echo $texts['DEFINE_MAIN']; ?>"><i class="fa fa-star text-muted"></i></a>
                                                                    <?php
                                                                }else{ ?>
                                                                    <i class="fa fa-star text-muted"></i>
                                                                    <?php
                                                                }
                                                            }elseif($main == 1){ ?>
                                                                <i class="fa fa-star text-primary"></i>
                                                                <?php
                                                            } ?>
                                                        </td>
                                                        <?php
                                                    }
                                                    if(HOME){
                                                        $home = $row['home']; ?>
                                                        <td class="text-left">
                                                            <?php
                                                            if($home == 0){
                                                                if((in_array("publish", $permissions) || in_array("all", $permissions))){ ?>
                                                                    <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=display_home" title="<?php echo $texts['SHOW_HOMEPAGE']; ?>"><i class="fa fa-home text-danger"></i></a>
                                                                    <?php
                                                                }else{ ?>
                                                                    <i class="fa fa-home text-danger"></i>
                                                                    <?php
                                                                }
                                                            }elseif($home == 1){
                                                                if((in_array("publish", $permissions) || in_array("all", $permissions))){ ?>
                                                                    <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=remove_home" title="<?php echo $texts['REMOVE_HOMEPAGE']; ?>"><i class="fa fa-home text-success"></i></a>
                                                                    <?php
                                                                }else{ ?>
                                                                    <i class="fa fa-home text-success"></i>
                                                                    <?php
                                                                }
                                                            } ?>
                                                        </td>
                                                        <?php
                                                    }
                                                    if(VALIDATION){
                                                        $checked = $row['checked']; ?>
                                                        <td class="text-left">
                                                            <?php
                                                            if($checked == 0) echo "<span class=\"label label-warning\">".$texts['AWAITING']."</span>";
                                                            elseif($checked == 1) echo "<span class=\"label label-success\">".$texts['PUBLISHED']."</span>";
                                                            elseif($checked == 2) echo "<span class=\"label label-danger\">".$texts['NOT_PUBLISHED']."</span>";
                                                            elseif($checked == 3) echo "<span class=\"label label-default\">".$texts['ARCHIVED']."</span>"; ?>
                                                        </td>
                                                        <?php
                                                    } ?>
													

													
													<?php if(MODULE == "bc"){ ?>
													<?php 
													$count_be = 0 ;
													$query_bc = "SELECT SUM(qte) as bc FROM `pm_bc_product` WHERE id_bc = ".$id;
													$result_bc = $db->query($query_bc);
													if($result_bc !== false && $db->last_row_count() > 0){
														$row_bc = $result_bc->fetch();																
														$count_bc = $row_bc['bc'];
													}
													$query_be = "SELECT * from pm_be WHERE ref_bc = ".$id;
													$result_be = $db->query($query_be);
													if($result_be !== false){
														foreach($result_be as $row_be){
															$query_bep = "SELECT qte from pm_be_product WHERE id_be = ".$row_be["id"];
															// echo $query_bep.'<br>';
															$result_bep = $db->query($query_bep);
															if($result_bep !== false){
																foreach($result_bep as $row_bep){
																	$count_be = $count_be + $row_bep["qte"];
																}
															}	
														
														}
													}
													//========= 
													$query_bcs = "SELECT valider FROM `pm_bc` WHERE id = ".$id;
													$result_bcs = $db->query($query_bcs);
													if($result_bcs !== false && $db->last_row_count() > 0){
														$row_bcs = $result_bcs->fetch();																
														$status_bc = $row_bcs['valider'];
													}
													// echo 'status :'.$status_bc.'<br>';
													//=========
													if($count_bc > 0){
														$status = ($count_be*100) / $count_bc;
													}else{
														$status = 0;
													}
													$status = number_format($status, 0);
													if($status_bc == 1){
														$status = 100;
													}
													if($status < 10){
														$color_satatus = "red";
													}elseif($status < 50){
														$color_satatus = "orange";
													}elseif($status < 100){
														$color_satatus = "blue";
													}else{
														$color_satatus = "green";
													}
													?>
													
														<td class="text-left" width="160">
														<div class="w3-light-grey">
														  <div class="w3-container w3-round w3-<?php echo $color_satatus; ?>" style="width:<?php echo $status; ?>%;max-width:100%;color:#000 !important;font-weight: bold;"><?php echo $status; ?>%</div>
														</div>
														</td>
														<?php
													} ?>
                                                    <td class="text-center" width="150" >
														<?php if(MODULE == "transaction"){ ?>
														<?php 
															$query_rc = "SELECT * FROM `pm_transaction` WHERE id = ".$id;
															$result_rc = $db->query($query_rc);
															if($result_rc !== false && $db->last_row_count() > 0){
																$row_rc = $result_rc->fetch();																
																$retenue = $row_rc['retenue'];
																$journal = $row_rc['journal'];
																$type = $row_rc['type'];
																$mode_reglement = $row_rc['mode_reglement'];
															}
															if($journal == 1){
																$journal = "";
															}
														
														if(($mode_reglement == 2)&&($type == "out")){ //== chèque
														?>
														<a class="tips" href="../../print/cheque<?php echo $journal; ?>.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Imprimer Chèque"><i class="fa fa-hashtag text-success"></i></a>
														<?php 
														}else{
														?>	
															<i class="tips fa fa-hashtag text-danger"></i>
														<?php }
														if(($mode_reglement == 3)&&($type == "out")){  //== Traite
														?>
														<a class="tips" href="../../print/traite.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Imprimer Traite"><i class="fa fa-server text-success"></i></a>
														<?php 
														}else{
														?>	
															<i class="tips fa fa-server text-danger"></i>
														<?php }										
														
															if($retenue){
														?>
																<a class="tips" href="../../rs/index.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Imprimer RS"><i class="fa fa-registered text-success"></i></a>
															<?php }else{ ?>
																<i class="tips fa fa-registered text-danger"></i>
															<?php } ?>
													
													<?php } ?>													
													<?php if(MODULE == "bc"){ ?>
													<a class="tips get_bc" href="#" name="<?php echo $id; ?>" title="Voir état CMD"><i class="fa fa-eye text-success"></i></a>
													<a class="tips" href="print.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Imprimer BC"><i class="fa fa-print text-success"></i></a>
													<?php } ?>
																						

													<?php if(MODULE == "bf"){

															$query_rc = "SELECT status_inventaire,stock_valide FROM `pm_bf` WHERE id = ".$id;
															$result_rc = $db->query($query_rc);
															if($result_rc !== false && $db->last_row_count() > 0){
																$row_rc = $result_rc->fetch();																
																$status_inventaire = $row_rc['status_inventaire'];
																$stock_valide = $row_rc['stock_valide'];
															}

if($stock_valide == 0){
?>


    <i class="fa fa-sign-in" aria-hidden="true"  style="color:red;cursor: pointer;" onclick="confirmAction(<? echo $id; ?>)"></i>

<?
}else{
	
	echo '<i class="fa fa-sign-in" aria-hidden="true"  style="color:green;" title="Stock Validé"></i>';
}


if($status_inventaire == 0){

														?>
<i class="fa fa-exclamation-circle updateStatusIcon" style="color:orange;cursor: pointer;"  data-id="<? echo $id; ?>"></i>


<?
if(($_SESSION['user']['type'] == "administrator")||($_SESSION['user']['id'] == 19)){

?>

    <script>
        $(document).ready(function() {
            $('.updateStatusIcon').off('click').on('click', function() {
                var id = $(this).data('id');
                var confirmation = confirm("Are you sure you want to update the status?");
                if (confirmation) {
                    $.ajax({
                        url: 'update_status.php',
                        type: 'POST',
                        data: { id: id },
                        success: function(response) {
                            alert('Status updated successfully');
							$(this).removeClass('fa-exclamation-circle').addClass('fa-check-circle').css('color', 'green');
                        },
                        error: function() {
                            alert('Error updating status');
                        }
                    });
                }
            });
        });
    </script>

													<?
}
													
}else{
?>	
	<i title="Validé" class="fa fa-check-circle" style="color:green;"></i>
<?	
}													
													
													} ?>



													<?php if(MODULE == "bl"){ ?>
														<a class="tips" href="print.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Imprimer BL"><i class="fa fa-print text-success"></i></a>
													<?php } ?>
																
													
													<?php if(MODULE == "facture"){ ?>
													<?php 
															$query_rc = "SELECT * FROM `pm_facture` WHERE id = ".$id;
															$result_rc = $db->query($query_rc);
															if($result_rc !== false && $db->last_row_count() > 0){
																$row_rc = $result_rc->fetch();																
																$validate = $row_rc['validate'];
															}
															if($validate == 1){
																$permissions = array("add");
															}else{
																$permissions = array("edit","delete");
															}
													if($validate <> 1){		
													?>
													<a class="tips gen_facture" href="/admin/facture/index.php?id=<?php echo $id; ?>" name="<?php echo $id; ?>" title="Générer Facture"><i class="fa fa-send text-success"></i></a>
													<?php } ?>
													<a class="tips gen_facture" href="/admin/facture/print.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Imprimer Facture"><i class="fa fa-print text-success"></i></a>
													<?php } ?>



													
													<?php if(MODULE == "facture_avoir_vente"){ ?>
													<?php 
															$query_rc = "SELECT * FROM `pm_facture_avoir_vente` WHERE id = ".$id;
															$result_rc = $db->query($query_rc);
															if($result_rc !== false && $db->last_row_count() > 0){
																$row_rc = $result_rc->fetch();																
																$validate = $row_rc['validate'];
															}
															if($validate == 1){
																$permissions = array("add");
															}else{
																$permissions = array("edit","delete");
															}
													if($validate <> 1){		
													?>
													<a class="tips gen_facture" href="/admin/modules/facture_avoir_vente/gen.php?id=<?php echo $id; ?>" name="<?php echo $id; ?>" title="Générer Facture"><i class="fa fa-send text-success"></i></a>
													<?php } ?>
													<a class="tips gen_facture" href="/admin/modules/facture_avoir_vente/print.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Imprimer Facture"><i class="fa fa-print text-success"></i></a>
													<?php } ?>
													<?php if(MODULE == "facture_avoir"){ ?>
														<?php 
																$query_rc = "SELECT * FROM `pm_facture_avoir` WHERE id = ".$id;
																$result_rc = $db->query($query_rc);
																if($result_rc !== false && $db->last_row_count() > 0){
																	$row_rc = $result_rc->fetch();																
																	$validate = $row_rc['validate'];
																}
																if($validate == 1){
																	$permissions = array("add");
																}else{
																	$permissions = array("edit","delete");
																}
														?>
													<?php } ?>
													<?php if(MODULE == "facture_achat"){ ?>
													<?php 
															$query_rc = "SELECT * FROM `pm_facture_achat` WHERE id = ".$id;
															$result_rc = $db->query($query_rc);
															if($result_rc !== false && $db->last_row_count() > 0){
																$row_rc = $result_rc->fetch();																
																$validate = $row_rc['validate'];
															}
															if($validate == 1){
																$permissions = array("add");
															}else{
																$permissions = array("edit","delete");
															}
													?>		
													<a class="tips gen_facture" href="facture_achat.php?id=<?php echo $id; ?>" target="_blank" name="<?php echo $id; ?>" title="Générer Facture"><i class="fa fa-send text-success"></i></a>
													<?php } ?>
                                                        <?php
                                                        if(VALIDATION && (in_array("publish", $permissions) || in_array("all", $permissions))){
                                                            if($checked == 0){ ?>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=check" title="<?php echo $texts['PUBLISH']; ?>"><i class="fa fa-check text-success"></i></a>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=uncheck" title="<?php echo $texts['UNPUBLISH']; ?>"><i class="fa fa-ban text-danger"></i></a>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=archive" title="<?php echo $texts['ARCHIVE']; ?>"><i class="fa fa-archive text-warning"></i></a>
                                                                <?php
                                                            }elseif($checked == 1){ ?>
                                                                <i class="fa fa-check text-muted"></i>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=uncheck" title="<?php echo $texts['UNPUBLISH']; ?>"><i class="fa fa-ban text-danger"></i></a>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=archive" title="<?php echo $texts['ARCHIVE']; ?>"><i class="fa fa-archive text-warning"></i></a>
                                                                <?php
                                                            }elseif($checked == 2){ ?>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=check" title="<?php echo $texts['PUBLISH']; ?>"><i class="fa fa-check text-success"></i></a>
                                                                <i class="fa fa-ban text-muted"></i>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=archive" title="<?php echo $texts['ARCHIVE']; ?>"><i class="fa fa-archive text-warning"></i></a>
                                                                <?php
                                                            }elseif($checked == 3){ ?>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=check" title="<?php echo $texts['PUBLISH']; ?>"><i class="fa fa-check text-success"></i></a>
                                                                <a class="tips" href="index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=uncheck" title="<?php echo $texts['UNPUBLISH']; ?>"><i class="fa fa-ban text-danger"></i></a>
                                                                <i class="fa fa-archive text-muted"></i>
                                                                <?php
                                                            }
                                                        }
														$printed = 0;
														//== Disable Edite et delete si ==> VALIDATE
														$modules = array('br','be','bl','bc','bf','brv','facture_avoir');
														if(in_array(MODULE, $modules))
														{
															$query_rc = "SELECT validate,printed FROM `pm_".MODULE."` WHERE id = ".$id;
															// echo $query_rc ;
															$result_rc = $db->query($query_rc);
															if($result_rc !== false && $db->last_row_count() > 0){
																$row_rc = $result_rc->fetch();																
																$validate = $row_rc['validate'];
																$printed = $row_rc['printed'];
															}
															if($_SESSION['user']['type'] == "administrator"){
																if($validate == 1){
																	$permissions = array("edit","add");
																}else{
																	$permissions = array("edit","delete");
																}
															}
															if($_SESSION['user']['id'] == 15){
																if(MODULE == "bf"){
																	$permissions = array("add");
																}	
															}	
															
														}
														// echo "printed >> ".$printed;
														//=============
															if((in_array("edit", $permissions) || in_array("all", $permissions)) && ( $printed == 0 || $_SESSION['user']['type'] == "administrator" )){ ?>
																<a class="tips" href="index.php?view=form&id=<?php echo $id; ?>" title="<?php echo $texts['EDIT']; ?>"><i class="fa fa-pencil"></i></a>
																<?php
															}
															
															if(in_array("delete", $permissions) || in_array("all", $permissions)){ ?>
																<a class="tips" href="javascript:if(confirm('<?php echo $texts['DELETE_CONFIRM2']; ?>')) window.location = 'index.php?view=list&id=<?php echo $id; ?>&csrf_token=<?php echo $csrf_token; ?>&action=delete';" title="<?php echo $texts['DELETE']; ?>"><i class="fa fa-remove text-danger"></i></a>
																<?php
															} 
														
														?>
                                                    </td>
                                                </tr>
                                                <?php
                                            }
                                        } ?>
                                    </tbody>
                                </table>
                            </div>
                            <?php
                            if($total == 0){ ?>
                                <div class="text-left mt20 mb20">- <?php echo $texts['NO_ELEMENT']; ?> -</div>
                                <?php
                            } ?>
                            <div class="panel-footer form-inline clearfix">
                                <div class="row">
                                    <div class="col-md-6 text-left">
                                        <?php
                                        if($total > 0){ ?>
                                            &nbsp;<input type="checkbox" class="selectall"/>
                                            <?php echo $texts['SELECT_ALL']; ?>&nbsp;
                                            <select name="multiple_actions" class="form-control input-sm">
                                                <option value="">- <?php echo $texts['ACTIONS']; ?> -</option>
                                                <?php
                                                if(in_array("publish", $permissions) || in_array("all", $permissions)){
                                                    if(VALIDATION){ ?>
                                                        <option value="check_multi"><?php echo $texts['PUBLISH']; ?></option>
                                                        <option value="uncheck_multi"><?php echo $texts['UNPUBLISH']; ?></option>
                                                        <?php
                                                    }
                                                    if(HOME){ ?>
                                                        <option value="display_home_multi"><?php echo $texts['SHOW_HOMEPAGE']; ?></option>
                                                        <option value="remove_home_multi"><?php echo $texts['REMOVE_HOMEPAGE']; ?></option>
                                                        <?php
                                                    }
                                                }
                                                if(in_array("delete", $permissions) || in_array("all", $permissions)){ ?>
                                                    <option value="delete_multi"><?php echo $texts['DELETE']; ?></option>
                                                    <?php
                                                } ?>
                                            </select>
                                            <?php
                                        } ?>
                                    </div>
                                    <div class="col-md-6 text-right">
                                        <div class="input-group">
                                            <div class="input-group-addon"><i class="fa fa-th-list"></i> <?php echo $texts['DISPLAY']; ?></div>
                                            <select class="select-url form-control input-sm">
                                                <?php
                                                echo ($limit != 20) ? "<option value=\"index.php?view=list&limit=20\">20</option>" : "<option selected=\"selected\">20</option>";
                                                echo ($limit != 50) ? "<option value=\"index.php?view=list&limit=50\">50</option>" : "<option selected=\"selected\">50</option>";
                                                echo ($limit != 100) ? "<option value=\"index.php?view=list&limit=100\">100</option>" : "<option selected=\"selected\">100</option>"; ?>
                                            </select>
                                        </div>
                                        
                                        <?php
                                        if($limit > 0){
                                            $nb_pages = ceil($total/$limit);
                                            if($nb_pages > 1){ ?>
                                                <div class="input-group">
                                                    <div class="input-group-addon"><?php echo $texts['PAGE']; ?></div>
                                                    <select class="select-url form-control input-sm">
                                                        <?php

                                                        for($i = 1; $i <= $nb_pages; $i++){
                                                            $offset2 = ($i-1)*$limit;
                                                            
                                                            if($offset2 == $offset)
                                                                echo "<option value=\"\" selected=\"selected\">".$i."</option>";
                                                            else
                                                                echo "<option value=\"index.php?view=list&offset=".$offset2."\">".$i."</option>";
                                                        } ?>
                                                    </select>
                                                </div>
                                                <?php
                                            }
                                        } ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php
                        if(in_array("all", $permissions)){ ?>
                            <div class="well">
                                <?php
                                if($db != false && MULTILINGUAL && $total_lang > 0){ ?>
                                        
                                    <div id="translation">
                                        <p><?php echo $texts['COMPLETE_LANGUAGE']; ?></p>
                                        <?php
                                        foreach($result_lang as $row_lang){
                                            $id_lang = $row_lang['id'];
                                            $title_lang = $row_lang['title']; ?>
                                            
                                            <input type="checkbox" name="languages[]" value="<?php echo $id_lang; ?>">
                                            <?php
                                            $result_img_lang = $db->query("SELECT * FROM pm_lang_file WHERE id_item = ".$id_lang." AND type = 'image' AND file != '' ORDER BY rank LIMIT 1");
                                            if($result_img_lang !== false && $db->last_row_count() > 0){
                                                $row_img_lang = $result_img_lang->fetch();
                                                
                                                $id_img_lang = $row_img_lang['id'];
                                                $file_img_lang = $row_img_lang['file'];
                                            
                                                if(is_file(SYSBASE."medias/lang/big/".$id_img_lang."/".$file_img_lang))
                                                    echo "<img src=\"".DOCBASE."medias/lang/big/".$id_img_lang."/".$file_img_lang."\" alt=\"\" border=\"0\" class=\"flag\"> ";
                                            }
                                            echo $title_lang."<br>";
                                        } ?>
                                        <button type="submit" name="complete_lang" class="btn btn-default mt10" data-toggle="tooltip" data-placement="right" title="<?php echo $texts['COMPLETE_LANG_NOTICE']; ?>"><i class="fa fa-magic"></i> <?php echo $texts['APPLY_LANGUAGE']; ?></button>
                                    </div>
                                    <?php
                                } ?>
                            </div>
                            <?php
                        } ?>
                    </form>
                    <?php
                }else echo "<p>".$texts['ACCESS_DENIED']."</p>";
            } ?>
        </div>
    </div>
</body>
</html>
<?php
$_SESSION['redirect'] = false;
$_SESSION['msg_error'] = array();
$_SESSION['msg_success'] = array();
$_SESSION['msg_notice'] = array(); ?>


<!-- SweetAlert2 CDN -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
function confirmAction(beId) {
    Swal.fire({
        title: 'Êtes-vous sûr ?',
        text: "Vous allez valider le stock de ce BF.",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#28a745', // Couleur du bouton "Confirmer"
        cancelButtonColor: '#d33', // Couleur du bouton "Annuler"
        confirmButtonText: '<i class="fa fa-sign-in"></i> Confirmer',
        cancelButtonText: 'Annuler'
    }).then((result) => {
        if (result.isConfirmed) {
            // Redirection vers l'URL avec les paramètres GET pour l'enregistrement du BE
            window.location.href = `../stock_update.php?id_depot=2&source=BF&id_source=${beId}`;
        }
    });
}
</script>

