<?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_games")
 */
class Games extends AdminController
{

    use \app\admin\traits\Curd;

    /**
     * 字段排序
     * @var array
     */
    protected $sort = [
        'app_id' => 'desc',
    ];

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

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

    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = Db::table('app_games_config')
                ->alias('c')
                ->join(['app_products' => 'p'], 'c.pid=p.product_id', 'left')
                ->where($where)
                ->count();
            $list = Db::table('app_games_config')
                ->alias('c')
                ->join(['app_products' => 'p'], 'c.pid=p.product_id', 'left')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            try {
                $post['appkey'] = md5(uniqid().mt_rand(1, 999));
                $post['paykey'] = md5(uniqid().mt_rand(9999, 19999));
                $post['status'] = 0;
                $post['add_user'] = session('admin.username');
                $post['add_time'] = date('Y-m-d H:i:s');
                $save = Db::table('app_games_config')->save($post);
            } catch (\Exception $e) {
                $save = false;
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('products', Db::table('app_products')->column('product_name', 'product_id'));
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $table = Db::table('app_games_config');
        $this->checkPostRequest();
        $post = $this->request->post();
        if (!$table->find($post['id'])) {
            $this->error('数据不存在');
        }
        try {
            $table->where(['app_id' => $post['id']])->save([
                $post['field'] => $post['value'],
            ]);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($app_id)
    {
        $table_games_config = Db::table('app_games_config');
        $row = $table_games_config->find($app_id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $table_games_config->where(['app_id' => $app_id])->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        $this->assign('products', Db::table('app_products')->column('product_name', 'product_id'));
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($app_id)
    {
        $this->checkPostRequest();
        $model = Db::table('app_games_config');
        $row = $model->find($app_id);
        empty($row) && $this->error('数据不存在');
        try {
            $save = $model->where(['app_id' => $app_id])->delete();
        } catch (\Exception $e) {
            $save = false;
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

}
