<form id="formProducto" action="/productos/save" method="post">
	<input type="hidden" name="id" id="idProducto" value="0" />
	<div class="form-group row">
		<label for="nombre" class="col-sm-2 col-form-label">Nombre</label>
		<div class="col-sm-4">
			<input type="text" name="nombre" class="form-control nombre-producto" placeholder="Nombre">
		</div>
		<label for="idCategoria" class="col-sm-2 col-form-label">Categoría</label>
		<div class="col-sm-4">
			<select name ="id_categoria" class="form-control id-categoria">
				<?php
					foreach($categorias as $k => $v) {
						$selected = '';
						if (!empty($_POST['id_categoria'])) {
							if ($_POST['id_categoria'] == $k) {
								$selected =' selected="selected"';
							}
						}
						echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
					}
				?>
			</select>
		</div>
	</div>

	 <div class="form-group row">
	 	<label for="psNombre" class="col-sm-2 col-form-label">Prestashop</label>
		<div class="col-sm-4">
			<input type="text" name="ps_nombre" class="form-control ps-nombre" placeholder="Nombre Prestahop">
		</div>

		<label for="idProveedor" class="col-sm-2 col-form-label">Proveedor</label>
		<div class="col-sm-4">
			<select name ="id_proveedor" class="form-control id-proveedor">
				<?php
					foreach($distribuidores as $k => $v) {
						$selected = '';
						if (!empty($_POST['id_proveedor'])) {
							if ($_POST['id_proveedor'] == $k) {
								$selected =' selected="selected"';
							}
						}
						echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
					}
				?>
			</select>
		</div>
	</div>

	<?php /*
	<div class="form-group row">
		<label for="stock" class="col-sm-2 col-form-label">Stock</label>
		<div class="col-sm-4">
			<input type="number" name="stock" class="form-control stock">
		</div>

		<label for="disponibles" class="col-sm-2 col-form-label">Disponibles</label>
		<div class="col-sm-4">
			<input type="number" name="disponibles" class="form-control disponibles">
		</div>
	</div>
	*/ ?>

	<?php /*
	
	*/ ?>
	<div class="form-group row">
		<?php 
			/*
			<label for="precio" class="col-sm-2 col-form-label">Precio</label>
			<div class="col-sm-4">

				<input type="number" name="precio" class="form-control precio" placeholder="Precio">
				<span class="precio25"> 
				</span>
				<span class="precio30"> 
				</span>
			</div>
			*/
		?>
		<label for="ps_id" class="col-sm-2 col-form-label">Id Prestashop</label>
		<div class="col-sm-4">
			<input type="number" name="ps_id" class="form-control ps-id" placeholder="Id Prestashop" readonly="readonly">
		</div>
		<label for="ps_id" class="col-sm-2 col-form-label pr-0 el-ps-asociado">Id PS Asociado</label>
		<div class="col-sm-4 el-ps-asociado">
			<input type="number" name="ps_id_asociado" class="form-control ps-id-asociado" placeholder="Id Prestashop Asociado">
		</div>
	</div>

	<div class="form-group row">
		<label for="costoNeto" class="col-sm-2 col-form-label">Costo Neto</label>
		<div class="col-sm-4">
			<input type="number" name="costo_neto" class="form-control costo-neto" placeholder="Costo Neto">
		</div>

		<label for="costo" class="col-sm-2 col-form-label">Costo</label>
		<div class="col-sm-4">
			<input type="number" name="costo" class="form-control costo" placeholder="Costo">
		</div>
	</div>

	<div class="form-group row">
		<label for="precio" class="col-sm-2 col-form-label">Precio</label>
		<div class="col-sm-4">

			<input type="number" name="precio" class="form-control precio" placeholder="Precio">
			<span class="precio25"> 
			</span>
			<span class="precio30"> 
			</span>
		</div>
	</div>

	<div class="form-group row">
		<label for="codigo_barra" class="col-sm-2 col-form-label">Código Barra</label>
		<?php
			if (in_array($_SESSION['usuario']['perfil'], [1, 2, 3])) { ?>
				<div class="col-sm-3">
					<input type="text" name="codigo_barra" class="form-control input-codigo-barra" placeholder="Código barra">
				</div>
				<div class="col-sm-1 p-0">
					<button type="button" class="btn btn-primary get-code"><span class="d-sm-inline"><i class="fas fa-undo"></i></span></button>
				</div>
			<?php
			}
			else {
				?>
				<div class="col-sm-4">
					<input type="text" name="codigo_barra" class="form-control input-codigo-barra" placeholder="Código barra">
				</div>
				<?php
			}
		?>
		<label for="sku" class="col-sm-2 col-form-label">SKU</label>
		<div class="col-sm-4">
			<input type="text" name="sku" class="form-control input-sku" placeholder="SKU">
		</div>
	</div>

	<div class="form-group row">
		<label for="id_ranking" class="col-sm-2 col-form-label">Ranking</label>
		<div class="col-sm-4">
			<select name ="id_ranking" class="form-control id-ranking">
				<?php
					foreach($rankings as $k => $v) {
						$selected = '';
						if (!empty($_POST['id_ranking'])) {
							if ($_POST['id_ranking'] == $k) {
								$selected =' selected="selected"';
							}
						}
						echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
					}
				?>
			</select>
		</div>

		<div class="col-sm-4 offset-sm-2">
			<input type="hidden" name="ignora_descuadre" value="0">
			<input type="checkbox" name="ignora_descuadre" id="ignoraDescuadre" class="check-ignora-descuadre big-check" value="1">
			<label for="ignoraDescuadre" class="big-check-label">Ignora descuadre</label>
		</div>
	</div>

	<div class="form-group row">
		<label class="col-sm-2 col-form-label">Reposición</label>

		<div class="col-sm-4">
			<select name="tipo_reposicion" class="form-control tipo-reposicion">
				<option value="0">No definido</option>
				<option value="1">Stock mínimo 1 mes</option>
				<option value="2">Stock mínimo 2 meses</option>
				<option value="3">Stock mínimo 3 meses</option>
				<option value="-1">No reponer</option>
			</select>
		</div>

		<label class="col-sm-2 col-form-label razon-no-reponer-label d-none">
			Razón no reponer
		</label>

		<div class="col-sm-4 razon-no-reponer-wrapper d-none">

			<select name="razon_no_reponer" class="form-control razon-no-reponer">
				<option value="0">Seleccionar…</option>
				<option value="1">Producto descontinuado</option>
				<option value="2">Baja rotación / baja rentabilidad</option>
				<option value="3">Producto en liquidación</option>
				<option value="5">Problemas con proveedor</option>
				<option value="5">Reemplazado por otro producto</option>
				<option value="6">Producto de temporada</option>
				<option value="7">Otro motivo</option>
			</select>

			<input type="text"
				name="razon_no_reponer_detalle"
				class="form-control mt-2 razon-detalle d-none"
				placeholder="Describe el motivo">
		</div>
	</div>

	<?php
	if (!empty($marketplaces)) {
		?>
		<div class="form-group row">
		<?php 
		foreach ($marketplaces as $mpId => $mpNombre) {
			?>
			<div class="col-6 row mx-0 mp-detalles" data-id-mp="<?php echo $mpId;?>">
				<div class="col-12">
					<h5><?php echo $mpNombre;?></h5>
				</div>
				<div class="col-12 col-lg-4">
					<input type="hidden" name="mp_activo[<?php echo $mpId;?>]" value="0">
					<input type="checkbox" name="mp_activo[<?php echo $mpId;?>]" id="mp-activo-<?php echo $mpId;?>" class="check-mp-activo big-check" value="1">
					<label for="mp-activo-<?php echo $mpId;?>" class="big-check-label">Creado</label>
				</div>
				
				<label for="mp-stock-<?php echo $mpId;?>" class="col-5 col-lg-2 col-form-label">Stock</label>
				<div class="col-6 col-lg-4">
					<input type="number" id="mp-stock-<?php echo $mpId;?>" name="mp_stock[<?php echo $mpId;?>]" class="form-control input-mp-stock" placeholder="Stock">
				</div>
			</div>
			<?php
		}
		?>
		</div>
		<?php
	}
	?>

	<div class="form-group row">
		<div>
			<label for="descripcion" class="col-sm-12 col-form-label">Descripción</label>
		</div>
		<div class="col-sm-12">
			<textarea class="form-control text-descripcion" name="descripcion" rows="6"></textarea>
		</div>
	</div>

	<div class="form-group row">
		<div>
			<label for="observacion" class="col-sm-12 col-form-label">Observaciones</label>
		</div>
		<div class="col-sm-12">
			<textarea class="form-control text-observaciones" name="observaciones" rows="6"></textarea>
		</div>
	</div>
</form>

<div class="alert-producto alert alert-warning mt-3 mb-0" style="display: none;"></div>

<script type='text/javascript'>
<?php
	//variables en js para cargar selects
	//echo 'var ps_productos = ' . json_encode($ps_productos) . ';';
?>

$(document).ready(function () {

    function toggleRazon() {
        const tipo = $('.tipo-reposicion').val();

        if (tipo === "-1") {
            $('.razon-no-reponer-label').removeClass('d-none');
            $('.razon-no-reponer-wrapper').removeClass('d-none');
        } else {
            $('.razon-no-reponer-label').addClass('d-none');
            $('.razon-no-reponer-wrapper').addClass('d-none');

            $('.razon-no-reponer').val('');
            $('.razon-detalle').addClass('d-none').val('');
        }
    }

    function toggleRazonDetalle() {
        const razon = $('.razon-no-reponer').val();

        if (razon === "7") {
            $('.razon-detalle').removeClass('d-none');
        } else {
            $('.razon-detalle').addClass('d-none').val('');
        }
    }

    $('.tipo-reposicion').on('change', toggleRazon);
    $('.razon-no-reponer').on('change', toggleRazonDetalle);

    toggleRazon();
    toggleRazonDetalle();
});

</script>
