<script type="text/javascript">
$(document).ready(function(){
	$("#startdate,#enddate").datepicker({
        showButtonPanel: true,
        dateFormat:'yy-mm-dd'
    });

	//推荐月度优秀文章
    $(".best_article").click(function(){
    	var t_id=$(this).attr('data-taskid');
    	var ta_isbest=0;
    	var ta_count=$(this).attr('data-count');
    	var ta_charge=$(".charge"+t_id).text();
    	var total_charge=$("#total_charge").text();

    	//第一名150元，第二名100元，第三名50元
    	var rank = $(this).attr('data-rank');
    	rank=Number(rank);
    	var difcharge=50;
    	if (rank==1) {
    		difcharge=150;
    	}else if (rank==2) {
    		difcharge=100;
    	};

    	//更新的金额
    	if ($(this).attr("checked")) {
    		ta_charge=Number(ta_charge)+difcharge;
    		total_charge=Number(total_charge)+difcharge;
    		ta_isbest=1;
        }else{
        	ta_charge=Number(ta_charge)-difcharge;
        	total_charge=Number(total_charge)-difcharge;
        }
        //更新数据库里的金额
        var url="<?php echo site_url('author/set_task_charge'); ?>";
        $.post(url,{t_id:t_id,ta_count:ta_count,ta_charge:ta_charge,ta_isbest:ta_isbest},function(result){
        	var json_obj=eval("("+result+")");
        	if (json_obj.name=='ok') {
        		$(".charge"+t_id).text(ta_charge);
        		$("#total_charge").text(total_charge);
        	}else{
        		alert('评优设置失败');
        	}
        });
    });
});

</script>
<div class="row-fluid">
  <div class="span2">
  
