<?php

$cs = Yii::app()->getClientScript();

$cs->registerCssFile(FaHtml::getJsUrl('handsontable/dist/pikaday/pikaday.css'));
$cs->registerCssFile(FaHtml::getJsUrl('handsontable/handsontable.full.min.css'));
$cs->registerCssFile(FaHtml::getJsUrl('ruleJS/src/handsontable.formula.css'));
$cs->registerCssFile(FaHtml::getJsUrl('handsontable/plugins/removeRow/handsontable.removeRow.css'));

$cs->registerScriptFile(FaHtml::getJsUrl('handsontable/handsontable.full.js'));
$cs->registerScriptFile(FaHtml::getJsUrl('handsontable/dist/moment/moment.js'));
$cs->registerScriptFile(FaHtml::getJsUrl('handsontable/dist/pikaday/pikaday.js'));
$cs->registerScriptFile(FaHtml::getJsUrl('ruleJS/bower_components/ruleJS/dist/full/ruleJS.all.full.js'));
$cs->registerScriptFile(FaHtml::getJsUrl('ruleJS/src/handsontable.formula.js'));
$cs->registerScriptFile(FaHtml::getJsUrl('handsontable/plugins/removeRow/handsontable.removeRow.js'));
$cs->registerScriptFile(FaHtml::getJsUrl('estimativas.js'));

$this->renderPartial('_cabecalho', array('model' => $model));
$this->renderPartial('_estimativas_planejamento_estilos', array('model' => $model));
?>

	<div class="div-atividades">
	    <h1 class="novo-layout"><?php echo $arquivo->ARQ_TITULO; ?></h1>
	    <span class="options">
		    <a href="<?php echo $arquivo->getUrl(); ?>" id="download-xml" class="button">Download XML</a>
	    	<label id="ck-button" class="">
				<input type="checkbox" name="milestone-only" id="milestone-only" /><i class="fa fa-check"></i><span class="milestone-label-text">Mostrar somente marcos</span>
			</label>
		</span>
		<div id="planilha-planejamento"></div>
	</div>
	<script type="text/javascript">
		function milestoneRenderer(instance, td, row, col, prop, value, cellProperties) {
		    Handsontable.renderers.TextRenderer.apply(this, arguments);
		    if(instance.getDataAtRowProp(row, 'milestone') == 1) {
		    	td.style.fontWeight = 'bold';
			    td.style.color = 'green';
			    td.style.background = '#CEC';
			    $(td).parent('tr').addClass('planejamento-milestone');
		    } else {
			    $(td).parent('tr').addClass('planejamento-task');
				if($('#milestone-only').is(':checked')) {
					$(td).parent('tr').addClass('sr-only');
				} else {
					$(td).parent('tr').removeClass('sr-only');
				}
		    }
		    if(prop == 'descricao') {
				$(td).addClass('planejamento-nivel-'+instance.getDataAtRowProp(row, 'nivel'));
				td.innerHTML = instance.getDataAtRowProp(row, 'codigo') + ' ' + value;
		    }
		    if(instance.getDataAtRowProp(row, 'summary') == 1) {
			    $(td).parent('tr').addClass('planejamento-summary');
		    }
		    return td;
		}
		var dataPlanejamento = <?php echo CJSON::encode($planejamento); ?>;
		var containerPlanejamento,	hotPlanejamento;
		containerPlanejamento = document.getElementById('planilha-planejamento');

		hotPlanejamento = new Handsontable(containerPlanejamento, {
			data: dataPlanejamento,
			rowHeights: 30,
	    	stretchH: 'all',
			colWidths: [300, 100, 100, 100, 120, 120, 180],
			colHeaders: ['Descrição', 'Antecessores','Duração', 'Trabalho', 'Início', 'Fim', 'Envolvidos'],
			columns: [
				{data: 'descricao', renderer: milestoneRenderer},
				{data: 'predecessor', renderer: milestoneRenderer},
				{data: 'duracao', renderer: milestoneRenderer},
				{data: 'trabalho', renderer: milestoneRenderer},
				{data: 'inicio', renderer: milestoneRenderer},
				{data: 'fim', renderer: milestoneRenderer},
				{data: 'envolvidos', renderer: milestoneRenderer},
			],
	    	readOnly: true,
		});
		function checkMilestone() {
			if($('#milestone-only').is(':checked')) {
				$('.planejamento-task').addClass('sr-only');
				$('#ck-button').addClass('checked');
			} else {
				$('.planejamento-task').removeClass('sr-only');
				$('#ck-button').removeClass('checked');
			}
		}
		checkMilestone();
		$('#milestone-only').change(checkMilestone);
	</script>