{template 'common/header'}
<style>
	.datetimepicker{width:200px}
</style>
<ul class="nav nav-tabs">
	<li {if $do == 'list'} class="active"{/if}><a href="{php echo url('cron/display/list');}">任务列表</a></li>
	<li {if $do == 'post' && empty($id)} class="active"{/if}><a href="{php echo url('cron/display/post');}">添加任务</a></li>
	{if $do == 'post'  && !empty($id)}<li class="active"><a href="{php echo url('cron/display/post');}">编辑任务</a></li>{/if}
</ul>
{if $do == 'post'}
<div class="clearfix">
	<div class="panel panel-default">
		<div class="panel-heading">计划任务</div>
		<div class="panel-body">
			<form class="form-horizontal form" id="form1" action="" method="post">
				<input type="hidden" name="id" value="{$cron['id']}">
				<div class="form-group">
					<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"><span class="text-danger">* </span>任务名称</label>
					<div class="col-sm-10 col-xs-12">
						<input type="text" class="form-control" placeholder="设置本任务的任务名称" name="name" value="{$cron['name']}"/>
						<span class="help-block help-text">设置本任务的任务名称</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"><span class="text-danger">* </span>所属模块</label>
					<div class="col-sm-10 col-xs-12">
						<select name="module" id="module" class="form-control">
							{loop $modules $module}
							<option value="{$module['name']}">{$module['title']}</option>
							{/loop}
						</select>
						<span class="help-block help-text">设置本任务的任务名称</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"><span class="text-danger">* </span> 任务脚本:</label>
					<div class="col-sm-10 col-xs-12">
						<input type="text" class="form-control" placeholder="设置本任务的执行程序文件名" name="filename" value="{$cron['filename']}"/>
						<span class="help-block help-text">设置本任务的执行程序文件名，必须是PHP文件。请勿包含路径，系统计划任务位于 source/include/cron/ 目录中，插件计划任务位于 source/plugin/插件目录/cron/ 目录中</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"><span class="text-danger"> </span> 是否可用:</label>
					<div class="col-sm-10 col-xs-12">
						<label class="radio-inline">
							<input type="radio" name="status" value="1" {if $cron['status'] == 1}checked{/if}/> 可用
						</label>
						<label class="radio-inline">
							<input type="radio" name="status" value="0" {if $cron['status'] == 0}checked{/if}/> 不可用
						</label>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"><span class="text-danger"> </span> 任务类型:</label>
					<div class="col-sm-10 col-xs-12">
						<label class="radio-inline" onclick="$('.type').hide();$('#type1').show();">
							<input type="radio" name="type" value="1" {if $cron['type'] == 1}checked{/if}/> 定时任务
						</label>
						<label class="radio-inline" onclick="$('.type').hide();$('#type2').show();">
							<input type="radio" name="type" value="2" {if $cron['type'] == 2}checked{/if}/> 循环任务
						</label>
						<span class="help-block">定时任务是某个时间只执行一次，执行后将会自动关闭。定时群发的执行时间必须在当前时间一小时之后</span>
					</div>
				</div>
				<div class="form-group type" id="type1" {if $cron['type'] == 2}style="display:none"{/if}>
					<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">执行时间:</label>
					<div class="col-sm-10 col-xs-12">
						{php echo tpl_form_field_date('executetime', $cron['lastruntime'], true);}
					</div>
				</div>
				<div class="type" id="type2" {if $cron['type'] == 1}style="display:none"{/if}>
					<div class="form-group">
						<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">执行时间:</label>
						<div class="col-sm-10 col-xs-12">
							<label class="radio-inline" style="padding-top:0"><input type="radio" name="time" {if $cron['weekday'] == -1 && $cron['day'] > 0}checked{/if}/> 每　月</label>
							<select name="day" id="day" class="form-control" style="display:inline-block;width:200px;" onclick="$(this).prev().find(':radio').prop('checked', true);$('#weekday').val(-1);">
								<option value="-1" {if $cron['day'] == '-1'}selected{/if}>*</option>
								<?php
									for($i = 1; $i < 32; $i ++) {
										if($cron['day'] == $i) {
											echo '<option selected value="'.$i.'">'.$i.'日</option>';
								} else {
									echo '<option value="'.$i.'">'.$i.'日</option>';
								}
								}
								?>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
						<div class="col-sm-10 col-xs-12">
							<label class="radio-inline" style="padding-top:0"><input type="radio" name="time" {if $cron['weekday'] != -1}checked{/if}/> 或每周</label>
							<select name="weekday" id="weekday" class="form-control" style="display:inline-block;width:200px;"  onclick="$(this).prev().find(':radio').prop('checked', true);$('#day').val(-1);">
								<option value="-1" {if $cron['weekday'] == '-1'}selected{/if}>*</option>
								<option value="0" {if $cron['weekday'] == '0'}selected{/if}>周日</option>
								<option value="1" {if $cron['weekday'] == '1'}selected{/if}>周一</option>
								<option value="2" {if $cron['weekday'] == '2'}selected{/if}>周二</option>
								<option value="3" {if $cron['weekday'] == '3'}selected{/if}>周三</option>
								<option value="4" {if $cron['weekday'] == '4'}selected{/if}>周四</option>
								<option value="5" {if $cron['weekday'] == '5'}selected{/if}>周五</option>
								<option value="6" {if $cron['weekday'] == '6'}selected{/if}>周六</option>
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
						<div class="col-sm-10 col-xs-12">
							<label class="radio-inline" style="padding-top:0" onclick="$('#weekday, #day').val(-1);"><input type="radio" name="time" {if $cron['weekday'] == -1 && $cron['day'] == -1}checked{/if}/> 或每天</label>
						</div>
					</div>
					<div class="form-group">
						<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
						<div class="col-sm-10 col-xs-12">
							<div class="input-group">
								<span class="input-group-addon">当天</span>
								<select name="hour" class="form-control" style="display:inline-block;">
									<option value="-1" {if $cron['hour'] == '-1'}selected{/if}>*</option>
									<?php
									for($i = 0; $i < 24; $i ++) {
										if($cron['hour'] == $i) {
											echo '<option selected value="'.$i.'">'.$i.'时</option>';
									} else {
									echo '<option value="'.$i.'">'.$i.'时</option>';
									}
									}
									?>
								</select>
								<span class="input-group-addon">分钟</span>
								<input type="text" name="minute" value="{$cron['minute']}" placeholder="设置哪些分钟执行本任务" class="form-control" style="display:inline-block;"/>
							</div>
							<span class="help-block help-text">设置哪些分钟执行本任务，至多可以设置 2 个分钟值，多个值之间用半角逗号 "," 隔开</span>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
					<div class="col-sm-10 col-xs-12">
						<input type="hidden" value="{$_W['token']}" name="token"/>
						<input type="hidden" name="form" value="{$_W['token']}"/>
						<input type="submit" value="提交" class="btn btn-primary col-lg-1"/>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<script>
	require(['select2', 'validator'], function(){
		$('#module').select2();

		$('#form1').bootstrapValidator({
			fields: {
				name: {
					validators: {
						notEmpty: {
							message: '任务名称不能为空'
						}
					}
				},
				module: {
					validators: {
						notEmpty: {
							message: '所属模块不能为空'
						}
					}
				},
				filename: {
					validators: {
						notEmpty: {
							message: '任务脚本不能为空'
						},
						regexp: {
							regexp: /^[a-z0-9_]+$/,
							message: '脚本名称只能包含字母数字下划线'
						}
					}
				}
			}
		});
	});
