<?php

namespace app\home\controller;

use think\Lang;

class Config extends AdminControl {

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

    /**
     * 接入信息管理
     */
    public function index()
    {
        $model_access = Model('Access');
        $title = input('post.title');
        $condition = array();
        $condition['access_pid'] = session('service_pid');
        if($title){
            $condition['access_appid|access_appsecrect'] = $title;
        }
        $access_list = $model_access->getAccessList($condition, '*', 10);
        $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');
            $data = array(
                'access_url' => input('post.access_url'),
                'access_status' => input('post.access_status') ? input('post.access_status'):0,
                'access_appid' => random(22,0).time(),
                'access_appsecrect' => random(32,0),
                'access_pid' => session('service_pid'),//所属三方公司id
                'access_addtime' => TIMESTAMP,
            );
            //添加到数据库
            $result = $model_access->addAccess($data);
            if ($result) {
                dsLayerOpenSuccess("添加成功!");
            } else {
                $this->error("添加失败!");
            }
        } else {
            $access = array(
                'access_url' => '',
                'access_status' => 0,
            );
            $model_access = Model('Access');
            $this->assign('access',$access);
            return $this->fetch('form');
        }
    }

    public function edit()
    {
        $this->assign('is_super',session('access_id'));
        $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 = $model_access->getAccessInfo($condition);
            $this->assign('access', $access);
            return $this->fetch('form');
        } else {
            $data = array(
                'access_status' => input('post.access_status'),
                '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('Config/index')
            ), array(
                'name' => 'add', 'text' => lang('ds_add'), 'url' => "javascript:dsLayerOpen('".url('Config/add')."','".lang('ds_add')."')"
            ),
        );
        return $menu_array;
    }

    


}
