<?php
require_once (dirname(__DIR__, 3) . '/init.php');
    $menuid = m_u_g('menuid');
    $ids = $_GET['ids'];
    $type = m_u_g('type');
    $target = m_u_g('target');
    $menu = post($menuid);
    if ($type =='custom'){
        $link = m_u_g('link');
        $url = m_u_g('url');
        if($menu['content'] == ''){
            $data = [
                'title '=> $link,
                'slug '=>  $url ,
                'type '=> $type,
                'target '=> $target,
                'menu_id '=> $menuid,
            ];
            $db->table('menuitems')->insert($data);
        }else{
            $olddata = json_decode($menu['content'],true);
            $data = [
                'title '=> $link,
                'slug '=>  $url ,
                'type '=> $type,
                'target '=> $target,
                'menu_id '=> $menuid,
            ];
            $lastdata = $db->table('menuitems')->insert($data);
            $newdata = [];
            $newdata['id'] = $db->table('menuitems')->last_id();
            $newdata['children'] = [[]];
            array_push($olddata[0],$newdata);
            $olddata = json_encode($olddata);
            $db->table('posts')->where('id','=', $menuid)->update(['content' => $olddata]);
        }
    }elseif($type == 'menuUpdate'){
        $menuid = m_u_g('menuid');
        $data= $_GET['data'];
        $data = [
            'content' => $data,
        ];
        $query = $db->table('posts')->where('id', '=', $menuid)->update($data);
    }elseif($type =='update'){
        $id = m_u_g('id');
        $title = m_u_g('title');
        $url= m_u_g('url');
        $target= m_u_g('target');

        $data = [
            'title' => $title,
            'slug' => $url,
            'target' => $target,

        ];
        $query = $db->table('menuitems')->where('id', '=', $id)->update($data);

    }elseif($type =='delete'){
        $in = m_u_g('indis');
        $key = m_u_g('key');
        if($menu['content'] != ''){
            $data = json_decode($menu['content'],true);
            $maindata = $data[0];
            if($in == ''){
                unset($data[0][$key]);
                $newdata = json_encode($data);
                $db->table('posts')->where('id','=', $menuid)->update(['content' => $newdata]);
            }else{
                unset($data[0][$key]['children'][0][$in]);
                $newdata = json_encode($data);
                $db->table('posts')->where('id','=', $menuid)->update(['content' => $newdata]);
            }
        }
        $db->table('menuitems')->where('id','=', m_u_g('id'))->delete();

    }else{
        if($menu['content'] == ''){
            foreach($ids as $id){
                $post = post($id);
                $data = [
                    'title '=> $post['title'],
                    'slug '=> $post['sef'],
                    'type '=> $type,
                    'target '=> $target,
                    'menu_id '=> $menuid,
                ];
                $db->table('menuitems')->insert($data);
            }
        }else{

            $olddata = json_decode($menu['content'], true);

            foreach ($ids as $id) {
                $post = post($id);
                $data = [
                    'title' => $post['title'],
                    'slug' => $post['sef'],
                    'type' => $type,
                    'target' => $target,
                    'menu_id' => $menuid,
                ];
                $lastdata = $db->table('menuitems')->insert($data);
                $newdata = [];
                $newdata['id'] = $db->table('menuitems')->last_id();
                $newdata['children'] = [[]];
                array_push($olddata[0], $newdata);
            }

            $olddata = json_encode($olddata);
            $db->table('posts')->where('id', '=', $menuid)->update(['content' => $olddata]);


        }
    }