</script>
{/if}

{if $do == 'list'}
<div class="clearfix">
	<form action="{php echo url('cron/display/del')}" id="form1" method="post">
		<div class="panel panel-default">
			<div class="table-responsive panel-body">
				<table class="table table-hover">
					<thead class="navbar-inner">
					<tr>
						<th width="30"></th>
						<th>名称</th>
						<th>所属模块</th>
						<th>类型</th>
						<th style="min-width:180px;">执行时间</th>
						<th>上次执行时间</th>
						<th>下次执行时间</th>
						<th>添加时间</th>
						<th>状态</th>
						<th width="200">操作</th>
					</tr>
					</thead>
					<tbody>
						{loop $crons $cron}
						<tr>
							<td>
								<input type="checkbox" name="id[]" value="{$cron['id']}"/>
							</td>
							<td>{$cron['name']}</td>
							<td><span class="label label-success">{$modules[$cron['module']]['title']}</span></td>
							<td>
								{if $cron['type'] == 1}
								<span class="label label-success">定时任务</span>
								{else}
								<span class="label label-danger">循环任务</span>
								{/if}
							</td>
							<td>{$cron['cn']}</td>
							<td>{$cron['lastruntime']}</td>
							<td>{$cron['nextruntime']}</td>
							<td>{php echo date('Y-m-d H:i', $cron['createtime']);}</td>
							<td>
								<span class="switch">
									<input type="checkbox" value="1" {if $cron['status']==1} checked="checked" {/if} data-id="{$cron['id']}"/>
								</span>
							</td>
							<td>
								<div class="btn-group">
									<a title="同步" class="btn btn-default btn-sm" href="{php echo url('cron/display/sync', array('id' => $cron['id']))}">同步</a>
									<a title="编辑" class="btn btn-default btn-sm" href="{php echo url('cron/display/post', array('id' => $cron['id']))}">编辑</a>
									<a title="执行" class="btn btn-default btn-sm"  onclick="if(!confirm('确定执行吗？'))  return false;" href="{php echo url('cron/display/run', array('id' => $cron['id']))}">执行</a>
									<a title="删除" class="btn btn-default btn-sm" onclick="if(!confirm('确定删除吗？'))  return false;" href="{php echo url('cron/display/del', array('id' => $cron['id']))}">删除</a>
								</div>
							</td>
						</tr>
						{/loop}
						{if !empty($crons)}
						<tr>
							<td>
								<input type="checkbox" id="selectall"/>
							</td>
							<td colspan="9">
								<input type="hidden" value="{$_W['token']}" name="token"/>
								<input type="submit" value="删除" name="submit" class="btn btn-primary" onclick="if(!confirm('删除后将不可恢复,确定删除吗?'))return false;"/>
							</td>
						</tr>
						{/if}
					</tbody>
				</table>
			</div>
		</div>
	</form>
</div>
{/if}
<script>
	require(['bootstrap.switch', 'select2', 'validator'], function(){
		$('#selectall').click(function(){
			$('#form1 :checkbox').prop('checked', $(this).prop('checked'));
		});

		$('.switch :checkbox').bootstrapSwitch();
		$('.switch :checkbox').on('switchChange.bootstrapSwitch', function(e, state){
			$this = $(this);
			var id = $this.data('id');
			var status = this.checked ? 1 : 0;
			$.post("{php echo url('cron/display/status');}", {id:id, status:status}, function(resp){
				if(resp != 'success') {
					util.message(resp, '', 'error')
				} else {
					location.reload();
				}
				return false;
			});
		});
	});
</script>
{template 'common/footer'}
