<?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 Insert extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/task/insert", method="GET|POST", title="任务新增")
    */
    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();
    }

    // Ajax处理
    private function _insert() {
        $param = \x\Request::post();
        if (empty($param['task_title'])) {
            return $this->returnJson('01', '请先输入任务名称');
        }
        if ($param['item_id'] == 'undefined') {
            return $this->returnJson('01', '请先选择对应项目');
        }
        if ($param['edition_id'] == 'undefined') {
            return $this->returnJson('01', '请先选择对应版本');
        }
        $Db = new \x\Db();
        $id = $Db->name('task')->insertGetId([
            'item_id' => $param['item_id'],
            'edition_id' => $param['edition_id'],
            'task_title' => $param['task_title'],
            'des' => $param['des'],
            'create_time' => time()
        ]);
        if (!$id) {
            $Db->return();
            return $this->returnJson('01', '添加失败，数据库写入异常');
        }

        if (!empty($param['user'])) {
            foreach ($param['user'] as $v) {
                $data = [
                    'task_id' => $id,
                    'admin_id' => $v['admin_id'],
                    'duty' => $v['duty'],
                    'status' => $v['status'],
                ];
                if (!empty($v['start_time'])) $data['start_time'] = strtotime($v['start_time']);
                if (!empty($v['final_start_time'])) $data['final_start_time'] = strtotime($v['final_start_time']);
                if (!empty($v['end_time'])) $data['end_time'] = strtotime($v['end_time']);
                if (!empty($v['final_end_time'])) $data['final_end_time'] = strtotime($v['final_end_time']);
                $Db->name('task_user')->insert($data);
            }
        }
        $Db->return();

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

}

