<?php

namespace app\admin\controller;

use think\Lang;


class Admin extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/admin.lang.php');
    }

    /**
     * 管理员列表
     * @return mixed
     */
    public function index()
    {
        $model_admin = Model('admin');
        $admsuos = $model_admin->suosgsAdmin(session('admin_id'));
        $groupid = input('post.admingroupid');
        $title = input('post.title');
        $condition = array();
        if($groupid){
            $condition['admin_group_id'] = $groupid;
        }
        if($title){
            $condition['admin_name'] = $title;
        }
        if($admsuos['admin_id']!=1){
            $condition['admin_pid'] = $admsuos['admin_id'];
        }
        $admin_list = $model_admin->getAdminList($condition, '*', 10);
        $allpower = $this->qxhans();
        $condition = array();
        $condition['suosgs'] =1;
        $admin_group = model('admingroup')->getAdminGroupList($condition,'group_name,group_id');
        $this->assign('admin_group', $admin_group);//管理组
        $this->assign('allpower',$allpower);
        $this->assign('admin_list', $admin_list);
        $this->assign('show_page', $model_admin->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 添加管理员
     * @return mixed
     */
    public function add()
    { 
        $this->assign('is_super',session('admin_id'));
        if (request()->isPost()) {
            $model_admin = Model('admin');
            //判断用户名是否存在
            if ($model_admin->getOneAdmin(['admin_name' => input('post.admin_name')])) {
                $this->error(lang('admin_existence'));
            }
            $data = array(
                'admin_name' => input('post.admin_name'),
                'admin_password' => input('post.admin_password') ? md5(input('post.admin_password')) : md5('123'),
                'admin_group_id' => input('post.group_id'),
                'admin_status' => input('post.admin_status'),
                'admin_add_time' => TIMESTAMP,
            );
            if(input('post.group_id')==3){
                $suosgsid = $model_admin->suosgsAdmin(session('admin_id'));
                $data['admin_pid'] = input('post.admin_pid') ? input('post.admin_pid') : $suosgsid['admin_id'];
            }

            //添加到数据库
            $result = $model_admin->addAdmin($data);
            if ($result) {
                dsLayerOpenSuccess(lang('admin_add_succ'));
            } else {
                $this->error(lang('admin_add_fail'));
            }
        } else {
            $model_admin = Model('admin');
            $jlzgsmz = $model_admin->ogroupAdmin(0);
            $admin_array = array('add' => 1,'admin_status'=>0);
            $condition = array();
            $condition['suosgs'] = 1;
            $admin_group = model('admingroup')->getAdminGroupList($condition,'group_name,group_id');
            $this->assign('admin_group', $admin_group);
            $this->assign('admin', $admin_array);
            $this->assign('jlzgsmz',$jlzgsmz);
            return $this->fetch('form');
        }
    }

    public function edit()
    {
        $this->assign('is_super',session('admin_id'));
        $admin_id = input('param.admin_id');
        if (empty($admin_id)) {
            $this->error(lang('param_error'));
        }
        $model_admin = Model('admin');
        if (!request()->isPost()) {
            $condition['admin_id'] = $admin_id;
            $admin_array = $model_admin->getOneAdmin($condition);
            $admin_array['add'] = 0;
            $where = array();
            $admin_group = model('admingroup')->getAdminGroupList($where,'group_name,group_id');
            $group_id = $admin_array['admin_group_id'];
            $jlzgsmz = $model_admin->ogroupAdmin($admin_id);
            $suosgs = $model_admin->suosgsAdmin($admin_array['admin_pid']);
            $this->assign('suosgs',$suosgs);
            $this->assign('jlzgsmz',$jlzgsmz);
            $this->assign('admin_group', $admin_group);
            $this->assign('qxgroup_name',$admin_group[$group_id-1]['group_name']);
            $this->assign('qxgroup_id',$admin_group[$group_id-1]['group_id']);
            $this->assign('admin', $admin_array);
            return $this->fetch('form');
        } else {
            $data = array(
                'admin_name' => input('post.admin_name'),
                'admin_status' => input('post.admin_status'),
                'admin_group_id' => input('post.group_id')
            );
            if (input('post.admin_password')) {
                $data['admin_password'] = md5(input('post.admin_password'));
            }
            if(input('post.group_id')==3){
                $suosgsid = $model_admin->suosgsAdmin(session('admin_id'));
                $data['admin_pid'] = input('post.admin_pid')? input('post.admin_pid') : $suosgsid['admin_id'];
            }
            if(input('post.group_id')==2){
                $data['admin_pid'] = '';   
            }
            //验证数据  END
            $result = $model_admin->editAdmin(array('admin_id' => intval($admin_id)), $data);
            if ($result) {
                dsLayerOpenSuccess(lang('admin_edit_succ'));
            } else {
                $this->error(lang('admin_edit_fail'));
            }
        }
    }

    public function del()
    {
        $admin_id = input('param.admin_id');
        if (empty($admin_id)) {
            $this->error(lang('param_error'));
        }
        $result = db('admin')->delete($admin_id);
        if ($result) {
            ds_json_encode(10000, lang('admin_del_succ'));
        } else {
            ds_json_encode(10001, lang('admin_del_fail'));
        }
    }

    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'index', 'text' => lang('ds_manage'), 'url' => url('Admin/index')
            ), array(
                'name' => 'add', 'text' => lang('ds_add'), 'url' => "javascript:dsLayerOpen('".url('Admin/add')."','".lang('ds_add')."')"
            ),
        );
        return $menu_array;
    }
}