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

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

class Ui_insert extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/test/ui_insert", method="GET|POST", title="用例-UI-新增")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->_insert();
        }
        $param = \x\Request::get();
        $Db = new \x\Db();
        // 人员
        $admin_list = $Db->name('auth_admin')->order('id ASC')->select();
        $this->assign('admin_list', $admin_list);
        // 项目
        $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);
        $Db->return();
        $this->assign('status', task_status());
        $this->assign('param', $param);
        return $this->display();
    }

    // 主表处理
    private function _insert_table($param) {
        $Db = new \x\Db();
        $id = $Db->name('test_ui')->insertGetId([
            'item_id' => $param['item_id'],
            'edition_id' => $param['edition_id'],
            'test_title' => $param['test_title'],
            'admin_id' => $this->admin['id'],
            'create_time' => time()
        ]);
        if (!$id) {
            $Db->return();
            return $this->returnJson('01', '添加失败，数据库写入异常');
        }
        
        $Db->return();
        return $this->returnJson('00', '创建成功！');
    }

    // Ajax处理
    private function _insert() {
        $param = \x\Request::post();
        if (!empty($param['model'])) {
            return $this->_insert_table($param);
        }

        return $this->returnJson('00', '创建成功！');
    }

}

