<?php
header("Content-Type: text/html;charset=utf-8");
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
if (!isset($_SESSION["usuario_editor"]) || $_SESSION["usuario_editor"] == 0){
	header("Location: ./");
	exit;
}
if ($_SESSION["control"]["tipo_editor"] == "administracion_tienda"){
	header("Location: ./ventas.php");
	exit;
}
require_once("data/Conexion.php");
require_once("clases/cls_tienda.php");
?>
<?php require_once("seccion_cabecera.php"); ?>
<?php require_once("seccion_css.php"); ?>
<?php require_once("seccion_dialogo.php"); ?>
<?php require_once("seccion_alertas.php"); ?>
<?php require_once("seccion_producto.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">

<head>
	<link rel="icon" type="image/png" href="libreria/imagenes/favicon.png" sizes="64x64">
	<title>Productos</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="generator" content="Geany 1.38" />
	<?php if (isset($_COOKIE["custom_css"])){ ?>
	<link id="link_css" rel="stylesheet" type="text/css" href="libreria/css/demo_css_<?php echo($_COOKIE["custom_css"]); ?>.css?vervion=1.3">
	<?php } else { ?>
	<link id="link_css" rel="stylesheet" type="text/css" href="libreria/css/demo_css_Cristal.css?vervion=1.3">
	<?php } ?>
	<link rel="stylesheet" type="text/css" href="libreria/css/jquery-ui.css">
	<script language="JavaScript" type="text/javascript" src="libreria/js/jquery.min.js"></script>
	<script language="JavaScript" type="text/javascript" src="libreria/js/jquery-ui.js"></script>
</head>
<style>
<?php get_css(); ?>
<?php dialogo_loading_css(); ?>
<?php dialogo_image_css(); ?>
<?php dialogo_alert_css(); ?>
<?php dialogo_confirm_css(); ?>
<?php if ($_SESSION["control"]["tipo_editor"] == "administracion_general" || $_SESSION["control"]["tipo_editor"] == "administracion_almacen" || $_SESSION["control"]["tipo_editor"] == "administracion_almacen"){ ?>
<?php dialogo_product_css(); ?>
<?php } ?>
#box_input_tools{
	display: none;
    position: absolute;
    background-color: white;
    border: 1px solid #ccc;
    padding: 3px;
	width: 150px;
	height: 50px;
	top: 0;
	right: 0;
	flex-direction: row;
	flex-wrap: nowrap;
	justify-content: center;
	align-items: center;
	align-content: center;
}
#box_input_tools .info{
	display: flex;
	flex-direction: column;
	flex-wrap: nowrap;
	justify-content: center;
	align-items: center;
	align-content: center;
	flex:1;
}
#box_input_tools .info div{
	color:#000000;
}
#box_input_tools .buttons{
	display: flex;
	flex-direction: column;
	flex-wrap: nowrap;
	justify-content: space-between;
	align-items: flex-end;
	align-content: stretch;
	height: 100%;
}
#box_input_tools .btn_apply{
	width: 33px;
	height: 25px;
	font-size:20px;
	border-radius:10px;
	color:white;
	display: flex;
	flex-direction: row;
	flex-wrap: nowrap;
	justify-content: center;
	align-items: center;
	align-content: center;
	cursor:pointer;
	background: #0000FF;
	color: white;
}
#box_input_tools .btn_close{
	width: 15px;
	height: 15px;
	font-size: 10px;
	cursor: pointer;
	background: red;
	border-radius: 50%;
	color: white;
	display: flex;
	flex-direction: row;
	flex-wrap: nowrap;
	justify-content: center;
	align-items: center;
	align-content: center;
}
.row_info{
	display: flex;
	flex-direction: row;
	flex-wrap: nowrap;
	justify-content: flex-start;
	align-items: center;
	align-content: stretch;
	padding: 3px 0px;
}
.row_info span:nth-child(odd){
	width:50%;
	max-width:100px;
	font-weight: bold;
}
.row_info span:nth-child(even){
	flex:1;
}
</style>
<body>
	<?php alertas_pedidos_html(); ?>
	<?php dialogo_loading_html(); ?>
	<?php dialogo_image_html(); ?>
	<?php dialogo_alert_html(); ?>
	<?php dialogo_confirm_html(); ?>
	<?php dialogo_product_html(); ?>
	<?php get_header_html(); ?>
	<section class="section_body">
		<div class="dv_left">
			<div class="dv_border">
				<div class="box" style="overflow: auto;">
					<div class="header">
						<span>Producto</span>
					</div>
					<hr>
					<div id="producto_informacion">
							<img src="libreria/imagenes/sin_imagen.png" style="max-width:80%;max-height:200px;margin:auto;">
							<br>
							<img src="" style="max-width:80%;max-height:200px;">
							<br><strong>Información del articulo</strong><br>
							<table>
							<tr class=""><td><strong>Nombre</strong></td><td></td></tr>
							<tr class="row_info_extra" style="display:none;"><td><strong>Nombres alternativos</strong></td><td></td></tr>
							<tr class="row_info_extra" style="display:none;"><td><strong>Fabricante</strong></td><td></td></tr>
							<tr class="row_info_extra" style="display:none;"><td><strong>Modelo</strong></td><td></td></tr>
							<tr class="row_info_extra" style="display:none;"><td><strong>Serie</strong></td><td></td></tr>
							<tr class=""><td><strong>Codigo</strong></td><td></td></tr>
							<tr class="row_info_extra" style="display:none;"><td><strong>Codigo de barras</strong></td><td></td></tr>
							<tr class="row_info_extra" style="display:none;"><td><strong>Stock minimo</strong></td><td></td></tr>
							<tr class=""><td><strong>Estado</strong></td><td></td></tr>
							</table>
					</div>
					<div id="btn_producto_informacion" style="padding:10px 0px;cursor:pointer;">Ver mas +</div>
					<hr>
					<ul class="list_store">
					</ul>
				</div>
			</div>
		</div>
		<div class="dv_middle"><span></span><span></span><span></span></div>
		<div class="dv_right">
			<div class="menu_button"><span></span><span></span><span></span></div>
			<div id="new_panel_edit" class="dv_border">
				<div class="header">
					<span>Productos</span>
				</div>
					<div class="buscador_input">
						<input type="text" placeholder="producto" id="filtro_buscar" name="filtro_buscar" />
						<svg width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M11 6C13.7614 6 16 8.23858 16 11M16.6588 16.6549L21 21M19 11C19 15.4183 15.4183 19 11 19C6.58172 19 3 15.4183 3 11C3 6.58172 6.58172 3 11 3C15.4183 3 19 6.58172 19 11Z" stroke="#1C274C" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>
					</div>
					<div class="buscador_options">
						<span style="flex:1;"></span>
						<button class="option_button" id="btn_order_asc">
							<svg width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M10.22 15.97L9 17.19V5C9 4.59 8.66 4.25 8.25 4.25C7.84 4.25 7.5 4.59 7.5 5V17.19L6.28 15.97C5.99 15.68 5.51 15.68 5.22 15.97C4.93 16.26 4.93 16.74 5.22 17.03L7.72 19.53C7.79 19.6 7.87 19.65 7.96 19.69C8.05 19.73 8.15 19.75 8.25 19.75C8.35 19.75 8.45 19.73 8.54 19.69C8.63 19.65 8.71 19.6 8.78 19.53L11.28 17.03C11.57 16.74 11.57 16.26 11.28 15.97C10.99 15.68 10.51 15.68 10.22 15.97Z" fill="#1c274c"></path> <path d="M14 11.21C14.39 11.35 14.82 11.15 14.96 10.76L15.24 9.98001H17.27L17.55 10.76C17.66 11.07 17.95 11.26 18.26 11.26C18.34 11.26 18.43 11.25 18.51 11.22C18.9 11.08 19.1 10.65 18.96 10.26L17.25 5.47001C17.08 5.04001 16.69 4.76001 16.25 4.76001C15.81 4.76001 15.42 5.04001 15.25 5.49001L13.55 10.26C13.41 10.65 13.61 11.08 14 11.22V11.21ZM16.73 8.48001H15.77L16.25 7.14001L16.73 8.48001Z" fill="#1c274c"></path> <path d="M18.67 13.46C18.48 13.02 18.08 12.75 17.62 12.75H14.51C14.1 12.75 13.76 13.09 13.76 13.5C13.76 13.91 14.1 14.25 14.51 14.25H16.9L14.07 17.2C13.73 17.56 13.64 18.08 13.83 18.54C14.02 18.98 14.42 19.25 14.88 19.25H18.01C18.42 19.25 18.76 18.91 18.76 18.5C18.76 18.09 18.42 17.75 18.01 17.75H15.62L18.44 14.82C18.78 14.46 18.88 13.93 18.68 13.47L18.67 13.46Z" fill="#1c274c"></path> </g></svg>
						</button>
						<button class="option_button" id="btn_order_desc" style="display:none;">
							<svg width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M10.22 15.97L9 17.19V5C9 4.59 8.66 4.25 8.25 4.25C7.84 4.25 7.5 4.59 7.5 5V17.19L6.28 15.97C5.99 15.68 5.51 15.68 5.22 15.97C4.93 16.26 4.93 16.74 5.22 17.03L7.72 19.53C7.79 19.6 7.87 19.65 7.96 19.69C8.05 19.73 8.15 19.75 8.25 19.75C8.35 19.75 8.44 19.73 8.54 19.69C8.63 19.65 8.71 19.6 8.78 19.53L11.28 17.03C11.57 16.74 11.57 16.26 11.28 15.97C10.99 15.68 10.51 15.68 10.22 15.97Z" fill="#1c274c"></path> <path d="M18.96 18.25L17.25 13.46C17.08 13.03 16.69 12.75 16.25 12.75C15.81 12.75 15.42 13.03 15.25 13.48L13.55 18.25C13.41 18.64 13.61 19.07 14 19.21C14.39 19.35 14.82 19.15 14.96 18.76L15.24 17.98H17.27L17.55 18.76C17.66 19.07 17.95 19.26 18.26 19.26C18.34 19.26 18.43 19.25 18.51 19.22C18.9 19.08 19.1 18.65 18.96 18.26V18.25ZM15.77 16.48L16.25 15.14L16.73 16.48H15.77Z" fill="#1c274c"></path> <path d="M13.83 10.54C14.02 10.98 14.42 11.25 14.88 11.25H18.01C18.42 11.25 18.76 10.91 18.76 10.5C18.76 10.09 18.42 9.75001 18.01 9.75001H15.62L18.44 6.82001C18.78 6.46001 18.88 5.93001 18.68 5.47001C18.49 5.03001 18.09 4.76001 17.63 4.76001H14.52C14.11 4.76001 13.77 5.10001 13.77 5.51001C13.77 5.92001 14.11 6.26001 14.52 6.26001H16.91L14.08 9.21001C13.74 9.57001 13.65 10.09 13.84 10.55L13.83 10.54Z" fill="#1c274c"></path> </g></svg>
						</button>
						<button id="btn_nuevo" class="header_button buttons_active">
							<svg width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M15 12L12 12M12 12L9 12M12 12L12 9M12 12L12 15" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> <path d="M22 12C22 16.714 22 19.0711 20.5355 20.5355C19.0711 22 16.714 22 12 22C7.28595 22 4.92893 22 3.46447 20.5355C2 19.0711 2 16.714 2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C21.5093 4.43821 21.8356 5.80655 21.9449 8" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> </g></svg>
							<span>Nuevo</span>
						</button>
					</div>
					<div class="opciones_adicionales_btn">Opciones Adicionales</div>
					<div class="opciones_adicionales_view">
						<label><input type="checkbox" id="tool_editar_datos" name="tool_editar_datos" />Editar stock minimo y estados</label>
						<div style="padding-left: 25px;display:none;align-items: center;justify-content: flex-start;"><span>Guardar cambios</span></button>&nbsp;<svg id="btn_save_all" style="width:25px;height:25px;margin-left:5px;cursor:pointer;" width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M22 12C22 16.714 22 19.0711 20.5355 20.5355C19.0711 22 16.714 22 12 22C7.28595 22 4.92893 22 3.46447 20.5355C2 19.0711 2 16.714 2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C21.5093 4.43821 21.8356 5.80655 21.9449 8" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> <path d="M17 2V8.8076C17 9.78253 17 10.27 16.8709 10.5607C16.5766 11.2233 15.8506 11.5805 15.1461 11.4095C14.8369 11.3344 14.4507 11.037 13.6782 10.4422C13.2421 10.1064 13.024 9.9385 12.797 9.83985C12.2886 9.61899 11.7114 9.61899 11.203 9.83985C10.976 9.9385 10.7579 10.1064 10.3218 10.4422C9.5493 11.037 9.16307 11.3344 8.85391 11.4095C8.14942 11.5805 7.42342 11.2233 7.12914 10.5607C7 10.27 7 9.78253 7 8.8076V2" stroke="#1C274C" stroke-width="1.5"></path> </g></svg></div>
						<div class="opciones_adicionales_columns" style="display:flex;">
							<label><input type="checkbox" data-column="v_column_3" checked />Ver códigos</label>
							<label><input type="checkbox" data-column="v_column_5" />Ver Fabricante</label>
							<label><input type="checkbox" data-column="v_column_6" />Ver Modelo</label>
							<label><input type="checkbox" data-column="v_column_7" />Ver Serie</label>
							<label><input type="checkbox" data-column="v_column_10" />Ver Producto unitario</label>
							<label><input type="checkbox" data-column="v_column_11" />Ver Fracciones</label>
							<div style="flex:1;"></div>
						</div>
					</div>
				<div class="table_view">
					<table style="width:100%;" id="table_list">
						<thead>
							<tr>
								<th class="v_column_1" width="40px">Nº</th>
								<th class="v_column_2" width="100px">Imagen</th>
								<th class="v_column_3" width="100px">Código</th>
								<th class="v_column_4">Articulo</th>
								<th class="v_column_5" width="100px">Fabricante</th>
								<th class="v_column_6" width="100px">Modelo</th>
								<th class="v_column_7" width="100px">Serie</th>
								<th class="v_column_8" width="70px">Stock minimo</th>
								<th class="v_column_9" width="100px">Estado</th>
								<th class="v_column_10">Producto Unitario</th>
								<th class="v_column_11" width="50px">Fracciones</th>
								<th class="v_column_12" width="40px">E</th>
							</tr>
						</thead>
						<tbody>
						</tbody>
					</table>
				</div>
				<div class="header">
					<span></span>
				</div>
			</div>
			<div id="panel_splash" class="splash" style="display:none;flex:1;">
				<span class="splash_title">La Casa De La Seguridad S.A.C.</span>
				<div class="splash_image" style="background-image: url(libreria/imagenes/logo_casa_de_seguridad.png);"></div>
				<span class="splash_subtitle">Productos</span>
			</div>
		</div>
	</section>