<?php echo $nav_view; ?>
  
  </div>
  <div class="span10">
        <form action="" method="post" id="form-search-tasks" style="background:#f5f5f5;padding:15px;margin-right:20px;">
            <div class="input-append">
                <input class="span2" type="text" id="startdate" name="startdate" placeholder="startdate">
                <input class="span2" type="text" id="enddate" name="enddate" placeholder="enddate">
                <input class="span2" type="text" id="tasktitle" name="tasktitle" placeholder="task title">
                <select class="span2" name="author">
                	<option value="0" selected="selected">作者</option>
                	<?php foreach ($author_list as $a) { ?>
                		<option value="<?php echo $a->a_id; ?>"><?php echo $a->a_name; ?></option>
                	<?php } ?>
                </select>
                <select class="span2" name="tasktype">
                    <option value="-">任务类型</option>
                    <?php foreach ($this->config->item('auhtor_task_type') as $type_key => $type_value) { ?>
                        <option value="<?php echo $type_value; ?>" ><?php echo $type_key; ?></option>
                    <?php } ?>
                </select>
                <input type="hidden" name="taskstatus" value="reviewed">
                <input type="hidden" name="filetype" id="filetype" value="2">
                <button class="btn" type="button" onclick="$('#form-search-tasks').attr('action', '<?php echo site_url('author/in_reviewed_tasks') ?>');get_ajax_reviewed_list('form-search-tasks','tasks_list',false);">查询任务</button>
                <a href="#excel_type" role="button" class="btn" data-toggle="modal" onclick="$('#form-search-tasks').attr('action', '<?php echo site_url('author/in_reviewed_tasks/loadexcel') ?>');">导出到excel</a>
            </div>
        </form>
        <div id="tasks_list">
  		<form action="<?php echo site_url('author/complete_task_submit') ?>" class="form-horizontal"  name="complete_task_form" id="complete_task_form" method="post">
		<table class="table table-striped">
		  <thead>
			<tr>
			  <th>#</th>
			  <th class="span5">任务名称</th>
			  <th>类型</th>
			  <th>作者</th>
			  <th>编辑</th>
			  <th>发布</th>
			  <th>修改次数</th>
			  <th>字数</th>
			  <th>金额</th>
			  <th>评分</th>
			  <th>操作</th>
			</tr>
		  </thead>
		  <tbody>
		  <?php
			$sum_count=0;
			$sum_charge=0;
			$flag=false;//最活跃作者是否在当前列表中
			foreach($task_list as $i=>$task){ 
				$sum_count+=$task->last_version->ta_count;
				$sum_charge+=$task->last_version->ta_charge;
				if (isset($active_author['t_a_id']) && $active_author['t_a_id']==$task->t_a_id) {
					$flag=true;
					$active_author['task_id']=$task->t_id;
				}
		  ?>
			<tr>
			  <td><input type="checkbox" checked name="t_ids[]" value="<?php echo $task->t_id; ?>"></td>
			  <td><a href="<?php echo site_url('author/edit_task/'.$task->t_id); ?>"><?php echo $task->t_title; ?></a></td>
			  <td><?php echo $task->t_td_type; ?></td>
			  <td><?php echo $task->t_a_id==0?'大厅':get_author_name($task->t_a_id); ?></td>
			  <td><?php echo get_author_name($task->t_ht_op_code); ?></td>
			  <td><?php echo date('m/d/Y',strtotime($task->t_datetime)); ?></td>
			  <td><?php echo $task->t_edit_count; ?></td>
			  <td><?php echo $task->last_version->ta_count; ?></td>
			  <td class="charge<?php echo $task->t_id; ?>"><?php echo $task->last_version->ta_charge; ?></td>
			  <td><?php if(empty($task->t_rating_total))$task->t_rating_total=0; echo $task->t_rating_total; ?></td>
			  <td>
			  	<label class="checkbox">
			  		<input  type="checkbox" <?php if($task->last_version->ta_isbest!='') echo 'checked="checked"';?>
			  				name="best_article" 
			  				class="best_article" 
			  				data-rank="<?php echo $i+1; ?>"
			  				data-taskid="<?php echo $task->t_id; ?>" 
			  				data-charge="<?php echo $task->last_version->ta_charge; ?>" 
			  				data-count="<?php echo $task->last_version->ta_count; ?>">优秀文章
			  	</label>
			  </td>
			</tr>
		  <?php } ?>
		  <?php if($flag && $isset_active_author==0){ //如果活跃作者在当前列表中则显示奖励金额 ?>
		  	 <tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td><?php if(isset($active_author['t_a_id']))  echo get_author_name($active_author['t_a_id']);?></td>
				<td><?php if(isset($active_author['count'])) echo $active_author['count']; ?></td>
				<td>+150</td>
				<td></td>
				<td>活跃作者</td>
			  </tr>
			<?php } ?>
			 <tr>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td>总计</td>
				<td><?php echo  $sum_count; ?></td>
				<td id="total_charge"><?php if($flag && $isset_active_author==0)$sum_charge=$sum_charge+150; echo $sum_charge; ?></td>
				<td></td>
				<td>
					<input type="hidden" name="isset_active_author" value="<?php if ($flag && !empty($active_author) && $active_author['author_num']>1 && $active_author['count']>=3000){ echo $isset_active_author;}else{echo 1;} ?>">
					<button type="button" class="btn btn-info"  onclick="submitForm('complete_task_form');">批量结算</button>
				</td>
			  </tr>
		  </tbody>
		</table>
		<?php if(!empty($active_author)){ ?>
		<input type="hidden" name="active_t_id" id="active_t_id" value="<?php echo $active_author['task_id']; ?>">
		<input type="hidden" name="active_ta_id" id="active_ta_id" value="<?php echo $active_author['ta_id']; ?>">
		<?php } ?>
		</form>
		</div>
  </div>
</div>

<!-- 选择导出excel的格式 -->
<div id="excel_type" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h4 id="myModalLabel">选择导出文件的编码格式</h4>
  </div>
  <div class="modal-body">
    <label class="radio">
      <input type="radio" name="load_type" value="2" onclick="$('#filetype').val(this.value)" checked>
      GBK格式
    </label>
    <label class="radio">
      <input type="radio" name="load_type" value="1" onclick="$('#filetype').val(this.value)">
      UTF-8格式
    </label>
    <p class="muted"><br>温馨提示: 如果导出的文件出现中文乱码，请使用另一种格式进行导出!</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
    <button class="btn btn-info" data-dismiss="modal" aria-hidden="true" onclick="$('#form-search-tasks').submit();">开始导出</button>
  </div>
</div>
