<?php

namespace app\admin\controller\app;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="app_ver_game")
 */
class VerGame extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\AppVerGame();

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $model = Db::table('app_ver_game')
                ->alias('avg')
                ->join(['app_games_config' => 'agc'], 'avg.app_id=agc.app_id', 'left')
                ->join(['app_ver' => 'av'], 'avg.ver=av.ver_class', 'left')
                ->where($where);
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $model->count(),
                'data' => $model->page($page, $limit)->order($this->sort)->select(),
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        // 获取渠道配置
        $model = Db::table('app_ver_game');
        $ver_row = $model->find($id);
        // 获取游戏配置
        empty($ver_row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $model->where(['id' => $id])->save($post);
            } catch (\Exception $e) {
                $save = false;
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $ver_row);
        $this->assign('game_row', Db::table('app_games_config')->column('app_name', 'app_id'));
        $this->assign('ver_row', Db::table('app_ver')->column('ver_name', 'ver_class'));
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            // 判断渠道号是否已存在
            $model = Db::table('app_ver_game');
            if ($model->where(['ver_code' => $post['ver_code']])->find()) {
                $this->error("渠道号：{$post['ver_code']} 已存在");
            }
            $rule = [];
            $this->validate($post, $rule);
            try {
                $post['add_user'] = session('admin.username');
                $post['add_time'] = date('Y-m-d H:i:s');
                $save = $model->insert($post);
            } catch (\Exception $e) {
                $save = false;
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('ver_row', Db::table('app_ver')->column('ver_name', 'ver_class'));
        $this->assign('game_row', Db::table('app_games_config')->column('app_name', 'app_id'));
        return $this->fetch();
    }


}
