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

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

        $item_list = $this->Db->name('item')->order('id ASC')->select();
        $this->assign('item_list', $item_list);
        $info = $this->Db->name('edition')->where('id', $param['id'])->find();
        if ($info['start_time']) {$info['start_time'] = date('Y-m-d H:i:s', $info['start_time']);}else{$info['start_time']='';}
        if ($info['test_time']) {$info['test_time'] = date('Y-m-d H:i:s', $info['test_time']);}else{$info['test_time']='';}
        if ($info['publish_time']) {$info['publish_time'] = date('Y-m-d H:i:s', $info['publish_time']);}else{$info['publish_time']='';}

        if ($info['final_start_time']) {$info['final_start_time'] = date('Y-m-d H:i:s', $info['final_start_time']);}else{$info['final_start_time']='';}
        if ($info['final_test_time']) {$info['final_test_time'] = date('Y-m-d H:i:s', $info['final_test_time']);}else{$info['final_test_time']='';}
        if ($info['final_publish_time']) {$info['final_publish_time'] = date('Y-m-d H:i:s', $info['final_publish_time']);}else{$info['final_publish_time']='';}

        $this->assign('info', $info);
        $this->assign('status', edition_status());

        $this->Db->return();
        return $this->display();
    }

    // Ajax处理
    private function _update() {
        $param = \x\Request::post();
        $param['update_time'] = time();
        if (!empty($param['start_time'])) {$param['start_time'] = strtotime($param['start_time']);}else{$param['start_time'] = 0;}
        if (!empty($param['final_start_time'])) {$param['final_start_time'] = strtotime($param['final_start_time']);}else{$param['final_start_time'] = 0;}
        if (!empty($param['test_time'])) {$param['test_time'] = strtotime($param['test_time']);}else{$param['test_time'] = 0;}
        if (!empty($param['final_test_time'])) {$param['final_test_time'] = strtotime($param['final_test_time']);}else{$param['final_test_time'] = 0;}
        if (!empty($param['publish_time'])) {$param['publish_time'] = strtotime($param['publish_time']);}else{$param['publish_time'] = 0;}
        if (!empty($param['final_publish_time'])) {$param['final_publish_time'] = strtotime($param['final_publish_time']);}else{$param['final_publish_time'] = 0;}

        $res = $this->Db->name('edition')->where('id', $param['id'])->update($param);
        $this->Db->return();
        if ($res != false) {
            return $this->returnJson('00', '修改成功！');
        }

        return $this->returnJson('01', '修改失败！');
    }

}

