<?php
    $titles = __('error_titles', true);
    $bodies = __('error_bodies', true);

    $date_format = pjUtil::toBootstrapDate($tpl['option_arr']['o_date_format']);
    $months = __('months', true);
    ksort($months);
    $short_days = __('short_days', true);
?>
<div id="datePickerOptions" style="display:none;" data-wstart="<?php echo (int) $tpl['option_arr']['o_week_start']; ?>" data-format="<?php echo $date_format; ?>" data-months="<?php echo implode("_", $months);?>" data-days="<?php echo implode("_", $short_days);?>"></div>

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-12">
        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12">
                <h2><?php echo __('infoRequestsTitle', true); ?></h2>
            </div>
        </div>
    </div>
</div>

<div class="row wrapper wrapper-content animated fadeInRight">
    <?php
        $error_code = $controller->_get->toString('err');
        if (!empty($error_code))
        {
            switch (true)
            {
                case in_array($error_code, array('AOR01')):
                    ?>
                    <div class="alert alert-success">
                        <i class="fa fa-check m-r-xs"></i>
                        <strong><?php echo @$titles[$error_code]; ?></strong>
                        <?php echo @$bodies[$error_code]?>
                    </div>
                    <?php
                    break;
                case in_array($error_code, array('')):
                    ?>
                    <div class="alert alert-danger">
                        <i class="fa fa-exclamation-triangle m-r-xs"></i>
                        <strong><?php echo @$titles[$error_code]; ?></strong>
                        <?php echo @$bodies[$error_code]?>
                    </div>
                    <?php
                    break;
            }
        }
	$filter_date_from = $controller->_get->check('filter_date_from') ? $controller->_get->toString('filter_date_from') : '';
    $filter_date_to = $controller->_get->check('filter_date_to') ? $controller->_get->toString('filter_date_to') : '';
    $filter_location = $controller->_get->check('filter_location') ? $controller->_get->toInt('filter_location') : '';
	$filter_type = $controller->_get->check('filter_type') ? $controller->_get->toInt('filter_type') : '';
    ?>
    <div class="col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
	
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>?controller=pjAdminOptions&amp;action=pjActionRequests" method="get" class="">
            		<input type="hidden" name="controller" value="pjAdminOptions" />
            		<input type="hidden" name="action" value="pjActionRequests" />
            		<div class="row">
            			<div class="col-lg-4 col-md-12 col-sm-12 col-xs-12">
            				<div class="form-group">
								<label class="control-label"><?php __('lblFilerDate'); ?></label>
								<div class="row">
									<div class="col-sm-6">
										<div class="form-group">
											<div class="input-group">
												<input type="text" class="form-control datepick" name="filter_date_from" value="<?php echo pjSanitize::html($filter_date_from);?>" readonly="readonly" />
	
												<span class="input-group-addon"><i class="fa fa-calendar"></i></span> 
											</div>
										</div>
									</div>
									<div class="col-sm-6">
										<div class="input-group">
											<input type="text" class="form-control datepick" name="filter_date_to" value="<?php echo pjSanitize::html($filter_date_to);?>" readonly="readonly" />
											<span class="input-group-addon"><i class="fa fa-calendar"></i></span> 
										</div>
									</div>
								</div>								
							</div>
            			</div>
            			
            			<div class="col-lg-3 col-md-5 col-sm-5 col-xs-12">
            				<div class="form-group">
								<label class="control-label"><?php __('lblFilerLocation'); ?></label>
								<select class="form-control" name="filter_location">
									<option value="">-- <?php __('lblChoose');?> --</option>
									<?php
									if (isset($tpl['locations']) && count($tpl['locations']) > 0)
									{
										foreach ($tpl['locations'] as $v)
										{
											?><option value="<?php echo $v['id']; ?>" <?php echo $filter_location == $v['id'] ? 'selected="selected"' :'';?>><?php echo stripslashes($v['name']); ?></option><?php
										}
									}
									?>
								</select>
							</div>
            			</div>
            			
            			<div class="col-lg-3 col-md-5 col-sm-5 col-xs-12">
            				<div class="form-group">
								<label class="control-label"><?php __('lblFilerType'); ?></label>
								<select class="form-control" name="filter_type">
									<option value="">-- <?php __('lblChoose');?> --</option>
									<?php
									if (isset($tpl['types']) && count($tpl['types']) > 0)
									{
										foreach ($tpl['types'] as $v)
										{
											?><option value="<?php echo $v['id']; ?>" <?php echo $filter_type == $v['id'] ? 'selected="selected"' :'';?>><?php echo stripslashes($v['name']); ?></option><?php
										}
									}
									?>
								</select>
							</div>
            			</div>
            			
            			<div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
            				<div class="form-group">
								<label class="control-label">&nbsp;</label>
								<button type="submit" class="btn btn-primary btn-block"><?php __('btnFilter');?></button>
							</div>
						</div>
            			
            		</div>
            	</form>
            	
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>?controller=pjAdminOptions&amp;action=pjActionRequests" method="post" class="form-horizontal" id="frmUpdateOptions">
                    <input type="hidden" name="save" value="1" />
                    <input type="hidden" name="request_ids" value="<?php echo $tpl['request_ids'] ? implode('-', $tpl['request_ids']) : '';?>" />					
					<input type="hidden" name="filter_date_from" value="<?php echo pjSanitize::html($filter_date_from);?>" />
            		<input type="hidden" name="filter_date_to" value="<?php echo pjSanitize::html($filter_date_to);?>" />
            		<input type="hidden" name="filter_location" value="<?php echo $filter_location;?>" />
            		<input type="hidden" name="filter_type" value="<?php echo $filter_type?>" />

                    <div class="table-responsive table-responsive-secondary">
                        <table class="table table-striped table-hover" id="tblRequests">
                            <thead>
                            <tr>
                                <th><?php __('lblRequestsDateFrom'); ?></th>
                                <th><?php __('lblRequestsDateTo'); ?></th>
                                <th><?php __('lblRequestsLocation'); ?></th>
                                <th><?php __('lblRequestsType'); ?></th>
                                <th><?php __('lblRequestsTypeOnRequest'); ?></th>
                                <th></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if (!empty($tpl['requests'])): ?>
                                <?php foreach ($tpl['requests'] as $request): ?>
                                    <tr>
                                        <td>
                                            <div class="input-group">
                                                <input type="text" name="requests[<?php echo $request['id']; ?>][date_from]" value="<?php echo pjDateTime::formatDate($request['date_from'], 'Y-m-d', $tpl['option_arr']['o_date_format']); ?>" class="form-control datepick required" data-wt="open" readonly="readonly" data-msg-required="<?php __('plugin_base_this_field_is_required');?>" data-msg-remote="<?php __('lblPickupWorkingTime');?>">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="input-group">
                                                <input type="text" name="requests[<?php echo $request['id']; ?>][date_to]" value="<?php echo pjDateTime::formatDate($request['date_to'], 'Y-m-d', $tpl['option_arr']['o_date_format']); ?>" class="form-control datepick required" data-wt="open" readonly="readonly" data-msg-required="<?php __('plugin_base_this_field_is_required');?>" data-msg-remote="<?php __('lblPickupWorkingTime');?>">
                                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                            </div>
                                        </td>
                                        <td>
                                            <select name="requests[<?php echo $request['id']; ?>][location_id]" class="form-control">
                                                <option value="0">-- <?php echo __('lblRequestsAllLocations'); ?> --</option>
                                                <?php if (!empty($tpl['locations'])): ?>
                                                    <?php foreach ($tpl['locations'] as $location): ?>
                                                        <option value="<?php echo $location['id']; ?>" <?php echo ($location['id'] == $request['location_id']) ? 'selected' : ''; ?>><?php echo $location['name']; ?></option>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </select>
                                        </td>
                                        <td>
                                            <select name="requests[<?php echo $request['id']; ?>][type_id]" class="form-control">
                                                <option value="0">-- <?php echo __('lblRequestsAllTypes'); ?> --</option>
                                                <?php if (!empty($tpl['types'])): ?>
                                                    <?php foreach ($tpl['types'] as $type): ?>
                                                        <option value="<?php echo $type['id']; ?>" <?php echo ($type['id'] == $request['type_id']) ? 'selected' : ''; ?>><?php echo $type['name']; ?></option>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </select>
                                        </td>
                                        <td>
                                            <select name="requests[<?php echo $request['id']; ?>][type_on_request]" class="form-control">
												<?php foreach (__('type_of_on_request', true) as $k => $v): ?>
                                                	<option value="<?php echo $k; ?>" <?php echo ($k == $request['type_on_request']) ? 'selected' : ''; ?>><?php echo $v; ?></option>
												<?php endforeach; ?>
                                            </select>
                                        </td>
                                        <td class="text-center">
                                            <a href="javascript:;" class="btn btn-danger btn-outline btn-sm m-l-xs btnRemoveRequest"><i class="fa fa-trash"></i></a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td>
                                        <div class="input-group">
                                            <input type="text" name="requests[0][date_from]" value="" class="form-control datepick required" data-wt="open" readonly="readonly" data-msg-required="<?php __('plugin_base_this_field_is_required');?>" data-msg-remote="<?php __('lblPickupWorkingTime');?>">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="input-group">
                                            <input type="text" name="requests[0][date_to]" value="" class="form-control datepick required" data-wt="open" readonly="readonly" data-msg-required="<?php __('plugin_base_this_field_is_required');?>" data-msg-remote="<?php __('lblPickupWorkingTime');?>">
                                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                        </div>
                                    </td>
                                    <td>
                                        <select name="requests[0][location_id]" class="form-control">
                                            <option value="0">-- <?php echo __('lblRequestsAllLocations'); ?> --</option>
                                            <?php if (!empty($tpl['locations'])): ?>
                                                <?php foreach ($tpl['locations'] as $location): ?>
                                                    <option value="<?php echo $location['id']; ?>"><?php echo $location['name']; ?></option>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="requests[0][type_id]" class="form-control">
                                            <option value="0">-- <?php echo __('lblRequestsAllTypes'); ?> --</option>
                                            <?php if (!empty($tpl['types'])): ?>
                                                <?php foreach ($tpl['types'] as $type): ?>
                                                    <option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?></option>
                                                <?php endforeach; ?>
                                            <?php endif; ?>
                                        </select>
                                    </td>
                                    <td>
										<select name="requests[0][type_on_request]" class="form-control">
											<?php foreach (__('type_of_on_request', true) as $k => $v): ?>
												<option value="<?php echo $k; ?>" <?php echo ($k == 1) ? 'selected' : ''; ?>><?php echo $v; ?></option>
											<?php endforeach; ?>
										</select>
									</td>
                                    <td class="text-center">
                                        <a href="javascript:;" class="btn btn-danger btn-outline btn-sm m-l-xs btnRemoveRequest"><i class="fa fa-trash"></i></a>
                                    </td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <button type="button" id="btnAddRequest" class="btn btn-default"><?php __('btnRequestAdd'); ?></button>

                    <div class="hr-line-dashed"></div>

                    <div class="clearfix">
                        <button class="ladda-button btn btn-primary btn-lg pull-left btn-phpjabbers-loader" data-style="zoom-in">
                            <span class="ladda-label"><?php __('plugin_base_btn_save'); ?></span>
                            <?php include $controller->getConstant('pjBase', 'PLUGIN_VIEWS_PATH') . 'pjLayouts/elements/button-animation.php'; ?>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div><!-- /.col-lg-12 -->
