<?php
$that  = $this;
$bodyKryterium = function ($kryterium, $i, $attr) use ($that) {

	echo $that->Form->input("bKryteriumOceny{$i}B", array('type' => 'hidden', 'value' => $kryterium['bGid']));
	echo $that->Form->input("bKryteriumOceny{$i}C", array('type' => 'hidden', 'value' => $kryterium['bNazwa']));

	if ($kryterium['bRodzaj'] === 'OcenaOpisowa') {
		$attr['type'] = 'textarea';
		$attr['rows'] = '2';
		echo '<div class="panel-heading">' . $kryterium['bNazwa'] . '</div>';
		echo '<div class="panel-body">';
		echo $that->Form->input("bKryteriumOceny$i", $attr);
		echo '</div>';
	} else if ($kryterium['bRodzaj'] === 'OcenaPunktowa') {
		$attr['maxlength'] = '3';
		$attr['size'] = '3';
		$attr['style'] = 'width: 50px; border: thin solid #32a1ce;';
		echo '<div class="panel-heading inline-block" style="width: 100%;">' . $kryterium['bNazwa'] . '&nbsp;&nbsp;&nbsp;' . $that->Form->input("bKryteriumOceny$i", $attr) . '</div>';
	} else if($kryterium['bRodzaj'] === 'OcenaPunktowoOpisowa') {
		echo '<div class="panel-heading">' . $kryterium['bNazwa'] . '&nbsp;&nbsp;&nbsp;' . $that->Form->input("bKryteriumOceny$i", array_merge($attr, array('maxlength' => '3', 'size' => '3', 'style' => 'width: 50px; border: thin solid #32a1ce;'))) . '</div>';
		echo '<div class="panel-body ">';
		echo $that->Form->input("bKryteriumOceny{$i}A", array_merge($attr, array('type' => 'textarea', 'rows' => '2')));
		echo '</div>';
	}
}

?>

<div class="row" id="EdytujOcenaFormContainer">
	<div class="col-xs-12">
		<div class="box box-danger with-border">
			<?php echo $this->Form->create(null, ['templates' => ['submitContainer' => '{{content}}',], 'id' => 'bOcenaProfesjonalna', 'name' => 'bOcenaProfesjonalna', 'class' => 'form-horizontal']); ?>
			<?php echo $this->Form->input('bGid', array('type' => 'hidden', 'value' => $ocena['bGid'])); ?>
			<?php echo $this->Form->input('bDataDo', array('type' => 'hidden', 'value' => $ocena['bDataDo'])); ?>
			<?php echo $this->Form->input('bDataOd', array('type' => 'hidden', 'value' => $ocena['bDataOd'])); ?>
			<?php echo $this->Form->input('bNazwa', array('type' => 'hidden', 'value' => $ocena['bNazwa'])); ?>
			<?php echo $this->Form->input('bDataUtworzenia', array('type' => 'hidden', 'value' => $ocena['bDataUtworzenia'])); ?>

			<div class="box-body">
				<div class="row">
					<div class="col-xs-12">
						<h4 style="margin-left: 0.5em;">Dane podstawowe</h4>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12 form-horizontal">
						<div class="form-group">
							<label class="col-xs-12 col-md-2 control-label">Nazwa</label>
							<div class="col-xs-12 col-md-9 pt7">
								<?php echo $ocena['bNazwa']; ?>
							</div>
						</div>
						<?php
							$dataOd = date('Y-m-d', strtotime($ocena['bDataOd']));
							$dataDo = date('Y-m-d', strtotime($ocena['bDataDo']));
						?>
						<div class="form-group">
							<label class="col-xs-12 col-md-2 control-label">Data Od</label>
							<div class="col-xs-12 col-md-2 pt7">
								<?php echo $dataOd; ?>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-12 col-md-2 control-label">Data Do</label>
							<div class="col-xs-12 col-md-2 pt7">
								<?php echo $dataDo; ?>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<h4 style="margin-left: 0.5em;">Ocena wybranych kryteriów</h4>
					</div>
				</div>
				<div class="row" style="margin-left: 3%">
					<div class="col-xs-12">
						<div class="well">
							<h5><strong>Wybrane kryteria obowiązkowe</strong></h5>
							<?php
								$i = 0;
								foreach ($kryteriaWymagane as $kryterium) :
									$attr = array('required' => 'required', 'class' => 'form-control inline-block', 'div' => false, 'label' => false);
								?>
								<?php if ($kryterium['bRodzaj'] !== 'Grupa') : ?>

								<div class="panel panel-success">
									<?php
									$bodyKryterium($kryterium, $i, $attr);
									$i++;
									?>
								</div>
								<?php endif; ?>
							<?php endforeach; ?>
						</div>
						<div class="well">
							<h5><strong>Wybrane kryteria dodatkowe</strong></h5>
							<?php foreach ($kryteriaNieWymagane as $kryterium) :
								$attr = array('class' => 'form-control inline-block ', 'div' => false, 'label' => false);
								?>
								<?php if ($kryterium['bRodzaj'] !== 'Grupa') : ?>

									<div class="panel panel-info">
										<?php
										$bodyKryterium($kryterium, $i, $attr);
										$i++;
										?>
									</div>
								<?php endif; ?>
							<?php endforeach; ?>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<h4 style="margin-left: 0.5em;">Wnioski dotyczące pracownika</h4>
					</div>
				</div>
				<div class="row" style="margin-left: 3%">
					<div class="col-xs-12">
						<div class="well">
							<h5><strong>Rodzaje wniosku</strong></h5>
							<?php foreach ($wnioskiItems as $key => $wniosek) : ?>
							<?php echo $this->Form->input("bWniosekOceny{$key}B", array('type' => 'hidden', 'value' => $wniosek['bGid'])); ?>
							<?php echo $this->Form->input("bWniosekOceny{$key}C", array('type' => 'hidden', 'value' => $wniosek['bNazwa'])); ?>
								<div class="panel panel-warning">
									<div class="panel-heading"><?php echo $wniosek['bNazwa']; ?></div>
									<div class="panel-body">
									<?php
										echo '<small>Uszczegółowienie wniosku</small>';
										echo $this->Form->input("bWniosekOceny$key", array('class' => 'form-control', 'div' => true, 'label' => false));
									?>
									</div>
								</div>
							<?php endforeach; ?>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="pull-right">
								<a style="display: inline-block;" data-menu-action="/szablon/daj_oceny_pracownika" href="/szablon/daj_oceny_pracownika" class="btn btn-default pull-left">Anuluj</a>
								<?php echo $this->Form->submit('Zapisz', array('name' => 'data[action]', 'div' => false, 'class' => 'btn btn-success')); ?>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php echo $this->Form->end(); ?>
	</div>
</div>

<script>
    $(document).ready(function () {

        $("#EdytujOcenaFormContainer form").validate({
            submitHandler: function (form) {
                var formData = $(form).serializeArray();
                var formAction = '<?php echo $local_url; ?>szablon/edytuj_ocena_pracownika'

                $.ajax({
                    type: 'POST',
                    url: formAction,
                    data: formData
                }).done(function (response) {
                    response = JSON.parse(response);
                    console.log(response);
                    if(response['error'] != undefined) {
                        showError(response['error']);
                    } else {
                        hideMessages();
                        showSuccess(response['message']);
                        loadAjaxView('/szablony_oceny_pracownika');
                        $('[data-value="oceny"]').click();
                        $('[data-value="oceny"]').data('active', 1);
                    }
                });
            }
        });
    });
</script>

<?php echo $this->element('alert_close'); ?>

