<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\AdminModel;
use app\common\model\MenuModel;

use app\admin\validate\AdminValidate;
use think\exception\ValidateException;

class Admin extends AdminBaseController
{

    public function index()
    {
        return view('', [
            'role_list' => AdminModel::ROLE,
        ]);
    }

    public function adminForm()
    {
        $id          = input('id/d, 0');
        $info       = AdminModel::findOrEmpty($id);
        $menulist    = MenuModel::where(['mtype' => 'admin', 'status' => 1])->order(['pid' => 'asc', 'priority' => 'asc', 'id' => 'asc'])->select()->toArray();
        $powerarr    = [];
        $poweridsarr = ($info->powerids == null || empty($info->powerids)) ? [] : explode(",", $info->powerids);
        if (!empty($menulist)) {
            foreach ($menulist as $k => $v) {
                if ($v['pid'] == 0) {
                    $v['checked']       = false;
                    $v['children']      = [];
                    $v['spread']        = true;
                    $powerarr[$v['id']] = $v;
                } else {
                    $v['checked']                      = in_array($v['id'], $poweridsarr);
                    $powerarr[$v['pid']]['children'][] = $v;
                }
            }
        }
        return view('', [
            'info'    => $info,
            'powerarr' => json_encode(array_values($powerarr)),
        ]);
    }

    public function editAdmin()
    {
        $id    = input('id/d');
        $vdata = [
            'id'         => $id,
            'admin_name' => input('admin_name/s'),
            'realname'   => input('realname/s'),
            'mobile'     => input('mobile/s'),
        ];
        try {
            validate(AdminValidate::class)->check($vdata);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        $password = input('password/s');
        $role     = input('role/d', 2);
        $powerids = input('powerids/s', "");
        if ($role == 1) {
            $idsarr   = MenuModel::where(['mtype' => 'admin', 'status' => 1])->order(['pid' => 'asc', 'priority' => 'asc', 'id' => 'asc'])->column('id');
            $powerids = implode(",", $idsarr);
        }
        $data = [
            'role'      => $role,
            'admin_name' => input('admin_name/s', ""),
            'realname'  => input('realname/s', ""),
            'mobile'    => input('mobile/s', ""),
            'status'    => input('status/d') == 1 ? 1 : 2,
            'powerids'  => $powerids,
            'remark'    => input('remark', ""),
        ];
        if (empty($id)) {
            $data['password']  = empty($password) ? md5("123456789") : md5($password);
            $data['join_date'] = time();
            $data['join_ip']   = $_SERVER['SERVER_ADDR'];
            $data['last_date'] = time();
            $data['last_ip']   = $_SERVER['SERVER_ADDR'];
            AdminModel::create($data);
        } else {
            if (!empty($password)) {
                $data['password'] = md5($password);
            }
            AdminModel::update($data, ['id' => $id]);
        }
        ajax_return();
    }

    // 删除管理员
    public function delAdmin()
    {
        $access_admin = session('access_admin');
        $password     = input('password');
        if ($access_admin['password'] !== md5($password)) {
            ajax_return(1, '操作密码验证失败');
        }
        $id_arr = input('id_arr/a');
        if (in_array(1, $id_arr)) {
            ajax_return(1, '无法删除超级管理员');
        }
        AdminModel::destroy($id_arr);
        ajax_return();
    }

    public function listAdmin()
    {
        $limit      = input('limit');
        $page       = input('page');
        $map        = [];
        $admin_name = input('admin_name');
        if (!empty($admin_name)) {
            $map['admin_name'] = $admin_name;
        }
        $realname = input('realname');
        if (!empty($realname)) {
            $map['realname'] = $realname;
        }
        $mobile = input('mobile');
        if (!empty($mobile)) {
            $map['mobile'] = $mobile;
        }
        $role = input('role');
        if (!empty($role)) {
            $map['role'] = $role;
        }
        $list  = AdminModel::where($map)->order('id', 'asc')->limit($limit)->page($page)->append(['status_text', 'role_text'])->select();
        $count = AdminModel::where($map)->count();
        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }

        list_return($list, $count);
    }

    // 个人信息
    public function myInfo()
    {
        $access_admin = session('access_admin');
        $admin        = AdminModel::find($access_admin['id']);
        return view('', [
            'admin' => $admin,
        ]);
    }

    public function editMyInfo()
    {
        $access_admin = session('access_admin');
        AdminModel::update(['realname' => input('realname'), 'mobile' => input('mobile'), 'remark' => input('remark')], ['id' => $access_admin['id']]);
        ajax_return();
    }

    public function myPassword()
    {
        return view('');
    }

    public function editMyPassword()
    {
        $access_admin = session('access_admin');
        $oldpassword  = input('oldpassword');
        if ($access_admin['password'] !== md5($oldpassword)) {
            ajax_return(1, '当前密码不正确');
        }
        $password   = input('password');
        $repassword = input('repassword');
        if ($password !== $repassword) {
            ajax_return(1, '两次输入的新密码不一致');
        }
        AdminModel::update(['password' => md5($password)], ['id' => $access_admin['id']]);
        session('access_admin', null);
        ajax_return();
    }

}
