<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DistAppearanceTemplate;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Admin;
use App\Admin\Forms\monacoLeft;
use Illuminate\Http\Request;


class DistAppearanceTemplateController extends AdminController
{

    /*
     * monaco editor 编辑代码
     */
    public function monaco(Content $content,Request $request) {
        if ($request->isMethod('post')) {
            if($request->get('act') == 'tree') {
                //获取代码树形结构
                $appearance_id = $request->get('appearance_id');
                $dist_id = $request->get('dist_id');
                return $this->showTree($appearance_id, $dist_id);
            } elseif ($request->get('act') == 'content') {
                //得到文件内容
                $id = $request->get('id');
                return DistAppearanceTemplate::getContent($id);
            } elseif ($request->get('act') == 'save') {
                //保存文件内容
                $template_id = $request->get('template_id');
                $content = $request->get('content');
                return DistAppearanceTemplate::saveContent($template_id, $content);
            }
        }
        $leftForm = new monacoLeft();
        return $content->body(admin_view('admin.pages-custom.monaco',['leftForm'=>$leftForm]));
    }

    /*
     * 显示代码树
     */
    private function showTree($appearance_id, $dist_id) {
        $appearance_id = empty($appearance_id) ? 0 : $appearance_id;
        $dist_id = empty($dist_id) ? 0 : $dist_id;
        $tree = DistAppearanceTemplate::getTemplateTree($appearance_id, $dist_id);
        $html = '<ul class="list-group list-group-flush">';
        foreach ($tree as $key => $value) {
            $fa = 'fa-angle-down';
            $file_name = $value['file_name'];
            if ($value['file_type'] == 1) {
                $fa = 'fa-angle-right';
                $file_name = '<a href="#" class="file-action" file_id="'. $value['id'].'">'. $file_name.'</a>';
            }
            $html .= '<li class="list-group-item has-submenu">';
            $html .= '<i class="fa '.$fa.'"></i> '. $file_name;
            $html .= $this->treeBuilder($value);
            $html .= '</li>';
        }
        $html .= '</ul>';
        return $html;
    }

    private function treeBuilder($value) {
        $html = '';
        if (!empty($value['children'])) {
            $html = '<ul class="submenu">';
            foreach ($value['children'] as $k => $v) {
                $fa = 'fa-angle-down';
                $file_name = $v['file_name'];
                if ($v['file_type'] == 1) {
                    $fa = 'fa-angle-right';
                    $file_name = '<a href="#" class="file-action" file_id="'. $v['id'].'">'. $file_name.'</a>';
                }
                $html .= '<li class="list-group-item"><i class="fa '.$fa.'"></i> '. $file_name;
                if (!empty($v['children'])) {
                    $html .= $this->treeBuilder($v);
                }
                $html .=  '</li>';

            }
            $html .= '</ul>';
        }
        return $html;
    }




//
//    /**
//     * page index
//     */
//    public function index(Content $content)
//    {
//        return $content
//            ->header('列表')
//            ->description('全部')
//            ->breadcrumb(['text'=>'列表','url'=>''])
//            ->body($this->grid());
//    }
//
//    /**
//     * Make a grid builder.
//     *
//     * @return Grid
//     */
//    protected function grid()
//    {
//        return Grid::make(new DistAppearanceTemplate(), function (Grid $grid) {
//            $grid->column('id')->sortable();
//            $grid->column('dist_id');
//            $grid->column('appearance_id');
//            $grid->column('file_name');
//            $grid->column('file_path');
//            $grid->column('content');
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();
//
//            $grid->filter(function (Grid\Filter $filter) {
//                $filter->equal('id');
//
//            });
//        });
//    }
//
//    /**
//     * Make a show builder.
//     *
//     * @param mixed $id
//     *
//     * @return Show
//     */
//    protected function detail($id)
//    {
//        return Show::make($id, new DistAppearanceTemplate(), function (Show $show) {
//            $show->field('id');
//            $show->field('dist_id');
//            $show->field('appearance_id');
//            $show->field('file_name');
//            $show->field('file_path');
//            $show->field('content');
//            $show->field('created_at');
//            $show->field('updated_at');
//        });
//    }
//
//    /**
//     * Make a form builder.
//     *
//     * @return Form
//     */
//    protected function form()
//    {
//        return Form::make(new DistAppearanceTemplate(), function (Form $form) {
//            $form->display('id');
//            $form->text('dist_id');
//            $form->text('appearance_id');
//            $form->text('file_name');
//            $form->text('file_path');
//            $form->text('content');
//
//            $form->display('created_at');
//            $form->display('updated_at');
//        });
//    }
}