<?php

namespace app\admin\controller\user;


use think\Db;
use Exception;
use think\exception\DbException;
use think\exception\PDOException;
use app\common\controller\Backend;
use think\exception\ValidateException;

/**
 * 会员组管理
 *
 * @icon fa fa-users
 */
class Group extends Backend
{

    /**
     * @var \app\admin\model\UserGroup
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');
        $this->view->assign("statusList", $this->model->getStatusList());
    }

     /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->where($where)->where('is_agent', 1)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    

    /**
     * 禁用
     * @param $ids
     * @return string
     * @throws DbException
     * @throws \think\Exception
     */
    public function disable($ids = null, $is_lock=0)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $result = false;
        try {
            $result = $row->allowField(true)->save(['is_lock'=>$is_lock]);
        } catch (ValidateException|PDOException|Exception $e) {
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }



}
