<?php


namespace app\admin\controller;


use think\facade\Request;
use think\facade\Db;

class Account extends Base
{
    protected $middleware = ['app\middleware\CheckValidata::class'];

    //相关账号列表
    public function accountList()
    {
        $page = Request::param('page',1);
        $limit = Request::param('limit',config('app.limit'));
        $where[] = ['type','=',1];
        $account_name = Request::param('account_name');
        if($account_name) {
            $where[] = ['account_name','like','%'.$account_name.'%'];
        }
        $data = Db::name('account')->where($where)->order('create_time','desc')->page($page,$limit)->select()->toArray();
        $res['total'] = Db::name('account')->where($where)->count();
        $res['data'] = changeField($data);
        foreach ($data as $k =>$v) {
            if($v['content']) {
                $data[$k]['content'] = json_decode($v['content'],true);
            }
        }
        return $this->returnJson($res);
    }

    //添加|修改相关账号信息
    public function addAccount()
    {
        $account_id = Request::param('account_id');
        $data['phone'] = Request::param('phone','');
        $data['user_name'] = Request::param('user_name');
        $data['account_pwd'] = Request::param('account_pwd');
        $data['account_name'] = Request::param('account_name');
        $data['type'] = Request::param('type',1);
        $data['explain'] = Request::param('explain','');
        $data['account_url'] = Request::param('account_url');
        $data['content'] = json_encode(Request::param('content'));
        $data['create_time'] = time();
        if($account_id){
            $where['account_id'] = $account_id;
            $operater =  Db::name('account')->where($where)->update($data);
        } else {
            $operater =  Db::name('account')->insert($data);
        }
        if($operater) {
            return $this->returnJson([],'操作成功!');
        } else {
            return $this->returnJson([],'操作失败!',400);
        }
    }

    //账号详情
    public function accountDetail()
    {
        $account_id = Request::param('account_id');
        $where['account_id'] = $account_id;
        $info = Db::name('account')->where($where)->find();
        if($info) {
            if($info['content'] != null) {
                $info['content'] =  json_decode($info['content'],true);
            }
            return $this->returnJson($info);
        } else {
            return $this->returnJson([],'未找到该数据!',400);
        }
    }

    //删除账号
    public function delAccount()
    {
        $account_id = Request::param('account_id');
        $where['account_id'] = $account_id;
        $info = Db::name('account')->where($where)->delete();
        if($info) {
            return $this->returnJson([],'删除成功!');
        } else {
            return $this->returnJson([],'未找到该数据!',400);
        }
    }






}