</body>
<script>

var ctrl_datos = {
	buscar: "",
	items_pagina: 0,
	items_cantidad: 0,
	orden: "ASC",
	beep_activo: true,
}
function makeInt(value){
	if (value == null || value == undefined || value == NaN || value.length == 0){
		value = "0";
	}
	return parseInt(value);
}
function makeFloat(value){
	if (value == null || value == undefined || value == NaN || value.length == 0){
		value = "0.00";
	}
	return parseFloat(value);
}
function fecha_guardar(date) {
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let day = date.getDate();
	return year + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ? "0" : "") + day;
}
function cargar_lista(){
    $.ajax({
		type: 'POST',
		dataType: "json",
		url: "control/ctrl_producto.php",
		data: {
			accion: "lista",
			buscar: ctrl_datos.buscar,
			items_pagina: ctrl_datos.items_pagina,
			items_cantidad: ctrl_datos.items_cantidad,
			orden: ctrl_datos.orden
		},
		success: function (datos) {
			//console.log(datos);
			//datos = JSON.parse(datos);
			$("#table_list .row_data").remove();
			if (datos.estado == "ok") {
				for(let idx=0; idx<datos.datos.length; idx++){
					let row = datos.datos[idx];
					let childs = $("#table_list .row_data");
					let imagenes = row.imagen.split(",");
					let html = '';
					html += '<tr class="row_data" data-producto="'+row.producto_id+'" >';
					html += '<td align="center" class="v_column_1 td_numero"><span>'+(childs.length+1)+'</span><input type="checkbox" style="display:none;" /></td>';
					if (imagenes[0].length <= 0){
						html += '<td class="v_column_2 td_imagen_producto" align="center" data-img=""><img data-img="" src="libreria/imagenes/sin_imagen.png" height="50"></td>';
					} else {
						html += '<td class="v_column_2 td_imagen_producto" align="center" data-img="'+imagenes[0]+'"><img data-img="'+imagenes[0]+'" src="libreria/imagenes/productos/'+imagenes[0]+'" height="50"></td>';
					}
					html += '<td class="v_column_3" align="left">'+row.codigo+'</td>';
					html += '<td class="v_column_4" align="left">'+row.nombre+'</td>';
					html += '<td class="v_column_5" align="left">'+row.fabricante+'</td>';
					html += '<td class="v_column_6" align="left">'+row.modelo+'</td>';
					html += '<td class="v_column_7" align="left">'+row.serie+'</td>';
					html += '<td class="v_column_8 td_stock_minimo" align="right" width="70"><div class="dv_input_disabled"><input type="number" value="'+row.stock_minimo+'" min="0" step="1" disabled /></div></td>';
					if (row.estado == "Activo"){
						html += '<td class="v_column_9" align="left"><select disabled><option value="Activo" selected>Activo</option><option value="Inactivo">Inactivo</option></select></td>';
					} else {
						html += '<td class="v_column_9" align="left"><select disabled><option value="Activo">Activo</option><option value="Inactivo" selected>Inactivo</option></select></td>';
					}
					html += '<td class="v_column_10" align="left">'+row.producto_unitario_nombre+'</td>';
					html += '<td class="v_column_11" align="center">'+row.producto_fracciones+'</td>';
					html += '<td class="v_column_12" align="center" style="min-width:40px;" >';
					html += '<svg class="btn_edit" style="display:flex;" width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M18.562 2.9354L18.9791 2.5183C19.6702 1.82723 20.7906 1.82723 21.4817 2.5183C22.1728 3.20937 22.1728 4.32981 21.4817 5.02087L21.0646 5.43797M18.562 2.9354C18.562 2.9354 18.6142 3.82172 19.3962 4.60378C20.1783 5.38583 21.0646 5.43797 21.0646 5.43797M18.562 2.9354L14.7275 6.76995C14.4677 7.02968 14.3379 7.15954 14.2262 7.30273C14.0945 7.47163 13.9815 7.65439 13.8894 7.84776C13.8112 8.01169 13.7532 8.18591 13.637 8.53437L13.2651 9.65M21.0646 5.43797L17.23 9.27253C16.9703 9.53225 16.8405 9.66211 16.6973 9.7738C16.5284 9.90554 16.3456 10.0185 16.1522 10.1106C15.9883 10.1888 15.8141 10.2468 15.4656 10.363L14.35 10.7349M14.35 10.7349L13.6281 10.9755C13.4567 11.0327 13.2676 10.988 13.1398 10.8602C13.012 10.7324 12.9673 10.5433 13.0245 10.3719L13.2651 9.65M14.35 10.7349L13.2651 9.65" stroke="#1C274C" stroke-width="1.5"></path> <path d="M22 12C22 16.714 22 19.0711 20.5355 20.5355C19.0711 22 16.714 22 12 22C7.28595 22 4.92893 22 3.46447 20.5355C2 19.0711 2 16.714 2 12C2 10.8717 2 9.87835 2.02008 9M12 2C7.28595 2 4.92893 2 3.46447 3.46447C3.03965 3.88929 2.73806 4.38921 2.52396 5" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> </g></svg>';
					html += '</td>';
					html += '</tr> \
					';
					$("#table_list").append(html);
				}
				vista_columnas_ventas();
			}
		},
		error: function (){
			$("#table_list .row_data").remove();
		}
	});
}
function cargar_datos(){
	let producto = $("#table_list .row_select").attr("data-producto");
    $.ajax({
		type: 'POST',
		dataType: "json",
		url: "control/ctrl_producto.php",
		data: {
			accion: "datos",
			buscar: producto,
		},
		success: function (datos) {
			//console.log(datos);
			let html = '';
			if (datos.estado == "ok") {
				let imagenes = datos.datos.imagen.split(",");
				if (imagenes[0].length <= 0){
					html +="<img src='libreria/imagenes/sin_imagen.png' style='max-width:80%;max-height:200px;margin:auto;' /><br />";
				} else {
					html +="<img src='libreria/imagenes/productos/"+imagenes[0]+"' style='max-width:80%;max-height:200px;margin:auto;' /><br />";
				}
				html +="<img src='' style='max-width:80%;max-height:200px;' /><br />";
				html +="<strong>Información del articulo</strong><br /><br />";
				
				html += "<table>";
				html += "<tr class=''><td><strong>Nombre</strong></td><td>"+datos.datos.nombre+"</td></tr>";
				html += "<tr class='row_info_extra' style='display:none;'><td><strong>Nombres alternativos</strong></td><td>"+datos.datos.nombre_alternativo+"</td></tr>";
				html += "<tr class='row_info_extra' style='display:none;'><td><strong>Fabricante</strong></td><td>"+datos.datos.fabricante+"</td></tr>";
				html += "<tr class='row_info_extra' style='display:none;'><td><strong>Modelo</strong></td><td>"+datos.datos.modelo+"</td></tr>";
				html += "<tr class='row_info_extra' style='display:none;'><td><strong>Serie</strong></td><td>"+datos.datos.serie+"</td></tr>";
				html += "<tr class=''><td><strong>Codigo</strong></td><td>"+datos.datos.codigo+"</td></tr>";
				html += "<tr class='row_info_extra' style='display:none;'><td><strong>Codigo de barras</strong></td><td>"+datos.datos.codigo_barras+"</td></tr>";
				html += "<tr class='row_info_extra' style='display:none;'><td><strong>Stock minimo</strong></td><td>"+datos.datos.stock_minimo+"</td></tr>";
				html += "<tr class=''><td><strong>Estado</strong></td><td>"+datos.datos.estado+"</td></tr>";
				html += "</table>";
			}
			//console.log(html);
			$("#producto_informacion").html(html);
			$("#btn_producto_informacion").html("Ver mas +");
		},
		error: function (){
			$("#producto_informacion").html("");
		}
	});
}
function agregar_fila_producto(codigo, nombre){
	let row_data = $("#new_table_list").find("tr.row_data[data-producto='"+codigo+"']");
	if(row_data.length <= 0){
		let idx = $("#new_table_list .row_data").length;
		let html = '<tr class="row_data" data-indice="'+(idx+1)+'" data-producto="'+codigo+'" data-codigo="0" data-estado="R">';
		html += '<td align="center" class="td_numero">'+(idx+1)+'</td>';
		html += '<td align="left" class="td_nombre" style="text-align:left;"></td>';
		html += '<td align="right" width="70" class="td_cantidad"><div><input type="number" value="0" min="0" step="1" /></div></td>';
		html += '<td align="right" width="70" class="td_precio_compra"><div>s/. <input type="number" value="0" min="0.00" step="0.1" /></div></td>';
		html += '<td align="right" width="70" class="td_precio_venta"><div>s/. <input type="number" value="0" min="0.00" step="0.1"  /></div></td>';
		html += '<td align="right" width="70" class="td_precio_minimo"><div>s/. <input type="number" value="0" min="0.00" step="0.1" /></div></td>';
		html += '<td align="center">';
		html += '<svg class="btn_remove" width="64px" height="64px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M20.5001 6H3.5" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> <path d="M9.5 11L10 16" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> <path d="M14.5 11L14 16" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> <path d="M6.5 6C6.55588 6 6.58382 6 6.60915 5.99936C7.43259 5.97849 8.15902 5.45491 8.43922 4.68032C8.44784 4.65649 8.45667 4.62999 8.47434 4.57697L8.57143 4.28571C8.65431 4.03708 8.69575 3.91276 8.75071 3.8072C8.97001 3.38607 9.37574 3.09364 9.84461 3.01877C9.96213 3 10.0932 3 10.3553 3H13.6447C13.9068 3 14.0379 3 14.1554 3.01877C14.6243 3.09364 15.03 3.38607 15.2493 3.8072C15.3043 3.91276 15.3457 4.03708 15.4286 4.28571L15.5257 4.57697C15.5433 4.62992 15.5522 4.65651 15.5608 4.68032C15.841 5.45491 16.5674 5.97849 17.3909 5.99936C17.4162 6 17.4441 6 17.5 6" stroke="#1C274C" stroke-width="1.5"></path> <path d="M18.3735 15.3991C18.1965 18.054 18.108 19.3815 17.243 20.1907C16.378 21 15.0476 21 12.3868 21H11.6134C8.9526 21 7.6222 21 6.75719 20.1907C5.89218 19.3815 5.80368 18.054 5.62669 15.3991L5.16675 8.5M18.8334 8.5L18.6334 11.5" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"></path> </g></svg>';
		html += '</td>';
		html += '</tr>';
		row_data = $(html);
		$("#new_table_list").append(row_data);
	}
	$("#new_table_list .row_data").removeClass("row_select");
	row_data.addClass("row_select");
	row_data.find(".td_nombre").html(nombre);
	let cant = row_data.find(".td_cantidad input[type='number']").val();
	row_data.find(".td_cantidad input[type='number']").val(parseInt(cant)+1);
	return row_data;
}
function vista_columnas_ventas(){
	let checks = $(".opciones_adicionales_view .opciones_adicionales_columns input[type='checkbox']");
	let tabla = $("#table_list");
	for (let idx=0; idx<checks.length; idx++){
		let check = $(checks[idx]);
		if (check.prop("checked")){
			tabla.find("."+check.attr("data-column")).css("display", "table-cell");
		} else {
			tabla.find("."+check.attr("data-column")).css("display", "none");
		}
	}
}
function resizeWindow(){
	resizePanel();
}
<?php get_header_javascript(); ?>
<?php alertas_pedidos_javascript(); ?>
<?php dialogo_loading_javascript(); ?>
<?php dialogo_image_javascript(); ?>
<?php dialogo_alert_javascript(); ?>
<?php dialogo_confirm_javascript(); ?>
<?php dialogo_product_javascript(); ?>
$(document).ready(function() {
	$( window ).on("resize", function(){
		resizeWindow();
	});
	resizeWindow();
	<?php get_header_ready(); ?>
	<?php alertas_pedidos_ready(); ?>
	<?php dialogo_loading_ready(); ?>
	<?php dialogo_image_ready(); ?>
	<?php dialogo_alert_ready(); ?>
	<?php dialogo_confirm_ready(); ?>
	<?php dialogo_product_ready(); ?>
    $("#filtro_buscar").on("input", function(event) {
		if (event.originalEvent.inputType === 'insertLineBreak') { // Comprueba si se ha ingresado un "retorno de carro" (Enter)
			let valor = $(this).val();
			ctrl_datos.buscar = valor;
			cargar_lista();
		}
	});
	$("#filtro_buscar").on("keydown", function(event) {
		if (event.keyCode === 13) {
			let valor = $(this).val();
			ctrl_datos.buscar = valor;
			cargar_lista();
			event.preventDefault();
		}
	});
	$("#filtro_buscar").next().click(function(){
		if ($("#filtro_buscar").prop("disabled")){
			return;
		}
		let valor = $("#filtro_buscar").val();
		ctrl_datos.buscar = valor;
		cargar_lista();
	});
	$("#btn_order_asc").click(function(){
		if ($("#filtro_buscar").prop("disabled")){
			return;
		}
		ctrl_datos.items_pagina = 0;
		ctrl_datos.items_cantidad = 0;
		ctrl_datos.orden = "DESC";
		$("#btn_order_asc").css("display", "none");
		$("#btn_order_desc").css("display", "flex");
		cargar_lista();
	});
    $("#btn_order_desc").click(function(){
		if ($("#filtro_buscar").prop("disabled")){
			return;
		}
		ctrl_datos.items_pagina = 0;
		ctrl_datos.items_cantidad = 0;
		ctrl_datos.orden = "ASC";
		$("#btn_order_asc").css("display", "flex");
		$("#btn_order_desc").css("display", "none");
		cargar_lista();
	});
	$("#btn_producto_informacion").click(function(){
		if ($("#btn_producto_informacion").html() == "Ver mas +"){
			$("#btn_producto_informacion").html("Ver menos -");
			$("#producto_informacion .row_info_extra").css("display", "block");
		} else {
			$("#btn_producto_informacion").html("Ver mas +");
			$("#producto_informacion .row_info_extra").css("display", "none");
		}
	});
	$("#table_list").on("click", ".btn_edit", function(){
		let row_data = $(this).closest('.row_data');
		let codigo = row_data.attr("data-producto");
		$.ajax({
			type: 'POST',
			dataType: "json",
			url: "control/ctrl_producto.php",
			data: {
				accion: "datos",
				buscar: codigo,
			},
			success: function (datos) {
				//console.log(datos);
				if (datos.estado == "ok") {
					let producto = datos.datos;
					open_dialog_product(function (codigo, data){
						if (data.imagenes.length > 0){
							row_data.find(".v_column_2 img").attr("src", "libreria/imagenes/productos/"+data.imagenes);
						} else {
							row_data.find(".v_column_2 img").attr("src", "libreria/imagenes/sin_imagen.png");
						}
						row_data.find(".v_column_3").html(data.cod_codigo);
						row_data.find(".v_column_4").html(data.nombre);
						row_data.find(".v_column_5").html(data.fabricante);
						row_data.find(".v_column_6").html(data.modelo);
						row_data.find(".v_column_7").html(data.serie);
						row_data.find(".v_column_8 input").val(data.stock_minimo);
						row_data.find(".v_column_9 select").val(data.estado);
						row_data.find(".v_column_10").html(data.prod_unitario_nombre);
						row_data.find(".v_column_11").html(data.prod_fracciones);
						row_data.click();
					}, function (){});
					$("#txt_prod_codigo").val(producto.producto_id);
					$("#txt_prod_imagenes").val(producto.imagen);
					$("#txt_prod_nombre").val(producto.nombre);
					$("#txt_prod_nombre_alternativo").val(producto.nombre_alternativo);
					$("#txt_prod_cod_codigo").val(producto.codigo);
					$("#txt_prod_fabricante").val(producto.fabricante);
					$("#txt_prod_modelo").val(producto.modelo);
					$("#txt_prod_serie").val(producto.serie);
					$("#txt_prod_stock_minimo").val(producto.stock_minimo);
					$("#txt_prod_unitario_id").val(producto.producto_unitario_id);
					$("#txt_prod_fracciones").val(producto.producto_fracciones);
					$("#txt_prod_estado").val(producto.estado);
					if (producto.producto_unitario_id > 0){
						$("#_dialogo_product .btn_vincular_unidades").html(producto.producto_unitario_nombre);
					} else {
						$("#_dialogo_product .btn_vincular_unidades").html("No vender por unidad");
					}
					if (producto.imagen.length > 0){
						$("#_dialogo_product .input_image").css("background-image", "url(libreria/imagenes/productos/"+producto.imagen+")");
					} else {
						$("#_dialogo_product .input_image").css("background-image", "none");
					}
				} else if (datos.estado == "error") {
					open_dialog_alert(datos.mensaje);
				} else {
					open_dialog_alert("ocurrió un error inesperado.");
				}
				close_loading();
			}
		});
	});
	$("#btn_nuevo").click(function(){
		open_dialog_product(function(codigo, data){
			if (codigo > 0){
			}
		}, function(){});
	});
	$("#table_list").on("click", ".row_data", function(){
		tabla_lista = "#table_list";
		if($("#filtro_buscar").prop("disabled")){
			return;
		}
		$("#table_list .row_select").removeClass("row_select");
		$(this).addClass("row_select");
		cargar_datos();
	});
	$(".opciones_adicionales_btn").click(function(){
		let panel = $(this).next();
		if (panel.css("display") != "none"){
			panel.slideUp(500);
		} else {
			panel.slideDown(500);
		}
	});
	$(".opciones_adicionales_view .opciones_adicionales_columns").on("change", "input[type='checkbox']", function(){
		vista_columnas_ventas();
	});
	$("#tool_editar_datos").change(function(){
		if ($("#tool_editar_datos").prop("checked")){
			$("#tool_editar_datos").parent().next().css("display", "flex");
			$("#table_list input[type='number']").prop("disabled", false);
			$("#table_list select").prop("disabled", false);
			$("#table_list .btn_edit").css("display", "none");
		} else {
			$("#tool_editar_datos").parent().next().css("display", "none");
			$("#table_list input[type='number']").prop("disabled", true);
			$("#table_list select").prop("disabled", true);
			$("#table_list .btn_edit").css("display", "flex");
		}
	});
	$("#btn_save_all").click(function(){
		if($("#tool_editar_datos").prop("checked")){
			open_dialog_confirm("¿Desea guardar los cambios?", function(){
				let filas_seleccionadas = $("#table_list .row_data");
				let datos = [];
				for (let idx=0; idx<filas_seleccionadas.length; idx++){
					let row_data = $(filas_seleccionadas[idx]);
					let producto = row_data.attr("data-producto");
					let stock_minimo = row_data.find(".td_stock_minimo input[type='number']").val();
					let estado = row_data.find(".v_column_9 select").val();
					datos.push({
								producto: producto,
								stock_minimo: stock_minimo,
								estado: estado,
					});
				}	
				open_loading();
				$.ajax({
					type: 'POST',
					dataType: "json",
					url: "control/ctrl_producto.php",
					data: {
						accion: "registrar_masivo",
						datos: JSON.stringify(datos),
					},
					success: function (datos) {
						//console.log(datos);
						if (datos.estado == "ok") {
							cargar_lista();
							$("#tool_editar_datos").prop("checked", false);
							$("#tool_editar_datos").parent().next().css("display", "none");
						} else {
							open_dialog_alert("Ocurrio un error inesperado");
						}
						close_loading();
					},
					error: function (){
						close_loading();
						open_dialog_alert("Ocurrio un error inesperado");
					}
				});	
			});
		}
	});
	var tabla_lista = "#table_list";
	$("#_dialogo_producto_unidad .table_view").on("click", ".row_data", function(){
		tabla_lista = "#_dialogo_producto_unidad .table_view";
	});
	$(document).keydown(function(event) {
		var ajustarScroll = function($elemento) {
			let height_view = $(tabla_lista).parent().outerHeight()/2;
			let pos_top = $(tabla_lista).parent().position().top+($(tabla_lista).parent().outerHeight(true)/2.0);
			$(tabla_lista).parent().scrollTop($elemento.position().top+$(tabla_lista).parent().scrollTop()-pos_top);
		}
		if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
			let $filas = $(tabla_lista+' tr');
			let $filaSeleccionada = $(tabla_lista+' .row_select');
			if (!$filaSeleccionada.length) {
                $filas.eq(0).addClass('row_select');
                ajustarScroll($filas.eq(0));
                return;
            }
            let indiceSeleccionada = $filas.index($filaSeleccionada);
            if (event.key === 'ArrowUp' && indiceSeleccionada > 1) {
                $filaSeleccionada.removeClass('row_select');
                $filas.eq(indiceSeleccionada - 1).addClass('row_select');
                $filas.eq(indiceSeleccionada - 1).click();
                ajustarScroll($filas.eq(indiceSeleccionada - 1));
            } else if (event.key === 'ArrowDown' && indiceSeleccionada < $filas.length - 1) {
                $filaSeleccionada.removeClass('row_select');
                $filas.eq(indiceSeleccionada + 1).addClass('row_select');
                $filas.eq(indiceSeleccionada + 1).click();
                ajustarScroll($filas.eq(indiceSeleccionada + 1));
            }
			event.preventDefault();
		}
	});
	cargar_lista();
	close_loading();
});
</script>
</html>
