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

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/task/update", method="GET|POST", title="任务编辑")
    */
    public function run() {
        if (\x\Request::is_ajax()) {
            return $this->_update();
        }
        $param = \x\Request::get();

        $Db = new \x\Db();
        // 详情
        $info = $Db->name('task')->where('id', $param['id'])->find();
        $this->assign('info', $info);
        // 详情人员
        $list = $Db->name('task_user')->where('task_id', $param['id'])->order('id ASC')->select();
        $this->assign('list', $list);
        // 人员
        $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 = $Db->name('edition')->where('item_id='.$info['item_id'])->order('id DESC')->select();
        $this->assign('edition_list', $edition_list);
        $Db->return();
        $this->assign('status', task_status());
        return $this->display();
    }

    // Ajax处理
    private function _update() {
        $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();
        $res = $Db->name('task')->where('id', $param['id'])->update([
            'item_id' => $param['item_id'],
            'edition_id' => $param['edition_id'],
            'task_title' => $param['task_title'],
            'des' => $param['des'],
            'create_time' => time()
        ]);
        if (!$res) {
            $Db->return();
            return $this->returnJson('01', '编辑失败，数据库写入异常');
        }

        if (!empty($param['user'])) {
            $id = 0;
            foreach ($param['user'] as $v) {
                if (!empty($v['id'])) {
                    $id .= ','.$v['id'];
                }
            }
            // 删除
            if ($id) {
                $Db->name('task_user')->where('task_id', $param['id'])->whereNotIn('id', '('.$id.')')->delete();
            }

            foreach ($param['user'] as $v) {
                $data = [
                    'task_id' => $param['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']);

                if (!empty($v['id'])) {
                    $Db->name('task_user')->where('id', $v['id'])->update($data);
                } else {
                    $Db->name('task_user')->insert($data);
                }
                
            }
        } else {
            $Db->name('task_user')->where('task_id', $param['id'])->delete();
        }
        $Db->return();

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

}

