<?php

namespace Admin\Controller;

class SdkMenuController extends ThinkController
{

    public $but_type = [
        '0' => 'h5页面',
        '1' => '原生',
        '2' => '外部链接',
        '3' => 'h5资源',
    ];

    public $menu_version = [
        '0' => '双平台',
        '1' => '安卓',
        '2' => 'ios',
    ];

    public function lists()
    {
        $menu_name = I('get.menu_name', 'sdk_menu');
        $sdkMenus  = M('tool', 'tab_')->where(['name' => $menu_name])->find();
        $config    = json_decode($sdkMenus['config'], true);
        if (!empty($config)) {
            foreach ($config as $k => $v) {
                $config[$k]['icon']         = (substr($v['icon'], 0, 7) == "http://" || substr($v['icon'], 0, 8) == "https://" ) ? $v['icon'] : (is_https() ? 'https://' : 'http://' . $_SERVER['HTTP_HOST'] . $v['icon']);
                $config[$k]['type']         = $this->but_type[$v['type']];
                $config[$k]['menu_version'] = $this->menu_version[$v['menu_version']];
                $config[$k]['status'] = ((!isset($v['status']) || $v['status']) == 1) ? '显示':'隐藏';
            }
        }
        $this->assign('list', $config);
        $this->assign('menu_name', $menu_name);
        $this->assign('menu_list', $this->getMenuList());
        $this->meta_title = '悬浮球菜单';
        $this->display('lists');
    }

    public function add()
    {
        $menu_name = I('get.menu_name', 'sdk_menu');
        $sdkMenus  = M('tool', 'tab_')->where(['name' => $menu_name])->find();
        $config    = json_decode($sdkMenus['config'], true);
        if (!empty($config)) {
            $config1 = $config;
            $id      = array_pop($config1);
            $id      = $id['id'] + 1;
        } else {
            $config = [];
            $id     = 1;
        }

        if (IS_POST) {
            $post          = I('post.');
            $post['cover'] = $post['icon'];
            $post['id']    = $id;
            $post['icon']  = get_cover($post['icon'], "path");
            array_push($config, $post);
            $sdkMenus['config'] = json_encode($config);
            if (empty($sdkMenus['name'])) {
                $sdkMenus['name']  = $menu_name;
                $sdkMenus['title'] = '悬浮球菜单';
                M('tool', 'tab_')->data($sdkMenus)->add();
            } else {
                M('tool', 'tab_')->where(['name' => $menu_name])->data($sdkMenus)->save();
            }
            $this->success('新增成功', U('lists', ['menu_name' => $menu_name]));
        }

        $data['sort']     = $id;
        $this->meta_title = '悬浮球菜单';
        $this->assign('type', 'add');
        $this->assign('data', $data);
        $this->assign('menu_name', $menu_name);
        $this->assign('but_type', $this->but_type);
        $this->assign('menu_version', $this->menu_version);
        $this->display('add');
    }

    public function edit($id)
    {
        $menu_name = I('get.menu_name', 'sdk_menu');
        $sdkMenus  = M('tool', 'tab_')->where(['name' => $menu_name])->find();
        $config    = json_decode($sdkMenus['config'], true);
        if (IS_POST) {
            foreach ($config as $k => $v) {
                if ($v['id'] == $id) {
                    $post = I('post.');
                    foreach ($post as $key => $val) {
	                    if ($key == 'icon') {
	                        $config[$k]['cover'] = $post['icon'];
	                        $config[$k]['icon'] = get_cover($post['icon'], "path");
	                    } else {
	                    	$config[$k][$key] = $post[$key];
	                    }
                    }
                    break;
                }
            }
            $sdkMenus['config'] = json_encode($config);
            M('tool', 'tab_')->where(['name' => $menu_name])->data($sdkMenus)->save();
            $this->success('修改成功', U('lists', ['menu_name' => $menu_name]));
        }
        $data = [];
        foreach ($config as $k => $v) {
            if ($v['id'] == $id) {
                $data = $v;
                break;
            }
        }
        if (empty($data)) {
            $this->error('菜单不存在', 'lists');
        }
        $img[0]           = $data['cover'];
        $this->meta_title = '悬浮球菜单';
        $this->assign('type', 'edit');
        $this->assign('data', $data);
        $this->assign('img', $img);
        $this->assign('menu_name', $menu_name);
        $this->assign('but_type', $this->but_type);
        $this->assign('menu_version', $this->menu_version);
        $this->display('add');
    }

    public function del($id)
    {
        $menu_name = I('get.menu_name', 'sdk_menu');
        if (!is_array($id)) {
            $data[] = $id;
        } else {
            $data = $id;
        }
        $sdkMenus = M('tool', 'tab_')->where(['name' => $menu_name])->find();
        $config   = json_decode($sdkMenus['config'], true);
        foreach ($config as $k => $v) {
            if (in_array($v['id'], $data)) {
                unset($config[$k]);
            }
        }
        $sdkMenus['config'] = json_encode($config);
        M('tool', 'tab_')->where(['name' => $menu_name])->data($sdkMenus)->save();
        $this->success('删除成功', U('lists', ['menu_name' => $menu_name]));
    }

    public function getMenuList()
    {
        $data = M('tool', 'tab_')->where(['type' => 5])->select();
        return $data;
    }

}