<?php

use home\assets\MainAsset;
use home\assets\MenuAsset;
use yii\helpers\Url;
use home\modules\task\models\Category;
use home\modules\task\models\Task;
use common\models\User;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use yii\helpers\StringHelper;
use yii\helpers\Html;
use home\modules\blog\models\Blog;

/* @var $this \yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \home\modules\task\models\Task */

MenuAsset::register($this);

$this->title = (isset($get['type']) && isset($typeTitle[$get['type']])) ? $typeTitle[$get['type']] . '任务' : '全部任务';

$search = $model->search();

$js = <<<JS
	$("#show-first").on('click',function(){
		var url = $(this).data('url');
		var showFirst = ($(this).is(":checked")) ? 1 : 0 ;
        $("input[name=show_first]").val(showFirst);
        $("#search-form").trigger('submit');
	});
    //重置
    $(".btn-reset").on('click',function(){
        $("select[name=search_field]").val('name');
        $("input[name=keywords],select[name=category_id],select[name=status_id],select[name=time_order]").val('');
    });
JS;
$this->registerJs($js, \yii\web\View::POS_END);
?>


<?= \home\widgets\Alert::widget(); ?>

<div class="task-title-date tasklist-serch">
    <?php $searchForm = ActiveForm::begin([ 'id' => 'search-form', 'action' => ['list-iframe'], 'method' => 'get']); ?>
    <div class="searchfl fix ">

        <input class="form-control fl date-form-control search-form" value="<?= (isset($get['keywords'])) ? Html::encode($get['keywords']) : '' ?>" name="keywords" style="width:15%;" placeholder="关键字" >
        <select class="form-control fl date-form-control search-form" name="search_field" style="width:130px;">
            <?php foreach ($search['keywords'] as $field => $name) : ?>
                <option <?= (isset($get['search_field']) && ($get['search_field'] == $field)) ? 'selected="selected"' : ''; ?> value="<?= $field ?>" ><?= $name ?></option>
            <?php endforeach; ?>
        </select>
        <select class="form-control fl date-form-control search-form" name="category_id" style="width:130px;">
            <option value="">---所属分类---</option>
            <?= Category::getDropDownTreeList(isset($get['category_id']) ? $get['category_id'] : 0) ?>
        </select>
        <select class="form-control fl date-form-control search-form" name="status_id" style="width:130px;">
            <option value="">---所属状态---</option>
            <?php foreach (\qcbase\models\TaskStatus::getDropDownList() as $id => $name) : ?>
                <option <?= (isset($get['status_id']) && ($get['status_id'] == $id)) ? 'selected="selected"' : ''; ?> value="<?= $id ?>" ><?= $name ?></option>
            <?php endforeach; ?>
        </select>
        <select class="form-control fl date-form-control search-form" name="time_order" style="width:130px;">
            <option value="">---时间排序---</option>
            <?php foreach (Task::getTimeOrder() as $id => $name) : ?>
                <option <?= (isset($get['time_order']) && ($get['time_order'] == $id)) ? 'selected="selected"' : ''; ?> value="<?= $id ?>" ><?= $name ?></option>
            <?php endforeach; ?>
        </select>

        <input name="show_first" type="hidden" value="<?= (isset($get['show_first'])) ? $get['show_first'] : 0 ?>" />
        <?php if (isset($get['type'])) : ?> <input name="type" type="hidden" value="<?= $get['type'] ?>" /><?php endif; ?>
        <input type="hidden" name="leader" value="<?= isset($get['leader']) ? $get['leader'] : '' ?>"/>
        <button type="submit" title="搜索"class="btn btn-primary fl btn-serch task-sou">
            <i class="icon-search"></i>
        </button>

        <button type="button" title="重置" class="btn btn-default fl btn-reset reset_search">
            <i class="icon-undo"></i>
        </button>


    </div>
    <?php ActiveForm::end(); ?>
    <div class="checkbox-task">
        <label>
            <input type="checkbox" <?= (isset($get['show_first']) && $get['show_first'] == 1) ? 'checked="checked"' : '' ?> id="show-first" style="position:relative; top:2px; left:-2px;">
            只看一级任务
        </label>
    </div>
