<?php
// +----------------------------------------------------------------------
// | 控制台
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://blog.junphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小黄牛 <1731223728@qq.com>
// +----------------------------------------------------------------------

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

class Main extends Admin
{

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

    // 看板模式
    public function main2() {
        $Db = new \x\Db();
        // 找出待开始的项目
        $where = [];
        $where[] = ['B.status', '=', 1];
        $where[] = ['B.admin_id', '=', $this->admin['id']];
        $list = $Db->name('task')->alias('A')
                ->join('task_user B', 'B.task_id=A.id')
                ->join('edition C', 'A.edition_id=C.id')
                ->join('item D', 'C.item_id=D.id')
                ->where($where)
                ->order('B.start_time ASC, B.id DESC')
                ->field('A.task_title, B.task_id, B.id, B.start_time, B.end_time, B.status, B.duty, C.code, D.title')
                ->select();
        foreach ($list as $key => $value) {
            $list[$key]['num'] = $Db->name('comment')->where('task_id', $value['task_id'])->count();
        }
        $this->assign('list_1', $list);
        // 找出待完成的项目
        $where = [];
        $where[] = ['B.status', '=', 2];
        $where[] = ['B.admin_id', '=', $this->admin['id']];
        $list = $Db->name('task')->alias('A')
                ->join('task_user B', 'B.task_id=A.id')
                ->join('edition C', 'A.edition_id=C.id')
                ->join('item D', 'C.item_id=D.id')
                ->where($where)
                ->order('B.end_time ASC, B.id DESC')
                ->field('A.task_title, B.task_id, B.id, B.start_time, B.end_time, B.status, B.duty, C.code, D.title')
                ->select();
        foreach ($list as $key => $value) {
            $list[$key]['num'] = $Db->name('comment')->where('task_id', $value['task_id'])->count();
        }
        $this->assign('list_2', $list);
        $Db->return();
        return $this->display('admin/index/main2');
    }

    // 列表
    private function showlist() {
        $Db = new \x\Db();
        $param = \x\Request::get();
        $where = $this->_where($param);
        $list = $Db->name('task')->alias('A')
                ->join('task_user B', 'B.task_id=A.id')
                ->join('edition C', 'A.edition_id=C.id')
                ->join('item D', 'C.item_id=D.id')
                ->where($where)
                ->order('C.item_id ASC, A.edition_id DESC, A.id DESC')
                ->field('A.task_title, B.id, B.start_time, B.end_time, B.status, B.duty, C.code, D.title')
                ->page($param['page'], $param['limit'])
                ->select();
        $count = $Db->name('task')->alias('A')
                ->join('task_user B', 'B.task_id=A.id')
                ->join('edition C', 'A.edition_id=C.id')
                ->join('item D', 'C.item_id=D.id')
                ->where($where)
                ->count();
        foreach ($list as $k=>$v) {
            $list[$k]['start_time'] = !empty($v['start_time']) ? date('Y-m-d H:i', $v['start_time']) : '';
            $list[$k]['end_time'] = !empty($v['end_time']) ? date('Y-m-d H:i', $v['end_time']) : '';

            if ($v['status'] == 1 && ($v['start_time']-3600) < time()) {
                $list[$k]['start_time'] = '<div style="background: #ef4136;color:#fff;border-radius: 10px;;padding-left:5px" title="即将过期">'.$list[$k]['start_time'].'</div>';
            }

            if ($v['status'] == 2 && ($v['end_time']-3600) < time()) {
                $list[$k]['end_time'] = '<div style="background: #d93a49;color:#fff;border-radius: 10px;;padding-left:5px" title="即将过期">'.$list[$k]['end_time'].'</div>';
            }

            $list[$k]['status_name'] = task_status($v['status']);;
        }
        $Db->return();
        return $this->_list('0', '', $count, $list);
    }

    // 搜索条件
    private function _where($param=[]) {
        $where = [];
        $where[] = ['B.status', '<>', 3];
        $where[] = ['B.admin_id', '=', $this->admin['id']];
        if (!empty($param['item_id'])) {
            $where[] = ['A.item_id', '=', $param['item_id']];
        }
        if (!empty($param['edition_id'])) {
            $where[] = ['A.edition_id', '=', $param['edition_id']];
        }

        return $where;
    }
}