</div>

<table style="display: none;" id="tblRequestsClone">
    <thead>
    <tbody>
    <tr>
        <td>
            <div class="input-group">
                <input type="text" name="requests[{INDEX}][date_from]" value="" class="form-control {DATEPICK} required" data-wt="open" readonly="readonly" data-msg-required="<?php __('plugin_base_this_field_is_required');?>" data-msg-remote="<?php __('lblPickupWorkingTime');?>">
                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
            </div>
        </td>
        <td>
            <div class="input-group">
                <input type="text" name="requests[{INDEX}][date_to]" value="" class="form-control {DATEPICK} required" data-wt="open" readonly="readonly" data-msg-required="<?php __('plugin_base_this_field_is_required');?>" data-msg-remote="<?php __('lblPickupWorkingTime');?>">
                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
            </div>
        </td>
        <td>
            <select name="requests[{INDEX}][location_id]" class="form-control">
                <option value="0">-- <?php echo __('lblRequestsAllLocations'); ?> --</option>
                <?php if (!empty($tpl['locations'])): ?>
                    <?php foreach ($tpl['locations'] as $location): ?>
                        <option value="<?php echo $location['id']; ?>"><?php echo $location['name']; ?></option>
                    <?php endforeach; ?>
                <?php endif; ?>
            </select>
        </td>
        <td>
            <select name="requests[{INDEX}][type_id]" class="form-control">
                <option value="0">-- <?php echo __('lblRequestsAllTypes'); ?> --</option>
                <?php if (!empty($tpl['types'])): ?>
                    <?php foreach ($tpl['types'] as $type): ?>
                        <option value="<?php echo $type['id']; ?>"><?php echo $type['name']; ?></option>
                    <?php endforeach; ?>
                <?php endif; ?>
            </select>
        </td>
        <td>
			<select name="requests[{INDEX}][type_on_request]" class="form-control">
				<?php foreach (__('type_of_on_request', true) as $k => $v): ?>
					<option value="<?php echo $k; ?>" <?php echo ($k == 1) ? 'selected' : ''; ?>><?php echo $v; ?></option>
				<?php endforeach; ?>
			</select>
		</td>
        <td class="text-center">
            <a href="javascript:;" class="btn btn-danger btn-outline btn-sm m-l-xs btnRemoveRequest"><i class="fa fa-trash"></i></a>
        </td>
    </tr>
    </tbody>
</table>