</div>
<div class="mgt10 fix">
    <?php $mainForm = ActiveForm::begin([ 'id' => 'main-form', 'action' => '']); ?>
    <table class="table task-table">
        <tr>
            <th width="17%">任务名称</th>
            <th width="10%">任务编号</th>
            <th width="8%">负责人</th>
            <th width="14%">参与人</th>
            <th width="8%">创建日期</th>
            <th width="8%">开始日期</th>
            <th width="8%">结束日期</th>
            <th width="8%">任务类别</th>
            <th width="6%">状态</th>
            <th width="28%">操作</th>
        </tr>

        <?php if (empty($models)) : ?>
            <tr><td colspan="10" class="tac red">还没有任何数据...</td></tr>
        <?php else : ?>
            <?php foreach ($models as $model) : ?>
                <tr>
                    <td style="text-align: left;" title="<?= $model['name'] ?>">
                        <a target="_blank" href="<?= Url::to(['detail', 'id' => $model['task_id'], 'from' => Url::to('')]) ?>">
                            <?=
                            (isset($model['_is_child'])) ? '&nbsp;&nbsp;└─&nbsp;' . StringHelper::truncate($model['name'], 12) : StringHelper::truncate($model['name'], 12)
                            ?>
                        </a><?php if (Blog::isWrite($model['task_no'], 'task')): ?><i class="icon-paste"></i><?php endif; ?>
                    </td>
                    <td><?= $model['task_no'] ?></td>
                    <td><?= (!empty($model['leader']['realname'])) ? $model['leader']['realname'] : '' ?></td>
                    <td><?= (!empty($model['join_ids'])) ? implode(',', ArrayHelper::getColumn(User::getUserByDot($model['join_ids']), 'realname')) : '' ?></td>
                    <td><?= date('Y-m-d', $model['create_time']) ?></td>
                    <td><?= (!empty($model['begin_time'])) ? date('Y-m-d', $model['begin_time']) : '' ?></td>
                    <td><?= (!empty($model['end_time'])) ? date('Y-m-d', $model['end_time']) : '' ?></td>
                    <td><?= $model['category']['name'] ?></td>
                    <td><?= $model['status_name'] ?></td>
                    <td>
                        <?php if (Task::checkTaskAccess($model, 'task/task/create-blog')) : ?>
                            <a href="javascript:;" class="open-dialog" data-title="<?= $model['name'] ?> - 写日志" data-url="<?=
                            Url::to(['/blog/blog/write-daily', 'link_id' => $model['task_no'], 'link_type' => 'task',
                                'link_title' => $model['name']])
                            ?>" data-reload="false" data-width="600" data-height="380" data-reload="true">写日志</a>
                           <?php endif; ?>
                        <?php if (Task::checkTaskAccess($model, 'task/task/create-reimbursement')) : ?>
                            <a href="javascript:;" href="javascript:;" class="button open-dialog" data-url="<?= Url::to(['/reimbursement/reimbursement/create', 'link_id' => $model['task_no'], 'link_type' => 'task']) ?>" data-reload="false" data-title="任务报销" data-width="500" data-height="260">报销</a>
                        <?php endif; ?>
                        <?php if (Task::checkTaskAccess($model, 'task/task/create-child')) : ?>
                            <a data-title="建子任务" data-url="<?= Url::to(['create', 'parent_id' => $model['task_id']]) ?>" data-width="800" data-height="570" href="javascript:;" class="open-dialog">建子任务</a>
        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
<?php endif; ?>

    </table>
<?php ActiveForm::end(); ?>

    <!--分页标签-->
    <div class="pages">
        <?php if ($pages->totalCount != 0) : ?><span class="pagetotal">共<em><?= $pages->totalCount ?></em>条记录<em><?= $pages->page + 1 ?>/<?= $pages->pageCount ?></em>页 </span><?php endif; ?>
<?= \yii\widgets\LinkPager::widget(['pagination' => $pages, 'prevPageLabel' => '上一页', 'nextPageLabel' => '下一页', 'firstPageLabel' => '首页', 'lastPageLabel' => '末页']); ?>
    </div>
    <!--分页标签-->
</div>


