<?php
namespace app\admin\controller;

use app\model\Admins;
use app\model\Onlines;
use app\model\Orders;
use support\Log;
use support\Request;
use support\Redis;

class AdminController extends base
{
    public function Index(Request $request) {

        $query = Admins::order('status', 'desc')->order('id', 'desc');
        if($username = $request->get('username')) {
            $query->where('username', $username);
        }
        if($status = $request->get('status')) {
            $query->where('status', $status);
        }
        if($is_order = $request->get('is_order')) {
            $query->where('is_order', $is_order);
        }

        $list = $query->paginate($request->get('limit',10));
        return $this->success($list->hidden(['password','remember_token']),null,['oss' => Orders::OSS]);
    }

    public function edit(Request $request) { 
        $id = $request->get('id', 0);

        $info = (new Admins())->find($id);

        return $this->success($info->hidden(['password','remember_token']));
    }

    public function pwd(Request $request) {
        $oldpwd = $request->post('oldpwd', null);
        $pwd = $request->post('pwd', null);

        if(empty($pwd) || empty($oldpwd)) return $this->error(2003, '请填写密码');

        $admin = Admins::where('id', $request->admin->id)->find();
        if($admin->checkPwd($oldpwd)) {
            $admin->password = $pwd;
            $back = $admin->save();
            if($back) {
                return $this->success("");
            }
        }else{
            return $this->error(2003, '密码输入错误');
        }
        return $this->error(2003, '修改密码失败');
    }

    public function save(Request $request) {
        $id = $request->post('id',0);
        $username = $request->post('username');
        $name = $request->post('name');
        $mobile = $request->post('mobile');
        $password = $request->post('password');
        $is_order = $request->post('is_order',0);
        $is_anchor = $request->post('is_anchor',0);

        if($id) {
            $item = (new Admins())->find($id);
        }else{
            $item = new Admins();
        }

        if(!$request->admin->is_super) return $this->error(2000, '超级管理员才可以管理人员');

        if($item->id > 0) {
            if(!empty($name) && $item->name != $name) {
                $item->name = $name;
            }
            if(!empty($password) && strlen($password) < 6) return $this->error(2001, '密码长度错误');
            if(!empty($password)) {
                $item->password = $password;
            }
        }else{
            if(empty($username)) return $this->error(2002);
            if(empty($name)) return $this->error(2003);
            if(empty($password) || strlen($password) < 6) return $this->error(2004, '密码长度错误');
            if(!empty($password)) {
                $item->password = $password;
            }
            $has = Admins::where('username', $username)->find();
            if($has) {
                return $this->error(2004, '用户已经存在,换个用户名');
            }
            $item->username = $username;
            $item->name = $name;
        }
        $item->mobile = $mobile;
        $item->is_order = $is_order;
        $item->is_anchor = $is_anchor;
        $back = $item->save();
        if($back) 
            return $this->success($item->hidden(['password','remember_token']));
        return $this->error(2003, '写入数据库错误');
    }

    //开启和关闭
    public function disabled(Request $request) {
        $id = $request->post('id',0);
        $status = $request->post('status',0);
        $status = $status == 1? 1:0;

        if(!$request->admin->is_super) return $this->error(2000, '超级管理员才可以管理人员');

        if($id) {
            $item = Admins::find($id);
            $item->status = $status;
            $back = $item->save();
            if($back) {
                if($status == 0) {
                    Redis::set('Admin:disabled:'.$item->id, time() , 'EX', 3600*24*10);
                }else{
                    Redis::del('Admin:disabled:'.$item->id);
                }
                return $this->success($item->hidden(['password','remember_token']));
            }else{
                return $this->error(2001); //找不到管理员
            }
        }
        return $this->error(2001); //找不到管理员
    }

    public function getOnlineList(Request $request)
    {
        $list = [];
        $users = Admins::where('status',1)->select();
        foreach ($users as $user){
            $adminId = $user->id;

            $list[$adminId]['isEndWork'] = 0;
            if ($user->is_order == 1){
                $list[$adminId]['isEndWork'] = 1;//是否分单
            }

            if (!empty($user->end_work_time)) {
                $list[$adminId]['onlineTime'] = strtotime(date('Y-m-d H:i:00',$user->end_work_time)) - strtotime(date('Y-m-d H:i:00',$user->start_work_time));
            }else{
                $list[$adminId]['onlineTime'] = $user->last_work_time > 0 ? strtotime(date('Y-m-d H:i:00',$user->last_work_time)) - strtotime(date('Y-m-d H:i:00',$user->start_work_time)) : 0;
            }

            $list[$adminId]['start_work_time'] = $user->start_work_time;
            $list[$adminId]['end_work_time'] = $user->end_work_time;
            $list[$adminId]['last_work_time'] = $user->last_work_time;
            $list[$adminId]['order_num'] = $user->order_num;
            $list[$adminId]['id'] = $adminId;
            $list[$adminId]['username'] = $user->username;
            $list[$adminId]['isOnline'] = Redis::get('CRM:USER:ONLINE:'.$adminId) ? 1 : 0;
        }

        return $this->success(array_values($list));
    }

    public function editInfo(Request $request)
    {
        $post = $request->post();

        if (empty($post['id'])){
            $post['id'] = $request->admin->id;
        }

        $post['end_work_time'] = time();
        if ($post['is_order'] == 1){
            $post['end_work_time'] = 0;
        }

        try {
            Admins::update($post);
        }catch (\Exception $e){
            return $this->error(2001,$e->getMessage());
        }

        return $this->success(true);
    }
}