<?php

namespace app\adminapi\controller;

use app\adminapi\model\Admin;
use app\adminapi\model\Admin as AdminModel;
use app\common\controller\AdminApi;
use fast\Random;
use think\Cookie;
use think\Hook;
use think\Session;
use think\Validate;


/**
 * 个人中心管理
 *
 * @icon   fa fa-group
 */
class Profile extends AdminApi
{


    protected $model = null;

    /**
     * 初始化操作
     * @access protected
     */
    public function _initialize()
    {
        $this->model = new AdminModel;
        parent::_initialize();


    }

    /**
     * 我的操作日志列表
     *
     * @ApiMethod (GET)
     * @ApiParams (name="limit", type="int", required=true, description="每页条数")
     * @ApiParams (name="page", type="int", required=true, description="页数")
     * @ApiParams (name="url", type="string", required=false, description="url")
     * @ApiParams (name="title", type="string", required=false, description="日志标题")
     * @ApiParams (name="content", type="string", required=false, description="日志内容")
     * @ApiParams (name="ip", type="string", required=false, description="IP")
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
            $this->model =  new \app\adminapi\model\AdminLog();

        $where = [];
        $limit =  $this->request->get("limit/d",10);

        $url =  $this->request->get("url/s","");
        if($url){
            $where['url'] = ['like',"%{$url}%"];
        }

        $title =  $this->request->get("title/s","");
        if($title){
            $where['title'] = ['like',"%{$title}%"];
        }

        $content =  $this->request->get("content/s","");
        if($content){
            $where['content'] = ['like',"%{$content}%"];
        }

        $ip =  $this->request->get("ip/s","");
        if($ip){
            $where['ip'] = ['like',"%{$ip}%"];
        }


        $list = $this->model
                ->where($where)
                ->where('admin_id', $this->auth->id)
                ->order("id desc")
                ->paginate($limit);

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

        $this->success("查询成功！",$result);

    }



    /**
     * 更新管理员个人信息
     *
     * @ApiMethod (POST)
     * @ApiParams (name="email", type="int", required=true, description="电子邮箱")
     * @ApiParams (name="password", type="int", required=true, description="登录密码")
     * @ApiParams (name="nickname", type="string", required=true, description="昵称")
     * @ApiParams (name="mobile", type="int", required=true, description="手机号码")
     * @ApiParams (name="avatar", type="string", required=true, description="头像")
     */
    public function update()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('email', 'nickname', 'password', 'avatar',"mobile"))
            ));
            unset($v);
            if (!Validate::is($params['email'], "email")) {
                $this->error(__("Please input correct email"));
            }
            if (isset($params['password'])) {
                if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
                    $this->error(__("Please input correct password"));
                }
                $params['salt'] = Random::alnum();
                $params['password'] = md5(md5($params['password']) . $params['salt']);
            }
            $exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
            if ($exist) {
                $this->error(__("Email already exists"));
            }
            if ($params) {
                $admin = Admin::get($this->auth->id);
                $admin->save($params);

                $this->success("更新成功！");
            }
            $this->error("更新失败！");
        }
        return;
    }

}