<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\DistAdminDistributor;
use App\Admin\Repositories\DistAppearance;
use App\Admin\Repositories\DistAppearancePublishList;
use App\Admin\Repositories\DistAppearanceTemplate;
use App\Admin\Repositories\DistAppearanceTemplateLog;
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\AceLeft;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Dcat\Admin\Widgets\Card;


class DistAppearanceTemplateController extends AdminController
{
    /*
     * monaco editor 编辑代码
     */
    public function ace(Content $content,Request $request) {
        $content->view('admin.pages-custom.ace_content');
        if ($request->isMethod('post') || false == empty($request->get('act'))) {
            if($request->get('act') == 'tree') {
                //获取代码树形结构
                $appearanceId = $request->get('appearance_id');
                $distId = $request->get('dist_id');
                return $this->showTree($appearanceId, $distId);
            } elseif ($request->get('act') == 'content') {
                //得到文件内容
                $appearanceId = $request->get('appearance_id');
                $distId = $request->get('dist_id');
                $templateCode = $request->get('id');
                return DistAppearanceTemplate::getContent($appearanceId,$distId,$templateCode);
            } elseif ($request->get('act') == 'content_save') {
                //保存文件内容
                $appearanceId = $request->get('appearance_id');
                $distId = $request->get('dist_id');
                $templateCode = $request->get('template_code');
                $content = $request->get('content');
                return DistAppearanceTemplate::saveContent($appearanceId,$distId,$templateCode, $content);
            } else if ($request->get('act') == 'add_del_tree') {
                //新建或删除文件
                $fileId = $request->get('file_id');
                $appearanceId = $request->get('appearance_id');
                $distId = $request->get('dist_id');
                $fileName = $request->get('file_name_input');
                $filePath = $request->get('file_path_name');
                $type = $request->get('type');
                return DistAppearanceTemplate::addDelTree($appearanceId,$distId,$fileId,$fileName, $filePath,$type);
            }elseif ($request->get('act') == 'publish') {
                //发报
                $appearanceId = $request->get('appearance_id');
                $distId = $request->get('dist_id');
                DistAppearancePublishList::publish($appearanceId,$distId);
                //清缓存
                DistAdminDistributor::clearCache($distId);
                return ['status'=>1];
            } else if ($request->get('act') == 'get_appearance_id') {
                //返回appearance_id
                $distId = $request->get('dist_id');
                $row = DistAdminDistributor::getOneById($distId);
                return $row ? $row->appearance_id : '';
            } else if ($request->get('act') == 'fetch_template_logs') {
                //返回模版修改日志
                $fileId = $request->get('file_id');
                $appearanceId = $request->get('appearance_id');
                $distId = $request->get('dist_id');
                $rows = DistAppearanceTemplateLog::fetchTemplateLogs($appearanceId,$distId,$fileId);
                return response()->json($rows);
            } else if ($request->get('act') == 'fetch_template_log_content') {
                //返回模版修改日志内容
                $logId = $request->get('log_id');
                $rows = DistAppearanceTemplateLog::fetchTemplateLogContent($logId);
                $rows->previous_content = htmlspecialchars($rows->previous_content);
                return response()->json($rows);
            } else if ($request->get('act') == 'restore_template_log') {
                //还原模版修改日志
                $logId = $request->get('log_id');
                DistAppearanceTemplateLog::restoreTemplateLog($logId);
                return ['status'=>1];
            }
        }

        $leftForm = new AceLeft();
        return $content
            ->header('Template Editor')
            ->body(admin_view('admin.pages-custom.ace',['leftForm'=>$leftForm]));
    }

    /*
     * 显示代码树
     */
    private function showTree($appearanceId, $distId) {
        $appearanceId = empty($appearanceId) ? 0 : $appearanceId;
        $distId = empty($distId) ? 0 : $distId;
        $appearanceRow = DistAppearance::getOneById($appearanceId);

        $tree = DistAppearanceTemplate::getTemplateTree($appearanceId, $distId);
        //print_r($tree);exit;
        $html = '<ul class="list-group list-group-flush">';
        $i = 0;
        foreach ($tree as $key => $value) {
            $fa = 'fa-angle-down';
            $file_name = $value['file_name'];
            $file_name = empty($file_name) ? 'root' : $file_name;
            $file_name = '<span class="custom-blue-bold">'. $file_name.'</span>';
            if ($i == 0) {
                $file_name .= " ({$distId} - {$appearanceId})";
            }
            $file_name = $file_name . '<span class="float-right"><a href="javascript:void(0)" title="Add File" data-toggle="modal" data-target="#addFileModal" file_path_name="'. $value['file_name']. '"><i class="feather icon-plus" style="font-size: 12px"></i></a></span>';
            $html .= '<li class="list-group-item has-submenu">';
            $html .= '<div style="padding-top: 5px;padding-bottom: 0px"><i class="fa '.$fa.'"></i> '. $file_name.'</div>';
            $html .= $this->treeBuilder($value);
            $html .= '</li>';
            $i++;
        }
        $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 (empty($v['children'])) {
                    $fa = 'fa-angle-right';
                    $file_name = '<a href="#" class="file-action" file_id="'. $v['template_code'].'" title="'.$file_name.'">'. $file_name.'</a>'. '<span class="float-right"><a href="javascript:void(0)" title="delete File" class="delFileButton" file_id="'. $v['id'].'" file_name="'. $v['file_name'].'"><i class="feather icon-x"></i></a></span>';
                } else {
                    $file_name = '<span class="custom-blue-bold">'. $file_name.'</span>';
                }
                $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;
    }



}
