<?php
/**
 * +----------------------------------------------------------------------
 * 任务列表
 * +----------------------------------------------------------------------
 * 官网：https://www.sw-x.cn
 * +----------------------------------------------------------------------
 * 作者：小黄牛 <1731223728@qq.com>
 * +----------------------------------------------------------------------
 * 开源协议：http://www.apache.org/licenses/LICENSE-2.0
 * +----------------------------------------------------------------------
*/

namespace app\controller\admin\task;
use app\controller\SuperClass\Admin;

class Index extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/task/index", method="GET|POST", title="任务列表")
     * @Param(name="page", value="1")
     * @Param(name="limit", value="10")
     * @Param(name="item_id", value="0")
     * @Param(name="edition_id", value="0")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->showlist();
        }
        $param = \x\Request::get();
        $Db = new \x\Db();
        // 项目
        $item_list = $Db->name('item')->order('id ASC')->select();
        $this->assign('item_list', $item_list);
        // 版本
        $edition_list = [];
        if ($item_list) {
            $id = $item_list[0]['id'];
            if ($param['item_id']) $id =$param['item_id'];
            $edition_list = $Db->name('edition')->where('item_id='.$id)->order('id DESC')->select();
        }
        $this->assign('edition_list', $edition_list);
        $this->assign('param', $param);

        $Db->return();
        return $this->display();
    }

    // 列表
    private function showlist() {
        $Db = new \x\Db();
        $param = \x\Request::get();
        $where = $this->_where($param);
        $list = $Db->name('task')->where($where)->order('id DESC')->field('id, task_title, create_time')->page($param['page'], $param['limit'])->select();
        $count = $Db->name('task')->where($where)->count();
        foreach ($list as $k=>$v) {
            $list[$k]['create_time'] = date('Y-m-d H:i', $v['create_time']);
            $admin_list = '';
            $arr = $Db->name('task_user')->alias('A')->join('auth_admin B', 'A.admin_id = B.id')->where('A.task_id', $v['id'])->field('B.nick')->select();
            foreach ($arr as $val) {
                $admin_list .= $val['nick'].'、';
            }
            $list[$k]['admin_list'] = rtrim($admin_list, '、');
        }
        $Db->return();
        return $this->_list('0', '', $count, $list);
    }

    // 搜索条件
    private function _where($param=[]) {
        $where = [];
        if (!empty($param['task_title'])) {
            $where[] = ['task_title', 'like', "%".$param['task_title']."%"];
        }
        if (!empty($param['item_id'])) {
            $where[] = ['item_id', '=', $param['item_id']];
        }
        if (!empty($param['edition_id'])) {
            $where[] = ['edition_id', '=', $param['edition_id']];
        }

        return $where;
    }
}