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

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

class Index extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/edition/index", method="GET|POST", title="版本列表")
     * @Param(name="page", value="1")
     * @Param(name="limit", value="10")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->showlist();
        }
        $Db = new \x\Db();
        $item_list = $Db->name('item')->order('id ASC')->select();
        $Db->return();
        $this->assign('item_list', $item_list);
        return $this->display();
    }

    // 列表
    private function showlist() {
        $Db = new \x\Db();
        $param = \x\Request::get();
        $where = $this->_where($param);
        $list = $Db->name('edition')->alias('A')->join('item B', 'A.item_id=B.id')->where($where)->order('A.id DESC')->field('A.*, B.title')->page($param['page'], $param['limit'])->select();
        $count = $Db->name('edition')->alias('A')->join('item B', 'A.item_id=B.id')->where($where)->count();
        foreach ($list as $k=>$v) {

            $list[$k]['create_time'] = date('Y-m-d H:i', $v['create_time']);
            $list[$k]['status_name'] = edition_status($v['status']);

            if ($v['start_time']) {$list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);}else{$list[$k]['start_time']='';}
            if ($v['test_time']) {$list[$k]['test_time'] = date('Y-m-d H:i', $v['test_time']);}else{$list[$k]['test_time']='';}
            if ($v['publish_time']) {$list[$k]['publish_time'] = date('Y-m-d H:i', $v['publish_time']);}else{$list[$k]['publish_time']='';}

            if ($v['final_start_time']) {$list[$k]['final_start_time'] = date('Y-m-d H:i', $v['final_start_time']);}else{$list[$k]['final_start_time']='';}
            if ($v['final_test_time']) {$list[$k]['final_test_time'] = date('Y-m-d H:i', $v['final_test_time']);}else{$list[$k]['final_test_time']='';}
            if ($v['final_publish_time']) {$list[$k]['final_publish_time'] = date('Y-m-d H:i', $v['final_publish_time']);}else{$list[$k]['final_publish_time']='';}
        }
        $Db->return();
        return $this->_list('0', '', $count, $list);
    }

    // 搜索条件
    private function _where($param=[]) {
        $where = [];
        if (!empty($param['code'])) {
            $where[] = ['A.code', '=', $param['code']];
        }
        if (!empty($param['item_id'])) {
            $where[] = ['A.item_id', '=', $param['item_id']];
        }

        return $where;
    }
}