<?php

namespace app\admin\controller;

use think\Validate;
use think\Lang;

class Accesskf extends AdminControl
{

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

    /**
     * 客户接入列表
     * @return mixed
     */
    public function index()
    {
        $model_Access = Model('Access');
        $title = input('post.title');
        $condition = array();
        if($title){
            $condition['access_gsjrname|access_gsname|access_appid'] = $title;
        }
        $access_list = $model_Access->getAccessList($condition, '*', 10);
        if($access_list){
            for ($i=0; $i < count($access_list); $i++) { 
                $where['member_id'] = $access_list[$i]["access_pid"];
                $member_list[] = model('member')->getMemberInfo($where,$field = 'member_name');
            }
            $this->assign('member_list',$member_list);
        }
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('access_list', $access_list);
        $this->assign('show_page', $model_Access->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add()
    {
        if (request()->isPost()) {
            $model_access = Model('access');
            //判断用户名是否存在
            if ($model_access->getAccessInfo(['access_gsjrname' => input('post.access_gsjrname')])) {
                $this->error("接入用户名已存在");
            }
            $data = array(
                'access_gsjrname' => input('post.access_gsjrname'),
                'access_pid' => input('post.access_pid'),
                'access_gsname' => input('post.access_gsname'),
                'access_url' => input('post.access_url'),
                'access_appid' => random(22,0).time(),
                'access_appsecrect' => random(32,0),
                'access_addtime' => TIMESTAMP,
            );
            //添加到数据库
            $result = $model_access->addAccess($data);
            if ($result) {
                dsLayerOpenSuccess("接入添加成功!");
            } else {
                $this->error("接入添加失败!");
            }
        } else {
            $member_array = array(
                'member_status' => 0,
                'add' => 1,
            );
            $onlygs = array(
                'member_id'=>'',
                'member_name'=>'',
            );
            $allgs = Model('member')->allcompany('');
            $this->assign('allgs',$allgs);
            $this->assign('onlygs',$onlygs);
            $this->assign('member', $member_array);
            $this->setAdminCurItem('add');
            return $this->fetch('form');
        }
    }

    public function edit()
    {
        $access_id = input('param.access_id');
        if (empty($access_id)) {
            $this->error(lang('param_error'));
        }
        $model_access = Model('Access');
        if (!request()->isPost()) {
            $condition['access_id'] = $access_id;
            $access_array = $model_access->getAccessInfo($condition);
            $access_array['add'] = 0;
            $allgs = Model('member')->allcompany('');
            $where['member_id'] = $access_array['access_pid'];
            $onlygs = Model('member')->getMemberInfo($where,$field = 'member_id,member_name');
            $this->assign('onlygs',$onlygs);
            $this->assign('allgs',$allgs);
            $this->assign('access', $access_array);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        } else {
            $data = array(
                'access_gsjrname' => input('post.access_gsjrname'),
                'access_pid' => input('post.access_pid'),
                'access_gsname' => input('post.access_gsname'),
                'access_url' => input('post.access_url'),
            );
            //验证数据  END
            $result = $model_access->editAccess(array('access_id' => intval($access_id)), $data);
            if ($result) {
                dsLayerOpenSuccess("接入编辑成功!");
            } else {
                $this->error("接入编辑失败!");
            }
        }
    }

    public function del()
    {
        $access_id = input('param.access_id');
        if (empty($access_id)) {
            $this->error(lang('param_error'));
        }
        $result = db('Access')->delete($access_id);
        if ($result) {
            ds_json_encode(10000, "删除成功");
        } else {
            ds_json_encode(10001, "删除失败");
        }
    }

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

}
