<?php $helper = $this->getHelper() ?>
<?php $params = $this->getParams() ?>
<?php $exist = false ?>
<?php $child = null ?>
<?php $settings = null ?>
<?php $categories = $helper->getModel('MGS\Blog\Model\Category')->getCollection()->addFieldToFilter('status', 1) ?>

<div class="edit-static-form">
	
	<div class="line">
		<div class="col-des-12 builder-title">
			<h2><?php echo __('Latest Post Block') ?></h2>
		</div>
	</div>
	<form id="section-form" enctype="multipart/form-data" method="POST" action="<?php echo $this->getUrl('fbuilder/element/save') ?>" data-hasrequired="<?php echo __('* Required Fields') ?>" data-mage-init='{"validation":{}}'>
		<input type="hidden" name="page_id" value="<?php echo $this->getRequest()->getParam('page_id') ?>">
		<input id="block_type" type="hidden" name="type" value="latest_post">
		<input id="block_block_name" type="hidden" name="block_name" value="<?php echo $params['block'] ?>">
		<?php if(isset($params['id'])): ?>
			<?php 
				$child =  $this->getModel('MGS\Fbuilder\Model\Child')->load($params['id']);
				$exist = true;
				$settings = json_decode($child->getSetting(), true);
			?>
			<input type="hidden" name="child_id" value="<?php echo $params['id'] ?>">
		<?php endif ?>
		
		<div class="mgs-tab data items product" data-mage-init='{"tabs":{"openedState":"active"}}'>
			
			<?php 
				echo $this->getLayout()->createBlock('Magento\Framework\View\Element\Template')
					->setExist($exist)
					->setBuilderChild($child)
					->setDisableContent(1)
					->setBuilderHelper($helper)
					->setTemplate('MGS_Fbuilder::panel/create/element/widgets/general_tab.phtml')->toHtml() ;
			?>
			
			<div class="data item title" aria-labeledby="tab-label-additional-title" data-role="collapsible" id="tab-label-additional">
				<a class="data switch" tabindex="-1" data-toggle="switch" href="#additional" id="tab-label-additional-title">
					<?php echo __('Post Config') ?>
				</a>
			</div>
			
			<div class="data item content" id="additional" data-role="content" style="display:none">
				<?php if(count($categories)>0): ?>
					<?php 
						$arrCats = [];
						if($exist){
							$arrCats = $settings['post_category'];
						}
					?>
				
					<div class="form-group line grid-perrow">
						<label for="perrow" class="col-tb-4 control-label"><?php echo __('Filter post by category') ?></label>
						<div class="col-tb-8">
							<select multiple="multiple" name="setting[post_category][]" class="multiple input-text" size="5" id="post_category">
								<?php foreach($categories as $blogCategory): ?>
									<?php $value = $blogCategory->getCategoryId(); ?>
									<option value="<?php echo $value ?>"<?php if($arrCats != null && in_array($value, $arrCats)): ?> selected="selected"<?php endif ?>><?php echo $blogCategory->getTitle() ?></option>
								<?php endforeach ?>
							</select>
						</div>
					</div>
				<?php endif ?>
				
				<div class="form-group line">
					<label for="number_of_posts" class="col-tb-4 control-label"><?php echo __('Number of post to display') ?></label>
					<div class="col-tb-8">
						<input type="text" id="limit" class="input-text required-entry validate-number" name="setting[limit]"<?php if($exist): ?> value="<?php echo $settings['limit'] ?>"<?php endif ?> />
					</div>
				</div>
				
				<div class="form-group line grid-perrow">
					<label for="perrow" class="col-tb-4 control-label"><?php echo __('Items per row (default)') ?></label>
					<div class="col-tb-8">
						<select id="perrow" class="input-text" name="setting[perrow]">
							<option value="1"<?php if($exist && ($settings['perrow']==1)): ?> selected="selected"<?php endif ?>>1</option>
							<option value="2"<?php if($exist && ($settings['perrow']==2)): ?> selected="selected"<?php endif ?>>2</option>
							<option value="3"<?php if($exist && ($settings['perrow']==3)): ?> selected="selected"<?php endif ?>>3</option>
							<option value="4"<?php if($exist && ($settings['perrow']==4)): ?> selected="selected"<?php endif ?>>4</option>
							<option value="5"<?php if($exist && ($settings['perrow']==5)): ?> selected="selected"<?php endif ?>>5</option>
							<option value="6"<?php if($exist && ($settings['perrow']==6)): ?> selected="selected"<?php endif ?>>6</option>
							<option value="7"<?php if($exist && ($settings['perrow']==7)): ?> selected="selected"<?php endif ?>>7</option>
							<option value="8"<?php if($exist && ($settings['perrow']==8)): ?> selected="selected"<?php endif ?>>8</option>
						</select>
					</div>
				</div>
				
				<div class="form-group line grid-perrow">
					<label for="perrow_tablet" class="col-tb-4 control-label"><?php echo __('Items per row (tablet)') ?></label>
					<div class="col-tb-8">
						<select id="perrow_tablet" class="input-text" name="setting[perrow_tablet]">
							<option value="1"<?php if($exist && ($settings['perrow_tablet']==1)): ?> selected="selected"<?php endif ?>>1</option>
							<option value="2"<?php if($exist && ($settings['perrow_tablet']==2)): ?> selected="selected"<?php endif ?>>2</option>
							<option value="3"<?php if($exist && ($settings['perrow_tablet']==3)): ?> selected="selected"<?php endif ?>>3</option>
							<option value="4"<?php if($exist && ($settings['perrow_tablet']==4)): ?> selected="selected"<?php endif ?>>4</option>
							<option value="5"<?php if($exist && ($settings['perrow_tablet']==5)): ?> selected="selected"<?php endif ?>>5</option>
							<option value="6"<?php if($exist && ($settings['perrow_tablet']==6)): ?> selected="selected"<?php endif ?>>6</option>
							<option value="7"<?php if($exist && ($settings['perrow_tablet']==7)): ?> selected="selected"<?php endif ?>>7</option>
							<option value="8"<?php if($exist && ($settings['perrow_tablet']==8)): ?> selected="selected"<?php endif ?>>8</option>
						</select>
					</div>
				</div>
				
				<div class="form-group line grid-perrow">
					<label for="perrow_mobile" class="col-tb-4 control-label"><?php echo __('Items per row (mobile)') ?></label>
					<div class="col-tb-8">
						<select id="perrow_mobile" class="input-text" name="setting[perrow_mobile]">
							<option value="1"<?php if($exist && ($settings['perrow_mobile']==1)): ?> selected="selected"<?php endif ?>>1</option>
							<option value="2"<?php if($exist && ($settings['perrow_mobile']==2)): ?> selected="selected"<?php endif ?>>2</option>
							<option value="3"<?php if($exist && ($settings['perrow_mobile']==3)): ?> selected="selected"<?php endif ?>>3</option>
							<option value="4"<?php if($exist && ($settings['perrow_mobile']==4)): ?> selected="selected"<?php endif ?>>4</option>
							<option value="5"<?php if($exist && ($settings['perrow_mobile']==5)): ?> selected="selected"<?php endif ?>>5</option>
							<option value="6"<?php if($exist && ($settings['perrow_mobile']==6)): ?> selected="selected"<?php endif ?>>6</option>
							<option value="7"<?php if($exist && ($settings['perrow_mobile']==7)): ?> selected="selected"<?php endif ?>>7</option>
							<option value="8"<?php if($exist && ($settings['perrow_mobile']==8)): ?> selected="selected"<?php endif ?>>8</option>
						</select>
					</div>
				</div>
				
				<div class="form-group line" id="list_number_col">
					<label for="numbercol" class="col-tb-4 control-label"><?php echo __('Number col') ?></label>
					<div class="col-tb-8">
						<select id="numbercol" class="input-text" name="setting[numbercol]">
							<option value="1"<?php if($exist && ($settings['numbercol']==1)): ?> selected="selected"<?php endif ?>>1</option>
							<option value="2"<?php if($exist && ($settings['numbercol']==2)): ?> selected="selected"<?php endif ?>>2</option>
							<option value="3"<?php if($exist && ($settings['numbercol']==3)): ?> selected="selected"<?php endif ?>>3</option>
							<option value="4"<?php if($exist && ($settings['numbercol']==4)): ?> selected="selected"<?php endif ?>>4</option>
						</select>
					</div>
				</div>
				
				<div class="form-group line" id="item_per_col">
					<label for="percol" class="col-tb-4 control-label"><?php echo __('Item per col') ?></label>
					<div class="col-tb-8">
						<input type="text" id="percol" class="input-text required-entry validate-number" name="setting[percol]"<?php if($exist): ?> value="<?php echo $settings['percol'] ?>"<?php endif ?> />
					</div>
				</div>
				
				<div class="form-group line">
					<label for="template" class="col-tb-4 control-label"><?php echo __('View mode') ?></label>
					<div class="col-tb-8">
						<select id="template" class="input-text" name="setting[template]" onchange="setTemplate()">
							<option value="grid.phtml"<?php if($exist && ($settings['template']=='grid.phtml')): ?> selected="selected"<?php endif ?>><?php echo __('Grid') ?></option>
							<option value="list.phtml"<?php if($exist && ($settings['template']=='list.phtml')): ?> selected="selected"<?php endif ?>><?php echo __('List') ?></option>
							<option value="grid-item.phtml"<?php if($exist && ($settings['template']=='grid-item.phtml')): ?> selected="selected"<?php endif ?>><?php echo __('Grid Item') ?></option>
							<option value="grid-masory.phtml"<?php if($exist && ($settings['template']=='grid-masory.phtml')): ?> selected="selected"<?php endif ?>><?php echo __('Grid Masory') ?></option>
						</select>
					</div>
				</div>
				
				<div class="form-group line">
					<label for="hide_thumbnail" class="col-tb-4 control-label"><?php echo __('Hide Thumbnail') ?></label>
					<div class="col-tb-8">
						<div class="admin__actions-switch" data-role="switcher">
							<input type="checkbox"<?php if($exist && $settings['hide_thumbnail']): ?> checked="checked"<?php endif ?> value="1" name="setting[hide_thumbnail]" id="hide_thumbnail" class="admin__actions-switch-checkbox">
							<label for="hide_thumbnail" class="admin__actions-switch-label">
								<span data-text-off="<?php echo __('No') ?>" data-text-on="<?php echo __('Yes') ?>" class="admin__actions-switch-text"></span>
							</label>
						</div>
					</div>
				</div>
				
				<div class="form-group line">
					<label for="hide_description" class="col-tb-4 control-label"><?php echo __('Hide Short Description') ?></label>
					<div class="col-tb-8">
						<div class="admin__actions-switch" data-role="switcher">
							<input type="checkbox"<?php if($exist && $settings['hide_description']): ?> checked="checked"<?php endif ?> value="1" name="setting[hide_description]" id="hide_description" class="admin__actions-switch-checkbox" onclick="hideShortDescription()">
							<label for="hide_description" class="admin__actions-switch-label">
								<span data-text-off="<?php echo __('No') ?>" data-text-on="<?php echo __('Yes') ?>" class="admin__actions-switch-text"></span>
							</label>
						</div>
					</div>
				</div>
				
				<div class="form-group line" id="summary_character">
					<label for="character_count" class="col-tb-4 control-label"><?php echo __('Summary character count') ?></label>
					<div class="col-tb-8">
						<div class="admin__actions-switch" data-role="switcher">
							<input type="text" id="character_count" class="input-text validate-number" name="setting[character_count]"<?php if($exist): ?> value="<?php echo $settings['character_count'] ?>"<?php endif ?> />
						</div>
					</div>
				</div>
				
				<div class="form-group line">
					<label for="hide_create" class="col-tb-4 control-label"><?php echo __('Hide Create by and Create at') ?></label>
					<div class="col-tb-8">
						<div class="admin__actions-switch" data-role="switcher">
							<input type="checkbox"<?php if($exist && $settings['hide_create']): ?> checked="checked"<?php endif ?> value="1" name="setting[hide_create]" id="hide_create" class="admin__actions-switch-checkbox">
							<label for="hide_create" class="admin__actions-switch-label">
								<span data-text-off="<?php echo __('No') ?>" data-text-on="<?php echo __('Yes') ?>" class="admin__actions-switch-text"></span>
							</label>
						</div>
					</div>
				</div>
				
				<div class="form-group line" id="use-slider">
					<label for="use_slider" class="col-tb-4 control-label"><?php echo __('Use slider') ?></label>
					<div class="col-tb-8">
						<div class="admin__actions-switch" data-role="switcher">
							<input type="checkbox"<?php if($exist && $settings['use_slider']): ?> checked="checked"<?php endif ?> value="1" name="setting[use_slider]" id="use_slider" class="admin__actions-switch-checkbox" onclick="useSlider()">
							<label for="use_slider" class="admin__actions-switch-label">
								<span data-text-off="<?php echo __('No') ?>" data-text-on="<?php echo __('Yes') ?>" class="admin__actions-switch-text"></span>
							</label>
						</div>
					</div>
				</div>
				
				<?php 
					echo $this->getLayout()->createBlock('Magento\Framework\View\Element\Template')
						->setExist($exist)
						->setSettings($settings)
						->setDisableSliderMargin(1)
						->setTemplate('MGS_Fbuilder::panel/create/element/widgets/slider_setting.phtml')->toHtml() ;
				?>
			</div>
			
			<?php if($exist): ?>
				<?php 
					echo $this->getLayout()->createBlock('Magento\Framework\View\Element\Template')
						->setExist($exist)
						->setBuilderChild($child)
						->setBuilderHelper($helper)
						->setTemplate('MGS_Fbuilder::panel/create/element/widgets/code_tab.phtml')->toHtml() ;
				?>
				
			<?php endif ?>
		</div>
		
		
		<div class="line">
			<div class="form-group">
				<div class="col-des-12 builder-action">
					<div class="buttons">
						<?php if(!$exist): ?>
							<button type="button" class="action" onclick="history.go(-1)"><span><?php echo __('Back') ?></span></button>
						<?php endif ?>
						<button type="submit" class="action primary"><span><?php echo __('Submit') ?></span></button>
					</div>
				</div>
			</div>
		</div>
	</form>
</div>
<script type="text/javascript">
	require([
		'jquery',
	], function(jQuery){
		(function($) {
			$(document).ready(function(){
				hideShortDescription();
			});
		})(jQuery);
	});
	
	function hideShortDescription(){
		require([
			'jquery',
		], function(jQuery){
			(function($) {
				if($('#hide_description').prop("checked") == true){
					$('#summary_character').hide();
				}else{
					$('#summary_character').show();
					
				}
			})(jQuery);
		});
		
	}
</script>