<?php

namespace app\home\controller;

use think\Lang;

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

    /**
     * 管理员列表
     * @return mixed
     */
    public function index()
    {
        $model_service = Model('service');
        $groupid = input('post.admingroupid');
        $title = input('post.title');
        $condition = array();
        if($groupid){
            $condition['admin_group_id'] = $groupid;
        }
        if($title){
            $condition['service_name'] = $title;
        }
        $condition['service_pid'] = session('service_pid');
        $service_list = $model_service->getserviceList($condition, '*', 10);
        if($service_list){
            for ($i=0; $i < count($service_list); $i++) { 
                $ere['group_id'] = $service_list[$i]['service_group_id'];
                $qxzlist[] = Model('admingroup')->getOneAdmingroup($ere,$field = 'group_name');
            }
            $this->assign('qxzlist',$qxzlist);
        }
        
        $allpower = $this->qxhans();
        $where = array();
        $where['suosgs'] = session('service_pid');
        $admin_group = model('admingroup')->getAdminGroupList($where,'group_name,group_id');
        $this->assign('admin_group', $admin_group);//管理组
        $this->assign('allpower',$allpower);
        $this->assign('service_list', $service_list);
        $this->assign('show_page', $model_service->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 添加管理员
     * @return mixed
     */
    public function add(){
        $model_service = model('service');
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        if (!request()->isPost()) {
            $admin_array = array('add' => 1,'service_status'=>0);
            $condition = array();
            $condition['suosgs'] = session('service_pid');
            $admin_group = model('admingroup')->getAdminGroupList($condition,'group_name,group_id');
            $onlyz = array(
                'group_id'=>'',
                'group_name'=>'',
            );
            $this->assign('onlyz',$onlyz);
            $this->assign('admin_group', $admin_group);
            $this->assign('admin', $admin_array);
            return $this->fetch('form');
        } else {
            //上传文件保存路径
            $upload_file = ROOT_PATH . DS . DIR_UPLOAD . DS . ATTACH_COMMON;
            if (!empty($_FILES['service_imgurl']['name'])) {
                $file = request()->file('service_imgurl');
                $use_imgurl = random(2,0).time();
                $info = $file->validate(['ext' => 'jpg,png,gif'])->move($upload_file, $use_imgurl);
                if ($info) {
                    $upload['service_imgurl'] = $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            if (!empty($upload['service_imgurl'])) {
                $data['service_imgurl'] = "uploads/home/common".DS.$upload['service_imgurl'];
            }

            $update_array['service_name'] = input('post.service_name');
            $update_array['service_password'] = input('post.service_password') ? md5(input('post.service_password')) : md5('123456');
            $update_array['service_group_id'] = input('post.service_group_id');
            $update_array['service_status'] = input('post.service_status');
            $update_array['service_nickname'] = input('post.service_nickname');
            $update_array['service_pid'] = session('service_pid');
            $update_array['service_add_time'] = TIMESTAMP;

            $result = $model_service->addService($update_array);
            if($data && $result){
                $where = array();
                $where['service_id'] = $result;
                $erre = $model_service->editService($where,$data);
            }

            if ($result) {
                dsLayerOpenSuccess("添加成功!");
            } else {
                $this->error("添加失败!");
            }
        }
    }

    public function edit()
    {
        $this->assign('is_super',session('service_id'));
        $service_id = input('param.service_id');
        if (empty($service_id)) {
            $this->error(lang('param_error'));
        }
        $model_service = Model('service');
        if (!request()->isPost()) {
            $condition['service_id'] = $service_id;
            $admin_array = $model_service->getserviceInfo($condition);
            $admin_array['add'] = 0;
            $where = array();
            $where['suosgs'] = session('service_pid');
            $admin_group = model('admingroup')->getAdminGroupList($where,'group_name,group_id');
            $ewhe = array();
            $ewhe['group_id'] = $admin_array['service_group_id'];
            $onlyz = model('admingroup')->getOneAdmingroup($ewhe,'group_name,group_id');
            $suosgs = session("admin_pid");
            $this->assign('suosgs',$suosgs);
            $this->assign('onlyz',$onlyz);
            $this->assign('admin_group',$admin_group);
            $this->assign('admin', $admin_array);
            return $this->fetch('form');
        } else {
            //上传文件保存路径
            $upload_file = ROOT_PATH . DS . DIR_UPLOAD . DS . ATTACH_COMMON;
            if (!empty($_FILES['service_imgurl']['name'])) {
                $file = request()->file('service_imgurl');
                $use_imgurl = random(2,0).time();
                $info = $file->validate(['ext' => 'jpg,png,gif'])->move($upload_file, $use_imgurl);
                if ($info) {
                    $upload['service_imgurl'] = $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            if (!empty($upload['service_imgurl'])) {
                $update_array['service_imgurl'] = "uploads/home/common".DS.$upload['service_imgurl'];
            }

            $update_array['service_name'] = input('post.service_name');
            $update_array['service_status'] = input('post.service_status');
            $update_array['service_nickname'] = input('post.service_nickname');
            $update_array['service_group_id'] = input('post.service_group_id');
            if (input('post.service_password')) {
                $update_array['service_password'] = md5(input('post.service_password'));
            }

            $where = array();
            $where['service_id'] = $service_id;
            $result = $model_service->editService($where,$update_array);
            if ($result) {
                dsLayerOpenSuccess("编辑成功!");
            } else {
                $this->error("编辑失败!");
            }
        }
    }

    public function del()
    {
        $service_id = input('param.service_id');
        if (empty($service_id)) {
            $this->error(lang('param_error'));
        }
        $result = db('service')->delete($service_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;
    }
}